@@ -2,12 +2,14 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"flag"
6
7
"fmt"
7
8
"os"
8
9
"runtime"
9
10
10
11
"github.com/nginxinc/nginx-ingress-operator/pkg/controller/nginxingresscontroller"
12
+
11
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
14
"k8s.io/apimachinery/pkg/runtime/schema"
13
15
@@ -25,7 +27,6 @@ import (
25
27
"github.com/operator-framework/operator-sdk/pkg/leader"
26
28
"github.com/operator-framework/operator-sdk/pkg/log/zap"
27
29
"github.com/operator-framework/operator-sdk/pkg/metrics"
28
- "github.com/operator-framework/operator-sdk/pkg/restmapper"
29
30
sdkVersion "github.com/operator-framework/operator-sdk/version"
30
31
"github.com/spf13/pflag"
31
32
v1 "k8s.io/api/core/v1"
@@ -74,12 +75,6 @@ func main() {
74
75
75
76
printVersion ()
76
77
77
- namespace , err := k8sutil .GetWatchNamespace ()
78
- if err != nil {
79
- log .Error (err , "Failed to get watch namespace" )
80
- os .Exit (1 )
81
- }
82
-
83
78
// Get a config to talk to the apiserver
84
79
cfg , err := config .GetConfig ()
85
80
if err != nil {
@@ -97,10 +92,7 @@ func main() {
97
92
98
93
// Create a new Cmd to provide shared dependencies and start components
99
94
mgr , err := manager .New (cfg , manager.Options {
100
- Namespace : v1 .NamespaceAll ,
101
- // This is because the operator framework generated this code using a deprecated call to NewDynamicRESTMapper
102
- //nolint
103
- MapperProvider : restmapper .NewDynamicRESTMapper ,
95
+ Namespace : v1 .NamespaceAll ,
104
96
MetricsBindAddress : fmt .Sprintf ("%s:%d" , metricsHost , metricsPort ),
105
97
})
106
98
if err != nil {
@@ -140,7 +132,30 @@ func main() {
140
132
os .Exit (1 )
141
133
}
142
134
143
- if err = serveCRMetrics (cfg ); err != nil {
135
+ addMetrics (ctx , cfg )
136
+
137
+ log .Info ("Starting the Cmd." )
138
+
139
+ // Start the Cmd
140
+ if err := mgr .Start (signals .SetupSignalHandler ()); err != nil {
141
+ log .Error (err , "Manager exited non-zero" )
142
+ os .Exit (1 )
143
+ }
144
+ }
145
+
146
+ // addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
147
+ // the Prometheus operator
148
+ func addMetrics (ctx context.Context , cfg * rest.Config ) {
149
+ // Get the namespace the operator is currently deployed in.
150
+ operatorNs , err := k8sutil .GetOperatorNamespace ()
151
+ if err != nil {
152
+ if errors .Is (err , k8sutil .ErrRunLocal ) {
153
+ log .Info ("Skipping CR metrics server creation; not running in a cluster." )
154
+ return
155
+ }
156
+ }
157
+
158
+ if err := serveCRMetrics (cfg , operatorNs ); err != nil {
144
159
log .Info ("Could not generate and serve custom resource metrics" , "error" , err .Error ())
145
160
}
146
161
@@ -149,6 +164,7 @@ func main() {
149
164
{Port : metricsPort , Name : metrics .OperatorPortName , Protocol : v1 .ProtocolTCP , TargetPort : intstr.IntOrString {Type : intstr .Int , IntVal : metricsPort }},
150
165
{Port : operatorMetricsPort , Name : metrics .CRPortName , Protocol : v1 .ProtocolTCP , TargetPort : intstr.IntOrString {Type : intstr .Int , IntVal : operatorMetricsPort }},
151
166
}
167
+
152
168
// Create Service object to expose the metrics port(s).
153
169
service , err := metrics .CreateMetricsService (ctx , cfg , servicePorts )
154
170
if err != nil {
@@ -158,7 +174,9 @@ func main() {
158
174
// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
159
175
// necessary to configure Prometheus to scrape metrics from this operator.
160
176
services := []* v1.Service {service }
161
- _ , err = metrics .CreateServiceMonitors (cfg , namespace , services )
177
+
178
+ // The ServiceMonitor is created in the same namespace where the operator is deployed
179
+ _ , err = metrics .CreateServiceMonitors (cfg , operatorNs , services )
162
180
if err != nil {
163
181
log .Info ("Could not create ServiceMonitor object" , "error" , err .Error ())
164
182
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
@@ -167,32 +185,22 @@ func main() {
167
185
log .Info ("Install prometheus-operator in your cluster to create ServiceMonitor objects" , "error" , err .Error ())
168
186
}
169
187
}
170
-
171
- log .Info ("Starting the Cmd." )
172
-
173
- // Start the Cmd
174
- if err := mgr .Start (signals .SetupSignalHandler ()); err != nil {
175
- log .Error (err , "Manager exited non-zero" )
176
- os .Exit (1 )
177
- }
178
188
}
179
189
180
190
// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
181
191
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
182
- func serveCRMetrics (cfg * rest.Config ) error {
183
- // Below function returns filtered operator/CustomResource specific GVKs.
184
- // For more control override the below GVK list with your own custom logic.
192
+ func serveCRMetrics (cfg * rest.Config , operatorNs string ) error {
193
+ // The function below returns a list of filtered operator/CR specific GVKs. For more control, override the GVK list below
194
+ // with your own custom logic. Note that if you are adding third party API schemas, probably you will need to
195
+ // customize this implementation to avoid permissions issues.
185
196
filteredGVK , err := k8sutil .GetGVKsFromAddToScheme (apis .AddToScheme )
186
197
if err != nil {
187
198
return err
188
199
}
189
- // Get the namespace the operator is currently deployed in.
190
- operatorNs , err := k8sutil .GetOperatorNamespace ()
191
- if err != nil {
192
- return err
193
- }
200
+
194
201
// To generate metrics in other namespaces, add the values below.
195
202
ns := []string {operatorNs }
203
+
196
204
// Generate and serve custom resource specific metrics.
197
205
err = kubemetrics .GenerateAndServeCRMetrics (cfg , ns , filteredGVK , metricsHost , operatorMetricsPort )
198
206
if err != nil {
0 commit comments