Skip to content
Open
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
46 changes: 23 additions & 23 deletions pkg/controllers/updaterun/controller_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/prometheus/client_golang/prometheus"
prometheusclientmodel "github.com/prometheus/client_model/go"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -363,24 +362,6 @@ func generateSucceededMetric(updateRun *placementv1beta1.ClusterStagedUpdateRun)
}
}

func labelPairsToMap(pairs []*prometheusclientmodel.LabelPair) prometheus.Labels {
m := prometheus.Labels{}
for _, p := range pairs {
m[p.GetName()] = p.GetValue()
}
return m
}

func removeMetricFromMetricList(metricList []*prometheusclientmodel.Metric, metricToRemove *prometheusclientmodel.Metric) []*prometheusclientmodel.Metric {
var result []*prometheusclientmodel.Metric
for _, metric := range metricList {
if !cmp.Equal(labelPairsToMap(metric.Label), labelPairsToMap(metricToRemove.Label)) {
result = append(result, metric)
}
}
return result
}

func generateTestClusterStagedUpdateRun() *placementv1beta1.ClusterStagedUpdateRun {
return &placementv1beta1.ClusterStagedUpdateRun{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -480,25 +461,44 @@ func generateTestClusterResourceBindingsAndClusters(policySnapshotIndex int) ([]
return resourceBindings, targetClusters, unscheduledClusters
}

func generateSmallTestClusterResourceBindingsAndClusters(policySnapshotIndex int) ([]*placementv1beta1.ClusterResourceBinding, []*clusterv1beta1.MemberCluster, []*clusterv1beta1.MemberCluster) {
func generateSmallTestClusterResourceBindingsAndClusters(policySnapshotIndex int, numUnscheduledClusters int) ([]*placementv1beta1.ClusterResourceBinding, []*clusterv1beta1.MemberCluster, []*clusterv1beta1.MemberCluster) {
numTargetClusters := 3
policySnapshotName := fmt.Sprintf(placementv1beta1.PolicySnapshotNameFmt, testCRPName, policySnapshotIndex)
resourceBindings := make([]*placementv1beta1.ClusterResourceBinding, numTargetClusters)
resourceBindings := make([]*placementv1beta1.ClusterResourceBinding, numTargetClusters+numUnscheduledClusters)
targetClusters := make([]*clusterv1beta1.MemberCluster, numTargetClusters)
for i := range targetClusters {
// split the clusters into 2 regions
region := regionEastus
if i%2 == 0 {
region = regionWestus
}
// reserse the order of the clusters by index
// reverse the order of the clusters by index
targetClusters[i] = generateTestMemberCluster(numTargetClusters-1-i, "cluster-"+strconv.Itoa(i), map[string]string{"group": "prod", "region": region})
resourceBindings[i] = generateTestClusterResourceBinding(policySnapshotName, targetClusters[i].Name, placementv1beta1.BindingStateScheduled)
}
unscheduledClusters := make([]*clusterv1beta1.MemberCluster, 0)

resourceBindings, unscheduledClusters := generateTestUnscheduledClusterResourceBindingsAndClusters(policySnapshotIndex, numUnscheduledClusters, resourceBindings)
return resourceBindings, targetClusters, unscheduledClusters
}

func generateTestUnscheduledClusterResourceBindingsAndClusters(policySnapshotIndex int, numUnscheduledClusters int, bindings []*placementv1beta1.ClusterResourceBinding) ([]*placementv1beta1.ClusterResourceBinding, []*clusterv1beta1.MemberCluster) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pass policySnapshotName as the parameter instead of policySnapshotIndex

policySnapshotName := fmt.Sprintf(placementv1beta1.PolicySnapshotNameFmt, testCRPName, policySnapshotIndex)
targetClusters := len(bindings) - numUnscheduledClusters
unscheduledClusters := make([]*clusterv1beta1.MemberCluster, numUnscheduledClusters)
// Half of the unscheduled clusters have old policy snapshot.
for i := range numUnscheduledClusters / 2 {
unscheduledClusters[i] = generateTestMemberCluster(i, "unscheduled-cluster-"+strconv.Itoa(i), map[string]string{"group": "staging"})
// Update the policySnapshot name so that these clusters are considered to-be-deleted.
bindings[targetClusters+i] = generateTestClusterResourceBinding(policySnapshotName+"a", unscheduledClusters[i].Name, placementv1beta1.BindingStateUnscheduled)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we come up with a better format specifier for policySnapshotName

}
// The other half of the unscheduled clusters have latest policy snapshot but still unscheduled.
for i := numUnscheduledClusters / 2; i < numUnscheduledClusters; i++ {
unscheduledClusters[i] = generateTestMemberCluster(i, "unscheduled-cluster-"+strconv.Itoa(i), map[string]string{"group": "staging"})
bindings[targetClusters+i] = generateTestClusterResourceBinding(policySnapshotName, unscheduledClusters[i].Name, placementv1beta1.BindingStateUnscheduled)
}
return bindings, unscheduledClusters
}

func generateTestClusterResourceBinding(policySnapshotName, targetCluster string, state placementv1beta1.BindingState) *placementv1beta1.ClusterResourceBinding {
binding := &placementv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Expand Down
16 changes: 8 additions & 8 deletions pkg/controllers/updaterun/execution_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {

updateRun = generateTestClusterStagedUpdateRun()
crp = generateTestClusterResourcePlacement()
resourceBindings, targetClusters, _ = generateSmallTestClusterResourceBindingsAndClusters(1)
resourceBindings, targetClusters, _ = generateSmallTestClusterResourceBindingsAndClusters(1, 0)
policySnapshot = generateTestClusterSchedulingPolicySnapshot(1, len(targetClusters))
resourceSnapshot = generateTestClusterResourceSnapshot()
resourceSnapshot = generateTestClusterResourceSnapshot()
Expand Down Expand Up @@ -810,7 +810,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())

By("Validating the initialization succeeded and the execution started")
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy)
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy, 0)
wantStatus = generateExecutionStartedStatus(updateRun, initialized)
validateClusterStagedUpdateRunStatus(ctx, updateRun, wantStatus, "")

Expand Down Expand Up @@ -904,7 +904,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())

