Skip to content

Commit

Permalink
Merge pull request #6932 from mikesplain/automated-cherry-pick-of-#68…
Browse files Browse the repository at this point in the history
…86-origin-release-1.13

Automated cherry pick of #6886: Based on the Readme, if you specify a SSHKeyName in the
  • Loading branch information
k8s-ci-robot committed May 12, 2019
2 parents d26288b + bcbd347 commit c48d79e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/model/sshkey.go
Expand Up @@ -37,7 +37,9 @@ func (b *SSHKeyModelBuilder) Build(c *fi.ModelBuilderContext) error {
t := &awstasks.SSHKey{
Name: s(name),
Lifecycle: b.Lifecycle,
PublicKey: fi.WrapResource(fi.NewStringResource(string(b.SSHPublicKeys[0]))),
}
if len(b.SSHPublicKeys) >= 1 {
t.PublicKey = fi.WrapResource(fi.NewStringResource(string(b.SSHPublicKeys[0])))
}
c.AddTask(t)

Expand Down
4 changes: 2 additions & 2 deletions upup/pkg/fi/cloudup/apply_cluster.go
Expand Up @@ -441,13 +441,13 @@ func (c *ApplyClusterCmd) Run() error {
"spotinstElastigroup": &spotinsttasks.Elastigroup{},
})

if len(sshPublicKeys) == 0 {
if len(sshPublicKeys) == 0 && c.Cluster.Spec.SSHKeyName == "" {
return fmt.Errorf("SSH public key must be specified when running with AWS (create with `kops create secret --name %s sshpublickey admin -i ~/.ssh/id_rsa.pub`)", cluster.ObjectMeta.Name)
}

modelContext.SSHPublicKeys = sshPublicKeys

if len(sshPublicKeys) != 1 {
if len(sshPublicKeys) > 1 {
return fmt.Errorf("Exactly one 'admin' SSH public key can be specified when running with AWS; please delete a key using `kops delete secret`")
}

Expand Down
Expand Up @@ -38,7 +38,8 @@ func TestLaunchTemplateTerraformRender(t *testing.T) {
RootVolumeIops: fi.Int64(100),
RootVolumeSize: fi.Int64(64),
SSHKey: &SSHKey{
Name: fi.String("mykey"),
Name: fi.String("newkey"),
PublicKey: fi.WrapResource(fi.NewStringResource("newkey")),
},
SecurityGroups: []*SecurityGroup{
{Name: fi.String("nodes-1"), ID: fi.String("1111")},
Expand All @@ -64,7 +65,7 @@ resource "aws_launch_template" "test" {
}
instance_type = "t2.medium"
key_name = "${aws_key_pair.mykey.id}"
key_name = "${aws_key_pair.newkey.id}"
network_interfaces = {
associate_public_ip_address = true
Expand Down Expand Up @@ -141,7 +142,7 @@ resource "aws_launch_template" "test" {
}
instance_type = "t2.medium"
key_name = "${aws_key_pair.mykey.id}"
key_name = "mykey"
network_interfaces = {
associate_public_ip_address = true
Expand Down
23 changes: 23 additions & 0 deletions upup/pkg/fi/cloudup/awstasks/sshkey.go
Expand Up @@ -108,6 +108,16 @@ func (e *SSHKey) Run(c *fi.Context) error {
}
glog.V(2).Infof("Computed SSH key fingerprint as %q", keyFingerprint)
e.KeyFingerprint = &keyFingerprint
} else if e.IsExistingKey() && *e.Name != "" {
a, err := e.Find(c)
if err != nil {
return err
}
if a == nil {
return fmt.Errorf("unable to find specified SSH key %q", *e.Name)
}

e.KeyFingerprint = a.KeyFingerprint
}
return fi.DefaultDeltaRunMethod(e, c)
}
Expand Down Expand Up @@ -161,6 +171,10 @@ type terraformSSHKey struct {
}

func (_ *SSHKey) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *SSHKey) error {
// We don't want to render a key definition when we're using one that already exists
if e.IsExistingKey() {
return nil
}
tfName := strings.Replace(*e.Name, ":", "", -1)
publicKey, err := t.AddFile("aws_key_pair", tfName, "public_key", e.PublicKey)
if err != nil {
Expand All @@ -175,7 +189,16 @@ func (_ *SSHKey) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *SS
return t.RenderResource("aws_key_pair", tfName, tf)
}

// IsExistingKey will be true if the task has been initialized without using a public key
// this is when we want to use a key that is already present in AWS.
func (e *SSHKey) IsExistingKey() bool {
return e.PublicKey == nil
}

func (e *SSHKey) TerraformLink() *terraform.Literal {
if e.IsExistingKey() {
return terraform.LiteralFromStringValue(*e.Name)
}
tfName := strings.Replace(*e.Name, ":", "", -1)
return terraform.LiteralProperty("aws_key_pair", tfName, "id")
}
Expand Down

0 comments on commit c48d79e

Please sign in to comment.