Skip to content

Commit becafe0

Browse files
committed
Build new cmd/operator/main.go into bin/operator
Jira: OSPRH-11244
1 parent c6f9c4a commit becafe0

File tree

4 files changed

+170
-10
lines changed

4 files changed

+170
-10
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ cover: test ## Run tests and display functional test coverage
194194
.PHONY: build
195195
build: generate fmt vet ## Build manager binary.
196196
go build -o bin/manager main.go
197+
go build -o bin/operator cmd/operator/main.go
197198

198199
.PHONY: run
199200
run: export METRICS_PORT?=8080

cmd/operator/main.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"crypto/tls"
21+
"flag"
22+
"os"
23+
"strconv"
24+
"strings"
25+
26+
"go.uber.org/zap/zapcore"
27+
28+
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
29+
// to ensure that exec-entrypoint and run can make use of them.
30+
_ "k8s.io/client-go/plugin/pkg/client/auth"
31+
32+
"sigs.k8s.io/controller-runtime/pkg/client/config"
33+
34+
"k8s.io/apimachinery/pkg/runtime"
35+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
36+
"k8s.io/client-go/kubernetes"
37+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
38+
ctrl "sigs.k8s.io/controller-runtime"
39+
"sigs.k8s.io/controller-runtime/pkg/healthz"
40+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
41+
"sigs.k8s.io/controller-runtime/pkg/webhook"
42+
43+
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
44+
45+
operatorv1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/operator/v1beta1"
46+
operatorcontrollers "github.com/openstack-k8s-operators/openstack-operator/controllers/operator"
47+
// +kubebuilder:scaffold:imports
48+
)
49+
50+
var (
51+
scheme = runtime.NewScheme()
52+
setupLog = ctrl.Log.WithName("setup")
53+
)
54+
55+
func init() {
56+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
57+
utilruntime.Must(operatorv1beta1.AddToScheme(scheme))
58+
// +kubebuilder:scaffold:scheme
59+
}
60+
61+
func main() {
62+
var metricsAddr string
63+
var enableLeaderElection bool
64+
var probeAddr string
65+
var enableHTTP2 bool
66+
flag.BoolVar(&enableHTTP2, "enable-http2", enableHTTP2, "If HTTP/2 should be enabled for the metrics and webhook servers.")
67+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
68+
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
69+
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
70+
"Enable leader election for controller manager. "+
71+
"Enabling this will ensure there is only one active controller manager.")
72+
devMode, err := strconv.ParseBool(os.Getenv("DEV_MODE"))
73+
if err != nil {
74+
devMode = true
75+
}
76+
opts := zap.Options{
77+
Development: devMode,
78+
TimeEncoder: zapcore.ISO8601TimeEncoder,
79+
}
80+
opts.BindFlags(flag.CommandLine)
81+
flag.Parse()
82+
83+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
84+
85+
disableHTTP2 := func(c *tls.Config) {
86+
if enableHTTP2 {
87+
return
88+
}
89+
c.NextProtos = []string{"http/1.1"}
90+
}
91+
92+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
93+
Scheme: scheme,
94+
Metrics: metricsserver.Options{
95+
BindAddress: metricsAddr,
96+
},
97+
HealthProbeBindAddress: probeAddr,
98+
LeaderElection: enableLeaderElection,
99+
LeaderElectionID: "40ba705e.openstack.org",
100+
WebhookServer: webhook.NewServer(
101+
webhook.Options{
102+
Port: 9443,
103+
TLSOpts: []func(config *tls.Config){disableHTTP2},
104+
}),
105+
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
106+
// when the Manager ends. This requires the binary to immediately end when the
107+
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
108+
// speeds up voluntary leader transitions as the new leader don't have to wait
109+
// LeaseDuration time first.
110+
//
111+
// In the default scaffold provided, the program ends immediately after
112+
// the manager stops, so would be fine to enable this option. However,
113+
// if you are doing or is intended to do any operation such as perform cleanups
114+
// after the manager stops then its usage might be unsafe.
115+
// LeaderElectionReleaseOnCancel: true,
116+
})
117+
if err != nil {
118+
setupLog.Error(err, "unable to start manager")
119+
os.Exit(1)
120+
}
121+
// Setup the context that's going to be used in controllers and for the manager.
122+
ctx := ctrl.SetupSignalHandler()
123+
124+
cfg, err := config.GetConfig()
125+
if err != nil {
126+
setupLog.Error(err, "")
127+
os.Exit(1)
128+
}
129+
kclient, err := kubernetes.NewForConfig(cfg)
130+
if err != nil {
131+
setupLog.Error(err, "")
132+
os.Exit(1)
133+
}
134+
135+
// Webhooks
136+
checker := healthz.Ping
137+
if strings.ToLower(os.Getenv("ENABLE_WEBHOOKS")) != "false" {
138+
139+
checker = mgr.GetWebhookServer().StartedChecker()
140+
}
141+
142+
if err = (&operatorcontrollers.OpenStackReconciler{
143+
Client: mgr.GetClient(),
144+
Scheme: mgr.GetScheme(),
145+
Kclient: kclient,
146+
}).SetupWithManager(mgr); err != nil {
147+
setupLog.Error(err, "unable to create controller", "controller", "OpenStack")
148+
os.Exit(1)
149+
}
150+
// +kubebuilder:scaffold:builder
151+
if err := mgr.AddHealthzCheck("healthz", checker); err != nil {
152+
setupLog.Error(err, "unable to set up health check")
153+
os.Exit(1)
154+
}
155+
if err := mgr.AddReadyzCheck("readyz", checker); err != nil {
156+
setupLog.Error(err, "unable to set up ready check")
157+
os.Exit(1)
158+
}
159+
160+
setupLog.Info("starting manager")
161+
if err := mgr.Start(ctx); err != nil {
162+
setupLog.Error(err, "problem running manager")
163+
os.Exit(1)
164+
}
165+
}

