Upgrade operator-sdk bump version to 0.9.0
[demo.git] / vnfs / DAaaS / microservices / collectd-operator / cmd / manager / main.go
index 2db96c8..1660c7b 100644 (file)
@@ -9,17 +9,21 @@ import (
 
        // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
        _ "k8s.io/client-go/plugin/pkg/client/auth"
+       "k8s.io/client-go/rest"
 
-       "demo/vnfs/DAaaS/microservices/collectd-operator/pkg/apis"
-       "demo/vnfs/DAaaS/microservices/collectd-operator/pkg/controller"
+       "collectd-operator/pkg/apis"
+       "collectd-operator/pkg/controller"
 
        "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/leader"
        "github.com/operator-framework/operator-sdk/pkg/log/zap"
        "github.com/operator-framework/operator-sdk/pkg/metrics"
        "github.com/operator-framework/operator-sdk/pkg/restmapper"
        sdkVersion "github.com/operator-framework/operator-sdk/version"
        "github.com/spf13/pflag"
+       v1 "k8s.io/api/core/v1"
+       "k8s.io/apimachinery/pkg/util/intstr"
        "sigs.k8s.io/controller-runtime/pkg/client/config"
        "sigs.k8s.io/controller-runtime/pkg/manager"
        logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -28,8 +32,9 @@ import (
 
 // Change below variables to serve metrics on different host or port.
 var (
-       metricsHost       = "0.0.0.0"
-       metricsPort int32 = 8383
+       metricsHost               = "0.0.0.0"
+       metricsPort         int32 = 8383
+       operatorMetricsPort int32 = 8686
 )
 var log = logf.Log.WithName("cmd")
 
@@ -76,7 +81,6 @@ func main() {
        }
 
        ctx := context.TODO()
-
        // Become the leader before proceeding
        err = leader.Become(ctx, "collectd-operator-lock")
        if err != nil {
@@ -109,8 +113,17 @@ func main() {
                os.Exit(1)
        }
 
-       // Create Service object to expose the metrics port.
-       _, err = metrics.ExposeMetricsPort(ctx, metricsPort)
+       if err = serveCRMetrics(cfg); 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: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
+               {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).
+       _, err = metrics.CreateMetricsService(ctx, cfg, servicePorts)
        if err != nil {
                log.Info(err.Error())
        }
@@ -123,3 +136,27 @@ func main() {
                os.Exit(1)
        }
 }
+
+// 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) error {
+       // Below function returns filtered operator/CustomResource specific GVKs.
+       // For more control override the below GVK list with your own custom logic.
+       filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme)
+       if err != nil {
+               return err
+       }
+       // Get the namespace the operator is currently deployed in.
+       operatorNs, err := k8sutil.GetOperatorNamespace()
+       if err != nil {
+               return err
+       }
+       // To generate metrics in other namespaces, add the values below.
+       ns := []string{operatorNs}
+       // Generate and serve custom resource specific metrics.
+       err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort)
+       if err != nil {
+               return err
+       }
+       return nil
+}