@@ -45,10 +45,11 @@ const (
45
45
)
46
46
47
47
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
52
53
53
54
// External IP is filled in after instance creation
54
55
externalIP string
@@ -68,12 +69,13 @@ func (i *InstanceInfo) GetNodeID() string {
68
69
return common .CreateNodeID (i .project , i .zone , i .name )
69
70
}
70
71
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 ) {
72
73
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 ,
77
79
78
80
computeService : cs ,
79
81
}, nil
@@ -92,7 +94,7 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
92
94
return fmt .Errorf ("Failed to create firewall rule: %v" , err )
93
95
}
94
96
95
- inst := & compute.Instance {
97
+ newInst := & compute.Instance {
96
98
Name : i .name ,
97
99
MachineType : fmt .Sprintf ("zones/%s/machineTypes/%s" , i .zone , i .machineType ),
98
100
NetworkInterfaces : []* compute.NetworkInterface {
@@ -121,31 +123,47 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
121
123
Email : serviceAccount ,
122
124
Scopes : []string {"https://www.googleapis.com/auth/cloud-platform" },
123
125
}
124
- inst .ServiceAccounts = []* compute.ServiceAccount {saObj }
126
+ newInst .ServiceAccounts = []* compute.ServiceAccount {saObj }
125
127
126
128
if pubkey , ok := os .LookupEnv ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE" ); ok {
127
129
klog .V (4 ).Infof ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE set to %v, adding public key to Instance" , pubkey )
128
130
meta , err := generateMetadataWithPublicKey (pubkey )
129
131
if err != nil {
130
132
return err
131
133
}
132
- inst .Metadata = meta
134
+ newInst .Metadata = meta
133
135
}
134
136
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
142
155
}
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 )
146
162
}
147
163
} 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
+ }
149
167
}
150
168
151
169
then := time .Now ()
@@ -187,6 +205,21 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
187
205
return nil
188
206
}
189
207
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
+
190
223
func (i * InstanceInfo ) DeleteInstance () {
191
224
klog .V (4 ).Infof ("Deleting instance %q" , i .name )
192
225
_ , err := i .computeService .Instances .Delete (i .project , i .zone , i .name ).Do ()
0 commit comments