Skip to content

Commit

Permalink
PWX-32425: Non-managed Replicasets activating/deactivating with stork…
Browse files Browse the repository at this point in the history
…ctl.
  • Loading branch information
diptiranjanpx committed Aug 11, 2023
1 parent 745d8ee commit b15be13
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/resourceutils/updatereplicas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions pkg/storkctl/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}})
Expand Down Expand Up @@ -352,13 +373,18 @@ func createMigratedDeploymentConfig(t *testing.T) {

func TestActivateDeactivateMigrations(t *testing.T) {
createMigratedDeployment(t)
createMigratedReplicaset(t)
createMigratedStatefulSet(t)
createMigratedDeploymentConfig(t)

cmdArgs := []string{"activate", "migrations", "-n", "dep"}
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)
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit b15be13

Please sign in to comment.