|
| 1 | +package options |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + |
| 7 | + "github.com/spf13/pflag" |
| 8 | + "k8s.io/apimachinery/pkg/runtime" |
| 9 | + _ "k8s.io/client-go/plugin/pkg/client/auth" // Import all auth plugins (e.g. Azure, GCP, OIDC, etc.) to ensure exec-entrypoint and run can make use of them. |
| 10 | + "k8s.io/klog/v2" |
| 11 | + ctrl "sigs.k8s.io/controller-runtime" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/healthz" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 14 | +) |
| 15 | + |
| 16 | +type WebhookOptions struct { |
| 17 | + Port int |
| 18 | + CertDir string |
| 19 | + scheme *runtime.Scheme |
| 20 | + webhooks []WebhookInitializer |
| 21 | +} |
| 22 | + |
| 23 | +func NewWebhookOptions() *WebhookOptions { |
| 24 | + return &WebhookOptions{ |
| 25 | + Port: 9443, |
| 26 | + scheme: runtime.NewScheme(), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func (c *WebhookOptions) AddFlags(fs *pflag.FlagSet) { |
| 31 | + fs.IntVar(&c.Port, "port", c.Port, |
| 32 | + "Port is the port that the webhook server serves at.") |
| 33 | + fs.StringVar(&c.CertDir, "certdir", c.CertDir, |
| 34 | + "CertDir is the directory that contains the server key and certificate. If not set, "+ |
| 35 | + "webhook server would look up the server key and certificate in {TempDir}/k8s-webhook-server/serving-certs") |
| 36 | +} |
| 37 | + |
| 38 | +type WebhookInitializer interface { |
| 39 | + Init(mgr ctrl.Manager) error |
| 40 | +} |
| 41 | + |
| 42 | +func (c *WebhookOptions) InstallWebhook(webhooks ...WebhookInitializer) { |
| 43 | + c.webhooks = append(c.webhooks, webhooks...) |
| 44 | +} |
| 45 | + |
| 46 | +func (c *WebhookOptions) InstallScheme(fns ...func(s *runtime.Scheme) error) error { |
| 47 | + for _, f := range fns { |
| 48 | + if err := f(c.scheme); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + } |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func (c *WebhookOptions) RunWebhookServer(ctx context.Context) error { |
| 56 | + logger := klog.LoggerWithName(klog.FromContext(ctx), "Webhook Server") |
| 57 | + // This line prevents controller-runtime from complaining about log.SetLogger never being called |
| 58 | + ctrl.SetLogger(logger) |
| 59 | + |
| 60 | + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ |
| 61 | + Scheme: c.scheme, |
| 62 | + HealthProbeBindAddress: ":8000", |
| 63 | + WebhookServer: webhook.NewServer(webhook.Options{ |
| 64 | + Port: c.Port, |
| 65 | + CertDir: c.CertDir, |
| 66 | + TLSOpts: []func(config *tls.Config){ |
| 67 | + func(config *tls.Config) { |
| 68 | + config.MinVersion = tls.VersionTLS12 |
| 69 | + }, |
| 70 | + }, |
| 71 | + }), |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + logger.Error(err, "unable to start manager") |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + // add healthz/readyz check handler |
| 79 | + if err := mgr.AddHealthzCheck("healthz-ping", healthz.Ping); err != nil { |
| 80 | + logger.Error(err, "unable to add healthz check handler") |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + if err := mgr.AddReadyzCheck("readyz-ping", healthz.Ping); err != nil { |
| 85 | + logger.Error(err, "unable to add readyz check handler") |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + for _, webhookInitializer := range c.webhooks { |
| 90 | + if err := webhookInitializer.Init(mgr); err != nil { |
| 91 | + logger.Error(err, "unable to initilize webhook server") |
| 92 | + return err |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + logger.Info("starting manager") |
| 97 | + if err := mgr.Start(ctx); err != nil { |
| 98 | + logger.Error(err, "problem running manager") |
| 99 | + return err |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
0 commit comments