Skip to content

Commit 59275d3

Browse files
committed
Check machine type before insert intance. Set GOARCH through command
1 parent 81b9946 commit 59275d3

File tree

6 files changed

+69
-45
lines changed

6 files changed

+69
-45
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ gce-pd-driver: require-GCE_PD_CSI_STAGING_VERSION
4141
go build -mod=vendor -gcflags=$(GCFLAGS) -ldflags "-X main.version=$(STAGINGVERSION)" -o bin/${DRIVERBINARY} ./cmd/gce-pd-csi-driver/
4242

4343
gce-pd-driver-windows: require-GCE_PD_CSI_STAGING_VERSION
44-
ifeq ($(ARCH), amd64)
44+
ifeq (GOARCH, amd64)
4545
mkdir -p bin
46-
GOOS=windows GOARCH=$(ARCH) go build -mod=vendor -ldflags -X=main.version=$(STAGINGVERSION) -o bin/${DRIVERWINDOWSBINARY} ./cmd/gce-pd-csi-driver/
46+
GOOS=windows go build -mod=vendor -ldflags -X=main.version=$(STAGINGVERSION) -o bin/${DRIVERWINDOWSBINARY} ./cmd/gce-pd-csi-driver/
4747
else
48-
echo "Warning: gcp-pd-driver-windows only supports ARCH=amd64."
48+
$(warning gcp-pd-driver-windows only supports amd64.)
4949
endif
5050

5151
build-container: require-GCE_PD_CSI_STAGING_IMAGE require-GCE_PD_CSI_STAGING_VERSION init-buildx

