@@ -6,30 +6,22 @@ package main
6
6
7
7
import (
8
8
"context"
9
- "errors"
10
9
"flag"
11
10
"fmt"
12
11
"os"
13
12
"runtime"
14
13
15
- "github.com/operator-framework/operator-sdk/pkg/k8sutil"
16
- kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
17
-
18
14
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
19
- "github.com/operator-framework/operator-sdk/pkg/metrics"
15
+
20
16
"github.com/operator-framework/operator-sdk/pkg/ready"
21
17
sdkVersion "github.com/operator-framework/operator-sdk/version"
22
18
"github.com/spf13/pflag"
23
19
24
- v1 "k8s.io/api/core/v1"
25
- "k8s.io/apimachinery/pkg/util/intstr"
26
20
_ "k8s.io/client-go/plugin/pkg/client/auth"
27
- "k8s.io/client-go/rest"
28
21
"sigs.k8s.io/controller-runtime/pkg/client/config"
29
22
"sigs.k8s.io/controller-runtime/pkg/manager"
30
23
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
31
24
32
- "github.com/shipwright-io/build/pkg/apis"
33
25
buildconfig "github.com/shipwright-io/build/pkg/config"
34
26
"github.com/shipwright-io/build/pkg/controller"
35
27
"github.com/shipwright-io/build/pkg/ctxlog"
@@ -39,9 +31,8 @@ import (
39
31
40
32
// Change below variables to serve metrics on different host or port.
41
33
var (
42
- metricsHost = "0.0.0.0"
43
- metricsPort int32 = 8383
44
- operatorMetricsPort int32 = 8686
34
+ metricsHost = "0.0.0.0"
35
+ metricsPort int32 = 8383
45
36
)
46
37
47
38
func printVersion (ctx context.Context ) {
@@ -77,12 +68,6 @@ func main() {
77
68
78
69
printVersion (ctx )
79
70
80
- namespace , err := k8sutil .GetWatchNamespace ()
81
- if err != nil {
82
- ctxlog .Error (ctx , err , "Failed to get watch namespace" )
83
- os .Exit (1 )
84
- }
85
-
86
71
// Get a config to talk to the apiserver
87
72
cfg , err := config .GetConfig ()
88
73
if err != nil {
@@ -119,8 +104,6 @@ func main() {
119
104
os .Exit (1 )
120
105
}
121
106
122
- // Add the Metrics Service
123
- addMetrics (ctx , cfg , namespace )
124
107
buildMetrics .InitPrometheus (buildCfg )
125
108
126
109
// Add optionally configured extra handlers to metrics endpoint
@@ -139,58 +122,3 @@ func main() {
139
122
os .Exit (1 )
140
123
}
141
124
}
142
-
143
- // addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
144
- // the Prometheus operator
145
- func addMetrics (ctx context.Context , cfg * rest.Config , namespace string ) {
146
- if err := serveCRMetrics (cfg ); err != nil {
147
- if errors .Is (err , k8sutil .ErrRunLocal ) {
148
- ctxlog .Info (ctx , "Skipping CR metrics server creation; not running in a cluster." )
149
- return
150
- }
151
- ctxlog .Info (ctx , "Could not generate and serve custom resource metrics" , "error" , err .Error ())
152
- }
153
-
154
- // Add to the below struct any other metrics ports you want to expose.
155
- servicePorts := []v1.ServicePort {
156
- {Port : metricsPort , Name : metrics .OperatorPortName , Protocol : v1 .ProtocolTCP , TargetPort : intstr.IntOrString {Type : intstr .Int , IntVal : metricsPort }},
157
- {Port : operatorMetricsPort , Name : metrics .CRPortName , Protocol : v1 .ProtocolTCP , TargetPort : intstr.IntOrString {Type : intstr .Int , IntVal : operatorMetricsPort }},
158
- }
159
-
160
- // Create Service object to expose the metrics port(s).
161
- service , err := metrics .CreateMetricsService (ctx , cfg , servicePorts )
162
- if err != nil {
163
- ctxlog .Info (ctx , "Could not create metrics Service" , "error" , err .Error ())
164
- }
165
-
166
- // CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
167
- // necessary to configure Prometheus to scrape metrics from this operator.
168
- services := []* v1.Service {service }
169
- _ , err = metrics .CreateServiceMonitors (cfg , namespace , services )
170
- if err != nil {
171
- ctxlog .Info (ctx , "Could not create ServiceMonitor object" , "error" , err .Error ())
172
- // If this operator is deployed to a cluster without the prometheus-operator running, it will return
173
- // ErrServiceMonitorNotPresent, which can be used to safely skip ServiceMonitor creation.
174
- if err == metrics .ErrServiceMonitorNotPresent {
175
- ctxlog .Info (ctx , "Install prometheus-operator in your cluster to create ServiceMonitor objects" , "error" , err .Error ())
176
- }
177
- }
178
- }
179
-
180
- // serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
181
- // 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.
185
- filteredGVK , err := k8sutil .GetGVKsFromAddToScheme (apis .AddToScheme )
186
- if err != nil {
187
- return err
188
- }
189
- ns := []string {"" }
190
- // Generate and serve custom resource specific metrics.
191
- err = kubemetrics .GenerateAndServeCRMetrics (cfg , ns , filteredGVK , metricsHost , operatorMetricsPort )
192
- if err != nil {
193
- return err
194
- }
195
- return nil
196
- }
0 commit comments