|
| 1 | +/* |
| 2 | +Copyright 2020 The Operator-SDK Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package run |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "os" |
| 22 | + "runtime" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/spf13/cobra" |
| 26 | + "github.com/spf13/pflag" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 29 | + zapl "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 30 | + |
| 31 | + "github.com/joelanford/helm-operator/internal/version" |
| 32 | + "github.com/joelanford/helm-operator/pkg/annotation" |
| 33 | + "github.com/joelanford/helm-operator/pkg/manager" |
| 34 | + "github.com/joelanford/helm-operator/pkg/reconciler" |
| 35 | + "github.com/joelanford/helm-operator/pkg/watches" |
| 36 | +) |
| 37 | + |
| 38 | +func NewCmd() *cobra.Command { |
| 39 | + r := run{} |
| 40 | + zapfs := flag.NewFlagSet("zap", flag.ExitOnError) |
| 41 | + opts := &zapl.Options{} |
| 42 | + opts.BindFlags(zapfs) |
| 43 | + |
| 44 | + cmd := &cobra.Command{ |
| 45 | + Use: "run", |
| 46 | + Short: "Run the operator", |
| 47 | + Run: func(cmd *cobra.Command, _ []string) { |
| 48 | + logf.SetLogger(zapl.New(zapl.UseFlagOptions(opts))) |
| 49 | + r.run(cmd) |
| 50 | + }, |
| 51 | + } |
| 52 | + r.bindFlags(cmd.Flags()) |
| 53 | + cmd.Flags().AddGoFlagSet(zapfs) |
| 54 | + return cmd |
| 55 | +} |
| 56 | + |
| 57 | +type run struct { |
| 58 | + metricsAddr string |
| 59 | + enableLeaderElection bool |
| 60 | + leaderElectionID string |
| 61 | + leaderElectionNamespace string |
| 62 | + |
| 63 | + watchesFile string |
| 64 | + defaultMaxConcurrentReconciles int |
| 65 | + defaultReconcilePeriod time.Duration |
| 66 | +} |
| 67 | + |
| 68 | +func (r *run) bindFlags(fs *pflag.FlagSet) { |
| 69 | + fs.StringVar(&r.metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") |
| 70 | + fs.BoolVar(&r.enableLeaderElection, "enable-leader-election", false, |
| 71 | + "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.") |
| 72 | + fs.StringVar(&r.leaderElectionID, "leader-election-id", "", |
| 73 | + "Name of the configmap that is used for holding the leader lock.") |
| 74 | + fs.StringVar(&r.leaderElectionNamespace, "leader-election-namespace", "", |
| 75 | + "Namespace in which to create the leader election configmap for holding the leader lock (required if running locally with leader election enabled).") |
| 76 | + |
| 77 | + fs.StringVar(&r.watchesFile, "watches-file", "./watches.yaml", "Path to watches.yaml file.") |
| 78 | + fs.DurationVar(&r.defaultReconcilePeriod, "reconcile-period", time.Minute, "Default reconcile period for controllers (use 0 to disable periodic reconciliation)") |
| 79 | + fs.IntVar(&r.defaultMaxConcurrentReconciles, "max-concurrent-reconciles", runtime.NumCPU(), "Default maximum number of concurrent reconciles for controllers.") |
| 80 | +} |
| 81 | + |
| 82 | +var log = logf.Log.WithName("cmd") |
| 83 | + |
| 84 | +func printVersion() { |
| 85 | + log.Info("Version", |
| 86 | + "Go Version", runtime.Version(), |
| 87 | + "GOOS", runtime.GOOS, |
| 88 | + "GOARCH", runtime.GOARCH, |
| 89 | + "helm-operator", version.Version) |
| 90 | +} |
| 91 | + |
| 92 | +func (r *run) run(cmd *cobra.Command) { |
| 93 | + printVersion() |
| 94 | + |
| 95 | + // Deprecated: OPERATOR_NAME environment variable is an artifact of the legacy operator-sdk project scaffolding. |
| 96 | + // Flag `--leader-election-id` should be used instead. |
| 97 | + if operatorName, found := os.LookupEnv("OPERATOR_NAME"); found { |
| 98 | + log.Info("environment variable OPERATOR_NAME has been deprecated, use --leader-election-id instead.") |
| 99 | + if cmd.Flags().Lookup("leader-election-id").Changed { |
| 100 | + log.Info("ignoring OPERATOR_NAME environment variable since --leader-election-id is set") |
| 101 | + } else { |
| 102 | + r.leaderElectionID = operatorName |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + options := ctrl.Options{ |
| 107 | + MetricsBindAddress: r.metricsAddr, |
| 108 | + LeaderElection: r.enableLeaderElection, |
| 109 | + LeaderElectionID: r.leaderElectionID, |
| 110 | + LeaderElectionNamespace: r.leaderElectionNamespace, |
| 111 | + NewClient: manager.NewDelegatingClientFunc(), |
| 112 | + } |
| 113 | + manager.ConfigureWatchNamespaces(&options, log) |
| 114 | + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options) |
| 115 | + if err != nil { |
| 116 | + log.Error(err, "unable to start manager") |
| 117 | + os.Exit(1) |
| 118 | + } |
| 119 | + |
| 120 | + ws, err := watches.Load(r.watchesFile) |
| 121 | + if err != nil { |
| 122 | + log.Error(err, "unable to load watches.yaml", "path", r.watchesFile) |
| 123 | + os.Exit(1) |
| 124 | + } |
| 125 | + |
| 126 | + for _, w := range ws { |
| 127 | + reconcilePeriod := r.defaultReconcilePeriod |
| 128 | + if w.ReconcilePeriod != nil { |
| 129 | + reconcilePeriod = w.ReconcilePeriod.Duration |
| 130 | + } |
| 131 | + |
| 132 | + maxConcurrentReconciles := r.defaultMaxConcurrentReconciles |
| 133 | + if w.MaxConcurrentReconciles != nil { |
| 134 | + maxConcurrentReconciles = *w.MaxConcurrentReconciles |
| 135 | + } |
| 136 | + |
| 137 | + r, err := reconciler.New( |
| 138 | + reconciler.WithChart(*w.Chart), |
| 139 | + reconciler.WithGroupVersionKind(w.GroupVersionKind), |
| 140 | + reconciler.WithOverrideValues(w.OverrideValues), |
| 141 | + reconciler.SkipDependentWatches(w.WatchDependentResources != nil && !*w.WatchDependentResources), |
| 142 | + reconciler.WithMaxConcurrentReconciles(maxConcurrentReconciles), |
| 143 | + reconciler.WithReconcilePeriod(reconcilePeriod), |
| 144 | + reconciler.WithInstallAnnotations(annotation.DefaultInstallAnnotations...), |
| 145 | + reconciler.WithUpgradeAnnotations(annotation.DefaultUpgradeAnnotations...), |
| 146 | + reconciler.WithUninstallAnnotations(annotation.DefaultUninstallAnnotations...), |
| 147 | + ) |
| 148 | + if err != nil { |
| 149 | + log.Error(err, "unable to create helm reconciler", "controller", "Helm") |
| 150 | + os.Exit(1) |
| 151 | + } |
| 152 | + |
| 153 | + if err := r.SetupWithManager(mgr); err != nil { |
| 154 | + log.Error(err, "unable to create controller", "controller", "Helm") |
| 155 | + os.Exit(1) |
| 156 | + } |
| 157 | + log.Info("configured watch", "gvk", w.GroupVersionKind, "chartPath", w.ChartPath, "maxConcurrentReconciles", maxConcurrentReconciles, "reconcilePeriod", reconcilePeriod) |
| 158 | + } |
| 159 | + |
| 160 | + log.Info("starting manager") |
| 161 | + if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { |
| 162 | + log.Error(err, "problem running manager") |
| 163 | + os.Exit(1) |
| 164 | + } |
| 165 | +} |
0 commit comments