Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
move cpp config into reconciliation loop
Signed-off-by: Daniel Fan <[email protected]>
  • Loading branch information
Daniel-Fan committed Aug 1, 2024
commit 05006c87aadea4f8716449e655a54eb1f726610c
28 changes: 17 additions & 11 deletions controllers/bootstrap/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -1654,37 +1654,43 @@ func (b *Bootstrap) PropagateCPPConfig(instance *corev1.ConfigMap) error {

// Do not copy ibm-cpp-config in AllNamespace Mode
if len(watchNamespaceList) > 1 {
for _, watchNamespace := range watchNamespaceList {
if watchNamespace == instance.Namespace {
for _, ns := range watchNamespaceList {
if ns == instance.Namespace {
continue
}
copiedCPPConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: constant.IBMCPPCONFIG,
Namespace: watchNamespace,
Namespace: ns,
Labels: instance.GetLabels(),
},
Data: instance.Data,
}

if err := b.Client.Create(ctx, copiedCPPConfigMap); err != nil {
if errors.IsAlreadyExists(err) {
cmKey := types.NamespacedName{Name: constant.IBMCPPCONFIG, Namespace: watchNamespace}
cmKey := types.NamespacedName{Name: constant.IBMCPPCONFIG, Namespace: ns}
existingCM := &corev1.ConfigMap{}
if err := b.Client.Get(ctx, cmKey, existingCM); err != nil {
return fmt.Errorf("failed to get %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, watchNamespace, err)
if err := b.Reader.Get(ctx, cmKey, existingCM); err != nil {
return fmt.Errorf("failed to get %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, ns, err)
}
for k, v := range existingCM.Data {
if _, ok := copiedCPPConfigMap.Data[k]; !ok {
copiedCPPConfigMap.Data[k] = v
}
}
if !reflect.DeepEqual(copiedCPPConfigMap.Data, existingCM.Data) {
if !reflect.DeepEqual(copiedCPPConfigMap.Data, existingCM.Data) || !reflect.DeepEqual(copiedCPPConfigMap.Labels, existingCM.Labels) {
copiedCPPConfigMap.SetResourceVersion(existingCM.GetResourceVersion())
if err := b.Client.Update(ctx, copiedCPPConfigMap); err != nil {
return fmt.Errorf("failed to update %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, watchNamespace, err)
return fmt.Errorf("failed to update %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, ns, err)
}
klog.Infof("Global CPP config %s/%s is updated", watchNamespace, constant.IBMCPPCONFIG)
klog.Infof("Global CPP config %s/%s is updated", ns, constant.IBMCPPCONFIG)
}
} else {
return fmt.Errorf("failed to create cloned %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, watchNamespace, err)
return fmt.Errorf("failed to create cloned %s ConfigMap in namespace %s: %v", constant.IBMCPPCONFIG, ns, err)
}
} else {
klog.Infof("Global CPP config %s/%s is propagated to namespace %s", b.CSData.ServicesNs, constant.IBMCPPCONFIG, watchNamespace)
klog.Infof("Global CPP config %s/%s is propagated to namespace %s", b.CSData.ServicesNs, constant.IBMCPPCONFIG, ns)
}
}
}
Expand Down
30 changes: 16 additions & 14 deletions controllers/commonservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
apiv3 "github.com/IBM/ibm-common-service-operator/api/v3"
"github.com/IBM/ibm-common-service-operator/controllers/bootstrap"
util "github.com/IBM/ibm-common-service-operator/controllers/common"
"github.com/IBM/ibm-common-service-operator/controllers/configurationcollector"
"github.com/IBM/ibm-common-service-operator/controllers/constant"
odlm "github.com/IBM/operand-deployment-lifecycle-manager/v4/api/v1alpha1"
)
Expand Down Expand Up @@ -267,45 +268,46 @@ func (r *CommonServiceReconciler) ReconcileMasterCR(ctx context.Context, instanc
instance.Status.Phase = CRFailed
}

if statusErr := r.Client.Status().Update(ctx, instance); statusErr != nil {
if statusErr = r.Client.Status().Update(ctx, instance); statusErr != nil {
klog.Errorf("Fail to update %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, statusErr
}

isEqual, statusErr := r.updateOperandConfig(ctx, newConfigs, serviceControllerMapping)
if statusErr != nil {
var isEqual bool
if isEqual, statusErr = r.updateOperandConfig(ctx, newConfigs, serviceControllerMapping); statusErr != nil {
if statusErr := r.updatePhase(ctx, instance, CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}

// Create Event if there is no update in OperandConfig after applying current CR
if isEqual {
} else if isEqual {
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))
}

isEqual, statusErr = r.updateOperatorConfig(ctx, instance.Spec.OperatorConfigs)
if statusErr != nil {
if isEqual, statusErr = r.updateOperatorConfig(ctx, instance.Spec.OperatorConfigs); statusErr != nil {
if statusErr := r.updatePhase(ctx, instance, CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
} else if isEqual {
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))
}

// Create Event if there is no update in OperandConfig after applying current CR
if isEqual {
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))
if statusErr = configurationcollector.CreateUpdateConfig(r.Bootstrap); statusErr != nil {
if statusErr := r.updatePhase(ctx, instance, CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}

if statusErr := r.Bootstrap.PropagateDefaultCR(instance); statusErr != nil {
if statusErr = r.Bootstrap.PropagateDefaultCR(instance); statusErr != nil {
klog.Error(statusErr)
return ctrl.Result{}, statusErr
}

if statusErr := r.Bootstrap.UpdateResourceLabel(instance); statusErr != nil {
if statusErr = r.Bootstrap.UpdateResourceLabel(instance); statusErr != nil {
klog.Error(statusErr)
return ctrl.Result{}, statusErr
}
Expand Down
82 changes: 0 additions & 82 deletions controllers/configurationCollector/collector.go

This file was deleted.

134 changes: 134 additions & 0 deletions controllers/configurationcollector/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// Copyright 2022 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package configurationcollector

import (
"context"
"reflect"
"strings"

corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"

"github.com/IBM/ibm-common-service-operator/controllers/bootstrap"
util "github.com/IBM/ibm-common-service-operator/controllers/common"
"github.com/IBM/ibm-common-service-operator/controllers/constant"
)

func Buildconfig(config map[string]string, bs *bootstrap.Bootstrap) map[string]string {
builder := configbuilder{data: config, bs: bs}
updatedConfig := builder.setDefaultStorageClass()
return updatedConfig.data
}

type configbuilder struct {
data map[string]string
bs *bootstrap.Bootstrap
}

func (b *configbuilder) setDefaultStorageClass() *configbuilder {
scList := &storagev1.StorageClassList{}
err := b.bs.Reader.List(context.TODO(), scList)
if err != nil {
return b
}
if len(scList.Items) == 0 {
return b
}

var defaultSC string
var defaultSCList []string
var allSCList []string

for _, sc := range scList.Items {
if defaultSC == "" {
defaultSC = sc.Name
}
if sc.ObjectMeta.GetAnnotations()["storageclass.kubernetes.io/is-default-class"] == "true" {
defaultSCList = append(defaultSCList, sc.Name)
}
if sc.Provisioner == "kubernetes.io/no-provisioner" {
continue
}
allSCList = append(allSCList, sc.GetName())
}

if b.data == nil {
b.data = make(map[string]string)
}
if defaultSC != "" {
b.data["storageclass.default"] = defaultSC
}

if len(defaultSCList) != 1 {
b.data["storageclass.default.list"] = strings.Join(defaultSCList, ",")
}

if len(allSCList) != 0 {
b.data["storageclass.list"] = strings.Join(allSCList, ",")
}

return b
}

// CreateUpdateConfig deploys config builder for global cpp configmap
func CreateUpdateConfig(bs *bootstrap.Bootstrap) error {
config := &corev1.ConfigMap{}
if err := bs.Reader.Get(context.TODO(), types.NamespacedName{Name: constant.IBMCPPCONFIG, Namespace: bs.CSData.ServicesNs}, config); err != nil && !errors.IsNotFound(err) {
klog.Errorf("Failed to get ConfigMap %s/%s: %v", bs.CSData.ServicesNs, constant.IBMCPPCONFIG, err)
return err
} else if errors.IsNotFound(err) {
config.ObjectMeta.Name = constant.IBMCPPCONFIG
config.ObjectMeta.Namespace = bs.CSData.ServicesNs
config.Data = make(map[string]string)
config.Data = Buildconfig(config.Data, bs)
if !(config.Labels != nil && config.Labels[constant.CsManagedLabel] == "true") {
util.EnsureLabelsForConfigMap(config, map[string]string{
constant.CsManagedLabel: "true",
})
}
if err := bs.Client.Create(context.TODO(), config); err != nil {
klog.Errorf("Failed to create ConfigMap %s/%s: %v", bs.CSData.ServicesNs, constant.IBMCPPCONFIG, err)
return err
}
klog.Infof("Global CPP config %s/%s is created", bs.CSData.ServicesNs, constant.IBMCPPCONFIG)
} else {
orgConfig := config.DeepCopy()
config.Data = Buildconfig(config.Data, bs)
if !(config.Labels != nil && config.Labels[constant.CsManagedLabel] == "true") {
util.EnsureLabelsForConfigMap(config, map[string]string{
constant.CsManagedLabel: "true",
})
}
if !reflect.DeepEqual(orgConfig, config) {
if err := bs.Client.Update(context.TODO(), config); err != nil {
klog.Errorf("Failed to update ConfigMap %s/%s: %v", bs.CSData.ServicesNs, constant.IBMCPPCONFIG, err)
return err
}
klog.Infof("Global CPP config %s/%s is updated", bs.CSData.ServicesNs, constant.IBMCPPCONFIG)
}
}

if err := bs.PropagateCPPConfig(config); err != nil {
klog.Error(err)
return err
}
return nil
}
Loading