Skip to content

Commit

Permalink
Fix dell-replication-controller-config reconcile reset issue (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianAtDell authored Oct 24, 2024
1 parent f807e20 commit 5799506
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 33 deletions.
13 changes: 13 additions & 0 deletions controllers/csm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ func (r *ContainerStorageModuleReconciler) SyncCSM(ctx context.Context, cr csmv1

node.Rbac.ClusterRole = *clusterRoleForNode
case csmv1.Replication:
// This function adds replication sidecar to driver pods.
log.Info("Injecting CSM Replication")
dp, err := modules.ReplicationInjectDeployment(controller.Deployment, cr, operatorConfig)
if err != nil {
Expand Down Expand Up @@ -887,9 +888,17 @@ func (r *ContainerStorageModuleReconciler) SyncCSM(ctx context.Context, cr csmv1
}

if replicationEnabled {
// This will also create the dell-replication-controller namespace.
if err = modules.ReplicationManagerController(ctx, false, operatorConfig, cr, cluster.ClusterCTRLClient); err != nil {
return fmt.Errorf("failed to deploy replication controller: %v", err)
}

// Create ConfigMap if it does not already exist.
// ConfigMap requires namespace to be created.
_, err = modules.CreateReplicationConfigmap(ctx, cr, operatorConfig, ctrlClient)
if err != nil {
return fmt.Errorf("injecting replication into replication configmap: %v", err)
}
}

// if Observability is enabled, create or update obs components: topology, metrics of PowerScale and PowerFlex
Expand Down Expand Up @@ -1256,6 +1265,10 @@ func (r *ContainerStorageModuleReconciler) removeDriver(ctx context.Context, ins
if err = modules.ReplicationManagerController(ctx, true, operatorConfig, instance, cluster.ClusterCTRLClient); err != nil {
return err
}
log.Infow("Deleting Replication configmap")
if err = modules.DeleteReplicationConfigmap(cluster.ClusterCTRLClient); err != nil {
return err
}
}

// remove module observability
Expand Down
11 changes: 0 additions & 11 deletions operatorconfig/moduleconfig/replication/v1.10.0/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,6 @@ subjects:
namespace: dell-replication-controller
---
apiVersion: v1
data:
config.yaml: |
clusterId: ""
targets: []
CSI_LOG_LEVEL: "<REPLICATION_CTRL_LOG_LEVEL>"
kind: ConfigMap
metadata:
name: dell-replication-controller-config
namespace: dell-replication-controller
---
apiVersion: v1
kind: Service
metadata:
labels:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: dell-replication-controller-config
namespace: dell-replication-controller
data:
config.yaml: |
clusterId: ""
targets: []
CSI_LOG_LEVEL: "debug"
11 changes: 0 additions & 11 deletions operatorconfig/moduleconfig/replication/v1.8.1/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,6 @@ subjects:
namespace: dell-replication-controller
---
apiVersion: v1
data:
config.yaml: |
clusterId: ""
targets: []
CSI_LOG_LEVEL: "<REPLICATION_CTRL_LOG_LEVEL>"
kind: ConfigMap
metadata:
name: dell-replication-controller-config
namespace: dell-replication-controller
---
apiVersion: v1
kind: Service
metadata:
labels:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: dell-replication-controller-config
namespace: dell-replication-controller
data:
config.yaml: |
clusterId: ""
targets: []
CSI_LOG_LEVEL: "debug"
11 changes: 0 additions & 11 deletions operatorconfig/moduleconfig/replication/v1.9.0/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,6 @@ subjects:
namespace: dell-replication-controller
---
apiVersion: v1
data:
config.yaml: |
clusterId: ""
targets: []
CSI_LOG_LEVEL: "<REPLICATION_CTRL_LOG_LEVEL>"
kind: ConfigMap
metadata:
name: dell-replication-controller-config
namespace: dell-replication-controller
---
apiVersion: v1
kind: Service
metadata:
labels:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: dell-replication-controller-config
namespace: dell-replication-controller
data:
config.yaml: |
clusterId: ""
targets: []
CSI_LOG_LEVEL: "debug"
52 changes: 52 additions & 0 deletions pkg/modules/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
t1 "k8s.io/apimachinery/pkg/types"

csmv1 "github.com/dell/csm-operator/api/v1"

"github.com/dell/csm-operator/pkg/drivers"
Expand Down Expand Up @@ -428,3 +433,50 @@ func ReplicationManagerController(ctx context.Context, isDeleting bool, op utils

return nil
}

func CreateReplicationConfigmap(ctx context.Context, cr csmv1.ContainerStorageModule, op utils.OperatorConfig, ctrlClient client.Client) ([]crclient.Object, error) {
replica, err := getReplicaModule(cr)
if err != nil {
return nil, err
}

buf, err := readConfigFile(replica, cr, op, "dell-replication-controller-config.yaml")
if err != nil {
return nil, err
}

var cm corev1.ConfigMap
if err := yaml.Unmarshal(buf, &cm); err != nil {
return nil, err
}

// Check if the ConfigMap already exists
foundConfigMap := &corev1.ConfigMap{}

err = ctrlClient.Get(ctx, t1.NamespacedName{Name: cm.Name, Namespace: cm.Namespace}, foundConfigMap)
if err != nil && k8serrors.IsNotFound(err) {
// ConfigMap doesn't exist, create it
if err := ctrlClient.Create(ctx, &cm); err != nil {
return nil, err
}
}
return []crclient.Object{&cm}, nil
}

func DeleteReplicationConfigmap(ctrlClient client.Client) error {
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "dell-replication-controller-config",
Namespace: "dell-replication-controller",
},
}

if err := ctrlClient.Delete(context.Background(), configMap); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}

return nil
}
72 changes: 72 additions & 0 deletions pkg/modules/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import (
utils "github.com/dell/csm-operator/pkg/utils"
"github.com/dell/csm-operator/tests/shared"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
t1 "k8s.io/apimachinery/pkg/types"
applyv1 "k8s.io/client-go/applyconfigurations/apps/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -406,3 +410,71 @@ func TestReplicationManagerController(t *testing.T) {
})
}
}

