Skip to content

Commit 9112c32

Browse files
committed
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.
1 parent 1eb50ed commit 9112c32

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

cloud/scope/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ func (s *ClusterScope) ForwardingRuleSpec(lbname string) *compute.ForwardingRule
337337
IPProtocol: "TCP",
338338
LoadBalancingScheme: "EXTERNAL",
339339
PortRange: portRange,
340+
Labels: s.AdditionalLabels(),
340341
}
341342
}
342343

cloud/scope/machine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk {
249249
DiskType: path.Join("zones", m.Zone(), "diskTypes", string(diskType)),
250250
ResourceManagerTags: shared.ResourceTagConvert(context.TODO(), m.GCPMachine.Spec.ResourceManagerTags),
251251
SourceImage: sourceImage,
252+
Labels: m.ClusterGetter.AdditionalLabels().AddLabels(m.GCPMachine.Spec.AdditionalLabels),
252253
},
253254
}
254255

cloud/services/compute/instances/reconcile_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ func TestService_createOrGetInstance(t *testing.T) {
237237
DiskType: "zones/us-central1-c/diskTypes/pd-standard",
238238
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
239239
ResourceManagerTags: map[string]string{},
240+
Labels: map[string]string{
241+
"foo": "bar",
242+
},
240243
},
241244
},
242245
},
@@ -302,6 +305,9 @@ func TestService_createOrGetInstance(t *testing.T) {
302305
DiskType: "zones/us-central1-c/diskTypes/pd-standard",
303306
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
304307
ResourceManagerTags: map[string]string{},
308+
Labels: map[string]string{
309+
"foo": "bar",
310+
},
305311
},
306312
},
307313
},
@@ -369,6 +375,9 @@ func TestService_createOrGetInstance(t *testing.T) {
369375
DiskType: "zones/us-central1-c/diskTypes/pd-standard",
370376
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
371377
ResourceManagerTags: map[string]string{},
378+
Labels: map[string]string{
379+
"foo": "bar",
380+
},
372381
},
373382
},
374383
},
@@ -436,6 +445,9 @@ func TestService_createOrGetInstance(t *testing.T) {
436445
DiskType: "zones/us-central1-c/diskTypes/pd-standard",
437446
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
438447
ResourceManagerTags: map[string]string{},
448+
Labels: map[string]string{
449+
"foo": "bar",
450+
},
439451
},
440452
},
441453
},
@@ -506,6 +518,9 @@ func TestService_createOrGetInstance(t *testing.T) {
506518
DiskType: "zones/us-central1-c/diskTypes/pd-standard",
507519
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
508520
ResourceManagerTags: map[string]string{},
521+
Labels: map[string]string{
522+
"foo": "bar",
523+
},
509524
},
510525
},
511526
},
@@ -569,6 +584,9 @@ func TestService_createOrGetInstance(t *testing.T) {
569584
DiskType: "zones/us-central1-a/diskTypes/pd-standard",
570585
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
571586
ResourceManagerTags: map[string]string{},
587+
Labels: map[string]string{
588+
"foo": "bar",
589+
},
572590
},
573591
},
574592
},
@@ -639,6 +657,9 @@ func TestService_createOrGetInstance(t *testing.T) {
639657
DiskType: "zones/us-central1-c/diskTypes/pd-standard",
640658
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
641659
ResourceManagerTags: map[string]string{},
660+
Labels: map[string]string{
661+
"foo": "bar",
662+
},
642663
},
643664
DiskEncryptionKey: &compute.CustomerEncryptionKey{
644665
KmsKeyName: "projects/my-project/locations/us-central1/keyRings/us-central1/cryptoKeys/some-key",
@@ -712,6 +733,9 @@ func TestService_createOrGetInstance(t *testing.T) {
712733
DiskType: "zones/us-central1-c/diskTypes/pd-standard",
713734
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
714735
ResourceManagerTags: map[string]string{},
736+
Labels: map[string]string{
737+
"foo": "bar",
738+
},
715739
},
716740
DiskEncryptionKey: &compute.CustomerEncryptionKey{
717741
RawKey: "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0=",
@@ -785,6 +809,9 @@ func TestService_createOrGetInstance(t *testing.T) {
785809
DiskType: "zones/us-central1-c/diskTypes/pd-standard",
786810
SourceImage: "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19",
787811
ResourceManagerTags: map[string]string{},
812+
Labels: map[string]string{
813+
"foo": "bar",
814+
},
788815
},
789816
DiskEncryptionKey: &compute.CustomerEncryptionKey{
790817
RsaEncryptedKey: "ieCx/NcW06PcT7Ep1X6LUTc/hLvUDYyzSZPPVCVPTVEohpeHASqC8uw5TzyO9U+Fka9JFHiz0mBibXUInrC/jEk014kCK/NPjYgEMOyssZ4ZINPKxlUh2zn1bV+MCaTICrdmuSBTWlUUiFoDiD6PYznLwh8ZNdaheCeZ8ewEXgFQ8V+sDroLaN3Xs3MDTXQEMMoNUXMCZEIpg9Vtp9x2oe==",

cloud/services/compute/loadbalancers/reconcile_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ func getBaseClusterScope() (*scope.ClusterScope, error) {
9595
return clusterScope, nil
9696
}
9797

98+
func getBaseClusterScopeWithLabels() (*scope.ClusterScope, error) {
99+
clusterScope, err := getBaseClusterScope()
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
clusterScope.GCPCluster.Spec.AdditionalLabels = map[string]string{
105+
"foo": "bar",
106+
}
107+
return clusterScope, nil
108+
}
109+
98110
func TestService_createOrGetInstanceGroup(t *testing.T) {
99111
tests := []struct {
100112
name string
@@ -596,6 +608,7 @@ func TestService_createOrGetForwardingRule(t *testing.T) {
596608
mockForwardingRule *cloud.MockGlobalForwardingRules
597609
want *compute.ForwardingRule
598610
wantErr bool
611+
includeLabels bool
599612
}{
600613
{
601614
name: "forwarding rule does not exist for external load balancer (should create forwardingrule)",
@@ -622,11 +635,46 @@ func TestService_createOrGetForwardingRule(t *testing.T) {
622635
SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/global/forwardingRules/my-cluster-apiserver",
623636
},
624637
},
638+
{
639+
name: "forwarding rule does not exist for external load balancer (should create forwardingrule with labels)",
640+
scope: func(s *scope.ClusterScope) Scope { return s },
641+
lbName: infrav1.APIServerRoleTagValue,
642+
address: &compute.Address{
643+
Name: "my-cluster-apiserver",
644+
SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/regions/us-central1/addresses/my-cluster-apiserver",
645+
},
646+
backendService: &compute.BackendService{},
647+
targetTcpproxy: &compute.TargetTcpProxy{
648+
Name: "my-cluster-apiserver",
649+
},
650+
mockForwardingRule: &cloud.MockGlobalForwardingRules{
651+
ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"},
652+
Objects: map[meta.Key]*cloud.MockGlobalForwardingRulesObj{},
653+
},
654+
want: &compute.ForwardingRule{
655+
IPAddress: "https://www.googleapis.com/compute/v1/projects/proj-id/regions/us-central1/addresses/my-cluster-apiserver",
656+
IPProtocol: "TCP",
657+
LoadBalancingScheme: "EXTERNAL",
658+
PortRange: "443-443",
659+
Name: "my-cluster-apiserver",
660+
SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/global/forwardingRules/my-cluster-apiserver",
661+
Labels: map[string]string{
662+
"foo": "bar",
663+
},
664+
},
665+
includeLabels: true,
666+
},
625667
}
626668
for _, tt := range tests {
627669
t.Run(tt.name, func(t *testing.T) {
628670
ctx := context.TODO()
629-
clusterScope, err := getBaseClusterScope()
671+
var err error
672+
var clusterScope *scope.ClusterScope
673+
if tt.includeLabels {
674+
clusterScope, err = getBaseClusterScopeWithLabels()
675+
} else {
676+
clusterScope, err = getBaseClusterScope()
677+
}
630678
if err != nil {
631679
t.Fatal(err)
632680
}

0 commit comments

Comments
 (0)