From c9e6ecfa4444b8772d83ba741e2574c2601e027d Mon Sep 17 00:00:00 2001 From: Rodrigo Reis Date: Wed, 28 Feb 2024 17:43:13 +0100 Subject: [PATCH] Delete central ingress/routegroup when StackSet has no Stacks. (#597) * Delete central ingress/routegroup when StackSet has no Stacks. Signed-off-by: Rodrigo Reis * Wait for stack also, get name from return value. Signed-off-by: Rodrigo Reis * Fix presclaing timeout. Signed-off-by: Rodrigo Reis * Improve commenting. Signed-off-by: Rodrigo Reis * Fix prescaling timeout. Signed-off-by: Rodrigo Reis --------- Signed-off-by: Rodrigo Reis --- cmd/e2e/ingress_source_switch_test.go | 50 ++++++++++++++++++++++++++- cmd/e2e/test_utils.go | 8 +++++ controller/stackset.go | 16 +++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/cmd/e2e/ingress_source_switch_test.go b/cmd/e2e/ingress_source_switch_test.go index 159d6574..b4431f87 100644 --- a/cmd/e2e/ingress_source_switch_test.go +++ b/cmd/e2e/ingress_source_switch_test.go @@ -143,7 +143,7 @@ func TestStackTTLConvertToSegmentIngress(t *testing.T) { // create stackset with central ingress spec := specFactory.Create(t, "v1") - err := createStackSet(stacksetName, 1, spec) + err := createStackSet(stacksetName, 0, spec) require.NoError(t, err) _, err = waitForIngress(t, stacksetName) require.NoError(t, err) @@ -183,3 +183,51 @@ func TestStackTTLConvertToSegmentIngress(t *testing.T) { ).await() require.NoError(t, err) } + +func TestShallowStackSetConvertToSegmentIngress(t *testing.T) { + t.Parallel() + stacksetName := "stackset-shallow-convert-segment" + stackVersion := "v1" + specFactory := NewTestStacksetSpecFactory(stacksetName).Ingress() + + // create stackset with central ingress + spec := specFactory.Create(t, stackVersion) + err := createStackSet(stacksetName, 0, spec) + require.NoError(t, err) + + _, err = waitForIngress(t, stacksetName) + require.NoError(t, err) + stack, err := waitForStack(t, stacksetName, stackVersion) + require.NoError(t, err) + + err = deleteStack(stack.Name) + require.NoError(t, err) + + err = resourceDeleted( + t, + "stack", + stack.Name, + stackInterface(), + ).withTimeout(time.Second * 60).await() + require.NoError(t, err) + + // Add the annotation to convert to segment ingresses but DON'T increase the + // StackSet version. + err = updateStackSetWithAnnotations( + stacksetName, + spec, + map[string]string{controller.TrafficSegmentsAnnotationKey: "true"}, + ) + require.NoError(t, err) + + time.Sleep(time.Second * 20) + + // make sure controller DOES delete the ingress + err = resourceDeleted( + t, + "ingress", + stacksetName, + ingressInterface(), + ).await() + require.NoError(t, err) +} diff --git a/cmd/e2e/test_utils.go b/cmd/e2e/test_utils.go index 245792f0..3f07e0c5 100644 --- a/cmd/e2e/test_utils.go +++ b/cmd/e2e/test_utils.go @@ -382,6 +382,14 @@ func createStackSetWithAnnotations( return err } +func deleteStack(stackName string) error { + return stackInterface().Delete( + context.Background(), + stackName, + metav1.DeleteOptions{}, + ) +} + func stacksetExists(stacksetName string) bool { _, err := stacksetInterface().Get(context.Background(), stacksetName, metav1.GetOptions{}) return err == nil diff --git a/controller/stackset.go b/controller/stackset.go index b219a1b0..9abb46fb 100644 --- a/controller/stackset.go +++ b/controller/stackset.go @@ -1144,6 +1144,22 @@ func (c *StackSetController) convertToTrafficSegments( } } + if len(ssc.StackContainers) == 0 { + c.logger.Infof( + "No stacks found for StackSet %s, safe to delete central "+ + "ingress/routegroup", + ssc.StackSet.Name, + ) + + // If we don't have any stacks, we can delete the central ingress + // resources + oldEnough := metav1.NewTime( + time.Now().Add(-c.ingressSourceSwitchTTL-time.Minute), + ) + ingTimestamp = &oldEnough + rgTimestamp = &oldEnough + } + if ingTimestamp != nil && ssc.Ingress != nil { if !resourceReadyTime(ingTimestamp.Time, c.ingressSourceSwitchTTL) { c.logger.Infof(