Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Commit 510aad3

Browse files
committed
Update operator-sdk to 0.18
1 parent cd62b30 commit 510aad3

File tree

3 files changed

+46
-38
lines changed

3 files changed

+46
-38
lines changed

cmd/manager/main.go

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
78
"os"
89
"runtime"
910

1011
"github.com/nginxinc/nginx-ingress-operator/pkg/controller/nginxingresscontroller"
12+
1113
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1214
"k8s.io/apimachinery/pkg/runtime/schema"
1315

@@ -25,7 +27,6 @@ import (
2527
"github.com/operator-framework/operator-sdk/pkg/leader"
2628
"github.com/operator-framework/operator-sdk/pkg/log/zap"
2729
"github.com/operator-framework/operator-sdk/pkg/metrics"
28-
"github.com/operator-framework/operator-sdk/pkg/restmapper"
2930
sdkVersion "github.com/operator-framework/operator-sdk/version"
3031
"github.com/spf13/pflag"
3132
v1 "k8s.io/api/core/v1"
@@ -74,12 +75,6 @@ func main() {
7475

7576
printVersion()
7677

77-
namespace, err := k8sutil.GetWatchNamespace()
78-
if err != nil {
79-
log.Error(err, "Failed to get watch namespace")
80-
os.Exit(1)
81-
}
82-
8378
// Get a config to talk to the apiserver
8479
cfg, err := config.GetConfig()
8580
if err != nil {
@@ -97,10 +92,7 @@ func main() {
9792

9893
// Create a new Cmd to provide shared dependencies and start components
9994
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,
10496
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
10597
})
10698
if err != nil {
@@ -140,7 +132,30 @@ func main() {
140132
os.Exit(1)
141133
}
142134

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 {
144159
log.Info("Could not generate and serve custom resource metrics", "error", err.Error())
145160
}
146161

@@ -149,6 +164,7 @@ func main() {
149164
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
150165
{Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}},
151166
}
167+
152168
// Create Service object to expose the metrics port(s).
153169
service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts)
154170
if err != nil {
@@ -158,7 +174,9 @@ func main() {
158174
// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
159175
// necessary to configure Prometheus to scrape metrics from this operator.
160176
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)
162180
if err != nil {
163181
log.Info("Could not create ServiceMonitor object", "error", err.Error())
164182
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
@@ -167,32 +185,22 @@ func main() {
167185
log.Info("Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error())
168186
}
169187
}
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-
}
178188
}
179189

180190
// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
181191
// 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.
185196
filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme)
186197
if err != nil {
187198
return err
188199
}
189-
// Get the namespace the operator is currently deployed in.
190-
operatorNs, err := k8sutil.GetOperatorNamespace()
191-
if err != nil {
192-
return err
193-
}
200+
194201
// To generate metrics in other namespaces, add the values below.
195202
ns := []string{operatorNs}
203+
196204
// Generate and serve custom resource specific metrics.
197205
err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort)
198206
if err != nil {

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ module github.com/nginxinc/nginx-ingress-operator
33
go 1.14
44

55
require (
6-
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
76
github.com/go-logr/logr v0.1.0
87
github.com/openshift/api v3.9.1-0.20190924102528-32369d4db2ad+incompatible
9-
github.com/operator-framework/operator-sdk v0.15.1
8+
github.com/operator-framework/operator-sdk v0.15.2
109
github.com/spf13/pflag v1.0.5
11-
github.com/technosophos/moniker v0.0.0-20180509230615-a5dbd03a2245 // indirect
1210
k8s.io/api v0.0.0
1311
k8s.io/apiextensions-apiserver v0.0.0
1412
k8s.io/apimachinery v0.0.0
@@ -42,3 +40,5 @@ replace (
4240
)
4341

4442
replace github.com/docker/docker => github.com/moby/moby v0.7.3-0.20190826074503-38ab9da00309 // Required by Helm
43+
44+
replace github.com/openshift/api => github.com/openshift/api v0.0.0-20190924102528-32369d4db2ad // Required until https://github.com/operator-framework/operator-lifecycle-manager/pull/1241 is resolved

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6L
2323
github.com/Azure/go-autorest/tracing v0.5.0 h1:TRn4WjSnkcSy5AEG3pnbtFSwNtwzjr4VYyQflFE619k=
2424
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
2525
github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
26+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2627
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2728
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
2829
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
@@ -33,7 +34,6 @@ github.com/MakeNowJust/heredoc v0.0.0-20171113091838-e9091a26100e/go.mod h1:64YH
3334
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
3435
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
3536
github.com/Masterminds/semver/v3 v3.0.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
36-
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
3737
github.com/Masterminds/sprig/v3 v3.0.0/go.mod h1:NEUY/Qq8Gdm2xgYA+NwJM6wmfdRV9xkh8h/Rld20R0U=
3838
github.com/Masterminds/vcs v1.13.0/go.mod h1:N09YCmOQr6RLxC6UNHzuVwAdodYbbnycGHSmwVJjcKA=
3939
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
@@ -372,6 +372,7 @@ github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63
372372
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
373373
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
374374
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
375+
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
375376
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
376377
github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
377378
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
@@ -503,8 +504,8 @@ github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59P
503504
github.com/opencontainers/runc v1.0.0-rc2.0.20190611121236-6cc515888830/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U=
504505
github.com/opencontainers/runtime-spec v1.0.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
505506
github.com/opencontainers/selinux v1.2.2/go.mod h1:+BLncwf63G4dgOzykXAxcmnFlUaOlkDdmw/CqsW6pjs=
506-
github.com/openshift/api v3.9.1-0.20190924102528-32369d4db2ad+incompatible h1:6il8W875Oq9vycPkRV5TteLP9IfMEX3lyOl5yN+CtdI=
507-
github.com/openshift/api v3.9.1-0.20190924102528-32369d4db2ad+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
507+
github.com/openshift/api v0.0.0-20190924102528-32369d4db2ad h1:MiZEukiPd7ll8BQDwBfc3LKBxbqyeXIx+wl4CzVj5EQ=
508+
github.com/openshift/api v0.0.0-20190924102528-32369d4db2ad/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
508509
github.com/openshift/client-go v0.0.0-20190923180330-3b6373338c9b/go.mod h1:6rzn+JTr7+WYS2E1TExP4gByoABxMznR6y2SnUIkmxk=
509510
github.com/openshift/origin v0.0.0-20160503220234-8f127d736703/go.mod h1:0Rox5r9C8aQn6j1oAOQ0c1uC86mYbUFObzjBRvUKHII=
510511
github.com/openshift/prom-label-proxy v0.1.1-0.20191016113035-b8153a7f39f1/go.mod h1:p5MuxzsYP1JPsNGwtjtcgRHHlGziCJJfztff91nNixw=
@@ -515,8 +516,8 @@ github.com/operator-framework/operator-lifecycle-manager v0.0.0-20191115003340-1
515516
github.com/operator-framework/operator-registry v1.5.1/go.mod h1:agrQlkWOo1q8U1SAaLSS2WQ+Z9vswNT2M2HFib9iuLY=
516517
github.com/operator-framework/operator-registry v1.5.3/go.mod h1:agrQlkWOo1q8U1SAaLSS2WQ+Z9vswNT2M2HFib9iuLY=
517518
github.com/operator-framework/operator-registry v1.5.7-0.20200121213444-d8e2ec52c19a/go.mod h1:ekexcV4O8YMxdQuPb+Xco7MHfVmRIq7Jvj5e6NU7dHI=
518-
github.com/operator-framework/operator-sdk v0.15.1 h1:MSyez9UD47UtA0voGYotk/8i64Km7fZyfgwGRlhOvqY=
519-
github.com/operator-framework/operator-sdk v0.15.1/go.mod h1:RkC5LpluVONa08ORFIIVCYrEr855xG1/NltRL2jQ8qo=
519+
github.com/operator-framework/operator-sdk v0.15.2 h1:VlxI0J+HLmMKs5k53drQ/B1AXhyNj0OaD2BbREt8Hnk=
520+
github.com/operator-framework/operator-sdk v0.15.2/go.mod h1:RkC5LpluVONa08ORFIIVCYrEr855xG1/NltRL2jQ8qo=
520521
github.com/otiai10/copy v1.0.1/go.mod h1:8bMCJrAqOtN/d9oyh5HR7HhLQMvcGMpGdwRDYsfOCHc=
521522
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
522523
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776/go.mod h1:3HNVkVOU7vZeFXocWuvtcS0XSFLcf2XUSDHkq9t1jU4=
@@ -620,7 +621,6 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
620621
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
621622
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
622623
github.com/syndtr/gocapability v0.0.0-20160928074757-e7cb7fa329f4/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
623-
github.com/technosophos/moniker v0.0.0-20180509230615-a5dbd03a2245/go.mod h1:O1c8HleITsZqzNZDjSNzirUGsMT0oGu9LhHKoJrqO+A=
624624
github.com/thecodeteam/goscaleio v0.1.0/go.mod h1:68sdkZAsK8bvEwBlbQnlLS+xU+hvLYM/iQ8KXej1AwM=
625625
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
626626
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=

0 commit comments

Comments
 (0)