-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main: Split code between handler and operator (#858)
Currenctly operator depends on nmstatectl pkg but it does not needed and it present a problem at future when project will link to libnmstate.so dynmiac library. This change split main into ./cmd/handler and ./cmd/operator and controllers into ./controllres/handler and ./controllers/operator. Signed-off-by: Quique Llorente <[email protected]>
- Loading branch information
Showing
19 changed files
with
178 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
Copyright The Kubernetes NMState Authors. | ||
Licensed 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 ( | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
_ "net/http/pprof" | ||
"os" | ||
|
||
"go.uber.org/zap/zapcore" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
|
||
// +kubebuilder:scaffold:imports | ||
|
||
"github.com/kelseyhightower/envconfig" | ||
"github.com/spf13/pflag" | ||
|
||
nmstatev1 "github.com/nmstate/kubernetes-nmstate/api/v1" | ||
nmstatev1alpha1 "github.com/nmstate/kubernetes-nmstate/api/v1alpha1" | ||
nmstatev1beta1 "github.com/nmstate/kubernetes-nmstate/api/v1beta1" | ||
controllers "github.com/nmstate/kubernetes-nmstate/controllers/operator" | ||
) | ||
|
||
type ProfilerConfig struct { | ||
EnableProfiler bool `envconfig:"ENABLE_PROFILER"` | ||
ProfilerPort string `envconfig:"PROFILER_PORT" default:"6060"` | ||
} | ||
|
||
var ( | ||
scheme = runtime.NewScheme() | ||
setupLog = ctrl.Log.WithName("setup") | ||
) | ||
|
||
func init() { | ||
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) | ||
|
||
utilruntime.Must(nmstatev1.AddToScheme(scheme)) | ||
utilruntime.Must(nmstatev1beta1.AddToScheme(scheme)) | ||
utilruntime.Must(nmstatev1alpha1.AddToScheme(scheme)) | ||
// +kubebuilder:scaffold:scheme | ||
} | ||
|
||
func main() { | ||
opt := zap.Options{} | ||
opt.BindFlags(flag.CommandLine) | ||
var logType string | ||
pflag.StringVar(&logType, "v", "production", "Log type (debug/production).") | ||
pflag.CommandLine.MarkDeprecated("v", "please use the --zap-devel flag for debug logging instead") | ||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
pflag.Parse() | ||
|
||
if logType == "debug" { | ||
// workaround until --v flag got removed | ||
flag.CommandLine.Set("zap-devel", "true") | ||
} | ||
|
||
setupLogger(opt) | ||
|
||
ctrlOptions := ctrl.Options{ | ||
Scheme: scheme, | ||
MetricsBindAddress: "0", // disable metrics | ||
} | ||
|
||
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrlOptions) | ||
if err != nil { | ||
setupLog.Error(err, "unable to start manager") | ||
os.Exit(1) | ||
} | ||
|
||
if err = (&controllers.NMStateReconciler{ | ||
Client: mgr.GetClient(), | ||
Log: ctrl.Log.WithName("controllers").WithName("NMState"), | ||
Scheme: mgr.GetScheme(), | ||
}).SetupWithManager(mgr); err != nil { | ||
setupLog.Error(err, "unable to create NMState controller", "controller", "NMState") | ||
os.Exit(1) | ||
} | ||
|
||
setProfiler() | ||
|
||
setupLog.Info("starting manager") | ||
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { | ||
setupLog.Error(err, "problem running manager") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func setupLogger(opts zap.Options) { | ||
opts.EncoderConfigOptions = append(opts.EncoderConfigOptions, func(ec *zapcore.EncoderConfig) { | ||
ec.EncodeTime = zapcore.ISO8601TimeEncoder | ||
}) | ||
|
||
logger := zap.New(zap.UseFlagOptions(&opts)) | ||
ctrl.SetLogger(logger) | ||
} | ||
|
||
// Start profiler on given port if ENABLE_PROFILER is True | ||
func setProfiler() { | ||
cfg := ProfilerConfig{} | ||
envconfig.Process("", &cfg) | ||
if cfg.EnableProfiler { | ||
setupLog.Info("Starting profiler") | ||
go func() { | ||
profilerAddress := fmt.Sprintf("0.0.0.0:%s", cfg.ProfilerPort) | ||
setupLog.Info(fmt.Sprintf("Starting Profiler Server! \t Go to http://%s/debug/pprof/\n", profilerAddress)) | ||
err := http.ListenAndServe(profilerAddress, nil) | ||
if err != nil { | ||
setupLog.Info("Failed to start the server! Error: %v", err) | ||
} | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package controllers | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/onsi/ginkgo/reporters" | ||
) | ||
|
||
func TestUnit(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
junitReporter := reporters.NewJUnitReporter("junit.controllers_operator_suite_test.xml") | ||
RunSpecsWithDefaultAndCustomReporters(t, "Controllers Operator Test Suite", []Reporter{junitReporter}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters