Reimplementation of the helm operator to enrich the Helm operator's reconciliation with custom Go code to create a hybrid operator.
The Helm operator type automates Helm chart operations
by mapping the values of a Helm chart exactly to a
CustomResourceDefinition and defining its watched resources in a watches.yamlconfiguration file.
For creating a Level II+ operator that reuses an already existing Helm chart, a hybrid between the Go and Helm operator types is necessary.
The hybrid approach allows adding customizations to the Helm operator, such as:
- value mapping based on cluster state, or
- executing code in specific events.
// Operator's main.gochart, err:=loader.Load("path/to/chart")
iferr!=nil {
panic(err)
}
reconciler:=reconciler.New(
reconciler.WithChart(*chart),
reconciler.WithGroupVersionKind(gvk),
)
iferr:=reconciler.SetupWithManager(mgr); err!=nil {
panic(fmt.Sprintf("unable to create reconciler: %s", err))
}Add the WATCH_NAMESPACE to the manager files to restrict the namespace to observe where the operator is installed
name: managerenv:- name: WATCH_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespaceFilter the events to the namespace where the operator is installed
// Operator's main.gowatchNamespace=os.Getenv("WATCH_NAMESPACE")
varcacheOpts cache.OptionsifwatchNamespace!="" {
setupLog.Info("Watching specific namespace", "namespace", watchNamespace)
cacheOpts= cache.Options{
DefaultNamespaces: map[string]cache.Config{
watchNamespace: {},
},
}
} else {
setupLog.Info("Watching all namespaces")
}
mgr, err:=ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
...Cache: cacheOpts,
})
...chart, err:=loader.Load("path/to/chart")
iferr!=nil {
panic(err)
}
reconciler:=reconciler.New(
reconciler.WithChart(*chart),
reconciler.WithGroupVersionKind(gvk),
)
iferr:=reconciler.SetupWithManager(mgr); err!=nil {
panic(fmt.Sprintf("unable to create reconciler: %s", err))
}