/* Copyright © 2023 Martín Montes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package controller import ( "os" mariadbv1alpha1 "github.com/mmontes11/mariadb-operator/api/v1alpha1" "github.com/spf13/cobra" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/webhook" ) var ( certDir string port int ) var webhookCmd = &cobra.Command{ Use: "webhook", Short: "MariaDB operator webhook server.", Long: `Provides validation and inmutability checks for MariaDB resources.`, Run: func(cmd *cobra.Command, args []string) { setupLogger() mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, HealthProbeBindAddress: healthAddr, WebhookServer: &webhook.Server{ CertDir: certDir, Port: port, }, }) if err != nil { setupLog.Error(err, "unable to start manager") os.Exit(1) } if err = (&mariadbv1alpha1.MariaDB{}).SetupWebhookWithManager(mgr); err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "MariaDB") os.Exit(1) } if err = (&mariadbv1alpha1.Backup{}).SetupWebhookWithManager(mgr); err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "Backup") os.Exit(1) } if err = (&mariadbv1alpha1.Restore{}).SetupWebhookWithManager(mgr); err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "restore") os.Exit(1) } if err = (&mariadbv1alpha1.User{}).SetupWebhookWithManager(mgr); err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "User") os.Exit(1) } if err = (&mariadbv1alpha1.Grant{}).SetupWebhookWithManager(mgr); err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "Grant") os.Exit(1) } if err = (&mariadbv1alpha1.Database{}).SetupWebhookWithManager(mgr); err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "Database") os.Exit(1) } if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up health check") os.Exit(1) } setupLog.Info("starting manager") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { setupLog.Error(err, "problem running manager") os.Exit(1) } }, } func init() { rootCmd.AddCommand(webhookCmd) webhookCmd.Flags().StringVar(&healthAddr, "health-addr", ":8081", "The address the probe endpoint binds to.") webhookCmd.Flags().StringVar(&certDir, "cert-dir", "/tmp/k8s-webhook-server/serving-certs", "Path containing the TLS certificate for the webhook server.") webhookCmd.Flags().IntVar(&port, "port", 10250, "Port to be used by the webhook server.") }