aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/DAaaS/microservices/collectd-operator/cmd/manager
diff options
context:
space:
mode:
authorDileep Ranganathan <dileep.ranganathan@intel.com>2019-08-08 00:07:08 -0700
committerDileep Ranganathan <dileep.ranganathan@intel.com>2019-08-12 10:51:22 -0700
commitd59e778f2b97c96fe7f6c3945f4bb9c91ff01e74 (patch)
tree7119c654f748a5bc6e1fe28dcdaea7ce4ed4c28a /vnfs/DAaaS/microservices/collectd-operator/cmd/manager
parent3d1e26dc3e9af8cc066bef9971bb870b841c903b (diff)
Upgrade operator-sdk bump version to 0.9.0
Upgrade operator-sdk bump version to 0.9.0 Issue-ID: ONAPARC-461 Signed-off-by: Dileep Ranganathan <dileep.ranganathan@intel.com> Change-Id: I2b137599c3df9a0332862f46c5ac54892eeb5de2
Diffstat (limited to 'vnfs/DAaaS/microservices/collectd-operator/cmd/manager')
-rw-r--r--vnfs/DAaaS/microservices/collectd-operator/cmd/manager/main.go51
1 files changed, 44 insertions, 7 deletions
diff --git a/vnfs/DAaaS/microservices/collectd-operator/cmd/manager/main.go b/vnfs/DAaaS/microservices/collectd-operator/cmd/manager/main.go
index 2db96c80..1660c7bd 100644
--- a/vnfs/DAaaS/microservices/collectd-operator/cmd/manager/main.go
+++ b/vnfs/DAaaS/microservices/collectd-operator/cmd/manager/main.go
@@ -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
+}