From b2f2c61e69ffcb80eeae80d20f9aecc0b6167e65 Mon Sep 17 00:00:00 2001 From: Peter Rifel Date: Tue, 23 Mar 2021 19:03:26 -0500 Subject: [PATCH] Kubetest2 - Setup SSH keys for GCE Originally I had thought we were relying on ssh keys mounted from a secret, it turns out kubetest 1 generated the keys indirectly through gcloud. This runs the same command as kubetest 1, creating and uploading the SSH keys. --- tests/e2e/kubetest2-kops/deployer/common.go | 18 +++---- tests/e2e/kubetest2-kops/gce/ssh.go | 53 +++++++++++++++++++++ 2 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 tests/e2e/kubetest2-kops/gce/ssh.go diff --git a/tests/e2e/kubetest2-kops/deployer/common.go b/tests/e2e/kubetest2-kops/deployer/common.go index 0124eaa86d..3e6d5f5ba1 100644 --- a/tests/e2e/kubetest2-kops/deployer/common.go +++ b/tests/e2e/kubetest2-kops/deployer/common.go @@ -27,6 +27,7 @@ import ( "time" "k8s.io/klog/v2" + "k8s.io/kops/tests/e2e/kubetest2-kops/gce" "k8s.io/kops/tests/e2e/pkg/kops" "k8s.io/kops/tests/e2e/pkg/target" "sigs.k8s.io/kubetest2/pkg/boskos" @@ -74,14 +75,6 @@ func (d *deployer) initialize() error { d.SSHPublicKeyPath = os.Getenv("AWS_SSH_PUBLIC_KEY_FILE") } case "gce": - // These environment variables are defined by the "preset-k8s-ssh" prow preset - // https://github.com/kubernetes/test-infra/blob/432c6e7dca38f0785901a6159275524cec369c4a/config/prow/config.yaml#L639-L656 - if d.SSHPrivateKeyPath == "" { - d.SSHPrivateKeyPath = os.Getenv("JENKINS_GCE_SSH_PRIVATE_KEY_FILE") - } - if d.SSHPublicKeyPath == "" { - d.SSHPublicKeyPath = os.Getenv("JENKINS_GCE_SSH_PUBLIC_KEY_FILE") - } if d.GCPProject == "" { klog.V(1).Info("No GCP project provided, acquiring from Boskos") @@ -103,6 +96,15 @@ func (d *deployer) initialize() error { } d.GCPProject = resource.Name klog.V(1).Infof("Got project %s from boskos", d.GCPProject) + + if d.SSHPrivateKeyPath == "" && d.SSHPublicKeyPath == "" { + privateKey, publicKey, err := gce.SetupSSH(d.GCPProject) + if err != nil { + return err + } + d.SSHPrivateKeyPath = privateKey + d.SSHPublicKeyPath = publicKey + } } } if d.SSHUser == "" { diff --git a/tests/e2e/kubetest2-kops/gce/ssh.go b/tests/e2e/kubetest2-kops/gce/ssh.go new file mode 100644 index 0000000000..c69a7785f8 --- /dev/null +++ b/tests/e2e/kubetest2-kops/gce/ssh.go @@ -0,0 +1,53 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package gce + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "k8s.io/klog/v2" + "sigs.k8s.io/kubetest2/pkg/exec" +) + +func SetupSSH(project string) (string, string, error) { + dir, err := os.MkdirTemp("kops", "ssh") + if err != nil { + return "", "", err + } + + privateKey := filepath.Join(dir, "key") + configArgs := []string{ + "gcloud", + "compute", + fmt.Sprintf("--project=%v", project), + "config-ssh", + fmt.Sprintf("--ssh-key-file=%v", privateKey), + } + klog.Info(strings.Join(configArgs, " ")) + cmd := exec.Command(configArgs[0], configArgs[1:]...) + + exec.InheritOutput(cmd) + err = cmd.Run() + if err != nil { + return "", "", err + } + + return privateKey, fmt.Sprintf("%v.pub", privateKey), nil +}