@@ -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