|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + |
| 6 | + "github.com/argoproj-labs/argocd-image-updater/internal/controller" |
| 7 | + "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log" |
| 8 | + "github.com/bombsimon/logrusr/v2" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + ctrl "sigs.k8s.io/controller-runtime" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/healthz" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/metrics/filters" |
| 13 | + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +// newControllerCommand implements "controller" command |
| 19 | +func newControllerCommand() *cobra.Command { |
| 20 | + var metricsAddr string |
| 21 | + var enableLeaderElection bool |
| 22 | + var probeAddr string |
| 23 | + var secureMetrics bool |
| 24 | + var enableHTTP2 bool |
| 25 | + var LogLevel string |
| 26 | + var Interval time.Duration |
| 27 | + |
| 28 | + var controllerCmd = &cobra.Command{ |
| 29 | + Use: "controller", |
| 30 | + Short: "Manages ArgoCD Image Updater Controller.", |
| 31 | + Long: `The 'controller' command starts the Kubernetes controller responsible for managing |
| 32 | +ImageUpdater Custom Resources (CRs). |
| 33 | +
|
| 34 | +This controller monitors ImageUpdater CRs and reconciles them by: |
| 35 | + - Checking for new container image versions from specified registries. |
| 36 | + - Applying updates to applications based on CR policies. |
| 37 | + - Updating the status of the ImageUpdater CRs. |
| 38 | +
|
| 39 | +It operates as a long-running manager process within the Kubernetes cluster. |
| 40 | +Flags can configure its metrics, health probes, and leader election. |
| 41 | +This enables a CRD-driven approach to automated image updates with Argo CD. |
| 42 | +`, |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + if err := log.SetLogLevel(LogLevel); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + logrLogger := logrusr.New(log.Log()) // log.Log() should return the *logrus.Logger |
| 48 | + ctrl.SetLogger(logrLogger) |
| 49 | + setupLog := ctrl.Log.WithName("controller-setup") |
| 50 | + setupLog.Info("Controller runtime logger initialized.", "setAppLogLevel", LogLevel) |
| 51 | + |
| 52 | + // if the enable-http2 flag is false (the default), http/2 should be disabled |
| 53 | + // due to its vulnerabilities. More specifically, disabling http/2 will |
| 54 | + // prevent from being vulnerable to the HTTP/2 Stream Cancellation and |
| 55 | + // Rapid Reset CVEs. For more information see: |
| 56 | + // - https://github.com/advisories/GHSA-qppj-fm5r-hxr3 |
| 57 | + // - https://github.com/advisories/GHSA-4374-p667-p6c8 |
| 58 | + var tlsOpts []func(*tls.Config) |
| 59 | + disableHTTP2 := func(c *tls.Config) { |
| 60 | + setupLog.Info("disabling http/2") |
| 61 | + c.NextProtos = []string{"http/1.1"} |
| 62 | + } |
| 63 | + |
| 64 | + if !enableHTTP2 { |
| 65 | + tlsOpts = append(tlsOpts, disableHTTP2) |
| 66 | + } |
| 67 | + |
| 68 | + webhookServer := webhook.NewServer(webhook.Options{ |
| 69 | + TLSOpts: tlsOpts, |
| 70 | + }) |
| 71 | + |
| 72 | + // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server. |
| 73 | + // More info: |
| 74 | + // - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/server |
| 75 | + // - https://book.kubebuilder.io/reference/metrics.html |
| 76 | + metricsServerOptions := metricsserver.Options{ |
| 77 | + BindAddress: metricsAddr, |
| 78 | + SecureServing: secureMetrics, |
| 79 | + // TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are |
| 80 | + // not provided, self-signed certificates will be generated by default. This option is not recommended for |
| 81 | + // production environments as self-signed certificates do not offer the same level of trust and security |
| 82 | + // as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing |
| 83 | + // unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName |
| 84 | + // to provide certificates, ensuring the server communicates using trusted and secure certificates. |
| 85 | + TLSOpts: tlsOpts, |
| 86 | + } |
| 87 | + |
| 88 | + if secureMetrics { |
| 89 | + // FilterProvider is used to protect the metrics endpoint with authn/authz. |
| 90 | + // These configurations ensure that only authorized users and service accounts |
| 91 | + // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info: |
| 92 | + // https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization |
| 93 | + metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization |
| 94 | + } |
| 95 | + |
| 96 | + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ |
| 97 | + Scheme: scheme, |
| 98 | + Metrics: metricsServerOptions, |
| 99 | + WebhookServer: webhookServer, |
| 100 | + HealthProbeBindAddress: probeAddr, |
| 101 | + LeaderElection: enableLeaderElection, |
| 102 | + LeaderElectionID: "c21b75f2.argoproj.io", |
| 103 | + // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily |
| 104 | + // when the Manager ends. This requires the binary to immediately end when the |
| 105 | + // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly |
| 106 | + // speeds up voluntary leader transitions as the new leader don't have to wait |
| 107 | + // LeaseDuration time first. |
| 108 | + // |
| 109 | + // In the default scaffold provided, the program ends immediately after |
| 110 | + // the manager stops, so would be fine to enable this option. However, |
| 111 | + // if you are doing or is intended to do any operation such as perform cleanups |
| 112 | + // after the manager stops then its usage might be unsafe. |
| 113 | + // LeaderElectionReleaseOnCancel: true, |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + setupLog.Error(err, "unable to start manager") |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + reconcilerLogger := ctrl.Log.WithName("reconciler").WithName("ImageUpdater") |
| 121 | + if err = (&controller.ImageUpdaterReconciler{ |
| 122 | + Client: mgr.GetClient(), |
| 123 | + Scheme: mgr.GetScheme(), |
| 124 | + Interval: Interval, |
| 125 | + Log: reconcilerLogger, |
| 126 | + }).SetupWithManager(mgr); err != nil { |
| 127 | + setupLog.Error(err, "unable to create controller", "controller", "ImageUpdater") |
| 128 | + return err |
| 129 | + } |
| 130 | + // +kubebuilder:scaffold:builder |
| 131 | + |
| 132 | + if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { |
| 133 | + setupLog.Error(err, "unable to set up health check") |
| 134 | + return err |
| 135 | + } |
| 136 | + if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { |
| 137 | + setupLog.Error(err, "unable to set up ready check") |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + setupLog.Info("starting manager") |
| 142 | + if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { |
| 143 | + setupLog.Error(err, "problem running manager") |
| 144 | + return err |
| 145 | + } |
| 146 | + return nil |
| 147 | + }, |
| 148 | + } |
| 149 | + controllerCmd.Flags().StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+ |
| 150 | + "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.") |
| 151 | + controllerCmd.Flags().StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") |
| 152 | + controllerCmd.Flags().BoolVar(&enableLeaderElection, "leader-elect", false, |
| 153 | + "Enable leader election for controller manager. "+ |
| 154 | + "Enabling this will ensure there is only one active controller manager.") |
| 155 | + controllerCmd.Flags().BoolVar(&secureMetrics, "metrics-secure", true, |
| 156 | + "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.") |
| 157 | + controllerCmd.Flags().BoolVar(&enableHTTP2, "enable-http2", false, |
| 158 | + "If set, HTTP/2 will be enabled for the metrics and webhook servers") |
| 159 | + controllerCmd.Flags().StringVar(&LogLevel, "loglevel", "info", |
| 160 | + "set the loglevel to one of trace|debug|info|warn|error") |
| 161 | + controllerCmd.Flags().DurationVar(&Interval, "interval", 2*time.Minute, |
| 162 | + "interval for how often to check for updates") |
| 163 | + |
| 164 | + return controllerCmd |
| 165 | +} |
0 commit comments