@@ -137,7 +137,7 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
137
137
curInst , _ := i .computeService .Instances .Get (i .project , i .zone , newInst .Name ).Do ()
138
138
if curInst != nil {
139
139
if ! strings .Contains (curInst .MachineType , newInst .MachineType ) {
140
- klog .V (4 ).Infof ("Instance machine type doesn't match the required one. Remove instance." )
140
+ klog .V (4 ).Infof ("Instance machine type doesn't match the required one. Delete instance." )
141
141
if _ , err := i .computeService .Instances .Delete (i .project , i .zone , i .name ).Do (); err != nil {
142
142
return err
143
143
}
@@ -153,17 +153,23 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
153
153
if err != nil {
154
154
return err
155
155
}
156
+ }
157
+ }
156
158
157
- if err := insertInstance (i , newInst ); err != nil {
158
- return err
159
+ if _ , err := i .computeService .Instances .Get (i .project , i .zone , newInst .Name ).Do (); err != nil {
160
+ op , err := i .computeService .Instances .Insert (i .project , i .zone , newInst ).Do ()
161
+ klog .V (4 ).Infof ("Inserted instance %v in project: %v, zone: %v" , newInst .Name , i .project , i .zone )
162
+ if err != nil {
163
+ ret := fmt .Sprintf ("could not create instance %s: API error: %v" , i .name , err )
164
+ if op != nil {
165
+ ret = fmt .Sprintf ("%s. op error: %v" , ret , op .Error )
159
166
}
160
- } else {
161
- klog .V (4 ).Infof ("Compute service GOT instance %v, skipping instance creation" , newInst .Name )
167
+ return errors .New (ret )
168
+ } else if op .Error != nil {
169
+ return fmt .Errorf ("could not create instance %s: %+v" , i .name , op .Error )
162
170
}
163
171
} else {
164
- if err := insertInstance (i , newInst ); err != nil {
165
- return err
166
- }
172
+ klog .V (4 ).Infof ("Compute service GOT instance %v, skipping instance creation" , newInst .Name )
167
173
}
168
174
169
175
then := time .Now ()
@@ -205,21 +211,6 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
205
211
return nil
206
212
}
207
213
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
-
223
214
func (i * InstanceInfo ) DeleteInstance () {
224
215
klog .V (4 ).Infof ("Deleting instance %q" , i .name )
225
216
_ , err := i .computeService .Instances .Delete (i .project , i .zone , i .name ).Do ()
0 commit comments