-
Notifications
You must be signed in to change notification settings - Fork 71
Inject pod readiness gate #606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
060e0dd
38fc75b
8982db2
5640db2
51ec551
dda23bf
2547247
30922a1
ea7e303
33171e3
cc2f234
02831a8
9e19b6a
3df31c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,11 @@ package main | |
|
|
||
| import ( | ||
| "flag" | ||
| "os" | ||
|
|
||
| "github.com/aws/aws-application-networking-k8s/pkg/webhook" | ||
| "github.com/go-logr/zapr" | ||
| "go.uber.org/zap/zapcore" | ||
| "os" | ||
| k8swebhook "sigs.k8s.io/controller-runtime/pkg/webhook" | ||
|
|
||
| "github.com/aws/aws-application-networking-k8s/pkg/aws" | ||
| "github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog" | ||
|
|
@@ -49,7 +50,6 @@ import ( | |
| "github.com/aws/aws-application-networking-k8s/pkg/config" | ||
| "github.com/aws/aws-application-networking-k8s/pkg/k8s" | ||
| metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook" | ||
| ) | ||
|
|
||
| var ( | ||
|
|
@@ -128,14 +128,24 @@ func main() { | |
| setupLog.Fatal("cloud client setup failed: %s", err) | ||
| } | ||
|
|
||
| // do not create the webhook server when running locally | ||
| var webhookServer k8swebhook.Server | ||
| isLocalDev := config.DevMode != "" | ||
| if !isLocalDev { | ||
| webhookServer = k8swebhook.NewServer(k8swebhook.Options{ | ||
| Port: 9443, | ||
| CertDir: "/etc/webhook-cert/", | ||
| CertName: "tls.crt", | ||
| KeyName: "tls.key", | ||
| }) | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using |
||
| mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ | ||
| Scheme: scheme, | ||
| Metrics: metricsserver.Options{ | ||
| BindAddress: metricsAddr, | ||
| }, | ||
| WebhookServer: webhook.NewServer(webhook.Options{ | ||
| Port: 9443, | ||
| }), | ||
| WebhookServer: webhookServer, | ||
| HealthProbeBindAddress: probeAddr, | ||
| LeaderElection: enableLeaderElection, | ||
| LeaderElectionID: "amazon-vpc-lattice.io", | ||
|
|
@@ -144,6 +154,15 @@ func main() { | |
| setupLog.Fatal("manager setup failed:", err) | ||
| } | ||
|
|
||
| if !isLocalDev { | ||
| // register webhook handlers | ||
| readinessGateInjector := webhook.NewPodReadinessGateInjector( | ||
| mgr.GetClient(), | ||
| log.Named("pod-readiness-gate-injector"), | ||
| ) | ||
| webhook.NewPodMutator(scheme, readinessGateInjector).SetupWithManager(mgr) | ||
| } | ||
|
|
||
| finalizerManager := k8s.NewDefaultFinalizerManager(mgr.GetClient()) | ||
|
|
||
| // parent logging scope for all controllers | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,10 @@ | ||
| # Adds namespace to all resources. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much of this commented code came from the aws-load-balancer-controller. I've tried to remove what we don't need. One notable difference is that the gateway controller enables the webhook configuration by default, even though you still have to opt into it for any given namespace. |
||
| #namespace: code-aws-application-networking-system | ||
|
|
||
| # Value of this field is prepended to the | ||
| # names of all resources, e.g. a deployment named | ||
| # "wordpress" becomes "alices-wordpress". | ||
| # Note that it should also match with the prefix (text before '-') of the namespace | ||
| # field above. | ||
| #namePrefix: code- | ||
|
|
||
| # Labels to add to all resources and selectors. | ||
| #commonLabels: | ||
| # someName: someValue | ||
|
|
||
| bases: | ||
| apiVersion: kustomize.config.k8s.io/v1beta1 | ||
| kind: Kustomization | ||
| resources: | ||
| - ../crds | ||
| - ../rbac | ||
| - ../manager | ||
| # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in | ||
| # crd/kustomization.yaml | ||
| #- ../webhook | ||
| # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required. | ||
| #- ../certmanager | ||
| - ../webhook | ||
| # [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'. | ||
| #- ../prometheus | ||
|
|
||
|
|
@@ -33,42 +17,3 @@ patchesStrategicMerge: | |
| # Mount the controller config file for loading manager configurations | ||
| # through a ComponentConfig type | ||
| #- manager_config_patch.yaml | ||
|
|
||
| # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in | ||
| # crd/kustomization.yaml | ||
| #- manager_webhook_patch.yaml | ||
|
|
||
| # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. | ||
| # Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks. | ||
| # 'CERTMANAGER' needs to be enabled to use ca injection | ||
| #- webhookcainjection_patch.yaml | ||
|
|
||
| # the following config is for teaching kustomize how to do var substitution | ||
| vars: | ||
| # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix. | ||
| #- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR | ||
| # objref: | ||
| # kind: Certificate | ||
| # group: cert-manager.io | ||
| # version: v1 | ||
| # name: serving-cert # this name should match the one in certificate.yaml | ||
| # fieldref: | ||
| # fieldpath: metadata.namespace | ||
| #- name: CERTIFICATE_NAME | ||
| # objref: | ||
| # kind: Certificate | ||
| # group: cert-manager.io | ||
| # version: v1 | ||
| # name: serving-cert # this name should match the one in certificate.yaml | ||
| #- name: SERVICE_NAMESPACE # namespace of the service | ||
| # objref: | ||
| # kind: Service | ||
| # version: v1 | ||
| # name: webhook-service | ||
| # fieldref: | ||
| # fieldpath: metadata.namespace | ||
| #- name: SERVICE_NAME | ||
| # objref: | ||
| # kind: Service | ||
| # version: v1 | ||
| # name: webhook-service | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,5 +60,26 @@ spec: | |
| requests: | ||
| cpu: 10m | ||
| memory: 64Mi | ||
| volumeMounts: | ||
| - mountPath: /etc/webhook-cert | ||
| name: webhook-cert | ||
| readOnly: true | ||
| serviceAccountName: gateway-api-controller | ||
| terminationGracePeriodSeconds: 10 | ||
| volumes: | ||
| - name: webhook-cert | ||
| secret: | ||
| defaultMode: 420 | ||
| secretName: webhook-cert | ||
| --- | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This placeholder certificate is generated during When calling a webhook, the API server performs the following validations:
The placeholder cert is valid for 1 day (will pass check 1 for 1 day) but will fail checks 2 and 3 with this message (from Having a placeholder here is important because the controller's webhook server needs this value to start up successfully. The webhook server does not perform strict validation. Once the secret is patched with a valid cert, Kubernetes will distribute the value and the webhook server will be notified when the files are updated on the volume mount. See controller-runtime/pkg/webhook/server.go for more details.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the webhook validation don't pass, does the controller still work?(i.e., for only the placeholder cert, can it still enter the |
||
| # placeholder secret so volume can mount successfully and controller can start | ||
| # populated during make-deploy. Will not pass validations (no CA, expires after 1 day, wrong DNS names) | ||
| apiVersion: v1 | ||
| kind: Secret | ||
| metadata: | ||
| name: webhook-cert | ||
| namespace: aws-application-networking-system | ||
| type: kubernetes.io/tls | ||
| data: | ||
| tls.crt: Cg== | ||
| tls.key: Cg== | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| apiVersion: kustomize.config.k8s.io/v1beta1 | ||
| kind: Kustomization | ||
| resources: | ||
| - monitor.yaml |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| apiVersion: kustomize.config.k8s.io/v1beta1 | ||
| kind: Kustomization | ||
| resources: | ||
| - manifests.yaml |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| --- | ||
| apiVersion: admissionregistration.k8s.io/v1 | ||
| kind: MutatingWebhookConfiguration | ||
| metadata: | ||
| creationTimestamp: null | ||
| name: aws-appnet-gwc-mutating-webhook | ||
| webhooks: | ||
| - admissionReviewVersions: | ||
| - v1 | ||
| clientConfig: | ||
| service: | ||
| name: webhook-service | ||
| namespace: aws-application-networking-system | ||
| path: /mutate-pod | ||
| failurePolicy: Fail | ||
| name: mpod.gwc.k8s.aws | ||
| rules: | ||
| - apiGroups: | ||
| - "" | ||
| apiVersions: | ||
| - v1 | ||
| operations: | ||
| - CREATE | ||
| resources: | ||
| - pods | ||
| sideEffects: None | ||
| namespaceSelector: | ||
| matchExpressions: | ||
| - key: application-networking.k8s.aws/pod-readiness-gate-inject | ||
| operator: In | ||
| values: | ||
| - enabled | ||
| objectSelector: | ||
| matchExpressions: | ||
| - key: app.kubernetes.io/name | ||
| operator: NotIn | ||
| values: | ||
| - gateway-api-controller | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: webhook-service | ||
| namespace: aws-application-networking-system | ||
| spec: | ||
| ports: | ||
| - port: 443 | ||
| targetPort: 9443 | ||
| selector: | ||
| control-plane: gateway-api-controller |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~~Where this dns name come from
webhook-service.{namespace}.svc? Does k8s defined that or we define that? do we need to pass it when start the webhookServer? ~~oh, nvm, I saw this part: