Skip to content

Commit

Permalink
main: Split code between handler and operator (#858)
Browse files Browse the repository at this point in the history
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
qinqon authored Oct 27, 2021
1 parent 23236eb commit 2ec7eb9
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 34 deletions.
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ whitespace-format:
hack/whitespace.sh format

gofmt: $(GO)
$(GOFMT) -w *.go test/ hack/ api/ controllers/ pkg/
$(GOFMT) -w cmd/ test/ hack/ api/ controllers/ pkg/

whitespace-check:
hack/whitespace.sh check

gofmt-check: $(GO)
test -z "`$(GOFMT) -l *.go test/ hack/ api/ controllers/ pkg/`" || ($(GOFMT) -l *.go test/ hack/ api/ controllers/ pkg/ && exit 1)
test -z "`$(GOFMT) -l cmd/ test/ hack/ api/ controllers/ pkg/`" || ($(GOFMT) -l cmd/ test/ hack/ api/ controllers/ pkg/ && exit 1)

$(GO):
hack/install-go.sh $(BIN_DIR)
Expand All @@ -136,7 +136,7 @@ gen-crds: $(GO)
$(CONTROLLER_GEN) $(CRD_OPTIONS) paths="./..." output:crd:artifacts:config=deploy/crds

gen-rbac: $(GO)
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=nmstate-operator paths="./controllers/nmstate_controller.go" output:rbac:artifacts:config=deploy/operator
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=nmstate-operator paths="./controllers/operator/nmstate_controller.go" output:rbac:artifacts:config=deploy/operator

check-gen: generate
./hack/check-gen.sh
Expand All @@ -146,19 +146,22 @@ generate: gen-k8s gen-crds gen-rbac
manifests: $(GO)
$(GO) run hack/render-manifests.go -handler-prefix=$(HANDLER_PREFIX) -handler-namespace=$(HANDLER_NAMESPACE) -operator-namespace=$(OPERATOR_NAMESPACE) -handler-image=$(HANDLER_IMAGE) -operator-image=$(OPERATOR_IMAGE) -handler-pull-policy=$(HANDLER_PULL_POLICY) -operator-pull-policy=$(OPERATOR_PULL_POLICY) -input-dir=deploy/ -output-dir=$(MANIFESTS_DIR)

manager: $(GO)
hack/build-manager.sh $(BIN_DIR)
handler-manager: $(GO)
hack/build-manager.sh handler $(BIN_DIR)

operator-manager: $(GO)
hack/build-manager.sh operator $(BIN_DIR)

handler: SKIP_PUSH=true
handler: push-handler

push-handler: manager
push-handler: handler-manager
SKIP_PUSH=$(SKIP_PUSH) IMAGE=${HANDLER_IMAGE} hack/build-push-container.${IMAGE_BUILDER}.sh . -f build/Dockerfile --build-arg NMSTATE_COPR_REPO=$(NMSTATE_COPR_REPO) --build-arg NM_COPR_REPO=$(NM_COPR_REPO)

operator: SKIP_PUSH=true
operator: push-operator

push-operator: manager
push-operator: operator-manager
SKIP_PUSH=$(SKIP_PUSH) IMAGE=${OPERATOR_IMAGE} hack/build-push-container.${IMAGE_BUILDER}.sh . -f build/Dockerfile.operator

push: push-handler push-operator
Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ARG TARGETARCH

RUN env

COPY build/_output/bin/manager.linux-$TARGETARCH /usr/local/bin/manager
COPY build/_output/bin/handler.manager.linux-$TARGETARCH /usr/local/bin/manager

RUN dnf install -b -y dnf-plugins-core && \
dnf copr enable -y networkmanager/$NM_COPR_REPO && \
Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile.openshift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM registry.svc.ci.openshift.org/openshift/release:golang-1.15 AS builder
WORKDIR /go/src/github.com/openshift/kubernetes-nmstate
COPY . .
RUN GO111MODULE=on go build --mod=vendor -o build/_output/bin/manager main.go
RUN GO111MODULE=on go build --mod=vendor -o build/_output/bin/manager ./cmd/handler/

FROM ubi8-minimal

Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile.operator
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM registry.access.redhat.com/ubi8/ubi-minimal
ARG TARGETARCH

COPY build/_output/bin/manager.linux-$TARGETARCH /usr/local/bin/manager
COPY build/_output/bin/operator.manager.linux-$TARGETARCH /usr/local/bin/manager

COPY deploy/crds/nmstate.io_nodenetwork*.yaml /bindata/kubernetes-nmstate/crds/
COPY deploy/handler/namespace.yaml /bindata/kubernetes-nmstate/namespace/
Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile.operator.openshift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM registry.svc.ci.openshift.org/openshift/release:golang-1.15 AS builder
WORKDIR /go/src/github.com/openshift/kubernetes-nmstate
COPY . .
RUN GO111MODULE=on go build --mod=vendor -o build/_output/bin/manager main.go
RUN GO111MODULE=on go build --mod=vendor -o build/_output/bin/manager ./cmd/operator

FROM registry.access.redhat.com/ubi8/ubi-minimal

Expand Down
11 changes: 1 addition & 10 deletions main.go → cmd/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import (
nmstatev1 "github.com/nmstate/kubernetes-nmstate/api/v1"
nmstatev1alpha1 "github.com/nmstate/kubernetes-nmstate/api/v1alpha1"
nmstatev1beta1 "github.com/nmstate/kubernetes-nmstate/api/v1beta1"
"github.com/nmstate/kubernetes-nmstate/controllers"
controllers "github.com/nmstate/kubernetes-nmstate/controllers/handler"
"github.com/nmstate/kubernetes-nmstate/pkg/environment"
"github.com/nmstate/kubernetes-nmstate/pkg/file"
"github.com/nmstate/kubernetes-nmstate/pkg/names"
Expand Down Expand Up @@ -193,15 +193,6 @@ func main() {
setupLog.Error(err, "Cannot initialize webhook")
os.Exit(1)
}
} else if environment.IsOperator() {
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)
}
} else if environment.IsHandler() {
if err = (&controllers.NodeReconciler{
Client: mgr.GetClient(),
Expand Down
133 changes: 133 additions & 0 deletions cmd/operator/main.go
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)
}
}()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (

func TestUnit(t *testing.T) {
RegisterFailHandler(Fail)
junitReporter := reporters.NewJUnitReporter("junit.controllers_suite_test.xml")
RunSpecsWithDefaultAndCustomReporters(t, "Controllers Test Suite", []Reporter{junitReporter})
junitReporter := reporters.NewJUnitReporter("junit.controllers_handler_suite_test.xml")
RunSpecsWithDefaultAndCustomReporters(t, "Controllers Handler Test Suite", []Reporter{junitReporter})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,14 @@ func copyManifest(src, dst string) error {

func copyManifests(manifestsDir string) error {
srcToDest := map[string]string{
"../deploy/crds/nmstate.io_nodenetworkconfigurationenactments.yaml": "kubernetes-nmstate/crds/",
"../deploy/crds/nmstate.io_nodenetworkconfigurationpolicies.yaml": "kubernetes-nmstate/crds/",
"../deploy/crds/nmstate.io_nodenetworkstates.yaml": "kubernetes-nmstate/crds/",
"../deploy/handler/namespace.yaml": "kubernetes-nmstate/namespace/",
"../deploy/handler/operator.yaml": "kubernetes-nmstate/handler/handler.yaml",
"../deploy/handler/service_account.yaml": "kubernetes-nmstate/rbac/",
"../deploy/handler/role.yaml": "kubernetes-nmstate/rbac/",
"../deploy/handler/role_binding.yaml": "kubernetes-nmstate/rbac/",
"../../deploy/crds/nmstate.io_nodenetworkconfigurationenactments.yaml": "kubernetes-nmstate/crds/",
"../../deploy/crds/nmstate.io_nodenetworkconfigurationpolicies.yaml": "kubernetes-nmstate/crds/",
"../../deploy/crds/nmstate.io_nodenetworkstates.yaml": "kubernetes-nmstate/crds/",
"../../deploy/handler/namespace.yaml": "kubernetes-nmstate/namespace/",
"../../deploy/handler/operator.yaml": "kubernetes-nmstate/handler/handler.yaml",
"../../deploy/handler/service_account.yaml": "kubernetes-nmstate/rbac/",
"../../deploy/handler/role.yaml": "kubernetes-nmstate/rbac/",
"../../deploy/handler/role_binding.yaml": "kubernetes-nmstate/rbac/",
}

for src, dest := range srcToDest {
Expand Down
16 changes: 16 additions & 0 deletions controllers/operator/operator_suite_test.go
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})
}
7 changes: 4 additions & 3 deletions hack/build-manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

set -x -o errexit -o pipefail

destdir=$1
name=$1
destdir=$2

export GOOS=linux

if [ "$ARCHS" == "" ]; then
go build -o $destdir/manager.$GOOS-$(go env GOARCH) main.go
go build -o $destdir/$name.manager.$GOOS-$(go env GOARCH) ./cmd/$name
else
for arch in $ARCHS; do
GOARCH=$arch go build -o $destdir/manager.$GOOS-$arch main.go
GOARCH=$arch go build -o $destdir/$name.manager.$GOOS-$arch ./cmd/$name
done
fi

0 comments on commit 2ec7eb9

Please sign in to comment.