diff --git a/changelog/fragments/rm-helm-metrics.yml b/changelog/fragments/rm-helm-metrics.yml new file mode 100644 index 0000000000..539d0f68e4 --- /dev/null +++ b/changelog/fragments/rm-helm-metrics.yml @@ -0,0 +1,11 @@ +# entries is a list of entries to include in +# release notes and/or the migration guide +entries: + - description: > + Remove legacy metrics generation code. + kind: "removal" + # Is this a breaking change? + breaking: true + migration: + header: Remove legacy metrics generation code from cmd/helm-operator/main.go, and test-e2e-helm.sh checks for servicemonitor. + body: TBD \ No newline at end of file diff --git a/cmd/helm-operator/main.go b/cmd/helm-operator/main.go index 131d04859e..65cbb56479 100644 --- a/cmd/helm-operator/main.go +++ b/cmd/helm-operator/main.go @@ -15,18 +15,13 @@ package main import ( - "context" - "errors" "fmt" "os" "runtime" "strings" "github.com/spf13/pflag" - v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/cache" crclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -40,18 +35,11 @@ import ( "github.com/operator-framework/operator-sdk/pkg/helm/release" "github.com/operator-framework/operator-sdk/pkg/helm/watches" "github.com/operator-framework/operator-sdk/pkg/k8sutil" - kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics" "github.com/operator-framework/operator-sdk/pkg/log/zap" - "github.com/operator-framework/operator-sdk/pkg/metrics" sdkVersion "github.com/operator-framework/operator-sdk/version" ) -var ( - metricsHost = "0.0.0.0" - operatorMetricsPort int32 = 8686 - - log = logf.Log.WithName("cmd") -) +var log = logf.Log.WithName("cmd") func printVersion() { log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) @@ -135,7 +123,6 @@ func main() { log.Error(err, "Failed to create new manager factories.") os.Exit(1) } - var gvks []schema.GroupVersionKind for _, w := range ws { // Register the controller with the factory. err := controller.Add(mgr, controller.WatchOptions{ @@ -151,76 +138,11 @@ func main() { log.Error(err, "Failed to add manager factory to controller.") os.Exit(1) } - gvks = append(gvks, w.GroupVersionKind) } - addMetrics(context.TODO(), cfg, gvks) - // Start the Cmd if err = mgr.Start(signals.SetupSignalHandler()); err != nil { log.Error(err, "Manager exited non-zero.") os.Exit(1) } } - -// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using -// the Prometheus operator -func addMetrics(ctx context.Context, cfg *rest.Config, gvks []schema.GroupVersionKind) { - // Get the namespace the operator is currently deployed in. - operatorNs, err := k8sutil.GetOperatorNamespace() - if err != nil { - if errors.Is(err, k8sutil.ErrRunLocal) { - log.Info("Skipping CR metrics server creation; not running in a cluster.") - return - } - } - - if err := serveCRMetrics(cfg, operatorNs, gvks); err != nil { - log.Info("Could not generate and serve custom resource metrics", "error", err.Error()) - } - - // Add to the below struct any other metrics ports you want to expose. - servicePorts := []v1.ServicePort{ - {Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, - TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}}, - } - - // Create Service object to expose the metrics port(s). - service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts) - if err != nil { - log.Info("Could not create metrics Service", "error", err.Error()) - } - - // CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources - // necessary to configure Prometheus to scrape metrics from this operator. - services := []*v1.Service{service} - - // The ServiceMonitor is created in the same namespace where the operator is deployed - _, err = metrics.CreateServiceMonitors(cfg, operatorNs, services) - if err != nil { - log.Info("Could not create ServiceMonitor object", "error", err.Error()) - // If this operator is deployed to a cluster without the prometheus-operator running, it will return - // ErrServiceMonitorNotPresent, which can be used to safely skip ServiceMonitor creation. - if err == metrics.ErrServiceMonitorNotPresent { - log.Info("Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error()) - } - } -} - -// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types. -// It serves those metrics on "http://metricsHost:operatorMetricsPort". -func serveCRMetrics(cfg *rest.Config, operatorNs string, gvks []schema.GroupVersionKind) error { - // The metrics will be generated from the namespaces which are returned here. - // NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error. - ns, err := kubemetrics.GetNamespacesForMetrics(operatorNs) - if err != nil { - return err - } - - // Generate and serve custom resource specific metrics. - err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, gvks, metricsHost, operatorMetricsPort) - if err != nil { - return err - } - return nil -} diff --git a/hack/tests/e2e-helm.sh b/hack/tests/e2e-helm.sh index 215b06f14d..71c9d83a4d 100755 --- a/hack/tests/e2e-helm.sh +++ b/hack/tests/e2e-helm.sh @@ -84,13 +84,7 @@ test_operator() { exit 1 fi - header_text "verify that the servicemonitor is created" - if ! timeout 1m bash -c -- "until kubectl get servicemonitors/nginx-operator-metrics --namespace=${operator_namespace} > /dev/null 2>&1; do sleep 1; done"; - then - error_text "FAIL: Failed to get service monitor" - operator_logs - exit 1 - fi + release_name=$(kubectl get --namespace=${test_namespace} nginxes.helm.example.com nginx-sample -o jsonpath="{..status.deployedRelease.name}") nginx_deployment=$(kubectl get --namespace=${test_namespace} deployment -l "app.kubernetes.io/instance=${release_name}" -o jsonpath="{..metadata.name}") @@ -145,7 +139,6 @@ if echo $log | grep -q "failed to generate RBAC rules"; then exit 1 fi -install_service_monitor_crd sed -i".bak" -E -e 's/(FROM quay.io\/operator-framework\/helm-operator)(:.*)?/\1:dev/g' Dockerfile; rm -f Dockerfile.bak make docker-build IMG="$DEST_IMAGE" @@ -161,4 +154,4 @@ OPERATORDIR="$(pwd)" deploy_operator trap_add 'remove_operator' EXIT -test_operator +test_operator \ No newline at end of file diff --git a/internal/plugins/helm/v1/scaffolds/templates/manager/config.go b/internal/plugins/helm/v1/scaffolds/templates/manager/config.go index 4690238097..304492a7a9 100644 --- a/internal/plugins/helm/v1/scaffolds/templates/manager/config.go +++ b/internal/plugins/helm/v1/scaffolds/templates/manager/config.go @@ -93,14 +93,5 @@ spec: requests: cpu: 100m memory: 60Mi - env: - - name: WATCH_NAMESPACE - value: "" - - name: POD_NAME - valueFrom: - fieldRef: - fieldPath: metadata.name - - name: OPERATOR_NAME - value: "{{ .OperatorName }}" terminationGracePeriodSeconds: 10 `