Skip to content
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

feat: use cert ready checker for webhook readiness #721

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"net/http"

"github.com/open-policy-agent/cert-controller/pkg/rotator"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -13,7 +14,6 @@ import (
"monis.app/mlog"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
Expand Down Expand Up @@ -146,14 +146,7 @@ func mainErr() error {
close(setupFinished)
}

if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
return fmt.Errorf("entrypoint: unable to create ready check: %w", err)
}

if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
return fmt.Errorf("entrypoint: unable to create health check: %w", err)
}

setupProbeEndpoints(mgr, setupFinished)
go setupWebhook(mgr, setupFinished)

entryLog.Info("starting manager")
Expand All @@ -168,15 +161,38 @@ func setupWebhook(mgr manager.Manager, setupFinished chan struct{}) {
// Block until the setup (certificate generation) finishes.
<-setupFinished

// setup webhooks
entryLog.Info("setting up webhook server")
hookServer := mgr.GetWebhookServer()
hookServer.TLSMinVersion = tlsMinVersion

// setup webhooks
entryLog.Info("registering webhook to the webhook server")
podMutator, err := wh.NewPodMutator(mgr.GetClient(), mgr.GetAPIReader(), arcCluster, audience)
if err != nil {
panic(fmt.Errorf("unable to set up pod mutator: %w", err))
}
hookServer.Register("/mutate-v1-pod", &webhook.Admission{Handler: podMutator})
}

func setupProbeEndpoints(mgr ctrl.Manager, setupFinished chan struct{}) {
// Block readiness on the mutating webhook being registered.
// We can't use mgr.GetWebhookServer().StartedChecker() yet,
// because that starts the webhook. But we also can't call AddReadyzCheck
// after Manager.Start. So we need a custom ready check that delegates to
// the real ready check after the cert has been injected and validator started.
checker := func(req *http.Request) error {
select {
case <-setupFinished:
return mgr.GetWebhookServer().StartedChecker()(req)
default:
return fmt.Errorf("certs are not ready yet")
}
}

if err := mgr.AddHealthzCheck("healthz", checker); err != nil {
panic(fmt.Errorf("unable to add healthz check: %w", err))
}
if err := mgr.AddReadyzCheck("readyz", checker); err != nil {
panic(fmt.Errorf("unable to add readyz check: %w", err))
}
entryLog.Info("added healthz and readyz check")
}
5 changes: 5 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ spec:
httpGet:
path: /readyz
port: healthz
initialDelaySeconds: 5
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz
port: healthz
initialDelaySeconds: 15
periodSeconds: 20
failureThreshold: 6
resources:
limits:
cpu: 100m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ spec:
image: '{{ .Values.image.repository }}:{{ .Values.image.release }}'
imagePullPolicy: '{{ .Values.image.pullPolicy }}'
livenessProbe:
failureThreshold: 6
httpGet:
path: /healthz
port: healthz
initialDelaySeconds: 15
periodSeconds: 20
name: manager
ports:
- containerPort: {{ trimPrefix ":" .Values.metricsAddr }}
Expand All @@ -65,6 +68,8 @@ spec:
httpGet:
path: /readyz
port: healthz
initialDelaySeconds: 5
periodSeconds: 5
resources:
{{- toYaml .Values.resources | nindent 10 }}
securityContext:
Expand Down
5 changes: 5 additions & 0 deletions manifest_staging/deploy/azure-wi-webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,12 @@ spec:
image: mcr.microsoft.com/oss/azure/workload-identity/webhook:v0.15.0
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 6
httpGet:
path: /healthz
port: healthz
initialDelaySeconds: 15
periodSeconds: 20
name: manager
ports:
- containerPort: 9443
Expand All @@ -189,6 +192,8 @@ spec:
httpGet:
path: /readyz
port: healthz
initialDelaySeconds: 5
periodSeconds: 5
resources:
limits:
cpu: 100m
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ main() {

create_cluster
make deploy
poll_webhook_readiness
${KUBECTL} wait --for=condition=available --timeout=5m deployment/azure-wi-webhook-controller-manager -n azure-workload-identity-system

if [[ -n "${WINDOWS_NODE_NAME:-}" ]]; then
E2E_ARGS="--node-os-distro=windows ${E2E_ARGS:-}"
Expand Down Expand Up @@ -106,7 +106,7 @@ test_helm_chart() {
--wait \
--debug \
-v=5
poll_webhook_readiness
${KUBECTL} wait --for=condition=available --timeout=5m deployment/azure-wi-webhook-controller-manager -n azure-workload-identity-system
make test-e2e-run
}

Expand Down