diff --git a/pkg/resourceutils/updatereplicas.go b/pkg/resourceutils/updatereplicas.go index d14c37dc67..f6beb72893 100644 --- a/pkg/resourceutils/updatereplicas.go +++ b/pkg/resourceutils/updatereplicas.go @@ -26,6 +26,7 @@ import ( func ScaleReplicas(namespace string, activate bool, printFunc func(string, string), config *rest.Config) { updateStatefulSets(namespace, activate, printFunc) updateDeployments(namespace, activate, printFunc) + updateReplicaSets(namespace, activate, printFunc) updateDeploymentConfigs(namespace, activate, printFunc) updateIBPObjects("IBPPeer", namespace, activate, printFunc) updateIBPObjects("IBPCA", namespace, activate, printFunc) @@ -75,6 +76,28 @@ func updateDeployments(namespace string, activate bool, printFunc func(string, s } } +func updateReplicaSets(namespace string, activate bool, printFunc func(string, string)) { + replicasets, err := apps.Instance().ListReplicaSets(namespace, metav1.ListOptions{}) + if err != nil { + util.CheckErr(err) + return + } + for _, replicaset := range replicasets { + if replicaset.OwnerReferences != nil { + continue + } + if replicas, update := getUpdatedReplicaCount(replicaset.Annotations, activate, printFunc); update { + replicaset.Spec.Replicas = &replicas + _, err := apps.Instance().UpdateReplicaSet(&replicaset) + if err != nil { + printFunc(fmt.Sprintf("Error updating replicas for replicaset %v/%v : %v", replicaset.Namespace, replicaset.Name, err), "err") + continue + } + printFunc(fmt.Sprintf("Updated replicas for replicaset %v/%v to %v", replicaset.Namespace, replicaset.Name, replicas), "out") + } + } +} + func updateDeploymentConfigs(namespace string, activate bool, printFunc func(string, string)) { deployments, err := openshift.Instance().ListDeploymentConfigs(namespace) if err != nil { diff --git a/pkg/storkctl/migration_test.go b/pkg/storkctl/migration_test.go index f65247e269..e885a0e843 100644 --- a/pkg/storkctl/migration_test.go +++ b/pkg/storkctl/migration_test.go @@ -307,6 +307,27 @@ func createMigratedDeployment(t *testing.T) { require.NoError(t, err, "Error creating deployment") } +func createMigratedReplicaset(t *testing.T) { + replicas := int32(0) + _, err := core.Instance().CreateNamespace(&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "rs"}}) + require.NoError(t, err, "Error creating rs namespace") + + replicaset := &appv1.ReplicaSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "migratedReplicaset", + Namespace: "rs", + Annotations: map[string]string{ + migration.StorkMigrationReplicasAnnotation: "1", + }, + }, + Spec: appv1.ReplicaSetSpec{ + Replicas: &replicas, + }, + } + _, err = apps.Instance().CreateReplicaSet(replicaset, metav1.CreateOptions{}) + require.NoError(t, err, "Error creating replicaset") +} + func createMigratedStatefulSet(t *testing.T) { replicas := int32(0) _, err := core.Instance().CreateNamespace(&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "sts"}}) @@ -352,6 +373,7 @@ func createMigratedDeploymentConfig(t *testing.T) { func TestActivateDeactivateMigrations(t *testing.T) { createMigratedDeployment(t) + createMigratedReplicaset(t) createMigratedStatefulSet(t) createMigratedDeploymentConfig(t) @@ -359,6 +381,10 @@ func TestActivateDeactivateMigrations(t *testing.T) { expected := "Updated replicas for deployment dep/migratedDeployment to 1\n" testCommon(t, cmdArgs, nil, expected, false) + cmdArgs = []string{"activate", "migrations", "-n", "rs"} + expected = "Updated replicas for replicaset rs/migratedReplicaset to 1\n" + testCommon(t, cmdArgs, nil, expected, false) + cmdArgs = []string{"activate", "migrations", "-n", "depconf"} expected = "Updated replicas for deploymentconfig depconf/migratedDeploymentConfig to 1\n" testCommon(t, cmdArgs, nil, expected, false) @@ -371,6 +397,10 @@ func TestActivateDeactivateMigrations(t *testing.T) { expected = "Updated replicas for deployment dep/migratedDeployment to 0\n" testCommon(t, cmdArgs, nil, expected, false) + cmdArgs = []string{"deactivate", "migrations", "-n", "rs"} + expected = "Updated replicas for replicaset rs/migratedReplicaset to 0\n" + testCommon(t, cmdArgs, nil, expected, false) + cmdArgs = []string{"deactivate", "migrations", "-n", "depconf"} expected = "Updated replicas for deploymentconfig depconf/migratedDeploymentConfig to 0\n" testCommon(t, cmdArgs, nil, expected, false) @@ -382,12 +412,14 @@ func TestActivateDeactivateMigrations(t *testing.T) { cmdArgs = []string{"activate", "migrations", "-a"} expected = "Updated replicas for deployment dep/migratedDeployment to 1\n" expected += "Updated replicas for deploymentconfig depconf/migratedDeploymentConfig to 1\n" + expected += "Updated replicas for replicaset rs/migratedReplicaset to 1\n" expected += "Updated replicas for statefulset sts/migratedStatefulSet to 3\n" testCommon(t, cmdArgs, nil, expected, false) cmdArgs = []string{"deactivate", "migrations", "-a"} expected = "Updated replicas for deployment dep/migratedDeployment to 0\n" expected += "Updated replicas for deploymentconfig depconf/migratedDeploymentConfig to 0\n" + expected += "Updated replicas for replicaset rs/migratedReplicaset to 0\n" expected += "Updated replicas for statefulset sts/migratedStatefulSet to 0\n" testCommon(t, cmdArgs, nil, expected, false) }