Skip to content

Commit 8d60a12

Browse files
committed
fix lint and add e2e
Signed-off-by: Wantong Jiang <[email protected]>
1 parent 56e4c3c commit 8d60a12

File tree

4 files changed

+134
-12
lines changed

4 files changed

+134
-12
lines changed

test/e2e/updaterun_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package e2e
1818

1919
import (
2020
"fmt"
21+
"os/exec"
2122
"time"
2223

2324
. "github.com/onsi/ginkgo/v2"
@@ -1033,6 +1034,127 @@ var _ = Describe("test CRP rollout with staged update run", func() {
10331034
})
10341035
})
10351036

1037+
Context("Test kubectl-fleet approve plugin with cluster approval requests", Ordered, func() {
1038+
var strategy *placementv1beta1.ClusterStagedUpdateStrategy
1039+
updateRunName := fmt.Sprintf(updateRunNameWithSubIndexTemplate, GinkgoParallelProcess(), 0)
1040+
1041+
BeforeAll(func() {
1042+
// Create a test namespace and a configMap inside it on the hub cluster.
1043+
createWorkResources()
1044+
1045+
// Create the CRP with external rollout strategy.
1046+
crp := &placementv1beta1.ClusterResourcePlacement{
1047+
ObjectMeta: metav1.ObjectMeta{
1048+
Name: crpName,
1049+
// Add a custom finalizer; this would allow us to better observe
1050+
// the behavior of the controllers.
1051+
Finalizers: []string{customDeletionBlockerFinalizer},
1052+
},
1053+
Spec: placementv1beta1.PlacementSpec{
1054+
ResourceSelectors: workResourceSelector(),
1055+
Strategy: placementv1beta1.RolloutStrategy{
1056+
Type: placementv1beta1.ExternalRolloutStrategyType,
1057+
},
1058+
},
1059+
}
1060+
Expect(hubClient.Create(ctx, crp)).To(Succeed(), "Failed to create CRP")
1061+
1062+
// Create the clusterStagedUpdateStrategy.
1063+
strategy = createStagedUpdateStrategySucceed(strategyName)
1064+
})
1065+
1066+
AfterAll(func() {
1067+
// Remove the custom deletion blocker finalizer from the CRP.
1068+
ensureCRPAndRelatedResourcesDeleted(crpName, allMemberClusters)
1069+
1070+
// Delete the clusterStagedUpdateRun.
1071+
ensureUpdateRunDeletion(updateRunName)
1072+
1073+
// Delete the clusterStagedUpdateStrategy.
1074+
ensureUpdateRunStrategyDeletion(strategyName)
1075+
})
1076+
1077+
It("Should create a staged update run and verify cluster approval request is created", func() {
1078+
validateLatestResourceSnapshot(crpName, resourceSnapshotIndex1st)
1079+
validateLatestPolicySnapshot(crpName, policySnapshotIndex1st, 3)
1080+
createStagedUpdateRunSucceed(updateRunName, crpName, resourceSnapshotIndex1st, strategyName)
1081+
1082+
// Verify that cluster approval request is created for canary stage.
1083+
Eventually(func() error {
1084+
appReqList := &placementv1beta1.ClusterApprovalRequestList{}
1085+
if err := hubClient.List(ctx, appReqList, client.MatchingLabels{
1086+
placementv1beta1.TargetUpdatingStageNameLabel: envCanary,
1087+
placementv1beta1.TargetUpdateRunLabel: updateRunName,
1088+
}); err != nil {
1089+
return fmt.Errorf("failed to list approval requests: %w", err)
1090+
}
1091+
1092+
if len(appReqList.Items) != 1 {
1093+
return fmt.Errorf("want 1 approval request, got %d", len(appReqList.Items))
1094+
}
1095+
return nil
1096+
}, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to find cluster approval request")
1097+
})
1098+
1099+
It("Should approve cluster approval request using kubectl-fleet approve plugin", func() {
1100+
var approvalRequestName string
1101+
1102+
// Get the cluster approval request name.
1103+
Eventually(func() error {
1104+
appReqList := &placementv1beta1.ClusterApprovalRequestList{}
1105+
if err := hubClient.List(ctx, appReqList, client.MatchingLabels{
1106+
placementv1beta1.TargetUpdatingStageNameLabel: envCanary,
1107+
placementv1beta1.TargetUpdateRunLabel: updateRunName,
1108+
}); err != nil {
1109+
return fmt.Errorf("failed to list approval requests: %w", err)
1110+
}
1111+
1112+
if len(appReqList.Items) != 1 {
1113+
return fmt.Errorf("want 1 approval request, got %d", len(appReqList.Items))
1114+
}
1115+
1116+
approvalRequestName = appReqList.Items[0].Name
1117+
return nil
1118+
}, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to get approval request name")
1119+
1120+
// Use kubectl-fleet approve plugin to approve the request
1121+
cmd := exec.Command(fleetBinaryPath, "approve", "clusterapprovalrequest",
1122+
"--hubClusterContext", "kind-hub",
1123+
"--name", approvalRequestName)
1124+
output, err := cmd.CombinedOutput()
1125+
Expect(err).ToNot(HaveOccurred(), "kubectl-fleet approve failed: %s", string(output))
1126+
1127+
// Verify the approval request is approved
1128+
Eventually(func() error {
1129+
var appReq placementv1beta1.ClusterApprovalRequest
1130+
if err := hubClient.Get(ctx, client.ObjectKey{Name: approvalRequestName}, &appReq); err != nil {
1131+
return fmt.Errorf("failed to get approval request: %w", err)
1132+
}
1133+
1134+
approvedCondition := meta.FindStatusCondition(appReq.Status.Conditions, string(placementv1beta1.ApprovalRequestConditionApproved))
1135+
if approvedCondition == nil {
1136+
return fmt.Errorf("approved condition not found")
1137+
}
1138+
if approvedCondition.Status != metav1.ConditionTrue {
1139+
return fmt.Errorf("approved condition status is %s, want True", approvedCondition.Status)
1140+
}
1141+
return nil
1142+
}, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to verify approval request is approved")
1143+
})
1144+
1145+
It("Should complete the staged update run after approval", func() {
1146+
updateRunSucceededActual := updateRunStatusSucceededActual(updateRunName, policySnapshotIndex1st, len(allMemberClusters), defaultApplyStrategy, &strategy.Spec, [][]string{{allMemberClusterNames[1]}, {allMemberClusterNames[0], allMemberClusterNames[2]}}, nil, nil, nil)
1147+
Eventually(updateRunSucceededActual, updateRunEventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to validate updateRun %s succeeded", updateRunName)
1148+
checkIfPlacedWorkResourcesOnMemberClustersInUpdateRun(allMemberClusters)
1149+
})
1150+
1151+
It("Should update crp status as completed", func() {
1152+
crpStatusUpdatedActual := crpStatusWithExternalStrategyActual(workResourceIdentifiers(), resourceSnapshotIndex1st, true, allMemberClusterNames,
1153+
[]string{resourceSnapshotIndex1st, resourceSnapshotIndex1st, resourceSnapshotIndex1st}, []bool{true, true, true}, nil, nil)
1154+
Eventually(crpStatusUpdatedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to update CRP %s status as expected", crpName)
1155+
})
1156+
})
1157+
10361158
Context("Test CRP rollout strategy transition from external to rollingUpdate", Ordered, func() {
10371159
var strategy *placementv1beta1.ClusterStagedUpdateStrategy
10381160
updateRunName := fmt.Sprintf(updateRunNameWithSubIndexTemplate, GinkgoParallelProcess(), 0)

tools/fleet/cmd/approve/approve.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ func (o *approveOptions) setupClient() error {
123123
scheme := runtime.NewScheme()
124124

125125
if err := clusterv1beta1.AddToScheme(scheme); err != nil {
126-
return fmt.Errorf("failed to add custom APIs (cluster) to the runtime scheme: %v", err)
126+
return fmt.Errorf("failed to add custom APIs (cluster) to the runtime scheme: %w", err)
127127
}
128128
if err := placementv1beta1.AddToScheme(scheme); err != nil {
129-
return fmt.Errorf("failed to add custom APIs (placement) to the runtime scheme: %v", err)
129+
return fmt.Errorf("failed to add custom APIs (placement) to the runtime scheme: %w", err)
130130
}
131131

132132
hubClient, err := toolsutils.GetClusterClientFromClusterContext(o.hubClusterContext, scheme)
133133
if err != nil {
134-
return fmt.Errorf("failed to create hub cluster client: %v", err)
134+
return fmt.Errorf("failed to create hub cluster client: %w", err)
135135
}
136136

137137
o.hubClient = hubClient

tools/fleet/cmd/drain/drain.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (o *drainOptions) runDrain() error {
8585

8686
isDrainSuccessful, err := o.drain(ctx)
8787
if err != nil {
88-
return fmt.Errorf("failed to drain member cluster %s: %v", o.clusterName, err)
88+
return fmt.Errorf("failed to drain member cluster %s: %w", o.clusterName, err)
8989
}
9090

9191
if isDrainSuccessful {
@@ -97,7 +97,7 @@ func (o *drainOptions) runDrain() error {
9797
log.Printf("retrying drain to ensure all resources propagated from hub cluster are evicted")
9898
isDrainRetrySuccessful, err := o.drain(ctx)
9999
if err != nil {
100-
return fmt.Errorf("failed to drain cluster on retry %s: %v", o.clusterName, err)
100+
return fmt.Errorf("failed to drain cluster on retry %s: %w", o.clusterName, err)
101101
}
102102
if isDrainRetrySuccessful {
103103
log.Printf("drain retry was successful for cluster %s", o.clusterName)
@@ -114,15 +114,15 @@ func (o *drainOptions) setupClient() error {
114114
scheme := runtime.NewScheme()
115115

116116
if err := clusterv1beta1.AddToScheme(scheme); err != nil {
117-
return fmt.Errorf("failed to add custom APIs (cluster) to the runtime scheme: %v", err)
117+
return fmt.Errorf("failed to add custom APIs (cluster) to the runtime scheme: %w", err)
118118
}
119119
if err := placementv1beta1.AddToScheme(scheme); err != nil {
120-
return fmt.Errorf("failed to add custom APIs (placement) to the runtime scheme: %v", err)
120+
return fmt.Errorf("failed to add custom APIs (placement) to the runtime scheme: %w", err)
121121
}
122122

123123
hubClient, err := toolsutils.GetClusterClientFromClusterContext(o.hubClusterContext, scheme)
124124
if err != nil {
125-
return fmt.Errorf("failed to create hub cluster client: %v", err)
125+
return fmt.Errorf("failed to create hub cluster client: %w", err)
126126
}
127127

128128
o.hubClient = hubClient

tools/fleet/cmd/uncordon/uncordon.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewCmdUncordon() *cobra.Command {
7070
func (o *uncordonOptions) runUncordon() error {
7171
ctx := context.Background()
7272
if err := o.uncordon(ctx); err != nil {
73-
return fmt.Errorf("failed to uncordon cluster %s: %v", o.clusterName, err)
73+
return fmt.Errorf("failed to uncordon cluster %s: %w", o.clusterName, err)
7474
}
7575

7676
log.Printf("uncordoned member cluster %s", o.clusterName)
@@ -82,15 +82,15 @@ func (o *uncordonOptions) setupClient() error {
8282
scheme := runtime.NewScheme()
8383

8484
if err := clusterv1beta1.AddToScheme(scheme); err != nil {
85-
return fmt.Errorf("failed to add custom APIs (cluster) to the runtime scheme: %v", err)
85+
return fmt.Errorf("failed to add custom APIs (cluster) to the runtime scheme: %w", err)
8686
}
8787
if err := placementv1beta1.AddToScheme(scheme); err != nil {
88-
return fmt.Errorf("failed to add custom APIs (placement) to the runtime scheme: %v", err)
88+
return fmt.Errorf("failed to add custom APIs (placement) to the runtime scheme: %w", err)
8989
}
9090

9191
hubClient, err := toolsutils.GetClusterClientFromClusterContext(o.hubClusterContext, scheme)
9292
if err != nil {
93-
return fmt.Errorf("failed to create hub cluster client: %v", err)
93+
return fmt.Errorf("failed to create hub cluster client: %w", err)
9494
}
9595

9696
o.hubClient = hubClient

0 commit comments

Comments
 (0)