@@ -133,7 +133,7 @@ func main() {
133133 }
134134
135135 // Add the Metrics Service
136- addMetrics (ctx , cfg , namespace )
136+ addMetrics (ctx , cfg )
137137
138138 log .Info ("Starting the Manager." )
139139
@@ -146,12 +146,17 @@ func main() {
146146
147147// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
148148// the Prometheus operator
149- func addMetrics (ctx context.Context , cfg * rest.Config , namespace string ) {
150- if err := serveCRMetrics (cfg ); err != nil {
149+ func addMetrics (ctx context.Context , cfg * rest.Config ) {
150+ // Get the namespace the operator is currently deployed in.
151+ operatorNs , err := k8sutil .GetOperatorNamespace ()
152+ if err != nil {
151153 if errors .Is (err , k8sutil .ErrRunLocal ) {
152154 log .Info ("Skipping CR metrics server creation; not running in a cluster." )
153155 return
154156 }
157+ }
158+
159+ if err := serveCRMetrics (cfg , operatorNs ); err != nil {
155160 log .Info ("Could not generate and serve custom resource metrics" , "error" , err .Error ())
156161 }
157162
@@ -170,7 +175,9 @@ func addMetrics(ctx context.Context, cfg *rest.Config, namespace string) {
170175 // CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
171176 // necessary to configure Prometheus to scrape metrics from this operator.
172177 services := []* v1.Service {service }
173- _ , err = metrics .CreateServiceMonitors (cfg , namespace , services )
178+
179+ // The ServiceMonitor is created in the same namespace where the operator is deployed
180+ _ , err = metrics .CreateServiceMonitors (cfg , operatorNs , services )
174181 if err != nil {
175182 log .Info ("Could not create ServiceMonitor object" , "error" , err .Error ())
176183 // If this operator is deployed to a cluster without the prometheus-operator running, it will return
@@ -183,20 +190,22 @@ func addMetrics(ctx context.Context, cfg *rest.Config, namespace string) {
183190
184191// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
185192// It serves those metrics on "http://metricsHost:operatorMetricsPort".
186- func serveCRMetrics (cfg * rest.Config ) error {
187- // Below function returns filtered operator/CustomResource specific GVKs.
188- // For more control override the below GVK list with your own custom logic.
193+ func serveCRMetrics (cfg * rest.Config , operatorNs string ) error {
194+ // The function below returns a list of filtered operator/CR specific GVKs. For more control, override the GVK list below
195+ // with your own custom logic. Note that if you are adding third party API schemas, probably you will need to
196+ // customize this implementation to avoid permissions issues.
189197 filteredGVK , err := k8sutil .GetGVKsFromAddToScheme (apis .AddToScheme )
190198 if err != nil {
191199 return err
192200 }
193- // Get the namespace the operator is currently deployed in.
194- operatorNs , err := k8sutil .GetOperatorNamespace ()
201+
202+ // The metrics will be generated from the namespaces which are returned here.
203+ // NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error.
204+ ns , err := kubemetrics .GetNamespacesForMetrics (operatorNs )
195205 if err != nil {
196206 return err
197207 }
198- // To generate metrics in other namespaces, add the values below.
199- ns := []string {operatorNs }
208+
200209 // Generate and serve custom resource specific metrics.
201210 err = kubemetrics .GenerateAndServeCRMetrics (cfg , ns , filteredGVK , metricsHost , operatorMetricsPort )
202211 if err != nil {
0 commit comments