Skip to content

Commit

Permalink
Set Labels on Disks and ForwardingRules resources
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bfournie committed Jun 27, 2024
1 parent 1eb50ed commit 9112c32
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func (s *ClusterScope) ForwardingRuleSpec(lbname string) *compute.ForwardingRule
IPProtocol: "TCP",
LoadBalancingScheme: "EXTERNAL",
PortRange: portRange,
Labels: s.AdditionalLabels(),
}
}

Expand Down
1 change: 1 addition & 0 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}

Expand Down
27 changes: 27 additions & 0 deletions cloud/services/compute/instances/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
},
Expand Down Expand Up @@ -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",
},
},
},
},
Expand Down Expand Up @@ -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",
},
},
},
},
Expand Down Expand Up @@ -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",
},
},
},
},
Expand Down Expand Up @@ -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",
},
},
},
},
Expand Down Expand Up @@ -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",
},
},
},
},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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=",
Expand Down Expand Up @@ -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==",
Expand Down
50 changes: 49 additions & 1 deletion cloud/services/compute/loadbalancers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)",
Expand All @@ -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)
}
Expand Down

0 comments on commit 9112c32

Please sign in to comment.