Skip to content

Commit cea3b57

Browse files
pkg/util: Init service object + GetOperatorName env var and test
1 parent 3d8865a commit cea3b57

9 files changed

+235
-111
lines changed

pkg/generator/gen_deploy.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"io"
2020
"strings"
2121
"text/template"
22+
23+
"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
2224
)
2325

2426
const (
@@ -58,8 +60,11 @@ func renderCRDYaml(w io.Writer, kind, apiVersion string) error {
5860

5961
// OperatorYaml contains data needed to generate deploy/operator.yaml
6062
type OperatorYaml struct {
61-
ProjectName string
62-
Image string
63+
ProjectName string
64+
Image string
65+
NameEnv string
66+
NamespaceEnv string
67+
MetricsPort int
6368
}
6469

6570
// renderOperatorYaml generates deploy/operator.yaml.
@@ -71,8 +76,11 @@ func renderOperatorYaml(w io.Writer, projectName, image string) error {
7176
}
7277

7378
o := OperatorYaml{
74-
ProjectName: projectName,
75-
Image: image,
79+
ProjectName: projectName,
80+
Image: image,
81+
NameEnv: k8sutil.OperatorNameEnvVar,
82+
NamespaceEnv: k8sutil.WatchNamespaceEnvVar,
83+
MetricsPort: k8sutil.PrometheusMetricsPort,
7684
}
7785
return t.Execute(w, o)
7886
}

pkg/generator/gen_deploy_test.go

+3-45
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ spec:
4040
- name: app-operator
4141
image: quay.io/example-inc/app-operator:0.0.1
4242
ports:
43-
- containerPort: 9090
43+
- containerPort: 60000
4444
name: metrics
4545
command:
4646
- app-operator
@@ -50,6 +50,8 @@ spec:
5050
valueFrom:
5151
fieldRef:
5252
fieldPath: metadata.namespace
53+
- name: OPERATOR_NAME
54+
value: "app-operator"
5355
`
5456

5557
const rbacYamlExp = `kind: Role
@@ -100,34 +102,6 @@ roleRef:
100102
apiGroup: rbac.authorization.k8s.io
101103
`
102104

103-
const serviceYamlExp = `apiVersion: v1
104-
kind: Service
105-
metadata:
106-
name: app-operator
107-
labels:
108-
name: app-operator
109-
spec:
110-
selector:
111-
name: app-operator
112-
ports:
113-
- protocol: TCP
114-
targetPort: metrics
115-
port: 9090
116-
name: metrics`
117-
118-
const serviceMonitorYamlExp = `apiVersion: monitoring.coreos.com/v1
119-
kind: ServiceMonitor
120-
metadata:
121-
name: app-operator
122-
labels:
123-
name: app-operator
124-
spec:
125-
selector:
126-
matchLabels:
127-
name: app-operator
128-
endpoints:
129-
- port: metrics`
130-
131105
func TestGenDeploy(t *testing.T) {
132106
buf := &bytes.Buffer{}
133107
if err := renderCRDYaml(buf, appKind, appAPIVersion); err != nil {
@@ -152,20 +126,4 @@ func TestGenDeploy(t *testing.T) {
152126
if rbacYamlExp != buf.String() {
153127
t.Errorf(errorMessage, rbacYamlExp, buf.String())
154128
}
155-
156-
buf = &bytes.Buffer{}
157-
if err := renderServiceYaml(buf, appProjectName); err != nil {
158-
t.Error(err)
159-
}
160-
if serviceYamlExp != buf.String() {
161-
t.Errorf("want %v, got %v", serviceYamlExp, buf.String())
162-
}
163-
164-
buf = &bytes.Buffer{}
165-
if err := renderServiceMonitorYaml(buf, appProjectName); err != nil {
166-
t.Error(err)
167-
}
168-
if serviceMonitorYamlExp != buf.String() {
169-
t.Errorf("want %v, got %v", serviceMonitorYamlExp, buf.String())
170-
}
171129
}

pkg/generator/gen_main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"io"
1919
"path/filepath"
2020
"text/template"
21+
22+
"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
2123
)
2224

2325
const (
@@ -36,8 +38,9 @@ type Main struct {
3638
K8sutilImport string
3739
SDKVersionImport string
3840

39-
APIVersion string
40-
Kind string
41+
APIVersion string
42+
Kind string
43+
MetricsPort string
4144
}
4245

4346
// renderMainFile generates the cmd/<projectName>/main.go file.
@@ -55,6 +58,7 @@ func renderMainFile(w io.Writer, repo, apiVersion, kind string) error {
5558
SDKVersionImport: versionImport,
5659
APIVersion: apiVersion,
5760
Kind: kind,
61+
MetricsPort: k8sutil.GetPrometheusMetricsPort(),
5862
}
5963
return t.Execute(w, m)
6064
}

pkg/generator/gen_main_test.go

+41-30
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,57 @@ import (
2222
const mainExp = `package main
2323
2424
import (
25-
"context"
26-
"runtime"
27-
"net/http"
25+
"context"
26+
"runtime"
27+
"net/http"
2828
29-
stub "github.com/example-inc/app-operator/pkg/stub"
30-
sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
31-
k8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
32-
sdkVersion "github.com/operator-framework/operator-sdk/version"
29+
stub "github.com/example-inc/app-operator/pkg/stub"
30+
sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
31+
k8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
32+
sdkVersion "github.com/operator-framework/operator-sdk/version"
3333
34-
"github.com/prometheus/client_golang/prometheus/promhttp"
35-
"github.com/sirupsen/logrus"
34+
"github.com/prometheus/client_golang/prometheus/promhttp"
35+
"github.com/sirupsen/logrus"
3636
)
3737
38-
// Prometheus metrics port
39-
const promPort = ":9090"
40-
4138
func printVersion() {
42-
logrus.Infof("Go Version: %s", runtime.Version())
43-
logrus.Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
44-
logrus.Infof("operator-sdk Version: %v", sdkVersion.Version)
45-
logrus.Infof("operator prometheus port :%s", promPort)
39+
logrus.Infof("Go Version: %s", runtime.Version())
40+
logrus.Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
41+
logrus.Infof("operator-sdk Version: %v", sdkVersion.Version)
42+
logrus.Infof("operator prometheus port %s", ":60000")
43+
}
44+
45+
func initOperatorService() {
46+
service, err := k8sutil.InitOperatorService()
47+
if err != nil {
48+
logrus.Fatalf("Failed to init operator service: %v", err)
49+
}
50+
err = sdk.Create(service)
51+
if err != nil {
52+
logrus.Infof("Failed to create operator service: %v", err)
53+
return
54+
}
55+
logrus.Infof("Service %s have been created", service.Name)
4656
}
4757
4858
func main() {
49-
printVersion()
59+
printVersion()
60+
initOperatorService()
5061
51-
http.Handle("/metrics", promhttp.Handler())
52-
logrus.Fatalf("%s", http.ListenAndServe(promPort, nil))
62+
http.Handle("/metrics", promhttp.Handler())
63+
go http.ListenAndServe(":60000", nil)
5364
54-
resource := "app.example.com/v1alpha1"
55-
kind := "AppService"
56-
namespace, err := k8sutil.GetWatchNamespace()
57-
if err != nil {
58-
logrus.Fatalf("Failed to get watch namespace: %v", err)
59-
}
60-
resyncPeriod := 5
61-
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
62-
sdk.Watch(resource, kind, namespace, resyncPeriod)
63-
sdk.Handle(stub.NewHandler())
64-
sdk.Run(context.TODO())
65+
resource := "app.example.com/v1alpha1"
66+
kind := "AppService"
67+
namespace, err := k8sutil.GetWatchNamespace()
68+
if err != nil {
69+
logrus.Fatalf("Failed to get watch namespace: %v", err)
70+
}
71+
resyncPeriod := 5
72+
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
73+
sdk.Watch(resource, kind, namespace, resyncPeriod)
74+
sdk.Handle(stub.NewHandler())
75+
sdk.Run(context.TODO())
6576
}
6677
`
6778

pkg/generator/generator.go

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ const (
5555
gopkglock = "Gopkg.lock"
5656
config = "config.yaml"
5757
operatorYaml = deployDir + "/operator.yaml"
58-
serviceYaml = "service.yaml"
59-
serviceMonitorYaml = "serviceMonitor.yaml"
6058
rbacYaml = "rbac.yaml"
6159
crYaml = "cr.yaml"
6260
catalogPackageYaml = "package.yaml"

pkg/generator/templates.go

+47-28
Original file line numberDiff line numberDiff line change
@@ -130,45 +130,57 @@ spec:
130130
const mainTmpl = `package main
131131
132132
import (
133-
"context"
134-
"runtime"
133+
"context"
134+
"runtime"
135+
"net/http"
135136
136-
stub "{{.StubImport}}"
137-
sdk "{{.OperatorSDKImport}}"
138-
k8sutil "{{.K8sutilImport}}"
137+
stub "{{.StubImport}}"
138+
sdk "{{.OperatorSDKImport}}"
139+
k8sutil "{{.K8sutilImport}}"
139140
sdkVersion "{{.SDKVersionImport}}"
140-
141+
141142
"github.com/prometheus/client_golang/prometheus/promhttp"
142-
"github.com/sirupsen/logrus"
143+
"github.com/sirupsen/logrus"
143144
)
144145
145-
// Prometheus metrics port
146-
const promPort = ":9090"
147-
148146
func printVersion() {
149-
logrus.Infof("Go Version: %s", runtime.Version())
150-
logrus.Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
147+
logrus.Infof("Go Version: %s", runtime.Version())
148+
logrus.Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
151149
logrus.Infof("operator-sdk Version: %v", sdkVersion.Version)
152-
logrus.Infof("operator prometheus port :%s", promPort)
150+
logrus.Infof("operator prometheus port %s", "{{.MetricsPort}}")
151+
}
152+
153+
func initOperatorService() {
154+
service, err := k8sutil.InitOperatorService()
155+
if err != nil {
156+
logrus.Fatalf("Failed to init operator service: %v", err)
157+
}
158+
err = sdk.Create(service)
159+
if err != nil {
160+
logrus.Infof("Failed to create operator service: %v", err)
161+
return
162+
}
163+
logrus.Infof("Service %s have been created", service.Name)
153164
}
154165
155166
func main() {
156-
printVersion()
167+
printVersion()
168+
initOperatorService()
157169
158170
http.Handle("/metrics", promhttp.Handler())
159-
logrus.Fatalf("%s", http.ListenAndServe(promPort, nil))
160-
161-
resource := "{{.APIVersion}}"
162-
kind := "{{.Kind}}"
163-
namespace, err := k8sutil.GetWatchNamespace()
164-
if err != nil {
165-
logrus.Fatalf("Failed to get watch namespace: %v", err)
166-
}
167-
resyncPeriod := 5
168-
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
169-
sdk.Watch(resource, kind, namespace, resyncPeriod)
170-
sdk.Handle(stub.NewHandler())
171-
sdk.Run(context.TODO())
171+
go http.ListenAndServe("{{.MetricsPort}}", nil)
172+
173+
resource := "{{.APIVersion}}"
174+
kind := "{{.Kind}}"
175+
namespace, err := k8sutil.GetWatchNamespace()
176+
if err != nil {
177+
logrus.Fatalf("Failed to get watch namespace: %v", err)
178+
}
179+
resyncPeriod := 5
180+
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
181+
sdk.Watch(resource, kind, namespace, resyncPeriod)
182+
sdk.Handle(stub.NewHandler())
183+
sdk.Run(context.TODO())
172184
}
173185
`
174186

@@ -524,6 +536,8 @@ const operatorYamlTmpl = `apiVersion: apps/v1
524536
kind: Deployment
525537
metadata:
526538
name: {{.ProjectName}}
539+
labels:
540+
name: {{.ProjectName}}
527541
spec:
528542
replicas: 1
529543
selector:
@@ -537,14 +551,19 @@ spec:
537551
containers:
538552
- name: {{.ProjectName}}
539553
image: {{.Image}}
554+
ports:
555+
- containerPort: {{.MetricsPort}}
556+
name: metrics
540557
command:
541558
- {{.ProjectName}}
542559
imagePullPolicy: Always
543560
env:
544-
- name: WATCH_NAMESPACE
561+
- name: {{.NamespaceEnv}}
545562
valueFrom:
546563
fieldRef:
547564
fieldPath: metadata.namespace
565+
- name: {{.NameEnv}}
566+
value: "{{.ProjectName}}"
548567
`
549568

550569
const rbacYamlTmpl = `kind: Role

pkg/util/k8sutil/constants.go

+7
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ const (
88
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
99
// which is the namespace that the pod is currently running in.
1010
WatchNamespaceEnvVar = "WATCH_NAMESPACE"
11+
12+
// OperatorName is the constant for env variable OPERATOR_NAME
13+
// wich is the name of the current operator
14+
OperatorNameEnvVar = "OPERATOR_NAME"
15+
16+
// PrometheusMetricsPort defines the port which expose prometheus metrics
17+
PrometheusMetricsPort = 60000
1118
)

0 commit comments

Comments
 (0)