Skip to content

Commit 47b9b80

Browse files
Merge pull request #843 from reedcort/SREP-2822-remove-override
[SREP-2822] Add remove override flag to request-serving-nodes
2 parents 89927a5 + 09352ee commit 47b9b80

File tree

4 files changed

+168
-4
lines changed

4 files changed

+168
-4
lines changed

cmd/cluster/resize/requestserving_node.go

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ const (
2424
)
2525

2626
type requestServingNodesOpts struct {
27-
clusterID string
28-
cluster *cmv1.Cluster
29-
size string
30-
reason string
27+
clusterID string
28+
cluster *cmv1.Cluster
29+
size string
30+
reason string
31+
removeOverride bool
3132

3233
// mgmtClient is a K8s client to management cluster
3334
mgmtClient client.Client
@@ -58,6 +59,9 @@ func newCmdResizeRequestServingNodes() *cobra.Command {
5859
5960
# Resize a ROSA HCP cluster's request-serving nodes to a specific size
6061
osdctl cluster resize request-serving-nodes --cluster-id "${CLUSTER_ID}" --size m54xl --reason "${OHSS}"
62+
63+
# Remove the cluster-size-override annotation to revert to default sizing behavior
64+
osdctl cluster resize request-serving-nodes --cluster-id "${CLUSTER_ID}" --remove-override --reason "${OHSS}"
6165
`,
6266
Args: cobra.NoArgs,
6367
DisableAutoGenTag: true,
@@ -69,8 +73,10 @@ func newCmdResizeRequestServingNodes() *cobra.Command {
6973
cmd.Flags().StringVarP(&opts.clusterID, "cluster-id", "C", "", "The internal ID of the cluster to perform actions on")
7074
cmd.Flags().StringVar(&opts.size, "size", "", "The target request-serving node size (e.g. m54xl). If not specified, will auto-select the next size up")
7175
cmd.Flags().StringVar(&opts.reason, "reason", "", "The reason for this command, which requires elevation, to be run (usually an OHSS or PD ticket)")
76+
cmd.Flags().BoolVar(&opts.removeOverride, "remove-override", false, "Remove the cluster-size-override annotation to revert to default sizing behavior")
7277
_ = cmd.MarkFlagRequired("cluster-id")
7378
_ = cmd.MarkFlagRequired("reason")
79+
cmd.MarkFlagsMutuallyExclusive("size", "remove-override")
7480

7581
return cmd
7682
}
@@ -154,6 +160,11 @@ func (r *requestServingNodesOpts) run(ctx context.Context) error {
154160
printer.PrintlnGreen(fmt.Sprintf("HostedCluster namespace: %s", hcNamespace))
155161
printer.PrintlnGreen(fmt.Sprintf("HostedCluster name: %s", hcName))
156162

163+
// If remove-override flag is set, remove the annotation and exit
164+
if r.removeOverride {
165+
return r.handleRemoveOverride(ctx, hostedCluster, cluster.Name(), hcNamespace)
166+
}
167+
157168
// Get current hosted-cluster-size from label
158169
currentSize := hostedCluster.Labels["hypershift.openshift.io/hosted-cluster-size"]
159170
if currentSize == "" {
@@ -402,3 +413,73 @@ func (r *requestServingNodesOpts) sendCustomerServiceLog() error {
402413

403414
return postCmd.Run()
404415
}
416+
417+
func (r *requestServingNodesOpts) handleRemoveOverride(ctx context.Context, hostedCluster *hypershiftv1beta1.HostedCluster, clusterName, hcNamespace string) error {
418+
overrideAnnotation := "hypershift.openshift.io/cluster-size-override"
419+
currentOverride, hasOverride := hostedCluster.Annotations[overrideAnnotation]
420+
421+
if !hasOverride || currentOverride == "" {
422+
printer.PrintlnGreen("\nNo cluster-size-override annotation found. Cluster is already using default sizing behavior.")
423+
return nil
424+
}
425+
426+
printer.PrintlnGreen(fmt.Sprintf("Current cluster-size-override: %s", currentOverride))
427+
428+
currentSize := hostedCluster.Labels["hypershift.openshift.io/hosted-cluster-size"]
429+
if currentSize != "" {
430+
printer.PrintlnGreen(fmt.Sprintf("Current hosted-cluster-size: %s", currentSize))
431+
}
432+
433+
fmt.Printf("\nThis will remove the cluster-size-override annotation from cluster %s\n", clusterName)
434+
if !utils.ConfirmPrompt() {
435+
return errors.New("operation cancelled by user")
436+
}
437+
438+
printer.PrintlnGreen("\nRemoving cluster-size-override annotation...")
439+
if err := r.removeClusterSizeOverride(ctx, hostedCluster); err != nil {
440+
return fmt.Errorf("failed to remove cluster-size-override annotation: %v", err)
441+
}
442+
443+
printer.PrintlnGreen("Annotation removed successfully!")
444+
445+
time.Sleep(10 * time.Second)
446+
447+
updatedHC := &hypershiftv1beta1.HostedCluster{}
448+
if err := r.mgmtClient.Get(ctx, client.ObjectKey{Namespace: hcNamespace, Name: hostedCluster.Name}, updatedHC); err != nil {
449+
fmt.Printf("Warning: failed to verify annotation removal: %v\n", err)
450+
} else {
451+
if _, stillHasOverride := updatedHC.Annotations[overrideAnnotation]; stillHasOverride {
452+
fmt.Printf("Warning: cluster-size-override annotation still present\n")
453+
} else {
454+
printer.PrintlnGreen("Verified cluster-size-override annotation has been removed")
455+
}
456+
457+
newSize := updatedHC.Labels["hypershift.openshift.io/hosted-cluster-size"]
458+
if newSize != "" {
459+
printer.PrintlnGreen(fmt.Sprintf("Current hosted-cluster-size: %s", newSize))
460+
}
461+
}
462+
463+
fmt.Println("\nOverride removed successfully!")
464+
fmt.Println("\nUse the following commands to monitor the cluster:")
465+
fmt.Printf("\nVerify cluster size (annotation and label):\n")
466+
fmt.Printf(" oc get hostedcluster -n %s -oyaml | grep -E '(cluster-size-override|hosted-cluster-size)'\n", hcNamespace)
467+
fmt.Printf("\nMonitor nodes:\n")
468+
fmt.Printf(" oc get nodes -l hypershift.openshift.io/cluster-namespace=%s\n", hcNamespace)
469+
470+
return nil
471+
}
472+
473+
func (r *requestServingNodesOpts) removeClusterSizeOverride(ctx context.Context, hostedCluster *hypershiftv1beta1.HostedCluster) error {
474+
patch := client.MergeFrom(hostedCluster.DeepCopy())
475+
476+
if hostedCluster.Annotations != nil {
477+
delete(hostedCluster.Annotations, "hypershift.openshift.io/cluster-size-override")
478+
}
479+
480+
if err := r.mgmtClientAdmin.Patch(ctx, hostedCluster, patch); err != nil {
481+
return fmt.Errorf("failed to patch hostedcluster annotation: %v", err)
482+
}
483+
484+
return nil
485+
}

cmd/cluster/resize/requestserving_node_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,81 @@ func TestApplyClusterSizeOverride(t *testing.T) {
443443
})
444444
}
445445
}
446+
447+
func TestRemoveClusterSizeOverride(t *testing.T) {
448+
tests := []struct {
449+
name string
450+
hostedCluster *hypershiftv1beta1.HostedCluster
451+
patchError error
452+
expectErr bool
453+
errorContains string
454+
}{
455+
{
456+
name: "successful removal - existing override annotation",
457+
hostedCluster: &hypershiftv1beta1.HostedCluster{
458+
ObjectMeta: metav1.ObjectMeta{
459+
Name: "test-hc",
460+
Namespace: "test-namespace",
461+
Annotations: map[string]string{
462+
"hypershift.openshift.io/cluster-size-override": "m54xl",
463+
"other": "annotation",
464+
},
465+
},
466+
},
467+
expectErr: false,
468+
},
469+
{
470+
name: "successful removal - no annotations",
471+
hostedCluster: &hypershiftv1beta1.HostedCluster{
472+
ObjectMeta: metav1.ObjectMeta{
473+
Name: "test-hc",
474+
Namespace: "test-namespace",
475+
},
476+
},
477+
expectErr: false,
478+
},
479+
{
480+
name: "patch error",
481+
hostedCluster: &hypershiftv1beta1.HostedCluster{
482+
ObjectMeta: metav1.ObjectMeta{
483+
Name: "test-hc",
484+
Namespace: "test-namespace",
485+
Annotations: map[string]string{
486+
"hypershift.openshift.io/cluster-size-override": "m54xl",
487+
},
488+
},
489+
},
490+
patchError: fmt.Errorf("api server error"),
491+
expectErr: true,
492+
errorContains: "failed to patch hostedcluster annotation",
493+
},
494+
}
495+
496+
for _, tt := range tests {
497+
t.Run(tt.name, func(t *testing.T) {
498+
mockClient := &MockClient{}
499+
mockClient.On("Patch", mock.Anything, mock.AnythingOfType("*v1beta1.HostedCluster"), mock.Anything, mock.Anything).
500+
Return(tt.patchError)
501+
502+
r := &requestServingNodesOpts{
503+
mgmtClientAdmin: mockClient,
504+
}
505+
506+
err := r.removeClusterSizeOverride(context.Background(), tt.hostedCluster)
507+
508+
if tt.expectErr {
509+
assert.Error(t, err)
510+
if tt.errorContains != "" {
511+
assert.Contains(t, err.Error(), tt.errorContains)
512+
}
513+
} else {
514+
assert.NoError(t, err)
515+
// Verify the annotation was removed
516+
_, exists := tt.hostedCluster.Annotations["hypershift.openshift.io/cluster-size-override"]
517+
assert.False(t, exists, "cluster-size-override annotation should be removed")
518+
}
519+
520+
mockClient.AssertExpectations(t)
521+
})
522+
}
523+
}

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,7 @@ osdctl cluster resize request-serving-nodes [flags]
18871887
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
18881888
-o, --output string Valid formats are ['', 'json', 'yaml', 'env']
18891889
--reason string The reason for this command, which requires elevation, to be run (usually an OHSS or PD ticket)
1890+
--remove-override Remove the cluster-size-override annotation to revert to default sizing behavior
18901891
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
18911892
-s, --server string The address and port of the Kubernetes API server
18921893
--size string The target request-serving node size (e.g. m54xl). If not specified, will auto-select the next size up

docs/osdctl_cluster_resize_request-serving-nodes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ osdctl cluster resize request-serving-nodes [flags]
2020
# Resize a ROSA HCP cluster's request-serving nodes to a specific size
2121
osdctl cluster resize request-serving-nodes --cluster-id "${CLUSTER_ID}" --size m54xl --reason "${OHSS}"
2222
23+
# Remove the cluster-size-override annotation to revert to default sizing behavior
24+
osdctl cluster resize request-serving-nodes --cluster-id "${CLUSTER_ID}" --remove-override --reason "${OHSS}"
25+
2326
```
2427

2528
### Options
@@ -28,6 +31,7 @@ osdctl cluster resize request-serving-nodes [flags]
2831
-C, --cluster-id string The internal ID of the cluster to perform actions on
2932
-h, --help help for request-serving-nodes
3033
--reason string The reason for this command, which requires elevation, to be run (usually an OHSS or PD ticket)
34+
--remove-override Remove the cluster-size-override annotation to revert to default sizing behavior
3135
--size string The target request-serving node size (e.g. m54xl). If not specified, will auto-select the next size up
3236
```
3337

0 commit comments

Comments
 (0)