-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from yshch/I-29
I-30 -- Implement basic validating admission webhook
- Loading branch information
Showing
288 changed files
with
45,717 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.git/ | ||
*.md | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# binaries | ||
/build/loggo/loggo | ||
/build/validating-webhook/validating-webhook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM golang:1.18.6-bullseye as builder | ||
RUN apt update && apt install -y libsystemd-dev | ||
WORKDIR /src | ||
COPY . /src/ | ||
RUN make build-validating-webhook | ||
|
||
FROM debian:bullseye | ||
RUN apt update && apt install -y ca-certificates | ||
COPY --from=builder /src/build/validating-webhook/validating-webhook /validating-webhook | ||
CMD ["/validating-webhook"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
"sigs.k8s.io/controller-runtime/pkg/healthz" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
"github.com/2gis/loggo/components/k8s" | ||
"github.com/2gis/loggo/configuration" | ||
"github.com/2gis/loggo/logging" | ||
) | ||
|
||
type validator struct { | ||
Client client.Client | ||
Config configuration.Config | ||
decoder *admission.Decoder | ||
} | ||
|
||
func (v *validator) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
service := &corev1.Service{} | ||
|
||
err := v.decoder.Decode(req, service) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
if _, err := k8s.CreateService(v.Config.SLIExporterConfig, service.Annotations); err != nil { | ||
return admission.Denied(err.Error()) | ||
} | ||
|
||
return admission.Allowed("") | ||
} | ||
|
||
func (v *validator) InjectDecoder(d *admission.Decoder) error { | ||
v.decoder = d | ||
return nil | ||
} | ||
|
||
func main() { | ||
c := configuration.GetConfig() | ||
log.Printf("Starting with configuration: %s", c.ToString()) | ||
logger := logging.NewLogger("json", c.LogLevel, os.Stdout) | ||
|
||
logger.Printf("Setting up controller manager") | ||
restconfig, err := config.GetConfig() | ||
if err != nil { | ||
logger.Fatalln(err) | ||
} | ||
mgr, err := manager.New(restconfig, manager.Options{ | ||
HealthProbeBindAddress: ":8090", | ||
MetricsBindAddress: ":8080", | ||
Port: 9443, | ||
}) | ||
if err != nil { | ||
logger.Fatalln(err) | ||
} | ||
|
||
logger.Printf("Registering healthz and readyz checkers") | ||
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { | ||
logger.Fatalln(err) | ||
} | ||
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { | ||
logger.Fatalln(err) | ||
} | ||
|
||
logger.Printf("Setting up webhook server") | ||
hookServer := mgr.GetWebhookServer() | ||
|
||
logger.Printf("Registering validating-webhook to the webhook server") | ||
hookServer.Register("/validate", &webhook.Admission{ | ||
Handler: &validator{ | ||
Client: mgr.GetClient(), | ||
Config: c, | ||
}, | ||
}) | ||
|
||
logger.Printf("Starting manager") | ||
if err := mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
logger.Fatalln(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.