Skip to content

Commit

Permalink
Watch infrastructure and update AWS tags
Browse files Browse the repository at this point in the history
Signed-off-by: chiragkyal <[email protected]>
  • Loading branch information
chiragkyal committed Oct 3, 2024
1 parent 871b2b2 commit f2e5cf8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
15 changes: 15 additions & 0 deletions pkg/operator/controller/ingress/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import (

const (
controllerName = "ingress_controller"
// the name of the 'cluster' infrastructure object
clusterInfrastructureName = "cluster"
)

// TODO: consider moving these to openshift/api
Expand Down Expand Up @@ -134,6 +136,12 @@ func New(mgr manager.Manager, config Config) (controller.Controller, error) {
if err := c.Watch(source.Kind[client.Object](operatorCache, &configv1.Proxy{}, handler.EnqueueRequestsFromMapFunc(reconciler.ingressConfigToIngressController))); err != nil {
return nil, err
}
// Watch for changes to infrastructure config to update user defined tags
if err := c.Watch(source.Kind[client.Object](operatorCache, &configv1.Infrastructure{}, handler.EnqueueRequestsFromMapFunc(reconciler.ingressConfigToIngressController),
predicate.NewPredicateFuncs(hasName(clusterInfrastructureName)),
)); err != nil {
return nil, err
}
return c, nil
}

Expand Down Expand Up @@ -187,6 +195,13 @@ func enqueueRequestForOwningIngressController(namespace string) handler.EventHan
})
}

// hasName returns a predicate which checks whether an object has the given name.
func hasName(name string) func(o client.Object) bool {
return func(o client.Object) bool {
return o.GetName() == name
}
}

// Config holds all the things necessary for the controller to run.
type Config struct {
Namespace string
Expand Down
11 changes: 9 additions & 2 deletions pkg/operator/controller/ingress/load_balancer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ var (
//
// https://cloud.ibm.com/docs/containers?topic=containers-vpc-lbaas
iksLBEnableFeaturesAnnotation,
// awsLBAdditionalResourceTags annotation is populated by user tags present in `platform.AWS.ResourceTags`
awsLBAdditionalResourceTags,
)

// Azure and GCP support switching between internal and external
Expand All @@ -280,6 +282,7 @@ var (
// Always returns the current LB service if one exists (whether it already
// existed or was created during the course of the function).
func (r *reconciler) ensureLoadBalancerService(ci *operatorv1.IngressController, deploymentRef metav1.OwnerReference, platformStatus *configv1.PlatformStatus) (bool, *corev1.Service, error) {
log.Info("@chirag: Starting ensureLoadBalancerService()")
wantLBS, desiredLBService, err := desiredLoadBalancerService(ci, deploymentRef, platformStatus, r.config.IngressControllerLBSubnetsAWSEnabled, r.config.IngressControllerEIPAllocationsAWSEnabled)
if err != nil {
return false, nil, err
Expand Down Expand Up @@ -347,6 +350,7 @@ func isServiceOwnedByIngressController(service *corev1.Service, ic *operatorv1.I
// desired if the high availability type is Cloud. An LB service will declare an
// owner reference to the given deployment.
func desiredLoadBalancerService(ci *operatorv1.IngressController, deploymentRef metav1.OwnerReference, platform *configv1.PlatformStatus, subnetsAWSEnabled bool, eipAllocationsAWSEnabled bool) (bool, *corev1.Service, error) {
log.Info("@chirag: Starting desiredLoadBalancerService()")
if ci.Status.EndpointPublishingStrategy.Type != operatorv1.LoadBalancerServiceStrategyType {
return false, nil, nil
}
Expand Down Expand Up @@ -446,6 +450,7 @@ func desiredLoadBalancerService(ci *operatorv1.IngressController, deploymentRef
}

if platform.AWS != nil && len(platform.AWS.ResourceTags) > 0 {
log.Info("@chirag: platform.AWS.ResourceTags", "tags", platform.AWS.ResourceTags)
var additionalTags []string
for _, userTag := range platform.AWS.ResourceTags {
if len(userTag.Key) > 0 {
Expand All @@ -454,6 +459,7 @@ func desiredLoadBalancerService(ci *operatorv1.IngressController, deploymentRef
}
if len(additionalTags) > 0 {
service.Annotations[awsLBAdditionalResourceTags] = strings.Join(additionalTags, ",")
log.Info("@chirag: service.Annotations", "tags", service.Annotations[awsLBAdditionalResourceTags])
}
}

Expand Down Expand Up @@ -751,9 +757,10 @@ func IsServiceInternal(service *corev1.Service) bool {
return false
}

// loadBalancerServiceTagsModified verifies that none of the managedAnnotations have been changed and also the AWS tags annotation
// loadBalancerServiceTagsModified verifies that none of the managedAnnotations except awsLBAdditionalResourceTags have been changed
func loadBalancerServiceTagsModified(current, expected *corev1.Service) (bool, *corev1.Service) {
ignoredAnnotations := managedLoadBalancerServiceAnnotations.Union(sets.NewString(awsLBAdditionalResourceTags))
ignoredAnnotations := managedLoadBalancerServiceAnnotations.Clone()
ignoredAnnotations.Delete(awsLBAdditionalResourceTags)
return loadBalancerServiceAnnotationsChanged(current, expected, ignoredAnnotations)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ func Test_loadBalancerServiceChanged(t *testing.T) {
mutate: func(svc *corev1.Service) {
svc.Annotations["service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags"] = "Key3=Value3,Key4=Value4"
},
expect: false,
expect: true,
},
{
description: "if the service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout annotation changes",
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/controller/ingress/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3026,7 +3026,7 @@ func Test_computeIngressUpgradeableCondition(t *testing.T) {
mutate: func(svc *corev1.Service) {
svc.Annotations[awsLBAdditionalResourceTags] = "Key2=Value2"
},
expect: false,
expect: true,
},
{
description: "if the service.beta.kubernetes.io/load-balancer-source-ranges is set",
Expand Down

0 comments on commit f2e5cf8

Please sign in to comment.