You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/12/20 15:01:29 UTC
[shardingsphere-on-cloud] branch main updated: refactor: separate manager from cmd main (#153)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git
The following commit(s) were added to refs/heads/main by this push:
new a3e14af refactor: separate manager from cmd main (#153)
a3e14af is described below
commit a3e14afeb2db9cc1fd1eba26b8a08914738ccf67
Author: liyao <ma...@126.com>
AuthorDate: Tue Dec 20 23:01:23 2022 +0800
refactor: separate manager from cmd main (#153)
* chore: update Makefile with new layout
Signed-off-by: mlycore <ma...@126.com>
* refactor: seperate manager from server
Signed-off-by: mlycore <ma...@126.com>
Signed-off-by: mlycore <ma...@126.com>
---
shardingsphere-operator/Makefile | 2 +-
.../cmd/shardingsphere-operator/main.go | 41 +++++++++++++
.../shardingsphere-operator/manager/manager.go} | 70 ++++++++++++----------
3 files changed, 81 insertions(+), 32 deletions(-)
diff --git a/shardingsphere-operator/Makefile b/shardingsphere-operator/Makefile
index 557f879..7e005f6 100644
--- a/shardingsphere-operator/Makefile
+++ b/shardingsphere-operator/Makefile
@@ -59,7 +59,7 @@ test: manifests generate fmt envtest ## Run tests.
.PHONY: build
build: generate fmt ## Build manager binary.
- go build -o bin/manager main.go
+ go build -o bin/manager cmd/shardingsphere-operator/main.go
.PHONY: run
run: manifests generate fmt ## Run a controller from your host.
diff --git a/shardingsphere-operator/cmd/shardingsphere-operator/main.go b/shardingsphere-operator/cmd/shardingsphere-operator/main.go
new file mode 100644
index 0000000..3b668e2
--- /dev/null
+++ b/shardingsphere-operator/cmd/shardingsphere-operator/main.go
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package main
+
+import (
+ "os"
+
+ "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/cmd/shardingsphere-operator/manager"
+
+ // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
+ // to ensure that exec-entrypoint and run can make use of them.
+ _ "k8s.io/client-go/plugin/pkg/client/auth"
+
+ ctrl "sigs.k8s.io/controller-runtime"
+ "sigs.k8s.io/controller-runtime/pkg/healthz"
+)
+
+func main() {
+ opt := manager.ParseOptionsFromFlags()
+ if err := manager.New(opt).
+ SetHealthzCheck("healthz", healthz.Ping).
+ SetReadyzCheck("readyz", healthz.Ping).
+ Start(ctrl.SetupSignalHandler()); err != nil {
+ os.Exit(1)
+ }
+}
diff --git a/shardingsphere-operator/main.go b/shardingsphere-operator/cmd/shardingsphere-operator/manager/manager.go
similarity index 68%
rename from shardingsphere-operator/main.go
rename to shardingsphere-operator/cmd/shardingsphere-operator/manager/manager.go
index e0069ff..1d683e7 100644
--- a/shardingsphere-operator/main.go
+++ b/shardingsphere-operator/cmd/shardingsphere-operator/manager/manager.go
@@ -15,28 +15,23 @@
* limitations under the License.
*/
-package main
+package manager
import (
+ "context"
"flag"
"os"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/controllers"
-
"go.uber.org/zap/zapcore"
-
- // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
- // to ensure that exec-entrypoint and run can make use of them.
- _ "k8s.io/client-go/plugin/pkg/client/auth"
-
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
- //+kubebuilder:scaffold:imports
+ "sigs.k8s.io/controller-runtime/pkg/manager"
)
var (
@@ -46,37 +41,42 @@ var (
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
-
utilruntime.Must(v1alpha1.AddToScheme(scheme))
- //+kubebuilder:scaffold:scheme
}
-func main() {
- var metricsAddr string
- var enableLeaderElection bool
- var probeAddr string
+type Options struct {
+ ctrl.Options
+}
- flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
- flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
- flag.BoolVar(&enableLeaderElection, "leader-elect", false,
+func ParseOptionsFromFlags() *Options {
+ opt := &Options{}
+ flag.StringVar(&opt.MetricsBindAddress, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
+ flag.StringVar(&opt.HealthProbeBindAddress, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
+ flag.BoolVar(&opt.LeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
+
opts := zap.Options{
Development: true,
TimeEncoder: zapcore.RFC3339TimeEncoder,
}
+
opts.BindFlags(flag.CommandLine)
flag.Parse()
+ opt.Scheme = scheme
+ opt.LeaderElectionID = "shardingsphere.apache.org"
+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
+ return opt
+}
- mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
- Scheme: scheme,
- MetricsBindAddress: metricsAddr,
- HealthProbeBindAddress: probeAddr,
- LeaderElection: enableLeaderElection,
- LeaderElectionID: "0e5175ce.apache.org",
- })
+type Manager struct {
+ manager.Manager
+}
+
+func New(opts *Options) *Manager {
+ mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), opts.Options)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
@@ -96,20 +96,28 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "ShardingSphereProxyServerConfig")
os.Exit(1)
}
- //+kubebuilder:scaffold:builder
+ return &Manager{
+ Manager: mgr,
+ }
+}
- if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
+func (mgr *Manager) SetHealthzCheck(path string, check healthz.Checker) *Manager {
+ if err := mgr.Manager.AddHealthzCheck(path, check); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
- if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
+ return mgr
+}
+
+func (mgr *Manager) SetReadyzCheck(path string, check healthz.Checker) *Manager {
+ if err := mgr.Manager.AddReadyzCheck(path, check); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
+ return mgr
+}
+func (mgr *Manager) Start(ctx context.Context) error {
setupLog.Info("starting operator")
- if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
- setupLog.Error(err, "problem running operator")
- os.Exit(1)
- }
+ return mgr.Manager.Start(ctx)
}