test/e2e/tests/setup_e2e_test.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ import (
3535

3636
var (
3737
project = flag.String("project", "", "Project to run tests in")
38-
architecture = flag.String("arch", "amd64", "Architecture pd csi driver build on")
39-
vmName = flag.String("vm-name", "", "Name of the VM to run tests on")
4038
serviceAccount = flag.String("service-account", "", "Service account to bring up instance with")
39+
architecture = flag.String("arch", "amd64", "Architecture pd csi driver build on")
4140
zones = flag.String("zones", "us-central1-c,us-central1-b", "Zones to run tests in. If there are multiple zones, separate each by comma")
4241
machineType = flag.String("machine-type", "n1-standard-1", "Type of machine to provision instance on")
4342
imageURL = flag.String("image-url", "projects/debian-cloud/global/images/family/debian-11", "OS image url to get image from")
@@ -92,10 +91,10 @@ var _ = BeforeSuite(func() {
9291
for _, zone := range zones {
9392
go func(curZone string) {
9493
defer GinkgoRecover()
95-
nodeID := getNodeId(*vmName, curZone)
94+
nodeID := fmt.Sprintf("gce-pd-csi-e2e-%s", curZone)
9695
klog.Infof("Setting up node %s\n", nodeID)
9796

98-
i, err := remote.SetupInstance(*project, curZone, nodeID, *machineType, *serviceAccount, *imageURL, computeService)
97+
i, err := remote.SetupInstance(*project, *architecture, curZone, nodeID, *machineType, *serviceAccount, *imageURL, computeService)
9998
if err != nil {
10099
klog.Fatalf("Failed to setup instance %v: %v", nodeID, err)
101100
}
@@ -148,10 +147,3 @@ func getRandomTestContext() *remote.TestContext {
148147
rn := rand.Intn(len(testContexts))
149148
return testContexts[rn]
150149
}
151-
152-
func getNodeId(vmName, curZone string) string {
153-
if vmName == "" {
154-
return fmt.Sprintf("gce-pd-csi-e2e-%s", curZone)
155-
}
156-
return vmName
157-
}

test/remote/archiver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"k8s.io/klog"
2727
)
2828

29-
func CreateDriverArchive(archiveName, pkgPath, binPath string) (string, error) {
29+
func CreateDriverArchive(archiveName, architecture, pkgPath, binPath string) (string, error) {
3030
klog.V(2).Infof("Building archive...")
3131
tarDir, err := ioutil.TempDir("", "driver-temp-archive")
3232
if err != nil {
@@ -35,7 +35,7 @@ func CreateDriverArchive(archiveName, pkgPath, binPath string) (string, error) {
3535
defer os.RemoveAll(tarDir)
3636

3737
// Call the suite function to setup the test package.
38-
err = setupBinaries(tarDir, pkgPath, binPath)
38+
err = setupBinaries(architecture, tarDir, pkgPath, binPath)
3939
if err != nil {
4040
return "", fmt.Errorf("failed to setup test package %q: %v", tarDir, err)
4141
}
@@ -53,9 +53,9 @@ func CreateDriverArchive(archiveName, pkgPath, binPath string) (string, error) {
5353
return filepath.Join(dir, archiveName), nil
5454
}
5555

56-
func setupBinaries(tarDir, pkgPath, binPath string) error {
56+
func setupBinaries(architecture, tarDir, pkgPath, binPath string) error {
5757
klog.V(4).Infof("Making binaries and copying to temp dir...")
58-
out, err := exec.Command("make", "-C", pkgPath, "ARCH="+architecture).CombinedOutput()
58+
out, err := exec.Command("make", "-C", pkgPath, "GOARCH="+architecture).CombinedOutput()
5959
if err != nil {
6060
return fmt.Errorf("Failed to make at %s: %v: %v", pkgPath, string(out), err)
6161
}

test/remote/instance.go

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ const (
4545
)
4646

4747
type InstanceInfo struct {
48-
project string
49-
zone string
50-
name string
51-
machineType string
48+
project string
49+
architecture string
50+
zone string
51+
name string
52+
machineType string
5253

5354
// External IP is filled in after instance creation
5455
externalIP string
@@ -68,12 +69,13 @@ func (i *InstanceInfo) GetNodeID() string {
6869
return common.CreateNodeID(i.project, i.zone, i.name)
6970
}
7071

71-
func CreateInstanceInfo(project, instanceZone, name, machineType string, cs *compute.Service) (*InstanceInfo, error) {
72+
func CreateInstanceInfo(project, instanceArchitecture, instanceZone, name, machineType string, cs *compute.Service) (*InstanceInfo, error) {
7273
return &InstanceInfo{
73-
project: project,
74-
zone: instanceZone,
75-
name: name,
76-
machineType: machineType,
74+
project: project,
75+
architecture: instanceArchitecture,
76+
zone: instanceZone,
77+
name: name,
78+
machineType: machineType,
7779

7880
computeService: cs,
7981
}, nil
@@ -92,7 +94,7 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
9294
return fmt.Errorf("Failed to create firewall rule: %v", err)
9395
}
9496

95-
inst := &compute.Instance{
97+
newInst := &compute.Instance{
9698
Name: i.name,
9799
MachineType: fmt.Sprintf("zones/%s/machineTypes/%s", i.zone, i.machineType),
98100
NetworkInterfaces: []*compute.NetworkInterface{
@@ -121,31 +123,47 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
121123
Email: serviceAccount,
122124
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
123125
}
124-
inst.ServiceAccounts = []*compute.ServiceAccount{saObj}
126+
newInst.ServiceAccounts = []*compute.ServiceAccount{saObj}
125127

126128
if pubkey, ok := os.LookupEnv("JENKINS_GCE_SSH_PUBLIC_KEY_FILE"); ok {
127129
klog.V(4).Infof("JENKINS_GCE_SSH_PUBLIC_KEY_FILE set to %v, adding public key to Instance", pubkey)
128130
meta, err := generateMetadataWithPublicKey(pubkey)
129131
if err != nil {
130132
return err
131133
}
132-
inst.Metadata = meta
134+
newInst.Metadata = meta
133135
}
134136

135-
if _, err := i.computeService.Instances.Get(i.project, i.zone, inst.Name).Do(); err != nil {
136-
op, err := i.computeService.Instances.Insert(i.project, i.zone, inst).Do()
137-
klog.V(4).Infof("Inserted instance %v in project: %v, zone: %v", inst.Name, i.project, i.zone)
138-
if err != nil {
139-
ret := fmt.Sprintf("could not create instance %s: API error: %v", i.name, err)
140-
if op != nil {
141-
ret = fmt.Sprintf("%s. op error: %v", ret, op.Error)
137+
curInst, _ := i.computeService.Instances.Get(i.project, i.zone, newInst.Name).Do()
138+
if curInst != nil {
139+
if !strings.Contains(curInst.MachineType, newInst.MachineType) {
140+
klog.V(4).Infof("Instance machine type doesn't match the required one. Remove instance.")
141+
if _, err := i.computeService.Instances.Delete(i.project, i.zone, i.name).Do(); err != nil {
142+
return err
143+
}
144+
145+
then := time.Now()
146+
err := wait.Poll(15*time.Second, 5*time.Minute, func() (bool, error) {
147+
klog.V(2).Infof("Waiting for instance to be deleted. %v elapsed", time.Since(then))
148+
if instance, _ = i.computeService.Instances.Get(i.project, i.zone, i.name).Do(); instance != nil {
149+
return false, nil
150+
}
151+
return true, nil
152+
})
153+
if err != nil {
154+
return err
142155
}
143-
return errors.New(ret)
144-
} else if op.Error != nil {
145-
return fmt.Errorf("could not create instance %s: %+v", i.name, op.Error)
156+
157+
if err := insertInstance(i, newInst); err != nil {
158+
return err
159+
}
160+
} else {
161+
klog.V(4).Infof("Compute service GOT instance %v, skipping instance creation", newInst.Name)
146162
}
147163
} else {
148-
klog.V(4).Infof("Compute service GOT instance %v, skipping instance creation", inst.Name)
164+
if err := insertInstance(i, newInst); err != nil {
165+
return err
166+
}
149167
}
150168

151169
then := time.Now()
@@ -187,6 +205,21 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
187205
return nil
188206
}
189207

208+
func insertInstance(i *InstanceInfo, newInst *compute.Instance) error {
209+
op, err := i.computeService.Instances.Insert(i.project, i.zone, newInst).Do()
210+
klog.V(4).Infof("Inserted instance %v in project: %v, zone: %v", newInst.Name, i.project, i.zone)
211+
if err != nil {
212+
ret := fmt.Sprintf("could not create instance %s: API error: %v", i.name, err)
213+
if op != nil {
214+
ret = fmt.Sprintf("%s. op error: %v", ret, op.Error)
215+
}
216+
return errors.New(ret)
217+
} else if op.Error != nil {
218+
return fmt.Errorf("could not create instance %s: %+v", i.name, op.Error)
219+
}
220+
return nil
221+
}
222+
190223
func (i *InstanceInfo) DeleteInstance() {
191224
klog.V(4).Infof("Deleting instance %q", i.name)
192225
_, err := i.computeService.Instances.Delete(i.project, i.zone, i.name).Do()

test/remote/setup-teardown.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ type processes struct {
5454
}
5555

5656
// SetupInstance sets up the specified GCE Instance for E2E testing and returns a handle to the instance object for future use.
57-
func SetupInstance(instanceProject, instanceZone, instanceName, instanceMachineType, instanceServiceAccount, instanceImageURL string, cs *compute.Service) (*InstanceInfo, error) {
57+
func SetupInstance(instanceProject, instanceArchitecture, instanceZone, instanceName, instanceMachineType, instanceServiceAccount, instanceImageURL string, cs *compute.Service) (*InstanceInfo, error) {
5858
// Create the instance in the requisite zone
59-
instance, err := CreateInstanceInfo(instanceProject, instanceZone, instanceName, instanceMachineType, cs)
59+
instance, err := CreateInstanceInfo(instanceProject, instanceArchitecture, instanceZone, instanceName, instanceMachineType, cs)
6060
if err != nil {
6161
return nil, err
6262
}
@@ -73,7 +73,7 @@ func SetupInstance(instanceProject, instanceZone, instanceName, instanceMachineT
7373
// that the driver is on and the CSI Client object to make CSI calls to the remote driver.
7474
func SetupNewDriverAndClient(instance *InstanceInfo, config *ClientConfig) (*TestContext, error) {
7575
archiveName := fmt.Sprintf("e2e_driver_binaries_%s.tar.gz", uuid.NewUUID())
76-
archivePath, err := CreateDriverArchive(archiveName, config.PkgPath, config.BinPath)
76+
archivePath, err := CreateDriverArchive(archiveName, instance.architecture, config.PkgPath, config.BinPath)
7777
if err != nil {
7878
return nil, err
7979
}

test/run-e2e-local.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ set -o errexit
66
readonly PKGDIR=sigs.k8s.io/gcp-compute-persistent-disk-csi-driver
77

88
ginkgo --v "test/e2e/tests" -- --project "${PROJECT}" --service-account "${IAM_NAME}" --v=4 --logtostderr
9-

0 commit comments

Comments
 (0)