Skip to content

Commit

Permalink
Delete central ingress considering TTL timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
gargravarr committed Oct 23, 2023
1 parent ffa97f7 commit fcad1c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
51 changes: 39 additions & 12 deletions controller/stackset.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,19 +1021,33 @@ func (c *StackSetController) convertToTrafficSegments(
return nil
}

var ingTimestamp, rgTimestamp *metav1.Time
for _, sc := range ssc.StackContainers {
// If we find at least one stack with a segment, we can delete the
// central ingress resources.
if sc.Resources.IngressSegment != nil ||
sc.Resources.RouteGroupSegment != nil {
if ingTimestamp == nil && sc.Resources.IngressSegment != nil {
ingTimestamp = &sc.Resources.IngressSegment.CreationTimestamp
}

break
if rgTimestamp == nil && sc.Resources.RouteGroupSegment != nil {
rgTimestamp = &sc.Resources.RouteGroupSegment.CreationTimestamp
}

return nil
if ingTimestamp != nil && rgTimestamp != nil {
break
}
}

if ssc.Ingress != nil {
if ingTimestamp != nil && ssc.Ingress != nil {
if !resourceReadyTime(ingTimestamp.Time, c.ingressSourceSwitchTTL) {
c.logger.Infof(
"Not deleting Ingress %s yet, segments created less than %s ago",
ssc.Ingress.Name,
c.ingressSourceSwitchTTL,
)
return nil
}

err := c.client.NetworkingV1().Ingresses(ssc.Ingress.Namespace).Delete(
ctx,
ssc.Ingress.Name,
Expand All @@ -1047,14 +1061,23 @@ func (c *StackSetController) convertToTrafficSegments(
ssc.StackSet,
v1.EventTypeNormal,
"DeletedIngress",
"Deleted Ingress %s",
"Deleted Ingress %s, StackSet conversion complete",
ssc.Ingress.Namespace,
)

ssc.Ingress = nil
}

if ssc.RouteGroup != nil {
if rgTimestamp != nil && ssc.RouteGroup != nil {
if !resourceReadyTime(rgTimestamp.Time, c.ingressSourceSwitchTTL) {
c.logger.Infof(
"Not deleting RouteGroup %s yet, segments created less than %s ago",
ssc.RouteGroup.Name,
c.ingressSourceSwitchTTL,
)
return nil
}

err := c.client.RouteGroupV1().RouteGroups(
ssc.RouteGroup.Namespace,
).Delete(
Expand All @@ -1070,7 +1093,7 @@ func (c *StackSetController) convertToTrafficSegments(
ssc.RouteGroup,
v1.EventTypeNormal,
"DeletedRouteGroup",
"Deleted RouteGroup %s",
"Deleted RouteGroup %s, StackSet conversion complete",
ssc.RouteGroup.Namespace,
)

Expand Down Expand Up @@ -1337,9 +1360,13 @@ func resourceReady(timestamp string, ttl time.Duration) (bool, error) {
return false, err
}

if !resourceLastUpdated.IsZero() && time.Since(resourceLastUpdated) > ttl {
return true, nil
return resourceReadyTime(resourceLastUpdated, ttl), nil
}

func resourceReadyTime(timestamp time.Time, ttl time.Duration) bool {
if !timestamp.IsZero() && time.Since(timestamp) > ttl {
return true
}

return false, nil
}
return false
}
2 changes: 1 addition & 1 deletion pkg/core/stackset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func TestStackSetUpdateFromResourcesPopulatesIngress(t *testing.T) {
expectedIngress *zv1.StackSetIngressSpec
}{
{
name: "no ingress",
name: "no ingress",
expectedIngress: nil,
},
{
Expand Down

0 comments on commit fcad1c7

Please sign in to comment.