Skip to content

Commit 21ff75e

Browse files
committed
Move CPP Config into reconciliation
Signed-off-by: Daniel Fan <[email protected]>
1 parent 3764aec commit 21ff75e

File tree

6 files changed

+85
-96
lines changed

6 files changed

+85
-96
lines changed

controllers/bootstrap/init.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,37 +1654,45 @@ func (b *Bootstrap) PropagateCPPConfig(instance *corev1.ConfigMap) error {
16541654

16551655
// Do not copy ibm-cpp-config in AllNamespace Mode
16561656
if len(watchNamespaceList) > 1 {
1657-
for _, watchNamespace := range watchNamespaceList {
1658-
if watchNamespace == instance.Namespace {
1657+
for _, ns := range watchNamespaceList {
1658+
if ns == instance.Namespace {
16591659
continue
16601660
}
16611661
copiedCPPConfigMap := &corev1.ConfigMap{
16621662
ObjectMeta: metav1.ObjectMeta{
16631663
Name: constant.IBMCPPCONFIG,
1664-
Namespace: watchNamespace,
1664+
Namespace: ns,
1665+
Labels: instance.GetLabels(),
16651666
},
16661667
Data: instance.Data,
16671668
}
16681669

16691670
if err := b.Client.Create(ctx, copiedCPPConfigMap); err != nil {
16701671
if errors.IsAlreadyExists(err) {
1671-
cmKey := types.NamespacedName{Name: constant.IBMCPPCONFIG, Namespace: watchNamespace}
1672+
cmKey := types.NamespacedName{Name: constant.IBMCPPCONFIG, Namespace: ns}
16721673
existingCM := &corev1.ConfigMap{}
1673-
if err := b.Client.Get(ctx, cmKey, existingCM); err != nil {
1674-
return fmt.Errorf("failed to get %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, watchNamespace, err)
1674+
if err := b.Reader.Get(ctx, cmKey, existingCM); err != nil {
1675+
return fmt.Errorf("failed to get %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, ns, err)
1676+
}
1677+
if _, ok := existingCM.GetLabels()[constant.CsManagedLabel]; !ok {
1678+
// If the existing ConfigMap is not the same as copied CnfigMap, and does not have the label,
1679+
// return an error to indicate that the ConfigMap is not updated.
1680+
if !reflect.DeepEqual(copiedCPPConfigMap.Data, existingCM.Data) {
1681+
return fmt.Errorf("failed to update the CPP Config %s/%s, given it is not does not contain label %s", ns, constant.IBMCPPCONFIG, constant.CsManagedLabel)
1682+
}
16751683
}
1676-
if !reflect.DeepEqual(copiedCPPConfigMap.Data, existingCM.Data) {
1684+
if !reflect.DeepEqual(copiedCPPConfigMap.Data, existingCM.Data) || !reflect.DeepEqual(copiedCPPConfigMap.Labels, existingCM.Labels) {
16771685
copiedCPPConfigMap.SetResourceVersion(existingCM.GetResourceVersion())
16781686
if err := b.Client.Update(ctx, copiedCPPConfigMap); err != nil {
1679-
return fmt.Errorf("failed to update %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, watchNamespace, err)
1687+
return fmt.Errorf("failed to update %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, ns, err)
16801688
}
1681-
klog.Infof("Global CPP config %s/%s is updated", watchNamespace, constant.IBMCPPCONFIG)
1689+
klog.Infof("Global CPP config %s/%s is updated", ns, constant.IBMCPPCONFIG)
16821690
}
16831691
} else {
1684-
return fmt.Errorf("failed to create cloned %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, watchNamespace, err)
1692+
return fmt.Errorf("failed to create cloned %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, ns, err)
16851693
}
16861694
} else {
1687-
klog.Infof("Global CPP config %s/%s is propagated to namespace %s", b.CSData.ServicesNs, constant.IBMCPPCONFIG, watchNamespace)
1695+
klog.Infof("Global CPP config %s/%s is propagated to namespace %s", b.CSData.ServicesNs, constant.IBMCPPCONFIG, ns)
16881696
}
16891697
}
16901698
}

controllers/commonservice_controller.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
apiv3 "github.com/IBM/ibm-common-service-operator/api/v3"
4242
"github.com/IBM/ibm-common-service-operator/controllers/bootstrap"
4343
util "github.com/IBM/ibm-common-service-operator/controllers/common"
44+
"github.com/IBM/ibm-common-service-operator/controllers/configurationCollector"
4445
"github.com/IBM/ibm-common-service-operator/controllers/constant"
4546
odlm "github.com/IBM/operand-deployment-lifecycle-manager/v4/api/v1alpha1"
4647
)
@@ -272,32 +273,32 @@ func (r *CommonServiceReconciler) ReconcileMasterCR(ctx context.Context, instanc
272273
return ctrl.Result{}, statusErr
273274
}
274275

275-
isEqual, statusErr := r.updateOperandConfig(ctx, newConfigs, serviceControllerMapping)
276-
if statusErr != nil {
276+
if isEqual, statusErr := r.updateOperandConfig(ctx, newConfigs, serviceControllerMapping); statusErr != nil {
277277
if statusErr := r.updatePhase(ctx, instance, CRFailed); statusErr != nil {
278278
klog.Error(statusErr)
279279
}
280280
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
281281
return ctrl.Result{}, statusErr
282-
}
283-
284-
// Create Event if there is no update in OperandConfig after applying current CR
285-
if isEqual {
282+
} else if isEqual {
286283
r.Recorder.Event(instance, corev1.EventTypeNormal, "Noeffect", fmt.Sprintf("No update, resource sizings in the OperandConfig %s/%s are larger than the profile from CommonService CR %s/%s", r.Bootstrap.CSData.OperatorNs, "common-service", instance.Namespace, instance.Name))
287284
}
288285

289-
isEqual, statusErr = r.updateOperatorConfig(ctx, instance.Spec.OperatorConfigs)
290-
if statusErr != nil {
286+
if isEqual, statusErr := r.updateOperatorConfig(ctx, instance.Spec.OperatorConfigs); statusErr != nil {
291287
if statusErr := r.updatePhase(ctx, instance, CRFailed); statusErr != nil {
292288
klog.Error(statusErr)
293289
}
294290
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
295291
return ctrl.Result{}, statusErr
292+
} else if isEqual {
293+
r.Recorder.Event(instance, corev1.EventTypeNormal, "Noeffect", fmt.Sprintf("No update, replica sizings in the OperatorConfig %s/%s are larger than the profile from CommonService CR %s/%s", r.Bootstrap.CSData.OperatorNs, "common-service", instance.Namespace, instance.Name))
296294
}
297295

298-
// Create Event if there is no update in OperandConfig after applying current CR
299-
if isEqual {
300-
r.Recorder.Event(instance, corev1.EventTypeNormal, "Noeffect", fmt.Sprintf("No update, replica sizings in the OperatorConfig %s/%s are larger than the profile from CommonService CR %s/%s", r.Bootstrap.CSData.OperatorNs, "common-service", instance.Namespace, instance.Name))
296+
if statusErr := configurationCollector.CreateUpdateConfig(r.Bootstrap); statusErr != nil {
297+
if statusErr := r.updatePhase(ctx, instance, CRFailed); statusErr != nil {
298+
klog.Error(statusErr)
299+
}
300+
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
301+
return ctrl.Result{}, statusErr
301302
}
302303

303304
if statusErr := r.Bootstrap.PropagateDefaultCR(instance); statusErr != nil {

controllers/configurationCollector/collector.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414
// limitations under the License.
1515
//
1616

17-
package configurationcollector
17+
package configurationCollector
1818

1919
import (
2020
"context"
21+
"reflect"
2122
"strings"
2223

24+
corev1 "k8s.io/api/core/v1"
2325
storagev1 "k8s.io/api/storage/v1"
26+
"k8s.io/apimachinery/pkg/api/errors"
27+
"k8s.io/apimachinery/pkg/types"
28+
"k8s.io/klog"
2429

2530
"github.com/IBM/ibm-common-service-operator/controllers/bootstrap"
31+
util "github.com/IBM/ibm-common-service-operator/controllers/common"
32+
"github.com/IBM/ibm-common-service-operator/controllers/constant"
2633
)
2734

2835
func Buildconfig(config map[string]string, bs *bootstrap.Bootstrap) map[string]string {
@@ -80,3 +87,48 @@ func (b *configbuilder) setDefaultStorageClass() *configbuilder {
8087

8188
return b
8289
}
90+
91+
// CreateUpdateConfig deploys config builder for global cpp configmap
92+
func CreateUpdateConfig(bs *bootstrap.Bootstrap) error {
93+
config := &corev1.ConfigMap{}
94+
if err := bs.Reader.Get(context.TODO(), types.NamespacedName{Name: constant.IBMCPPCONFIG, Namespace: bs.CSData.ServicesNs}, config); err != nil && !errors.IsNotFound(err) {
95+
klog.Errorf("Failed to get ConfigMap %s/%s: %v", bs.CSData.ServicesNs, constant.IBMCPPCONFIG, err)
96+
return err
97+
} else if errors.IsNotFound(err) {
98+
config.ObjectMeta.Name = constant.IBMCPPCONFIG
99+
config.ObjectMeta.Namespace = bs.CSData.ServicesNs
100+
config.Data = make(map[string]string)
101+
config.Data = Buildconfig(config.Data, bs)
102+
if !(config.Labels != nil && config.Labels[constant.CsManagedLabel] == "true") {
103+
util.EnsureLabelsForConfigMap(config, map[string]string{
104+
constant.CsManagedLabel: "true",
105+
})
106+
}
107+
if err := bs.Client.Create(context.TODO(), config); err != nil {
108+
klog.Errorf("Failed to create ConfigMap %s/%s: %v", bs.CSData.ServicesNs, constant.IBMCPPCONFIG, err)
109+
return err
110+
}
111+
klog.Infof("Global CPP config %s/%s is created", bs.CSData.ServicesNs, constant.IBMCPPCONFIG)
112+
} else {
113+
orgConfig := config.DeepCopy()
114+
config.Data = Buildconfig(config.Data, bs)
115+
if !(config.Labels != nil && config.Labels[constant.CsManagedLabel] == "true") {
116+
util.EnsureLabelsForConfigMap(config, map[string]string{
117+
constant.CsManagedLabel: "true",
118+
})
119+
}
120+
if !reflect.DeepEqual(orgConfig, config) {
121+
if err := bs.Client.Update(context.TODO(), config); err != nil {
122+
klog.Errorf("Failed to update ConfigMap %s/%s: %v", bs.CSData.ServicesNs, constant.IBMCPPCONFIG, err)
123+
return err
124+
}
125+
klog.Infof("Global CPP config %s/%s is updated", bs.CSData.ServicesNs, constant.IBMCPPCONFIG)
126+
}
127+
}
128+
129+
if err := bs.PropagateCPPConfig(config); err != nil {
130+
klog.Error(err)
131+
return err
132+
}
133+
return nil
134+
}

controllers/goroutines/cppConfig.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

controllers/goroutines/waitToCreateCsCR.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/IBM/ibm-common-service-operator/controllers/bootstrap"
2828
)
2929

30-
// CreateUpdateConfig deploys config builder for global cpp configmap
30+
// WaitToCreateCsCR waits for the creation of the CommonService CR in the operator namespace.
3131
func WaitToCreateCsCR(bs *bootstrap.Bootstrap) {
3232
for {
3333
klog.Infof("Start to Create CommonService CR in the namespace %s", bs.CSData.OperatorNs)

main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ func main() {
152152
klog.Errorf("Cleanup Webhook Resources failed: %v", err)
153153
os.Exit(1)
154154
}
155-
// Create or Update CPP configuration
156-
go goroutines.CreateUpdateConfig(bs)
157155
// Update CS CR Status
158156
go goroutines.UpdateCsCrStatus(bs)
159157
// Create CS CR

0 commit comments

Comments
 (0)