|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | + |
| 12 | + // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) |
| 13 | + _ "k8s.io/client-go/plugin/pkg/client/auth" |
| 14 | + "k8s.io/client-go/rest" |
| 15 | + |
| 16 | + "github.com/jmelis/dv-operator/pkg/apis" |
| 17 | + "github.com/jmelis/dv-operator/pkg/controller" |
| 18 | + "github.com/jmelis/dv-operator/version" |
| 19 | + |
| 20 | + "github.com/operator-framework/operator-sdk/pkg/k8sutil" |
| 21 | + kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics" |
| 22 | + "github.com/operator-framework/operator-sdk/pkg/leader" |
| 23 | + "github.com/operator-framework/operator-sdk/pkg/log/zap" |
| 24 | + "github.com/operator-framework/operator-sdk/pkg/metrics" |
| 25 | + sdkVersion "github.com/operator-framework/operator-sdk/version" |
| 26 | + "github.com/spf13/pflag" |
| 27 | + v1 "k8s.io/api/core/v1" |
| 28 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 31 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/manager/signals" |
| 34 | +) |
| 35 | + |
| 36 | +// Change below variables to serve metrics on different host or port. |
| 37 | +var ( |
| 38 | + metricsHost = "0.0.0.0" |
| 39 | + metricsPort int32 = 8383 |
| 40 | + operatorMetricsPort int32 = 8686 |
| 41 | +) |
| 42 | +var log = logf.Log.WithName("cmd") |
| 43 | + |
| 44 | +func printVersion() { |
| 45 | + log.Info(fmt.Sprintf("Operator Version: %s", version.Version)) |
| 46 | + log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) |
| 47 | + log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) |
| 48 | + log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version)) |
| 49 | +} |
| 50 | + |
| 51 | +func main() { |
| 52 | + // Add the zap logger flag set to the CLI. The flag set must |
| 53 | + // be added before calling pflag.Parse(). |
| 54 | + pflag.CommandLine.AddFlagSet(zap.FlagSet()) |
| 55 | + |
| 56 | + // Add flags registered by imported packages (e.g. glog and |
| 57 | + // controller-runtime) |
| 58 | + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) |
| 59 | + |
| 60 | + pflag.Parse() |
| 61 | + |
| 62 | + // Use a zap logr.Logger implementation. If none of the zap |
| 63 | + // flags are configured (or if the zap flag set is not being |
| 64 | + // used), this defaults to a production zap logger. |
| 65 | + // |
| 66 | + // The logger instantiated here can be changed to any logger |
| 67 | + // implementing the logr.Logger interface. This logger will |
| 68 | + // be propagated through the whole operator, generating |
| 69 | + // uniform and structured logs. |
| 70 | + logf.SetLogger(zap.Logger()) |
| 71 | + |
| 72 | + printVersion() |
| 73 | + |
| 74 | + namespace, err := k8sutil.GetWatchNamespace() |
| 75 | + if err != nil { |
| 76 | + log.Error(err, "Failed to get watch namespace") |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + |
| 80 | + // Get a config to talk to the apiserver |
| 81 | + cfg, err := config.GetConfig() |
| 82 | + if err != nil { |
| 83 | + log.Error(err, "") |
| 84 | + os.Exit(1) |
| 85 | + } |
| 86 | + |
| 87 | + ctx := context.TODO() |
| 88 | + // Become the leader before proceeding |
| 89 | + err = leader.Become(ctx, "dv-operator-lock") |
| 90 | + if err != nil { |
| 91 | + log.Error(err, "") |
| 92 | + os.Exit(1) |
| 93 | + } |
| 94 | + |
| 95 | + // Set default manager options |
| 96 | + options := manager.Options{ |
| 97 | + Namespace: namespace, |
| 98 | + MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), |
| 99 | + } |
| 100 | + |
| 101 | + // Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2) |
| 102 | + // Note that this is not intended to be used for excluding namespaces, this is better done via a Predicate |
| 103 | + // Also note that you may face performance issues when using this with a high number of namespaces. |
| 104 | + // More Info: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder |
| 105 | + if strings.Contains(namespace, ",") { |
| 106 | + options.Namespace = "" |
| 107 | + options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ",")) |
| 108 | + } |
| 109 | + |
| 110 | + // Create a new manager to provide shared dependencies and start components |
| 111 | + mgr, err := manager.New(cfg, options) |
| 112 | + if err != nil { |
| 113 | + log.Error(err, "") |
| 114 | + os.Exit(1) |
| 115 | + } |
| 116 | + |
| 117 | + log.Info("Registering Components.") |
| 118 | + |
| 119 | + // Setup Scheme for all resources |
| 120 | + if err := apis.AddToScheme(mgr.GetScheme()); err != nil { |
| 121 | + log.Error(err, "") |
| 122 | + os.Exit(1) |
| 123 | + } |
| 124 | + |
| 125 | + // Setup all Controllers |
| 126 | + if err := controller.AddToManager(mgr); err != nil { |
| 127 | + log.Error(err, "") |
| 128 | + os.Exit(1) |
| 129 | + } |
| 130 | + |
| 131 | + // Add the Metrics Service |
| 132 | + addMetrics(ctx, cfg) |
| 133 | + |
| 134 | + log.Info("Starting the Cmd.") |
| 135 | + |
| 136 | + // Start the Cmd |
| 137 | + if err := mgr.Start(signals.SetupSignalHandler()); err != nil { |
| 138 | + log.Error(err, "Manager exited non-zero") |
| 139 | + os.Exit(1) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using |
| 144 | +// the Prometheus operator |
| 145 | +func addMetrics(ctx context.Context, cfg *rest.Config) { |
| 146 | + // Get the namespace the operator is currently deployed in. |
| 147 | + operatorNs, err := k8sutil.GetOperatorNamespace() |
| 148 | + if err != nil { |
| 149 | + if errors.Is(err, k8sutil.ErrRunLocal) { |
| 150 | + log.Info("Skipping CR metrics server creation; not running in a cluster.") |
| 151 | + return |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if err := serveCRMetrics(cfg, operatorNs); err != nil { |
| 156 | + log.Info("Could not generate and serve custom resource metrics", "error", err.Error()) |
| 157 | + } |
| 158 | + |
| 159 | + // Add to the below struct any other metrics ports you want to expose. |
| 160 | + servicePorts := []v1.ServicePort{ |
| 161 | + {Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}}, |
| 162 | + {Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}}, |
| 163 | + } |
| 164 | + |
| 165 | + // Create Service object to expose the metrics port(s). |
| 166 | + service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts) |
| 167 | + if err != nil { |
| 168 | + log.Info("Could not create metrics Service", "error", err.Error()) |
| 169 | + } |
| 170 | + |
| 171 | + // CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources |
| 172 | + // necessary to configure Prometheus to scrape metrics from this operator. |
| 173 | + services := []*v1.Service{service} |
| 174 | + |
| 175 | + // The ServiceMonitor is created in the same namespace where the operator is deployed |
| 176 | + _, err = metrics.CreateServiceMonitors(cfg, operatorNs, services) |
| 177 | + if err != nil { |
| 178 | + log.Info("Could not create ServiceMonitor object", "error", err.Error()) |
| 179 | + // If this operator is deployed to a cluster without the prometheus-operator running, it will return |
| 180 | + // ErrServiceMonitorNotPresent, which can be used to safely skip ServiceMonitor creation. |
| 181 | + if err == metrics.ErrServiceMonitorNotPresent { |
| 182 | + log.Info("Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error()) |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types. |
| 188 | +// It serves those metrics on "http://metricsHost:operatorMetricsPort". |
| 189 | +func serveCRMetrics(cfg *rest.Config, operatorNs string) error { |
| 190 | + // The function below returns a list of filtered operator/CR specific GVKs. For more control, override the GVK list below |
| 191 | + // with your own custom logic. Note that if you are adding third party API schemas, probably you will need to |
| 192 | + // customize this implementation to avoid permissions issues. |
| 193 | + filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme) |
| 194 | + if err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + |
| 198 | + // The metrics will be generated from the namespaces which are returned here. |
| 199 | + // NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error. |
| 200 | + ns, err := kubemetrics.GetNamespacesForMetrics(operatorNs) |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + |
| 205 | + // Generate and serve custom resource specific metrics. |
| 206 | + err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort) |
| 207 | + if err != nil { |
| 208 | + return err |
| 209 | + } |
| 210 | + return nil |
| 211 | +} |
0 commit comments