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.
This commit is contained in:
Peter Rifel 2021-03-23 19:03:26 -05:00
parent 1bf4fd744f
commit b2f2c61e69
No known key found for this signature in database
GPG Key ID: BC6469E5B16DB2B6
2 changed files with 63 additions and 8 deletions

View File

@ -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 == "" {

View File

@ -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
}