controllers/operator/openstack_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121

2222
"k8s.io/apimachinery/pkg/runtime"
23+
"k8s.io/client-go/kubernetes"
2324
ctrl "sigs.k8s.io/controller-runtime"
2425
"sigs.k8s.io/controller-runtime/pkg/client"
2526
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -30,7 +31,8 @@ import (
3031
// OpenStackReconciler reconciles a OpenStack object
3132
type OpenStackReconciler struct {
3233
client.Client
33-
Scheme *runtime.Scheme
34+
Scheme *runtime.Scheme
35+
Kclient kubernetes.Interface
3436
}
3537

3638
//+kubebuilder:rbac:groups=operator.openstack.org,resources=openstacks,verbs=get;list;watch;create;update;patch;delete

main.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,15 @@ import (
7676
clientv1 "github.com/openstack-k8s-operators/openstack-operator/apis/client/v1beta1"
7777
corev1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1"
7878
dataplanev1 "github.com/openstack-k8s-operators/openstack-operator/apis/dataplane/v1beta1"
79+
operatorv1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/operator/v1beta1"
7980

8081
ocp_configv1 "github.com/openshift/api/config/v1"
8182
machineconfig "github.com/openshift/api/machineconfiguration/v1"
8283
ocp_image "github.com/openshift/api/operator/v1alpha1"
8384

84-
operatorv1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/operator/v1beta1"
8585
clientcontrollers "github.com/openstack-k8s-operators/openstack-operator/controllers/client"
8686
corecontrollers "github.com/openstack-k8s-operators/openstack-operator/controllers/core"
8787
dataplanecontrollers "github.com/openstack-k8s-operators/openstack-operator/controllers/dataplane"
88-
operatorcontrollers "github.com/openstack-k8s-operators/openstack-operator/controllers/operator"
8988
"github.com/openstack-k8s-operators/openstack-operator/pkg/openstack"
9089
// +kubebuilder:scaffold:imports
9190
)
@@ -296,13 +295,6 @@ func main() {
296295
checker = mgr.GetWebhookServer().StartedChecker()
297296
}
298297

299-
if err = (&operatorcontrollers.OpenStackReconciler{
300-
Client: mgr.GetClient(),
301-
Scheme: mgr.GetScheme(),
302-
}).SetupWithManager(mgr); err != nil {
303-
setupLog.Error(err, "unable to create controller", "controller", "OpenStack")
304-
os.Exit(1)
305-
}
306298
// +kubebuilder:scaffold:builder
307299
if err := mgr.AddHealthzCheck("healthz", checker); err != nil {
308300
setupLog.Error(err, "unable to set up health check")

0 commit comments

Comments
 (0)