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