Skip to content

Commit

Permalink
update functions for configuring manager options
Browse files Browse the repository at this point in the history
Signed-off-by: everettraven <everettraven@gmail.com>
  • Loading branch information
everettraven committed Jan 30, 2024
1 parent b93b680 commit 1ce2acd
Showing 1 changed file with 33 additions and 51 deletions.
84 changes: 33 additions & 51 deletions internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"strings"
"time"

"github.com/go-logr/logr"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apimachruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -157,13 +157,8 @@ func run(cmd *cobra.Command, f *flags.Flags) {
os.Exit(1)
}

watchNamespaces := getWatchNamespaces(options.Cache.DefaultNamespaces)
options.NewCache, err = buildNewCacheFunc(watchNamespaces, ws, options.Scheme)
if err != nil {
log.Error(err, "Failed to create NewCache function for manager.")
os.Exit(1)
}

configureWatchNamespaces(&options, log)
err = configureSelectors(&options, ws, options.Scheme)

Check failure on line 161 in internal/cmd/helm-operator/run/cmd.go

View workflow job for this annotation

GitHub Actions / sanity

ineffectual assignment to err (ineffassign)
if options.NewClient == nil {
options.NewClient = client.New
}
Expand Down Expand Up @@ -228,46 +223,36 @@ func exitIfUnsupported(options manager.Options) {
}
}

func getWatchNamespaces(namespaces map[string]cache.Config) map[string]cache.Config {
namespace, found := os.LookupEnv(k8sutil.WatchNamespaceEnvVar)
log = log.WithValues("Namespace", namespace)
if found {
log.V(1).Info(fmt.Sprintf("Setting namespace with value in %s", k8sutil.WatchNamespaceEnvVar))
if namespace == metav1.NamespaceAll {
log.Info("Watching all namespaces.")
return map[string]cache.Config{
metav1.NamespaceAll: {
LabelSelector: labels.Everything(),
},
}
}
if strings.Contains(namespace, ",") {
log.Info("Watching multiple namespaces.")
retNamespaces := map[string]cache.Config{}
for _, ns := range strings.Split(namespace, ",") {
retNamespaces[ns] = cache.Config{}
}
return retNamespaces
}
log.Info("Watching single namespace.")
return map[string]cache.Config{
namespace: {},
func configureWatchNamespaces(options *manager.Options, log logr.Logger) {
namespaces := splitNamespaces(os.Getenv(k8sutil.WatchNamespaceEnvVar))

namespaceConfigs := make(map[string]cache.Config)
if len(namespaces) != 0 {
log.Info("Watching namespaces", "namespaces", namespaces)
for _, namespace := range namespaces {
namespaceConfigs[namespace] = cache.Config{}
}
} else {
log.Info("Watching all namespaces")
namespaceConfigs[metav1.NamespaceAll] = cache.Config{}
}
if len(namespaces) == 0 {
log.Info(fmt.Sprintf("Watch namespaces not configured by environment variable %s or file. "+
"Watching all namespaces.", k8sutil.WatchNamespaceEnvVar))
return map[string]cache.Config{
metav1.NamespaceAll: {
LabelSelector: labels.Everything(),
},

options.Cache.DefaultNamespaces = namespaceConfigs
}

func splitNamespaces(namespaces string) []string {
list := strings.Split(namespaces, ",")
var out []string
for _, ns := range list {
trimmed := strings.TrimSpace(ns)
if trimmed != "" {
out = append(out, trimmed)
}
}

return namespaces
return out
}

func buildNewCacheFunc(watchNamespaces map[string]cache.Config, ws []watches.Watch, sch *apimachruntime.Scheme) (cache.NewCacheFunc, error) {
func configureSelectors(opts *manager.Options, ws []watches.Watch, sch *apimachruntime.Scheme) error {
selectorsByObject := map[client.Object]cache.ByObject{}
chartNames := make([]string, 0, len(ws))
for _, w := range ws {
Expand All @@ -277,27 +262,24 @@ func buildNewCacheFunc(watchNamespaces map[string]cache.Config, ws []watches.Wat
crObj.SetGroupVersionKind(w.GroupVersionKind)
sel, err := metav1.LabelSelectorAsSelector(&w.Selector)
if err != nil {
return nil, fmt.Errorf("unable to parse watch selector for %s: %v", w.GroupVersionKind, err)
return fmt.Errorf("unable to parse watch selector for %s: %v", w.GroupVersionKind, err)
}
selectorsByObject[crObj] = cache.ByObject{Label: sel}

chrt, err := loader.LoadDir(w.ChartDir)
if err != nil {
return nil, fmt.Errorf("unable to load chart for %s: %v", w.GroupVersionKind, err)
return fmt.Errorf("unable to load chart for %s: %v", w.GroupVersionKind, err)
}
chartNames = append(chartNames, chrt.Name())

}
req, err := labels.NewRequirement("helm.sdk.operatorframework.io/chart", selection.In, chartNames)
if err != nil {
return nil, fmt.Errorf("unable to create label requirement for cache default selector: %v", err)
return fmt.Errorf("unable to create label requirement for cache default selector: %v", err)
}
defaultSelector := labels.NewSelector().Add(*req)

return func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
opts.ByObject = selectorsByObject
opts.DefaultLabelSelector = defaultSelector
opts.DefaultNamespaces = watchNamespaces
return cache.New(config, opts)
}, nil
opts.Cache.ByObject = selectorsByObject
opts.Cache.DefaultLabelSelector = defaultSelector
return nil
}

0 comments on commit 1ce2acd

Please sign in to comment.