Skip to content

Commit 3d310e2

Browse files
authored
WIP: operator-sdk update from v0.14.0 to v0.17.1 (#55)
* operator-sdk update from v0.14.0 to v0.15.2 * operator-sdk update from v0.15.2 to v0.16.0 * operator-sdk update from v0.16.0 to v0.17.1
1 parent 10209e1 commit 3d310e2

513 files changed

Lines changed: 71445 additions & 57298 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/manager/main.go

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
8+
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
9+
"github.com/operator-framework/operator-sdk/pkg/metrics"
10+
v1 "k8s.io/api/core/v1"
11+
"k8s.io/apimachinery/pkg/util/intstr"
12+
"k8s.io/client-go/rest"
713
"os"
814
"runtime"
15+
"sigs.k8s.io/controller-runtime/pkg/cache"
16+
"strings"
917

1018
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
1119
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -16,12 +24,8 @@ import (
1624
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
1725
"github.com/operator-framework/operator-sdk/pkg/leader"
1826
"github.com/operator-framework/operator-sdk/pkg/log/zap"
19-
"github.com/operator-framework/operator-sdk/pkg/metrics"
20-
"github.com/operator-framework/operator-sdk/pkg/restmapper"
2127
sdkVersion "github.com/operator-framework/operator-sdk/version"
2228
"github.com/spf13/pflag"
23-
v1 "k8s.io/api/core/v1"
24-
"k8s.io/apimachinery/pkg/util/intstr"
2529
"sigs.k8s.io/controller-runtime/pkg/client/config"
2630
"sigs.k8s.io/controller-runtime/pkg/manager"
2731
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -87,12 +91,23 @@ func main() {
8791
os.Exit(1)
8892
}
8993

90-
// Create a new Cmd to provide shared dependencies and start components
91-
mgr, err := manager.New(cfg, manager.Options{
94+
// Set default manager options
95+
options := manager.Options{
9296
Namespace: namespace,
93-
MapperProvider: restmapper.NewDynamicRESTMapper,
9497
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
95-
})
98+
}
99+
100+
// Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2)
101+
// Note that this is not intended to be used for excluding namespaces, this is better done via a Predicate
102+
// Also note that you may face performance issues when using this with a high number of namespaces.
103+
// More Info: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder
104+
if strings.Contains(namespace, ",") {
105+
options.Namespace = ""
106+
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ","))
107+
}
108+
109+
// Create a new manager to provide shared dependencies and start components
110+
mgr, err := manager.New(cfg, options)
96111
if err != nil {
97112
log.Error(err, "")
98113
os.Exit(1)
@@ -112,11 +127,42 @@ func main() {
112127
os.Exit(1)
113128
}
114129

130+
// Add the Metrics Service
131+
addMetrics(ctx, cfg)
132+
133+
log.Info("Starting the Cmd.")
134+
135+
// Start the Cmd
136+
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
137+
log.Error(err, "Manager exited non-zero")
138+
os.Exit(1)
139+
}
140+
}
141+
142+
// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
143+
// the Prometheus operator
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) {
147+
// Get the namespace the operator is currently deployed in.
148+
operatorNs, err := k8sutil.GetOperatorNamespace()
149+
if err != nil {
150+
if errors.Is(err, k8sutil.ErrRunLocal) {
151+
log.Info("Skipping CR metrics server creation; not running in a cluster.")
152+
return
153+
}
154+
}
155+
156+
if err := serveCRMetrics(cfg, operatorNs); err != nil {
157+
log.Info("Could not generate and serve custom resource metrics", "error", err.Error())
158+
}
159+
115160
// Add to the below struct any other metrics ports you want to expose.
116161
servicePorts := []v1.ServicePort{
117162
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
118163
{Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}},
119164
}
165+
120166
// Create Service object to expose the metrics port(s).
121167
service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts)
122168
if err != nil {
@@ -126,7 +172,9 @@ func main() {
126172
// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
127173
// necessary to configure Prometheus to scrape metrics from this operator.
128174
services := []*v1.Service{service}
129-
_, err = metrics.CreateServiceMonitors(cfg, namespace, services)
175+
176+
// The ServiceMonitor is created in the same namespace where the operator is deployed
177+
_, err = metrics.CreateServiceMonitors(cfg, operatorNs, services)
130178
if err != nil {
131179
log.Info("Could not create ServiceMonitor object", "error", err.Error())
132180
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
@@ -135,12 +183,30 @@ func main() {
135183
log.Info("Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error())
136184
}
137185
}
186+
}
138187

139-
log.Info("Starting the Cmd.")
188+
// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
189+
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
190+
func serveCRMetrics(cfg *rest.Config, operatorNs string) error {
191+
// The function below returns a list of filtered operator/CR specific GVKs. For more control, override the GVK list below
192+
// with your own custom logic. Note that if you are adding third party API schemas, probably you will need to
193+
// customize this implementation to avoid permissions issues.
194+
filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme)
195+
if err != nil {
196+
return err
197+
}
140198

141-
// Start the Cmd
142-
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
143-
log.Error(err, "Manager exited non-zero")
144-
os.Exit(1)
199+
// The metrics will be generated from the namespaces which are returned here.
200+
// NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error.
201+
ns, err := kubemetrics.GetNamespacesForMetrics(operatorNs)
202+
if err != nil {
203+
return err
204+
}
205+
206+
// Generate and serve custom resource specific metrics.
207+
err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort)
208+
if err != nil {
209+
return err
145210
}
211+
return nil
146212
}

