From 9112c32adb4f3914dc5d142e42a16dd0c042fe83 Mon Sep 17 00:00:00 2001 From: Bob Fournier Date: Thu, 27 Jun 2024 15:48:05 -0400 Subject: [PATCH] Set Labels on Disks and ForwardingRules resources The Labels field should be set on Disks and ForwardingRules resources from the machine or cluster AdditionalLabels field, similar to how it is set on Instances. --- cloud/scope/cluster.go | 1 + cloud/scope/machine.go | 1 + .../compute/instances/reconcile_test.go | 27 ++++++++++ .../compute/loadbalancers/reconcile_test.go | 50 ++++++++++++++++++- 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/cloud/scope/cluster.go b/cloud/scope/cluster.go index 7e4e36b00..e7add68c1 100644 --- a/cloud/scope/cluster.go +++ b/cloud/scope/cluster.go @@ -337,6 +337,7 @@ func (s *ClusterScope) ForwardingRuleSpec(lbname string) *compute.ForwardingRule IPProtocol: "TCP", LoadBalancingScheme: "EXTERNAL", PortRange: portRange, + Labels: s.AdditionalLabels(), } } diff --git a/cloud/scope/machine.go b/cloud/scope/machine.go index 695160bd1..96f745822 100644 --- a/cloud/scope/machine.go +++ b/cloud/scope/machine.go @@ -249,6 +249,7 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk { DiskType: path.Join("zones", m.Zone(), "diskTypes", string(diskType)), ResourceManagerTags: shared.ResourceTagConvert(context.TODO(), m.GCPMachine.Spec.ResourceManagerTags), SourceImage: sourceImage, + Labels: m.ClusterGetter.AdditionalLabels().AddLabels(m.GCPMachine.Spec.AdditionalLabels), }, } diff --git a/cloud/services/compute/instances/reconcile_test.go b/cloud/services/compute/instances/reconcile_test.go index fba51b7a6..74b33ba8b 100644 --- a/cloud/services/compute/instances/reconcile_test.go +++ b/cloud/services/compute/instances/reconcile_test.go @@ -237,6 +237,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-c/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, }, }, @@ -302,6 +305,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-c/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, }, }, @@ -369,6 +375,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-c/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, }, }, @@ -436,6 +445,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-c/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, }, }, @@ -506,6 +518,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-c/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, }, }, @@ -569,6 +584,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-a/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, }, }, @@ -639,6 +657,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-c/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, DiskEncryptionKey: &compute.CustomerEncryptionKey{ KmsKeyName: "projects/my-project/locations/us-central1/keyRings/us-central1/cryptoKeys/some-key", @@ -712,6 +733,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-c/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, DiskEncryptionKey: &compute.CustomerEncryptionKey{ RawKey: "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0=", @@ -785,6 +809,9 @@ func TestService_createOrGetInstance(t *testing.T) { DiskType: "zones/us-central1-c/diskTypes/pd-standard", SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19", ResourceManagerTags: map[string]string{}, + Labels: map[string]string{ + "foo": "bar", + }, }, DiskEncryptionKey: &compute.CustomerEncryptionKey{ RsaEncryptedKey: "ieCx/NcW06PcT7Ep1X6LUTc/hLvUDYyzSZPPVCVPTVEohpeHASqC8uw5TzyO9U+Fka9JFHiz0mBibXUInrC/jEk014kCK/NPjYgEMOyssZ4ZINPKxlUh2zn1bV+MCaTICrdmuSBTWlUUiFoDiD6PYznLwh8ZNdaheCeZ8ewEXgFQ8V+sDroLaN3Xs3MDTXQEMMoNUXMCZEIpg9Vtp9x2oe==", diff --git a/cloud/services/compute/loadbalancers/reconcile_test.go b/cloud/services/compute/loadbalancers/reconcile_test.go index 3572ae1ea..304e26d6f 100644 --- a/cloud/services/compute/loadbalancers/reconcile_test.go +++ b/cloud/services/compute/loadbalancers/reconcile_test.go @@ -95,6 +95,18 @@ func getBaseClusterScope() (*scope.ClusterScope, error) { return clusterScope, nil } +func getBaseClusterScopeWithLabels() (*scope.ClusterScope, error) { + clusterScope, err := getBaseClusterScope() + if err != nil { + return nil, err + } + + clusterScope.GCPCluster.Spec.AdditionalLabels = map[string]string{ + "foo": "bar", + } + return clusterScope, nil +} + func TestService_createOrGetInstanceGroup(t *testing.T) { tests := []struct { name string @@ -596,6 +608,7 @@ func TestService_createOrGetForwardingRule(t *testing.T) { mockForwardingRule *cloud.MockGlobalForwardingRules want *compute.ForwardingRule wantErr bool + includeLabels bool }{ { name: "forwarding rule does not exist for external load balancer (should create forwardingrule)", @@ -622,11 +635,46 @@ func TestService_createOrGetForwardingRule(t *testing.T) { SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/global/forwardingRules/my-cluster-apiserver", }, }, + { + name: "forwarding rule does not exist for external load balancer (should create forwardingrule with labels)", + scope: func(s *scope.ClusterScope) Scope { return s }, + lbName: infrav1.APIServerRoleTagValue, + address: &compute.Address{ + Name: "my-cluster-apiserver", + SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/regions/us-central1/addresses/my-cluster-apiserver", + }, + backendService: &compute.BackendService{}, + targetTcpproxy: &compute.TargetTcpProxy{ + Name: "my-cluster-apiserver", + }, + mockForwardingRule: &cloud.MockGlobalForwardingRules{ + ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"}, + Objects: map[meta.Key]*cloud.MockGlobalForwardingRulesObj{}, + }, + want: &compute.ForwardingRule{ + IPAddress: "https://www.googleapis.com/compute/v1/projects/proj-id/regions/us-central1/addresses/my-cluster-apiserver", + IPProtocol: "TCP", + LoadBalancingScheme: "EXTERNAL", + PortRange: "443-443", + Name: "my-cluster-apiserver", + SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/global/forwardingRules/my-cluster-apiserver", + Labels: map[string]string{ + "foo": "bar", + }, + }, + includeLabels: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ctx := context.TODO() - clusterScope, err := getBaseClusterScope() + var err error + var clusterScope *scope.ClusterScope + if tt.includeLabels { + clusterScope, err = getBaseClusterScopeWithLabels() + } else { + clusterScope, err = getBaseClusterScope() + } if err != nil { t.Fatal(err) }