Skip to content

Commit

Permalink
Add hetzner server labels for IG node labels
Browse files Browse the repository at this point in the history
  • Loading branch information
rifelpet committed Aug 7, 2024
1 parent 276d071 commit f10869f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
48 changes: 31 additions & 17 deletions pkg/model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awstasks"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/hetzner"
"k8s.io/kops/upup/pkg/fi/cloudup/scaleway"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -162,7 +163,12 @@ func (b *KopsModelContext) CloudTagsForInstanceGroup(ig *kops.InstanceGroup) (ma
return nil, fmt.Errorf("error building node labels: %w", err)
}
for k, v := range nodeLabels {
labels[nodeidentityaws.ClusterAutoscalerNodeTemplateLabel+k] = v
switch b.Cluster.Spec.GetCloudProvider() {
case kops.CloudProviderHetzner:
labels[hetzner.TagKubernetesNodeLabelPrefix+k] = v
default:
labels[nodeidentityaws.ClusterAutoscalerNodeTemplateLabel+k] = v
}
}

// Apply labels for cluster autoscaler node taints
Expand All @@ -173,27 +179,33 @@ func (b *KopsModelContext) CloudTagsForInstanceGroup(ig *kops.InstanceGroup) (ma
}
}

// The system tags take priority because the cluster likely breaks without them...
switch b.Cluster.Spec.GetCloudProvider() {
case kops.CloudProviderHetzner:
labels[hetzner.TagKubernetesInstanceRole] = string(ig.Spec.Role)
labels[hetzner.TagKubernetesClusterName] = b.ClusterName()
labels[hetzner.TagKubernetesInstanceGroup] = ig.Name
default:
// The system tags take priority because the cluster likely breaks without them...

if ig.Spec.Role == kops.InstanceGroupRoleControlPlane {
labels[awstasks.CloudTagInstanceGroupRolePrefix+"master"] = "1"
labels[awstasks.CloudTagInstanceGroupRolePrefix+kops.InstanceGroupRoleControlPlane.ToLowerString()] = "1"
}

if ig.Spec.Role == kops.InstanceGroupRoleControlPlane {
labels[awstasks.CloudTagInstanceGroupRolePrefix+"master"] = "1"
labels[awstasks.CloudTagInstanceGroupRolePrefix+kops.InstanceGroupRoleControlPlane.ToLowerString()] = "1"
}
if ig.Spec.Role == kops.InstanceGroupRoleAPIServer {
labels[awstasks.CloudTagInstanceGroupRolePrefix+strings.ToLower(string(kops.InstanceGroupRoleAPIServer))] = "1"
}

if ig.Spec.Role == kops.InstanceGroupRoleAPIServer {
labels[awstasks.CloudTagInstanceGroupRolePrefix+strings.ToLower(string(kops.InstanceGroupRoleAPIServer))] = "1"
}
if ig.Spec.Role == kops.InstanceGroupRoleNode {
labels[awstasks.CloudTagInstanceGroupRolePrefix+strings.ToLower(string(kops.InstanceGroupRoleNode))] = "1"
}

if ig.Spec.Role == kops.InstanceGroupRoleNode {
labels[awstasks.CloudTagInstanceGroupRolePrefix+strings.ToLower(string(kops.InstanceGroupRoleNode))] = "1"
}
if ig.Spec.Role == kops.InstanceGroupRoleBastion {
labels[awstasks.CloudTagInstanceGroupRolePrefix+strings.ToLower(string(kops.InstanceGroupRoleBastion))] = "1"
}

if ig.Spec.Role == kops.InstanceGroupRoleBastion {
labels[awstasks.CloudTagInstanceGroupRolePrefix+strings.ToLower(string(kops.InstanceGroupRoleBastion))] = "1"
labels[nodeidentityaws.CloudTagInstanceGroupName] = ig.Name
}

labels[nodeidentityaws.CloudTagInstanceGroupName] = ig.Name

return labels, nil
}

Expand Down Expand Up @@ -247,6 +259,8 @@ func (b *KopsModelContext) CloudTags(name string, shared bool) map[string]string
}
tags[k] = v
}
case kops.CloudProviderHetzner:
tags[hetzner.TagKubernetesClusterName] = b.ClusterName()
}
return tags
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/model/hetznermodel/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ func (b *ServerGroupModelBuilder) Build(c *fi.CloudupModelBuilderContext) error

for _, ig := range b.InstanceGroups {
igSize := fi.ValueOf(ig.Spec.MinSize)

labels := make(map[string]string)
labels[hetzner.TagKubernetesClusterName] = b.ClusterName()
labels[hetzner.TagKubernetesInstanceGroup] = ig.Name
labels[hetzner.TagKubernetesInstanceRole] = string(ig.Spec.Role)
labels, err := b.CloudTagsForInstanceGroup(ig)
if err != nil {
return err
}

userData, err := b.BootstrapScriptBuilder.ResourceNodeUp(c, ig)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/nodeidentity/hetzner/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func (i *nodeIdentifier) IdentifyNode(ctx context.Context, node *corev1.Node) (*

labels := map[string]string{}
for key, value := range server.Labels {
if key == hetzner.TagKubernetesInstanceRole {
switch {
case key == hetzner.TagKubernetesInstanceRole:
switch kops.InstanceGroupRole(value) {
case kops.InstanceGroupRoleControlPlane:
labels[nodelabels.RoleLabelControlPlane20] = ""
Expand All @@ -108,6 +109,8 @@ func (i *nodeIdentifier) IdentifyNode(ctx context.Context, node *corev1.Node) (*
default:
klog.Warningf("Unknown node role %q for server %s(%d)", value, server.Name, server.ID)
}
case strings.HasPrefix(key, hetzner.TagKubernetesNodeLabelPrefix):
labels[strings.TrimPrefix(key, hetzner.TagKubernetesNodeLabelPrefix)] = value
}
}

Expand Down
1 change: 1 addition & 0 deletions upup/pkg/fi/cloudup/hetzner/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
TagKubernetesInstanceUserData = "kops.k8s.io/instance-userdata"
TagKubernetesInstanceNeedsUpdate = "kops.k8s.io/needs-update"
TagKubernetesVolumeRole = "kops.k8s.io/volume-role"
TagKubernetesNodeLabelPrefix = "kops.k8s.io/node-label/"
)

// HetznerCloud exposes all the interfaces required to operate on Hetzner Cloud resources
Expand Down

0 comments on commit f10869f

Please sign in to comment.