Skip to content

Commit b3bcb7b

Browse files
authored
Merge pull request #413 from application-stacks/configmap
Create operator config map in main
2 parents 9840271 + 54bb2be commit b3bcb7b

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

controllers/runtimecomponent_controller.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ import (
5555
"sigs.k8s.io/controller-runtime/pkg/client"
5656
)
5757

58+
const (
59+
OperatorName = "runtime-component-operator"
60+
)
61+
5862
// RuntimeComponentReconciler reconciles a RuntimeComponent object
5963
type RuntimeComponentReconciler struct {
6064
appstacksutils.ReconcilerBase
@@ -97,11 +101,11 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
97101
ns = r.watchNamespaces[0]
98102
}
99103

100-
configMap, err := r.GetOpConfigMap("runtime-component-operator", ns)
104+
configMap, err := r.GetOpConfigMap(OperatorName, ns)
101105
if err != nil {
102106
reqLogger.Info("Failed to find runtime-component-operator config map")
103107
common.Config = common.DefaultOpConfig()
104-
configMap = &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "runtime-component-operator", Namespace: ns}}
108+
configMap = &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: OperatorName, Namespace: ns}}
105109
configMap.Data = common.Config
106110
} else {
107111
common.Config.LoadFromConfigMap(configMap)

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,14 @@ func main() {
117117
}
118118
// +kubebuilder:scaffold:builder
119119

120+
utils.CreateConfigMap(controllers.OperatorName)
121+
120122
setupLog.Info("starting manager")
121123
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
122124
setupLog.Error(err, "problem running manager")
123125
os.Exit(1)
124126
}
127+
125128
}
126129

127130
// getWatchNamespace returns the Namespace the operator should be watching for changes

utils/utils.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import (
1616
"k8s.io/client-go/kubernetes/scheme"
1717
"k8s.io/client-go/rest"
1818
"k8s.io/client-go/tools/remotecommand"
19+
ctrl "sigs.k8s.io/controller-runtime"
1920
"sigs.k8s.io/controller-runtime/pkg/client"
21+
clientcfg "sigs.k8s.io/controller-runtime/pkg/client/config"
22+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2023

2124
"github.com/application-stacks/runtime-component-operator/common"
2225
prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
@@ -1497,3 +1500,48 @@ func addSecretResourceVersionAsEnvVar(pts *corev1.PodTemplateSpec, object metav1
14971500
Value: secret.ResourceVersion})
14981501
return nil
14991502
}
1503+
1504+
// This should only be called once from main.go on operator start
1505+
// It checks for the presence of the operators config map and
1506+
// creates it if it doesn't exist
1507+
func CreateConfigMap(mapName string) {
1508+
utilsLog := ctrl.Log.WithName("utils-setup")
1509+
// This function is called from main, so the normal client isn't setup properly
1510+
client, clerr := client.New(clientcfg.GetConfigOrDie(), client.Options{})
1511+
if clerr != nil {
1512+
utilsLog.Error(clerr, "Couldn't create a client for config map creation")
1513+
return
1514+
}
1515+
1516+
operatorNs, _ := GetOperatorNamespace()
1517+
if operatorNs == "" {
1518+
// This should only happen when running locally in development
1519+
// Probably best to just return. The operator global config map is tried
1520+
// again in the reconcile loop, and don't want to duplicate logic to
1521+
// guess what the namespace should be
1522+
utilsLog.Info("Couldn't create operator config map as operator namespace was not found")
1523+
return
1524+
}
1525+
configMap := &corev1.ConfigMap{}
1526+
err := client.Get(context.TODO(), types.NamespacedName{Name: mapName, Namespace: operatorNs}, configMap)
1527+
if err != nil {
1528+
utilsLog.Error(err, "The operator config map was not found. Attempting to create it")
1529+
} else {
1530+
utilsLog.Info("Existing operator config map was found")
1531+
return
1532+
}
1533+
1534+
newConfigMap := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: mapName, Namespace: operatorNs}}
1535+
// The config map doesn't exist, so need to initialize the default config data, and then
1536+
// store it in a new map
1537+
common.Config = common.DefaultOpConfig()
1538+
_, cerr := controllerutil.CreateOrUpdate(context.TODO(), client, newConfigMap, func() error {
1539+
newConfigMap.Data = common.Config
1540+
return nil
1541+
})
1542+
if cerr != nil {
1543+
utilsLog.Error(cerr, "Couldn't create config map in namespace "+operatorNs)
1544+
} else {
1545+
utilsLog.Info("Operator Config map created in namespace " + operatorNs)
1546+
}
1547+
}

0 commit comments

Comments
 (0)