deploy/crds/db.movetokube.com_postgres_crd.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ spec:
1919
apiVersion:
2020
description: 'APIVersion defines the versioned schema of this representation
2121
of an object. Servers should convert recognized schemas to the latest
22-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
22+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
2323
type: string
2424
kind:
2525
description: 'Kind is a string value representing the REST resource this
2626
object represents. Servers may infer this from the endpoint the client
27-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
27+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
2828
type: string
2929
metadata:
3030
type: object

deploy/crds/db.movetokube.com_postgresusers_crd.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ spec:
1919
apiVersion:
2020
description: 'APIVersion defines the versioned schema of this representation
2121
of an object. Servers should convert recognized schemas to the latest
22-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
22+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
2323
type: string
2424
kind:
2525
description: 'Kind is a string value representing the REST resource this
2626
object represents. Servers may infer this from the endpoint the client
27-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
27+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
2828
type: string
2929
metadata:
3030
type: object
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
apiVersion: operators.coreos.com/v1alpha1
2+
kind: ClusterServiceVersion
3+
metadata:
4+
annotations:
5+
alm-examples: |-
6+
[
7+
{
8+
"apiVersion": "db.movetokube.com/v1alpha1",
9+
"kind": "Postgres",
10+
"metadata": {
11+
"name": "my-db",
12+
"namespace": "app"
13+
},
14+
"spec": {
15+
"database": "test-db",
16+
"dropOnDelete": false,
17+
"masterRole": "test-db-group",
18+
"schemas": [
19+
"stores",
20+
"customers"
21+
]
22+
}
23+
},
24+
{
25+
"apiVersion": "db.movetokube.com/v1alpha1",
26+
"kind": "PostgresUser",
27+
"metadata": {
28+
"name": "my-db-user",
29+
"namespace": "app"
30+
},
31+
"spec": {
32+
"database": "my-db",
33+
"privileges": "OWNER",
34+
"role": "username",
35+
"secretName": "my-secret"
36+
}
37+
}
38+
]
39+
capabilities: Basic Install
40+
name: ext-postgres-operator.v0.4.3
41+
namespace: placeholder
42+
spec:
43+
apiservicedefinitions: {}
44+
customresourcedefinitions:
45+
owned:
46+
- description: Postgres is the Schema for the postgres API
47+
kind: Postgres
48+
name: postgres.db.movetokube.com
49+
version: v1alpha1
50+
- description: PostgresUser is the Schema for the postgresusers API
51+
kind: PostgresUser
52+
name: postgresusers.db.movetokube.com
53+
version: v1alpha1
54+
description: Placeholder description
55+
displayName: Ext Postgres Operator
56+
install:
57+
spec:
58+
clusterPermissions:
59+
- rules:
60+
- apiGroups:
61+
- ""
62+
resources:
63+
- pods
64+
- services
65+
- endpoints
66+
- persistentvolumeclaims
67+
- events
68+
- configmaps
69+
- secrets
70+
verbs:
71+
- '*'
72+
- apiGroups:
73+
- apps
74+
resources:
75+
- deployments
76+
- daemonsets
77+
- replicasets
78+
- statefulsets
79+
verbs:
80+
- '*'
81+
- apiGroups:
82+
- apps
83+
resourceNames:
84+
- ext-postgres-operator
85+
resources:
86+
- deployments/finalizers
87+
verbs:
88+
- update
89+
- apiGroups:
90+
- db.movetokube.com
91+
resources:
92+
- '*'
93+
verbs:
94+
- '*'
95+
serviceAccountName: ext-postgres-operator
96+
deployments:
97+
- name: ext-postgres-operator
98+
spec:
99+
replicas: 1
100+
selector:
101+
matchLabels:
102+
name: ext-postgres-operator
103+
strategy: {}
104+
template:
105+
metadata:
106+
labels:
107+
name: ext-postgres-operator
108+
spec:
109+
containers:
110+
- command:
111+
- postgres-operator
112+
env:
113+
- name: WATCH_NAMESPACE
114+
valueFrom:
115+
fieldRef:
116+
fieldPath: metadata.annotations['olm.targetNamespaces']
117+
- name: POD_NAME
118+
valueFrom:
119+
fieldRef:
120+
fieldPath: metadata.name
121+
- name: OPERATOR_NAME
122+
value: ext-postgres-operator
123+
envFrom:
124+
- secretRef:
125+
name: ext-postgres-operator
126+
image: movetokube/postgres-operator
127+
imagePullPolicy: Always
128+
name: ext-postgres-operator
129+
resources: {}
130+
serviceAccountName: ext-postgres-operator
131+
strategy: deployment
132+
installModes:
133+
- supported: true
134+
type: OwnNamespace
135+
- supported: true
136+
type: SingleNamespace
137+
- supported: false
138+
type: MultiNamespace
139+
- supported: true
140+
type: AllNamespaces
141+
maturity: alpha
142+
provider: {}
143+
replaces: ext-postgres-operator.v0.4.1
144+
version: 0.4.3

