Skip to content

Commit

Permalink
[Helm] Move library packages and add helm binary
Browse files Browse the repository at this point in the history
The following changes are made in this PR:
1. Add code for building and running helm operator binary
2. Move pkg/internal/controllerutil out of internal/ dir
3. Add separate helpers to read watches.yaml file for helm operator
4. Move `diff.go` and `types.go` from `pkg/internal` to `internal/`
  • Loading branch information
varshaprasad96 committed Oct 12, 2021
1 parent 6500722 commit 33054cb
Show file tree
Hide file tree
Showing 38 changed files with 733 additions and 43 deletions.
221 changes: 221 additions & 0 deletions internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// Copyright 2020 The Operator-SDK 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 run

import (
"errors"
"flag"
"fmt"
"os"
"runtime"
"strings"

"github.com/operator-framework/helm-operator-plugins/internal/controller"
"github.com/operator-framework/helm-operator-plugins/internal/flags"
"github.com/operator-framework/helm-operator-plugins/internal/metrics"
"github.com/operator-framework/helm-operator-plugins/internal/release"
"github.com/operator-framework/helm-operator-plugins/internal/version"
helmmgr "github.com/operator-framework/helm-operator-plugins/pkg/manager"
watches "github.com/operator-framework/helm-operator-plugins/pkg/watches/helm"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
logf "sigs.k8s.io/controller-runtime/pkg/log"
zapf "sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)

var log = logf.Log.WithName("cmd")

func printVersion() {
log.Info("Version",
"Go Version", runtime.Version(),
"GOOS", runtime.GOOS,
"GOARCH", runtime.GOARCH,
"helm-operator", version.GitVersion,
"commit", version.GitCommit)
}

func NewCmd() *cobra.Command {
f := &flags.Flags{}
zapfs := flag.NewFlagSet("zap", flag.ExitOnError)
opts := &zapf.Options{}
opts.BindFlags(zapfs)

cmd := &cobra.Command{
Use: "run",
Short: "Run the operator",
Run: func(cmd *cobra.Command, _ []string) {
logf.SetLogger(zapf.New(zapf.UseFlagOptions(opts)))
run(cmd, f)
},
}

f.AddTo(cmd.Flags())
cmd.Flags().AddGoFlagSet(zapfs)
return cmd
}

func run(cmd *cobra.Command, f *flags.Flags) {
printVersion()
metrics.RegisterBuildInfo(crmetrics.Registry)

// Load config options from the config at f.ManagerConfigPath.
// These options will not override those set by flags.
var (
options manager.Options
err error
)
if f.ManagerConfigPath != "" {
cfgLoader := ctrl.ConfigFile().AtPath(f.ManagerConfigPath)
if options, err = options.AndFrom(cfgLoader); err != nil {
log.Error(err, "Unable to load the manager config file")
os.Exit(1)
}
}
exitIfUnsupported(options)

cfg, err := config.GetConfig()
if err != nil {
log.Error(err, "Failed to get config.")
os.Exit(1)
}

// TODO(2.0.0): remove
// Deprecated: OPERATOR_NAME environment variable is an artifact of the
// legacy operator-sdk project scaffolding. Flag `--leader-election-id`
// should be used instead.
if operatorName, found := os.LookupEnv("OPERATOR_NAME"); found {
log.Info("Environment variable OPERATOR_NAME has been deprecated, use --leader-election-id instead.")
if cmd.Flags().Changed("leader-election-id") {
log.Info("Ignoring OPERATOR_NAME environment variable since --leader-election-id is set")
} else if options.LeaderElectionID == "" {
// Only set leader election ID using OPERATOR_NAME if unset everywhere else,
// since this env var is deprecated.
options.LeaderElectionID = operatorName
}
}

//TODO(2.0.0): remove the following checks. they are required just because of the flags deprecation
if cmd.Flags().Changed("leader-elect") && cmd.Flags().Changed("enable-leader-election") {
log.Error(errors.New("only one of --leader-elect and --enable-leader-election may be set"), "invalid flags usage")
os.Exit(1)
}

if cmd.Flags().Changed("metrics-addr") && cmd.Flags().Changed("metrics-bind-address") {
log.Error(errors.New("only one of --metrics-addr and --metrics-bind-address may be set"), "invalid flags usage")
os.Exit(1)
}

// Set default manager options
options = f.ToManagerOptions(options)

if options.NewClient == nil {
options.NewClient = helmmgr.NewCachingClientFunc()
}
namespace, found := os.LookupEnv(helmmgr.WatchNamespaceEnvVar)
log = log.WithValues("Namespace", namespace)
if found {
log.V(1).Info(fmt.Sprintf("Setting namespace with value in %s", helmmgr.WatchNamespaceEnvVar))
if namespace == metav1.NamespaceAll {
log.Info("Watching all namespaces.")
options.Namespace = metav1.NamespaceAll
} else {
if strings.Contains(namespace, ",") {
log.Info("Watching multiple namespaces.")
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ","))
} else {
log.Info("Watching single namespace.")
options.Namespace = namespace
}
}
} else if options.Namespace == "" {
log.Info(fmt.Sprintf("Watch namespaces not configured by environment variable %s or file. "+
"Watching all namespaces.", helmmgr.WatchNamespaceEnvVar))
options.Namespace = metav1.NamespaceAll
}

