|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package awstasks |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 24 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 25 | + ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 26 | + elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types" |
| 27 | + "k8s.io/kops/cloudmock/aws/mockec2" |
| 28 | + "k8s.io/kops/cloudmock/aws/mockelbv2" |
| 29 | + "k8s.io/kops/upup/pkg/fi" |
| 30 | + "k8s.io/kops/upup/pkg/fi/cloudup/awsup" |
| 31 | +) |
| 32 | + |
| 33 | +// TestTargetGroupHealthCheckChange verifies that a health check change on an existing target group is applied, |
| 34 | +// instead of recurring as an unapplied change on every run. |
| 35 | +func TestTargetGroupHealthCheckChange(t *testing.T) { |
| 36 | + ctx := context.TODO() |
| 37 | + |
| 38 | + cloud := awsup.BuildMockAWSCloud("us-east-1", "abc") |
| 39 | + c := &mockec2.MockEC2{} |
| 40 | + cloud.MockEC2 = c |
| 41 | + cloud.MockELBV2 = &mockelbv2.MockELBV2{EC2: c} |
| 42 | + |
| 43 | + // Pre-create the VPC |
| 44 | + vpc, err := c.CreateVpc(ctx, &ec2.CreateVpcInput{ |
| 45 | + CidrBlock: aws.String("172.20.0.0/16"), |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("error creating test VPC: %v", err) |
| 49 | + } |
| 50 | + _, err = c.CreateTags(ctx, &ec2.CreateTagsInput{ |
| 51 | + Resources: []string{aws.ToString(vpc.Vpc.VpcId)}, |
| 52 | + Tags: []ec2types.Tag{ |
| 53 | + { |
| 54 | + Key: aws.String("Name"), |
| 55 | + Value: aws.String("ExistingVPC"), |
| 56 | + }, |
| 57 | + }, |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("error tagging test vpc: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + // We define a function so we can rebuild the tasks, because we modify in-place when running |
| 64 | + buildTasks := func(healthCheckProtocol elbv2types.ProtocolEnum, healthCheckPath *string) map[string]fi.CloudupTask { |
| 65 | + vpc1 := &VPC{ |
| 66 | + Name: s("vpc1"), |
| 67 | + Lifecycle: fi.LifecycleSync, |
| 68 | + CIDR: s("172.20.0.0/16"), |
| 69 | + Tags: map[string]string{"kubernetes.io/cluster/cluster.example.com": "shared"}, |
| 70 | + Shared: fi.PtrTo(true), |
| 71 | + ID: vpc.Vpc.VpcId, |
| 72 | + } |
| 73 | + tg1 := &TargetGroup{ |
| 74 | + Name: s("tg1"), |
| 75 | + Lifecycle: fi.LifecycleSync, |
| 76 | + VPC: vpc1, |
| 77 | + Tags: map[string]string{"Name": "tg1"}, |
| 78 | + Protocol: elbv2types.ProtocolEnumTcp, |
| 79 | + Port: fi.PtrTo(int32(3988)), |
| 80 | + Interval: fi.PtrTo(int32(10)), |
| 81 | + HealthyThreshold: fi.PtrTo(int32(2)), |
| 82 | + UnhealthyThreshold: fi.PtrTo(int32(2)), |
| 83 | + HealthCheckProtocol: healthCheckProtocol, |
| 84 | + HealthCheckPath: healthCheckPath, |
| 85 | + Shared: fi.PtrTo(false), |
| 86 | + } |
| 87 | + |
| 88 | + return map[string]fi.CloudupTask{ |
| 89 | + "vpc1": vpc1, |
| 90 | + "tg1": tg1, |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Create the target group with a TCP health check (as an older kOps version would). |
| 95 | + runTasks(t, cloud, buildTasks(elbv2types.ProtocolEnumTcp, nil)) |
| 96 | + |
| 97 | + // Upgrade to an HTTPS health check with a path. |
| 98 | + runTasks(t, cloud, buildTasks(elbv2types.ProtocolEnumHttps, s("/healthz"))) |
| 99 | + |
| 100 | + // The change must have been applied, so a subsequent run sees no changes. |
| 101 | + checkNoChanges(t, ctx, cloud, buildTasks(elbv2types.ProtocolEnumHttps, s("/healthz"))) |
| 102 | +} |
0 commit comments