|
| 1 | +/* |
| 2 | +Copyright 2022. |
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/tls" |
| 21 | + "flag" |
| 22 | + "os" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "go.uber.org/zap/zapcore" |
| 27 | + |
| 28 | + // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) |
| 29 | + // to ensure that exec-entrypoint and run can make use of them. |
| 30 | + _ "k8s.io/client-go/plugin/pkg/client/auth" |
| 31 | + |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 33 | + |
| 34 | + "k8s.io/apimachinery/pkg/runtime" |
| 35 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 36 | + "k8s.io/client-go/kubernetes" |
| 37 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 38 | + ctrl "sigs.k8s.io/controller-runtime" |
| 39 | + "sigs.k8s.io/controller-runtime/pkg/healthz" |
| 40 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 41 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 42 | + |
| 43 | + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 44 | + |
| 45 | + operatorv1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/operator/v1beta1" |
| 46 | + operatorcontrollers "github.com/openstack-k8s-operators/openstack-operator/controllers/operator" |
| 47 | + // +kubebuilder:scaffold:imports |
| 48 | +) |
| 49 | + |
| 50 | +var ( |
| 51 | + scheme = runtime.NewScheme() |
| 52 | + setupLog = ctrl.Log.WithName("setup") |
| 53 | +) |
| 54 | + |
| 55 | +func init() { |
| 56 | + utilruntime.Must(clientgoscheme.AddToScheme(scheme)) |
| 57 | + utilruntime.Must(operatorv1beta1.AddToScheme(scheme)) |
| 58 | + // +kubebuilder:scaffold:scheme |
| 59 | +} |
| 60 | + |
| 61 | +func main() { |
| 62 | + var metricsAddr string |
| 63 | + var enableLeaderElection bool |
| 64 | + var probeAddr string |
| 65 | + var enableHTTP2 bool |
| 66 | + flag.BoolVar(&enableHTTP2, "enable-http2", enableHTTP2, "If HTTP/2 should be enabled for the metrics and webhook servers.") |
| 67 | + flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") |
| 68 | + flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") |
| 69 | + flag.BoolVar(&enableLeaderElection, "leader-elect", false, |
| 70 | + "Enable leader election for controller manager. "+ |
| 71 | + "Enabling this will ensure there is only one active controller manager.") |
| 72 | + devMode, err := strconv.ParseBool(os.Getenv("DEV_MODE")) |
| 73 | + if err != nil { |
| 74 | + devMode = true |
| 75 | + } |
| 76 | + opts := zap.Options{ |
| 77 | + Development: devMode, |
| 78 | + TimeEncoder: zapcore.ISO8601TimeEncoder, |
| 79 | + } |
| 80 | + opts.BindFlags(flag.CommandLine) |
| 81 | + flag.Parse() |
| 82 | + |
| 83 | + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) |
| 84 | + |
| 85 | + disableHTTP2 := func(c *tls.Config) { |
| 86 | + if enableHTTP2 { |
| 87 | + return |
| 88 | + } |
| 89 | + c.NextProtos = []string{"http/1.1"} |
| 90 | + } |
| 91 | + |
| 92 | + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ |
| 93 | + Scheme: scheme, |
| 94 | + Metrics: metricsserver.Options{ |
| 95 | + BindAddress: metricsAddr, |
| 96 | + }, |
| 97 | + HealthProbeBindAddress: probeAddr, |
| 98 | + LeaderElection: enableLeaderElection, |
| 99 | + LeaderElectionID: "40ba705e.openstack.org", |
| 100 | + WebhookServer: webhook.NewServer( |
| 101 | + webhook.Options{ |
| 102 | + Port: 9443, |
| 103 | + TLSOpts: []func(config *tls.Config){disableHTTP2}, |
| 104 | + }), |
| 105 | + // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily |
| 106 | + // when the Manager ends. This requires the binary to immediately end when the |
| 107 | + // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly |
| 108 | + // speeds up voluntary leader transitions as the new leader don't have to wait |
| 109 | + // LeaseDuration time first. |
| 110 | + // |
| 111 | + // In the default scaffold provided, the program ends immediately after |
| 112 | + // the manager stops, so would be fine to enable this option. However, |
| 113 | + // if you are doing or is intended to do any operation such as perform cleanups |
| 114 | + // after the manager stops then its usage might be unsafe. |
| 115 | + // LeaderElectionReleaseOnCancel: true, |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + setupLog.Error(err, "unable to start manager") |
| 119 | + os.Exit(1) |
| 120 | + } |
| 121 | + // Setup the context that's going to be used in controllers and for the manager. |
| 122 | + ctx := ctrl.SetupSignalHandler() |
| 123 | + |
| 124 | + cfg, err := config.GetConfig() |
| 125 | + if err != nil { |
| 126 | + setupLog.Error(err, "") |
| 127 | + os.Exit(1) |
| 128 | + } |
| 129 | + kclient, err := kubernetes.NewForConfig(cfg) |
| 130 | + if err != nil { |
| 131 | + setupLog.Error(err, "") |
| 132 | + os.Exit(1) |
| 133 | + } |
| 134 | + |
| 135 | + // Webhooks |
| 136 | + checker := healthz.Ping |
| 137 | + if strings.ToLower(os.Getenv("ENABLE_WEBHOOKS")) != "false" { |
| 138 | + |
| 139 | + checker = mgr.GetWebhookServer().StartedChecker() |
| 140 | + } |
| 141 | + |
| 142 | + if err = (&operatorcontrollers.OpenStackReconciler{ |
| 143 | + Client: mgr.GetClient(), |
| 144 | + Scheme: mgr.GetScheme(), |
| 145 | + Kclient: kclient, |
| 146 | + }).SetupWithManager(mgr); err != nil { |
| 147 | + setupLog.Error(err, "unable to create controller", "controller", "OpenStack") |
| 148 | + os.Exit(1) |
| 149 | + } |
| 150 | + // +kubebuilder:scaffold:builder |
| 151 | + if err := mgr.AddHealthzCheck("healthz", checker); err != nil { |
| 152 | + setupLog.Error(err, "unable to set up health check") |
| 153 | + os.Exit(1) |
| 154 | + } |
| 155 | + if err := mgr.AddReadyzCheck("readyz", checker); err != nil { |
| 156 | + setupLog.Error(err, "unable to set up ready check") |
| 157 | + os.Exit(1) |
| 158 | + } |
| 159 | + |
| 160 | + setupLog.Info("starting manager") |
| 161 | + if err := mgr.Start(ctx); err != nil { |
| 162 | + setupLog.Error(err, "problem running manager") |
| 163 | + os.Exit(1) |
| 164 | + } |
| 165 | +} |
0 commit comments