By("Validating the initialization succeeded and the execution started")
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy)
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy, 0)
wantStatus = generateExecutionStartedStatus(updateRun, initialized)
validateClusterStagedUpdateRunStatus(ctx, updateRun, wantStatus, "")

Expand Down Expand Up @@ -1013,7 +1013,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())

By("Validating the initialization succeeded and the execution started")
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy)
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy, 0)
wantStatus = generateExecutionStartedStatus(updateRun, initialized)
validateClusterStagedUpdateRunStatus(ctx, updateRun, wantStatus, "")

Expand Down Expand Up @@ -1145,7 +1145,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())

By("Validating the initialization succeeded and the execution started")
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy)
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy, 0)
wantStatus = generateExecutionStartedStatus(updateRun, initialized)
validateClusterStagedUpdateRunStatus(ctx, updateRun, wantStatus, "")

Expand Down Expand Up @@ -1237,7 +1237,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())

By("Validating the initialization succeeded and the execution started")
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy)
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy, 0)
wantStatus = generateExecutionStartedStatus(updateRun, initialized)
validateClusterStagedUpdateRunStatus(ctx, updateRun, wantStatus, "")

Expand Down Expand Up @@ -1300,7 +1300,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())

By("Validating the initialization succeeded and the execution has not started")
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy)
initialized := generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy, 0)
wantStatus = generateExecutionNotStartedStatus(updateRun, initialized)
validateClusterStagedUpdateRunStatus(ctx, updateRun, wantStatus, "")

Expand Down Expand Up @@ -1534,7 +1534,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())

By("Validating the initialization succeeded and but not execution started")
wantStatus = generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy)
wantStatus = generateSucceededInitializationStatusForSmallClusters(crp, updateRun, testResourceSnapshotIndex, policySnapshot, updateStrategy, 0)
validateClusterStagedUpdateRunStatus(ctx, updateRun, wantStatus, "")

By("Checking update run status metrics are emitted")
Expand Down
5 changes: 5 additions & 0 deletions pkg/controllers/updaterun/initialization_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ func generateSucceededInitializationStatusForSmallClusters(
resourceSnapshotIndex string,
policySnapshot *placementv1beta1.ClusterSchedulingPolicySnapshot,
updateStrategy *placementv1beta1.ClusterStagedUpdateStrategy,
numUnscheduledClusters int,
) *placementv1beta1.UpdateRunStatus {
status := &placementv1beta1.UpdateRunStatus{
PolicySnapshotIndexUsed: policySnapshot.Labels[placementv1beta1.PolicyIndexLabel],
Expand All @@ -1065,6 +1066,10 @@ func generateSucceededInitializationStatusForSmallClusters(
generateTrueCondition(updateRun, placementv1beta1.StagedUpdateRunConditionInitialized),
},
}
for i := range numUnscheduledClusters {
status.DeletionStageStatus.Clusters = append(status.DeletionStageStatus.Clusters,
placementv1beta1.ClusterUpdatingStatus{ClusterName: fmt.Sprintf("unscheduled-cluster-%d", i)})
}
for i := range status.StagesStatus {
var beforeTasks []placementv1beta1.StageTaskStatus
for _, task := range updateStrategy.Spec.Stages[i].BeforeStageTasks {
Expand Down
Loading
Loading