Skip to content

Add Cluster UID in Loadbalancer name and Droplet target tag #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/v1alpha3/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func ClusterNameRoleTag(clusterName, role string) string {
return fmt.Sprintf("%s:%s:%s", NameDigitalOceanProviderPrefix, clusterName, role)
}

// ClusterNameUIDRoleTag generates the tag with prefix `NameDigitalOceanProviderPrefix` and `RoleValue` as suffix
// It will generated tag like `sigs-k8s-io:capdo:{clusterName}:{UID}:{role}`.
func ClusterNameUIDRoleTag(clusterName, clusterUID, role string) string {
return fmt.Sprintf("%s:%s:%s:%s", NameDigitalOceanProviderPrefix, clusterName, clusterUID, role)
}

// NameTagFromName returns DigitalOcean safe name tag from name.
func NameTagFromName(name string) string {
return fmt.Sprintf("name:%s", DOSafeName(name))
Expand All @@ -54,6 +60,8 @@ func NameTagFromName(name string) string {
type BuildTagParams struct {
// ClusterName is the cluster associated with the resource.
ClusterName string
// ClusterUID is the cluster uid from clusters.cluster.x-k8s.io uid
ClusterUID string
// Name is the name of the resource, it's applied as the tag "name" on DigitalOcean.
Name string
// Role is the role associated to the resource.
Expand All @@ -68,6 +76,7 @@ func BuildTags(params BuildTagParams) Tags {
var tags Tags
tags = append(tags, ClusterNameTag(params.ClusterName))
tags = append(tags, ClusterNameRoleTag(params.ClusterName, params.Role))
tags = append(tags, ClusterNameUIDRoleTag(params.ClusterName, params.ClusterUID, params.Role))
tags = append(tags, NameTagFromName(params.Name))

tags = append(tags, params.Additional...)
Expand Down
8 changes: 7 additions & 1 deletion api/v1alpha3/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ func TestBuildTags(t *testing.T) {
args: args{
params: BuildTagParams{
ClusterName: "foo",
ClusterUID: "155bd6ca-c6a9-45a8-8c9c-05e09b36bc42",
Name: "bar",
Role: APIServerRoleTagValue,
},
},
want: Tags{ClusterNameTag("foo"), ClusterNameRoleTag("foo", APIServerRoleTagValue), "name:" + "bar"},
want: Tags{
ClusterNameTag("foo"),
ClusterNameRoleTag("foo", APIServerRoleTagValue),
ClusterNameUIDRoleTag("foo", "155bd6ca-c6a9-45a8-8c9c-05e09b36bc42", APIServerRoleTagValue),
NameTagFromName("bar"),
},
},
}
for _, tt := range tests {
Expand Down
4 changes: 4 additions & 0 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func (s *ClusterScope) Namespace() string {
return s.Cluster.GetNamespace()
}

func (s *ClusterScope) UID() string {
return string(s.Cluster.UID)
}

// Region returns the cluster region.
func (s *ClusterScope) Region() string {
return s.DOCluster.Spec.Region
Expand Down
1 change: 1 addition & 0 deletions cloud/services/computes/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (s *Service) CreateDroplet(scope *scope.MachineScope) (*godo.Droplet, error

request.Tags = infrav1.BuildTags(infrav1.BuildTagParams{
ClusterName: clusterName,
ClusterUID: s.scope.UID(),
Name: instanceName,
Role: scope.Role(),
Additional: scope.AdditionalTags(),
Expand Down
4 changes: 2 additions & 2 deletions cloud/services/networking/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *Service) GetLoadBalancer(id string) (*godo.LoadBalancer, error) {

func (s *Service) CreateLoadBalancer(spec *infrav1.DOLoadBalancer) (*godo.LoadBalancer, error) {
clusterName := infrav1.DOSafeName(s.scope.Name())
name := clusterName + "-" + infrav1.APIServerRoleTagValue
name := clusterName + "-" + infrav1.APIServerRoleTagValue + "-" + s.scope.UID()
request := &godo.LoadBalancerRequest{
Name: name,
Algorithm: spec.Algorithm,
Expand All @@ -63,7 +63,7 @@ func (s *Service) CreateLoadBalancer(spec *infrav1.DOLoadBalancer) (*godo.LoadBa
UnhealthyThreshold: spec.HealthCheck.UnhealthyThreshold,
HealthyThreshold: spec.HealthCheck.HealthyThreshold,
},
Tag: infrav1.ClusterNameRoleTag(clusterName, infrav1.APIServerRoleTagValue),
Tag: infrav1.ClusterNameUIDRoleTag(clusterName, s.scope.UID(), infrav1.APIServerRoleTagValue),
VPCUUID: s.scope.VPC().VPCUUID,
}

Expand Down