go.mod

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,25 @@ module github.com/movetokube/postgres-operator
33
go 1.13
44

55
require (
6+
github.com/emicklei/go-restful v2.11.1+incompatible // indirect
67
github.com/go-logr/logr v0.1.0
78
github.com/go-openapi/spec v0.19.4
89
github.com/golang/mock v1.3.1
10+
github.com/imdario/mergo v0.3.8 // indirect
911
github.com/lib/pq v1.2.0
10-
github.com/onsi/ginkgo v1.10.1
11-
github.com/onsi/gomega v1.7.0
12-
github.com/operator-framework/operator-sdk v0.14.0
12+
github.com/onsi/ginkgo v1.11.0
13+
github.com/onsi/gomega v1.8.1
14+
github.com/operator-framework/operator-sdk v0.17.1
1315
github.com/spf13/pflag v1.0.5
14-
k8s.io/api v0.0.0
15-
k8s.io/apimachinery v0.0.0
16+
k8s.io/api v0.17.4
17+
k8s.io/apimachinery v0.17.4
1618
k8s.io/client-go v12.0.0+incompatible
17-
k8s.io/kube-openapi v0.0.0-20190918143330-0270cf2f1c1d
18-
sigs.k8s.io/controller-runtime v0.4.0
19+
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a
20+
//k8s.io/kubernetes v1.16.2 // indirect
21+
sigs.k8s.io/controller-runtime v0.5.2
1922
)
2023

21-
// Pinned to kubernetes-1.16.2
2224
replace (
23-
k8s.io/api => k8s.io/api v0.0.0-20191016110408-35e52d86657a
24-
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.0.0-20191016113550-5357c4baaf65
25-
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20191004115801-a2eda9f80ab8
26-
k8s.io/apiserver => k8s.io/apiserver v0.0.0-20191016112112-5190913f932d
27-
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20191016114015-74ad18325ed5
28-
k8s.io/client-go => k8s.io/client-go v0.0.0-20191016111102-bec269661e48
29-
k8s.io/cloud-provider => k8s.io/cloud-provider v0.0.0-20191016115326-20453efc2458
30-
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.0.0-20191016115129-c07a134afb42
31-
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20191004115455-8e001e5d1894
32-
k8s.io/component-base => k8s.io/component-base v0.0.0-20191016111319-039242c015a9
33-
k8s.io/cri-api => k8s.io/cri-api v0.0.0-20190828162817-608eb1dad4ac
34-
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.0.0-20191016115521-756ffa5af0bd
35-
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.0.0-20191016112429-9587704a8ad4
36-
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.0.0-20191016114939-2b2b218dc1df
37-
k8s.io/kube-proxy => k8s.io/kube-proxy v0.0.0-20191016114407-2e83b6f20229
38-
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.0.0-20191016114748-65049c67a58b
39-
k8s.io/kubectl => k8s.io/kubectl v0.0.0-20191016120415-2ed914427d51
40-
k8s.io/kubelet => k8s.io/kubelet v0.0.0-20191016114556-7841ed97f1b2
41-
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.0.0-20191016115753-cf0698c3a16b
42-
k8s.io/metrics => k8s.io/metrics v0.0.0-20191016113814-3b1a734dba6e
43-
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.0.0-20191016112829-06bb3c9d77c9
25+
github.com/Azure/go-autorest => github.com/Azure/go-autorest v13.3.2+incompatible // Required by OLM
26+
k8s.io/client-go => k8s.io/client-go v0.17.4 // Required by prometheus-operator
4427
)
45-
46-
replace github.com/docker/docker => github.com/moby/moby v0.7.3-0.20190826074503-38ab9da00309 // Required by Helm

0 commit comments

Comments
 (0)