Skip to content
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

Add multiple SSH keys support for Hetzner #14058

Merged
merged 1 commit into from
Jul 29, 2022
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
5 changes: 0 additions & 5 deletions pkg/model/hetznermodel/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,3 @@ func (b *HetznerModelContext) LinkToNetwork() *hetznertasks.Network {
name := b.ClusterName()
return &hetznertasks.Network{Name: &name}
}

func (b *HetznerModelContext) LinkToSSHKey() *hetznertasks.SSHKey {
name := b.ClusterName()
return &hetznertasks.SSHKey{Name: &name}
}
21 changes: 20 additions & 1 deletion pkg/model/hetznermodel/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package hetznermodel

import (
"k8s.io/kops/pkg/model"
"k8s.io/kops/pkg/pki"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/hetzner"
"k8s.io/kops/upup/pkg/fi/cloudup/hetznertasks"
Expand All @@ -33,6 +34,24 @@ type ServerGroupModelBuilder struct {
var _ fi.ModelBuilder = &ServerGroupModelBuilder{}

func (b *ServerGroupModelBuilder) Build(c *fi.ModelBuilderContext) error {
var sshkeyTasks []*hetznertasks.SSHKey
for _, sshkey := range b.SSHPublicKeys {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: If you have two ServerGroupModelBuilders, with overlapping SSHPublicKeys, this may result in duplicate tasks. So you might want to consider keeping the dedicated SSHPublicKeyBuilder task, or using EnsureTask (which allows for duplicate creation if they are identical, but is a bit more hacky). But not a blocker!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ServerGroupModelBuilder creates servers for all instance groups, so there is no need for using EnsureTask at the moment.

fingerprint, err := pki.ComputeOpenSSHKeyFingerprint(string(sshkey))
if err != nil {
return err
}
t := &hetznertasks.SSHKey{
Name: fi.String(b.ClusterName() + "-" + fingerprint),
Lifecycle: b.Lifecycle,
PublicKey: string(sshkey),
Labels: map[string]string{
hetzner.TagKubernetesClusterName: b.ClusterName(),
},
}
c.AddTask(t)
sshkeyTasks = append(sshkeyTasks, t)
}

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

Expand All @@ -49,7 +68,7 @@ func (b *ServerGroupModelBuilder) Build(c *fi.ModelBuilderContext) error {
serverGroup := hetznertasks.ServerGroup{
Name: fi.String(ig.Name),
Lifecycle: b.Lifecycle,
SSHKey: b.LinkToSSHKey(),
SSHKeys: sshkeyTasks,
Network: b.LinkToNetwork(),
Count: int(igSize),
Location: ig.Spec.Subnets[0],
Expand Down
48 changes: 0 additions & 48 deletions pkg/model/hetznermodel/sshkey.go

This file was deleted.

1 change: 0 additions & 1 deletion upup/pkg/fi/cloudup/apply_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,6 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error {
KopsModelContext: modelContext,
}
l.Builders = append(l.Builders,
&hetznermodel.SSHKeyModelBuilder{HetznerModelContext: hetznerModelContext, Lifecycle: securityLifecycle},
&hetznermodel.NetworkModelBuilder{HetznerModelContext: hetznerModelContext, Lifecycle: networkLifecycle},
&hetznermodel.ExternalAccessModelBuilder{HetznerModelContext: hetznerModelContext, Lifecycle: networkLifecycle},
&hetznermodel.LoadBalancerModelBuilder{HetznerModelContext: hetznerModelContext, Lifecycle: networkLifecycle},
Expand Down
16 changes: 8 additions & 8 deletions upup/pkg/fi/cloudup/hetznertasks/servergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
type ServerGroup struct {
Name *string
Lifecycle fi.Lifecycle
SSHKey *SSHKey
SSHKeys []*SSHKey
Network *Network

Count int
Expand Down Expand Up @@ -180,8 +180,8 @@ func (_ *ServerGroup) RenderHetzner(t *hetzner.HetznerAPITarget, a, e, changes *
return nil
}

if e.SSHKey == nil {
return fmt.Errorf("failed to find ssh key for server %q", fi.StringValue(e.Name))
if len(e.SSHKeys) == 0 {
return fmt.Errorf("failed to find ssh keys for server %q", fi.StringValue(e.Name))
}
if e.Network == nil {
return fmt.Errorf("failed to find network for server %q", fi.StringValue(e.Name))
Expand All @@ -204,11 +204,6 @@ func (_ *ServerGroup) RenderHetzner(t *hetzner.HetznerAPITarget, a, e, changes *
opts := hcloud.ServerCreateOpts{
Name: name,
StartAfterCreate: fi.Bool(true),
SSHKeys: []*hcloud.SSHKey{
{
ID: fi.IntValue(e.SSHKey.ID),
},
},
Networks: []*hcloud.Network{
{
ID: fi.IntValue(e.Network.ID),
Expand All @@ -231,6 +226,11 @@ func (_ *ServerGroup) RenderHetzner(t *hetzner.HetznerAPITarget, a, e, changes *
},
}

// Add the SSH keys
for _, sshkey := range e.SSHKeys {
opts.SSHKeys = append(opts.SSHKeys, &hcloud.SSHKey{ID: fi.IntValue(sshkey.ID)})
}

// Add the user-data hash label
opts.Labels[hetzner.TagKubernetesInstanceUserData] = userDataHash

Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/cloudup/hetznertasks/sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (_ *SSHKey) RenderHetzner(t *hetzner.HetznerAPITarget, a, e, changes *SSHKe
if len(tokens) == 3 {
sshkeyComment := tokens[2]
_, err := mail.ParseAddress(sshkeyComment)
if err != nil {
if err == nil {
name = sshkeyComment
}
}
Expand Down