func TestReplicationConfigmap(t *testing.T) {
// Create a fake client to use in the test
scheme := runtime.NewScheme()
_ = corev1.AddToScheme(scheme)
fakeClient := ctrlClientFake.NewClientBuilder().WithScheme(scheme).Build()

// Create a test ContainerStorageModule
cr, err := getCustomResource("./testdata/cr_powerscale_replica.yaml")
if err != nil {
panic(err)
}

// Call the function we want to test
// we can't use test config, as it doesn't have versionvalues
realConfig := utils.OperatorConfig{
ConfigDirectory: "../../operatorconfig",
}
objs, err := CreateReplicationConfigmap(context.Background(), cr, realConfig, fakeClient)
// Check that the function returned the expected results
if err != nil {
t.Errorf("CreateReplicationConfigmap returned an unexpected error: %v", err)
}

if len(objs) != 1 {
t.Errorf("CreateReplicationConfigmap returned the wrong number of objects: %d", len(objs))
}

cm, ok := objs[0].(*corev1.ConfigMap)
if !ok {
t.Errorf("CreateReplicationConfigmap returned the wrong type of object: %T", objs[0])
}

if cm.Name != "dell-replication-controller-config" {
t.Errorf("CreateReplicationConfigmap returned the wrong ConfigMap name: %s", cm.Name)
}

if cm.Namespace != "dell-replication-controller" {
t.Errorf("CreateReplicationConfigmap returned the wrong ConfigMap namespace: %s", cm.Namespace)
}

// Check that the ConfigMap was created in the fake client
foundConfigMap := &corev1.ConfigMap{}
err = fakeClient.Get(context.Background(), t1.NamespacedName{Name: "dell-replication-controller-config", Namespace: "dell-replication-controller"}, foundConfigMap)
if err != nil {
t.Errorf("ConfigMap was not created in the fake client: %v", err)
}

// Now verify that the ConfigMap can be deleted properly
// Call the function we want to test
if err := DeleteReplicationConfigmap(fakeClient); err != nil {
t.Errorf("DeleteReplicationConfigmap returned an unexpected error: %v", err)
}

// Check that the ConfigMap was deleted from the fake client
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "dell-replication-controller-config",
Namespace: "dell-replication-controller",
},
}
err = fakeClient.Get(context.Background(), t1.NamespacedName{Name: cm.Name, Namespace: cm.Namespace}, configMap)
if err == nil {
t.Errorf("ConfigMap was not deleted from the fake client")
} else if !k8serrors.IsNotFound(err) {
t.Errorf("ConfigMap was not deleted from the fake client: %v", err)
}
}
10 changes: 10 additions & 0 deletions pkg/modules/testdata/dell-replication-controller-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: dell-replication-controller-config
namespace: dell-replication-controller
data:
config.yaml: |
clusterId: ""
targets: []
CSI_LOG_LEVEL: "debug"
1 change: 1 addition & 0 deletions pkg/utils/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func getDaemonSetStatus(ctx context.Context, instance *csmv1.ContainerStorageMod
}

for _, cluster := range clusterClients {
totalRunning = 0
log.Infof("\ndaemonset status for cluster: %s", cluster.ClusterID)
msg += fmt.Sprintf("error message for %s \n", cluster.ClusterID)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ spec:
csiDriverType: "powermax"
configVersion: v2.12.0
forceRemoveDriver: true
common:
image: "quay.io/dell/container-storage-modules/csi-powermax:nightly"
modules:
- name: replication
enabled: true

0 comments on commit 5799506

Please sign in to comment.