mgr, err := manager.New(cfg, options)
if err != nil {
log.Error(err, "Failed to create a new manager")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
log.Error(err, "Unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
log.Error(err, "Unable to set up ready check")
os.Exit(1)
}

ws, err := watches.Load(f.WatchesFile)
if err != nil {
log.Error(err, "Failed to create new manager factories.")
os.Exit(1)
}

for _, w := range ws {
// Register the controller with the factory.
err := controller.Add(mgr, controller.WatchOptions{
Namespace: namespace,
GVK: w.GroupVersionKind,
ManagerFactory: release.NewManagerFactory(mgr, w.ChartDir),
ReconcilePeriod: f.ReconcilePeriod,
WatchDependentResources: *w.WatchDependentResources,
OverrideValues: w.OverrideValues,
MaxConcurrentReconciles: f.MaxConcurrentReconciles,
Selector: w.Selector,
})
if err != nil {
log.Error(err, "Failed to add manager factory to controller.")
os.Exit(1)
}
}

// Start the Cmd
if err = mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Error(err, "Manager exited non-zero.")
os.Exit(1)
}

}

// exitIfUnsupported prints an error containing unsupported field names and exits
// if any of those fields are not their default values.
func exitIfUnsupported(options manager.Options) {
var keys []string
// The below options are webhook-specific, which is not supported by ansible.
if options.CertDir != "" {
keys = append(keys, "certDir")
}
if options.Host != "" {
keys = append(keys, "host")
}
if options.Port != 0 {
keys = append(keys, "port")
}

if len(keys) > 0 {
log.Error(fmt.Errorf("%s set in manager options", strings.Join(keys, ", ")), "unsupported fields")
os.Exit(1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/operator-framework/helm-operator-plugins/pkg/annotation"
helmmgr "github.com/operator-framework/helm-operator-plugins/pkg/manager"
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler"
"github.com/operator-framework/helm-operator-plugins/pkg/watches"
watches "github.com/operator-framework/helm-operator-plugins/pkg/watches/hybrid"
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
"sigs.k8s.io/yaml"

"github.com/operator-framework/helm-operator-plugins/pkg/internal/release"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/internal/release"
"github.com/operator-framework/helm-operator-plugins/pkg/sdk/controllerutil"
libhandler "github.com/operator-framework/operator-lib/handler"
"github.com/operator-framework/operator-lib/predicate"
)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/operator-framework/helm-operator-plugins/pkg/internal/helm/diff"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/helm/types"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/release"
"github.com/operator-framework/helm-operator-plugins/internal/helm/diff"
"github.com/operator-framework/helm-operator-plugins/internal/helm/types"
"github.com/operator-framework/helm-operator-plugins/internal/release"
)

// blank assignment to verify that HelmOperatorReconciler implements reconcile.Reconciler
Expand Down
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.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import (
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/discovery"

"github.com/operator-framework/helm-operator-plugins/pkg/internal/helm/types"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/manifestutil"
"github.com/operator-framework/helm-operator-plugins/internal/helm/types"
"github.com/operator-framework/helm-operator-plugins/internal/manifestutil"
)

// Manager manages a Helm release. It can install, upgrade, reconcile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
crmanager "sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/operator-framework/helm-operator-plugins/internal/helm/types"
"github.com/operator-framework/helm-operator-plugins/pkg/client/helm/client"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/helm/types"
)

// ManagerFactory creates Managers that are specific to custom resources. It is
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/stage"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

"github.com/operator-framework/helm-operator-plugins/internal/cmd/run"
"github.com/operator-framework/helm-operator-plugins/internal/cmd/hybrid-operator/run"
"github.com/operator-framework/helm-operator-plugins/internal/version"
pluginv1alpha "github.com/operator-framework/helm-operator-plugins/pkg/plugins/hybrid/v1alpha"
kustomizev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/actionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/manifestutil"
"github.com/operator-framework/helm-operator-plugins/pkg/sdk/controllerutil"
)

type ActionClientGetter interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/helm/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"io"
"strings"

"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/sdk/controllerutil"
"github.com/operator-framework/operator-lib/handler"
"helm.sh/helm/v3/pkg/kube"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/reconciler/internal/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

corev1 "k8s.io/api/core/v1"

"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/status"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/status"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/internal/conditions/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"

"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/status"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/status"
. "github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/conditions"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
"sigs.k8s.io/yaml"

"github.com/operator-framework/helm-operator-plugins/pkg/hook"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/predicate"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/predicate"
"github.com/operator-framework/helm-operator-plugins/pkg/manifestutil"
"github.com/operator-framework/helm-operator-plugins/pkg/sdk/controllerutil"
)

func NewDependentResourceWatcher(c controller.Controller, rm meta.RESTMapper) hook.PostHook {
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/internal/hook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"

"github.com/operator-framework/helm-operator-plugins/pkg/hook"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/fake"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/fake"
internalhook "github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/hook"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/status"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/status"
"github.com/operator-framework/helm-operator-plugins/pkg/sdk/controllerutil"
)

func New(client client.Client) Updater {
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ import (
"github.com/operator-framework/helm-operator-plugins/pkg/annotation"
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
"github.com/operator-framework/helm-operator-plugins/pkg/hook"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/conditions"
internalhook "github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/hook"
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/updater"
internalvalues "github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/values"
"github.com/operator-framework/helm-operator-plugins/pkg/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/values"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ import (
"github.com/operator-framework/helm-operator-plugins/pkg/annotation"
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
"github.com/operator-framework/helm-operator-plugins/pkg/hook"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/status"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/status"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/testutil"
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/conditions"
helmfake "github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/fake"
"github.com/operator-framework/helm-operator-plugins/pkg/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/values"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

. "github.com/operator-framework/helm-operator-plugins/pkg/internal/sdk/controllerutil"
. "github.com/operator-framework/helm-operator-plugins/pkg/sdk/controllerutil"
)

var _ = Describe("Controllerutil", func() {
Expand Down
Loading

0 comments on commit 33054cb

Please sign in to comment.