@@ -12,7 +12,6 @@ import (
1212 "encoding/json"
1313 "errors"
1414 "fmt"
15- "github.com/aws/aws-sdk-go/service/ec2"
1615 "io"
1716 "io/ioutil"
1817 "net"
@@ -32,13 +31,15 @@ import (
3231 iov1 "github.com/openshift/api/operatoringress/v1"
3332 routev1 "github.com/openshift/api/route/v1"
3433
34+ configclientset "github.com/openshift/client-go/config/clientset/versioned"
3535 "github.com/openshift/cluster-ingress-operator/pkg/manifests"
3636 operatorclient "github.com/openshift/cluster-ingress-operator/pkg/operator/client"
3737 "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
3838 operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
3939 ingresscontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/ingress"
4040
4141 "github.com/aws/aws-sdk-go/aws/endpoints"
42+ "github.com/aws/aws-sdk-go/service/ec2"
4243
4344 "github.com/go-logr/logr"
4445 "github.com/stretchr/testify/assert"
@@ -116,6 +117,7 @@ var (
116117
117118var (
118119 kclient client.Client
120+ configClient * configclientset.Clientset
119121 dnsConfig configv1.DNS
120122 infraConfig configv1.Infrastructure
121123 operatorNamespace = operatorcontroller .DefaultOperatorNamespace
@@ -153,6 +155,12 @@ func TestMain(m *testing.M) {
153155 }
154156 kclient = kubeClient
155157
158+ configClient , err = configclientset .NewForConfig (kubeConfig )
159+ if err != nil {
160+ fmt .Printf ("failed to create config client: %s\n " , err )
161+ os .Exit (1 )
162+ }
163+
156164 if err := kclient .Get (context .TODO (), types.NamespacedName {Name : "cluster" }, & dnsConfig ); err != nil {
157165 fmt .Printf ("failed to get DNS config: %v\n " , err )
158166 os .Exit (1 )
@@ -1285,6 +1293,83 @@ func TestInternalLoadBalancerGlobalAccessGCP(t *testing.T) {
12851293 }
12861294}
12871295
1296+ // TestAWSResourceTagsChanged tests the functionality of updating AWS resource tags
1297+ // in the infrastructure configuration and validates that the expected
1298+ // awsLBAdditionalResourceTags is set correctly on the
1299+ // loadBalancer service associated with the default Ingress Controller.
1300+ //
1301+ // This test is a serial test because it modifies the cluster infrastructure config and
1302+ // therefore should not run in parallel with other tests.
1303+ func TestAWSResourceTagsChanged (t * testing.T ) {
1304+ if infraConfig .Status .Platform != "AWS" {
1305+ t .Skipf ("test skipped on platform %q" , infraConfig .Status .Platform )
1306+ }
1307+ if err := waitForIngressControllerCondition (t , kclient , 10 * time .Second , defaultName , defaultAvailableConditions ... ); err != nil {
1308+ t .Errorf ("did not get expected conditions: %v" , err )
1309+ }
1310+ defaultIC := & operatorv1.IngressController {
1311+ ObjectMeta : metav1.ObjectMeta {
1312+ Namespace : defaultName .Namespace ,
1313+ Name : defaultName .Name ,
1314+ },
1315+ }
1316+ awsLBAdditionalResourceTags := "service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags"
1317+
1318+ // Save a copy of the original infraConfig, to revert changes before exiting.
1319+ originalInfra := infraConfig .DeepCopy ()
1320+ t .Cleanup (func () {
1321+ err := updateInfrastructureConfigStatusWithRetryOnConflict (t , 5 * time .Minute , configClient , func (infra * configv1.Infrastructure ) * configv1.Infrastructure {
1322+ infra .Status = originalInfra .Status
1323+ return infra
1324+ })
1325+ if err != nil {
1326+ t .Logf ("Unable to remove changes from the infraConfig, possible corruption of test environment: %v" , err )
1327+ }
1328+ })
1329+
1330+ initialTags := []configv1.AWSResourceTag {
1331+ {Key : "Key1" , Value : "Value1" },
1332+ {Key : "Key2" , Value : "Value2" },
1333+ }
1334+ t .Logf ("Updating AWS ResourceTags in the cluster infrastructure config: %v" , initialTags )
1335+ err := updateInfrastructureConfigStatusWithRetryOnConflict (t , 5 * time .Minute , configClient , func (infra * configv1.Infrastructure ) * configv1.Infrastructure {
1336+ if infra .Status .PlatformStatus == nil {
1337+ infra .Status .PlatformStatus = & configv1.PlatformStatus {}
1338+ }
1339+ if infra .Status .PlatformStatus .AWS == nil {
1340+ infra .Status .PlatformStatus .AWS = & configv1.AWSPlatformStatus {}
1341+ }
1342+ infra .Status .PlatformStatus .AWS .ResourceTags = initialTags
1343+ return infra
1344+ })
1345+ if err != nil {
1346+ t .Errorf ("failed to update infrastructure status: %v" , err )
1347+ }
1348+
1349+ // Check awsLBAdditionalResourceTags annotation with initial tags.
1350+ expectedTags := "Key1=Value1,Key2=Value2"
1351+ t .Logf ("Validating the %s annotation for the load balancer service of the default ingresscontroller" , awsLBAdditionalResourceTags )
1352+ assertLoadBalancerServiceAnnotationWithPollImmediate (t , kclient , defaultIC , awsLBAdditionalResourceTags , expectedTags )
1353+
1354+ // Update the status again, removing one tag.
1355+ updatedTags := []configv1.AWSResourceTag {
1356+ {Key : "Key1" , Value : "Value1" },
1357+ }
1358+ t .Logf ("Updating AWS ResourceTags in the cluster infrastructure config: %v" , updatedTags )
1359+ err = updateInfrastructureConfigStatusWithRetryOnConflict (t , 5 * time .Minute , configClient , func (infra * configv1.Infrastructure ) * configv1.Infrastructure {
1360+ infra .Status .PlatformStatus .AWS .ResourceTags = updatedTags
1361+ return infra
1362+ })
1363+ if err != nil {
1364+ t .Errorf ("failed to update infrastructure status: %v" , err )
1365+ }
1366+
1367+ // Check awsLBAdditionalResourceTags annotation with updated tags.
1368+ expectedTags = "Key1=Value1"
1369+ t .Logf ("Validating the %s annotation for the load balancer service of the default ingresscontroller" , awsLBAdditionalResourceTags )
1370+ assertLoadBalancerServiceAnnotationWithPollImmediate (t , kclient , defaultIC , awsLBAdditionalResourceTags , expectedTags )
1371+ }
1372+
12881373func TestAWSLBTypeChange (t * testing.T ) {
12891374 t .Parallel ()
12901375
@@ -4287,6 +4372,29 @@ func assertServiceAnnotation(t *testing.T, serviceName types.NamespacedName, ann
42874372 }
42884373}
42894374
4375+ // assertLoadBalancerServiceAnnotationWithPollImmediate checks if the specified annotation on the
4376+ // LoadBalancer Service of the given IngressController matches the expected value.
4377+ func assertLoadBalancerServiceAnnotationWithPollImmediate (t * testing.T , kclient client.Client , ic * operatorv1.IngressController , annotationKey , expectedValue string ) {
4378+ err := wait .PollUntilContextTimeout (context .Background (), 5 * time .Second , 5 * time .Minute , true , func (ctx context.Context ) (bool , error ) {
4379+ service := & corev1.Service {}
4380+ if err := kclient .Get (ctx , controller .LoadBalancerServiceName (ic ), service ); err != nil {
4381+ t .Logf ("failed to get service %s: %v, retrying..." , controller .LoadBalancerServiceName (ic ), err )
4382+ return false , nil
4383+ }
4384+ if actualValue , ok := service .Annotations [annotationKey ]; ! ok {
4385+ t .Logf ("load balancer has no %q annotation yet: %v, retrying..." , annotationKey , service .Annotations )
4386+ return false , nil
4387+ } else if actualValue != expectedValue {
4388+ t .Logf ("expected %s, found %s" , expectedValue , actualValue )
4389+ return false , nil
4390+ }
4391+ return true , nil
4392+ })
4393+ if err != nil {
4394+ t .Fatalf ("timed out waiting for the %s annotation to be updated: %v" , annotationKey , err )
4395+ }
4396+ }
4397+
42904398// assertServiceNotDeleted asserts that a provide service wasn't deleted.
42914399func assertServiceNotDeleted (t * testing.T , serviceName types.NamespacedName , oldUid types.UID ) {
42924400 t .Helper ()
0 commit comments