Skip to content

Commit f53ed85

Browse files
committed
Add jenkins public key to instance
1 parent 4ce4008 commit f53ed85

File tree

6 files changed

+80
-16
lines changed

6 files changed

+80
-16
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ all: gce-pd-driver
2222
gce-pd-driver:
2323
mkdir -p bin
2424
go build -o bin/gce-pd-csi-driver ./cmd/
25-
ginkgo build test/e2e
25+
go test -c sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e -o bin/e2e.test
2626

2727
build-container: gce-pd-driver
2828
docker build -t $(STAGINGIMAGE):$(STAGINGVERSION) .

hi

24.3 MB
Binary file not shown.

test/remote/remote/e2e.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,24 @@ func InitE2ERemote() TestSuite {
3636

3737
// SetupTestPackage sets up the test package with binaries k8s required for node e2e tests
3838
func (n *E2ERemote) SetupTestPackage(tardir string) error {
39-
// Make sure we can find the newly built binaries
40-
gopath, ok := os.LookupEnv("GOPATH")
41-
if !ok {
42-
return fmt.Errorf("Could not find gopath")
43-
}
44-
4539
// TODO(dyzz): build the gce driver tests instead.
4640

47-
cmd := exec.Command("ginkgo", "build", "test/e2e")
41+
cmd := exec.Command("go", "test", "-c", "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e", "-o", "test/e2e/e2e.test")
4842
err := cmd.Run()
4943

5044
if err != nil {
51-
return fmt.Errorf("Failed to ginkgo build test: %v", err)
45+
return fmt.Errorf("Failed to build test: %v", err)
5246
}
5347

5448
cmd = exec.Command("cp", "test/e2e/e2e.test", "bin")
5549
err = cmd.Run()
5650
if err != nil {
5751
return fmt.Errorf("Failed to copy: %v", err)
5852
}
59-
60-
buildOutputDir := filepath.Join(gopath, "src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/bin")
61-
6253
// Copy binaries
6354
requiredBins := []string{"e2e.test"}
6455
for _, bin := range requiredBins {
65-
source := filepath.Join(buildOutputDir, bin)
56+
source := filepath.Join("./bin", bin)
6657
if _, err := os.Stat(source); err != nil {
6758
return fmt.Errorf("failed to locate test binary %s: %v", bin, err)
6859
}

test/remote/remote/ssh.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package remote
1919
import (
2020
"flag"
2121
"fmt"
22+
"os"
2223
"os/exec"
2324
"os/user"
2425
"strings"
@@ -69,6 +70,8 @@ func GetHostnameOrIP(hostname string) string {
6970
}
7071
if *sshUser != "" {
7172
host = fmt.Sprintf("%s@%s", *sshUser, host)
73+
} else if _, ok := os.LookupEnv("JENKINS_GCE_SSH_PRIVATE_KEY_FILE"); ok {
74+
host = fmt.Sprintf("%s@%s", "prow", host)
7275
}
7376
return host
7477
}
@@ -92,7 +95,10 @@ func SSHNoSudo(host string, cmd ...string) (string, error) {
9295

9396
// runSSHCommand executes the ssh or scp command, adding the flag provided --ssh-options
9497
func runSSHCommand(cmd string, args ...string) (string, error) {
95-
if *sshKey != "" {
98+
if pk, ok := os.LookupEnv("JENKINS_GCE_SSH_PRIVATE_KEY_FILE"); ok {
99+
glog.Infof("Running on Jenkins, using special private key file at %v", pk)
100+
args = append([]string{"-i", pk}, args...)
101+
} else if *sshKey != "" {
96102
args = append([]string{"-i", *sshKey}, args...)
97103
} else if key, found := sshDefaultKeyMap[*sshEnv]; found {
98104
args = append([]string{"-i", key}, args...)

test/remote/run_remote/run_remote.go

+68-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23+
"io/ioutil"
2324
"math/rand"
2425
"net/http"
2526
"os"
@@ -320,8 +321,20 @@ func createInstance(serviceAccount string) (string, error) {
320321
glog.Infof("Compute service GOT instance %v: %#v", i.Name, gotInstance)
321322
}
322323

324+
pubkey, ok := os.LookupEnv("JENKINS_GCE_SSH_PUBLIC_KEY_FILE")
325+
if ok {
326+
glog.Infof("Running on Jenkins and JENKINS_GCE_SSH_PUBLIC_KEY_FILE set")
327+
// If we're on CI add ServiceAccount Keys to the instance
328+
err = addPubKeyToInstance(*project, *zone, i.Name, pubkey)
329+
if err != nil {
330+
return "", fmt.Errorf("could not add Jenkins Public Key %v to instance %v: %v", pubkey, i.Name, err)
331+
}
332+
} else {
333+
glog.Infof("JENKINS_GCE_SSH_PUBLIC_KEY_FILE not set, not adding SSH Public Key to Instance")
334+
}
335+
323336
then := time.Now()
324-
err = wait.Poll(15*time.Second, 10*time.Minute, func() (bool, error) {
337+
err = wait.Poll(10*time.Second, 5*time.Minute, func() (bool, error) {
325338
glog.V(2).Infof("Waiting for instance %v to come up. %v elapsed", name, time.Since(then))
326339
var instance *compute.Instance
327340
instance, err = computeService.Instances.Get(*project, *zone, name).Do()
@@ -341,9 +354,10 @@ func createInstance(serviceAccount string) (string, error) {
341354
remote.AddHostnameIP(name, externalIP)
342355
}
343356

344-
if _, err = remote.SSHNoSudo(name, "echo"); err != nil {
357+
if sshOut, err := remote.SSHNoSudo(name, "echo"); err != nil {
345358
err = fmt.Errorf("Instance %v in state RUNNING but not available by SSH: %v", name, err)
346359
glog.Error(err)
360+
glog.Errorf("SSH output: %v", sshOut)
347361
return false, nil
348362
}
349363

@@ -359,6 +373,58 @@ func createInstance(serviceAccount string) (string, error) {
359373
return name, nil
360374
}
361375

376+
func addPubKeyToInstance(project, zone, name, pubKeyFile string) error {
377+
found := false
378+
i, err := computeService.Instances.Get(project, zone, name).Do()
379+
if err != nil {
380+
return err
381+
}
382+
fingerprint := i.Metadata.Fingerprint
383+
items := i.Metadata.Items
384+
for _, item := range items {
385+
if item.Key == "ssh-keys" {
386+
found = true
387+
}
388+
}
389+
newKeys := ""
390+
if found {
391+
// Append these to newKeys first
392+
glog.Infof("Found existing public keys on instance %v", name)
393+
}
394+
glog.Infof("Public key file: %v", pubKeyFile)
395+
publicKeyByte, err := ioutil.ReadFile(pubKeyFile)
396+
if err != nil {
397+
return err
398+
}
399+
400+
publicKey := string(publicKeyByte)
401+
402+
// Take username and prepend it to the public key
403+
tokens := strings.Split(publicKey, " ")
404+
if len(tokens) != 3 {
405+
return fmt.Errorf("Public key not comprised of 3 parts, instead was: %v", publicKey)
406+
}
407+
publicKey = strings.TrimSpace(tokens[2]) + ":" + publicKey
408+
409+
newKeys = newKeys + publicKey
410+
glog.Infof("New Keys: %v", newKeys)
411+
newMeta := &compute.Metadata{
412+
Fingerprint: fingerprint,
413+
Items: []*compute.MetadataItems{
414+
&compute.MetadataItems{
415+
Key: "ssh-keys",
416+
Value: &newKeys,
417+
},
418+
},
419+
}
420+
_, err = computeService.Instances.SetMetadata(project, zone, name, newMeta).Do()
421+
if err != nil {
422+
return err
423+
}
424+
return nil
425+
426+
}
427+
362428
func getexternalIP(instance *compute.Instance) string {
363429
for i := range instance.NetworkInterfaces {
364430
ni := instance.NetworkInterfaces[i]

test/run-tests.sh

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set -x
55

66
readonly PKGDIR=sigs.k8s.io/gcp-compute-persistent-disk-csi-driver
77

8+
echo ${USER}
89
echo "Testing CSI-Sanity"
910
go test -timeout 30s "${PKGDIR}/test/sanity/" -run ^TestSanity$
1011
echo "Running E2E Tests"

0 commit comments

Comments
 (0)