@@ -328,32 +328,41 @@ func test(tests []string) *TestResult {
328
328
return result
329
329
}
330
330
331
- // Provision a gce instance using image
332
- func createInstance ( serviceAccount string ) ( string , error ) {
331
+ // Create default SSH filewall rule if it does not exist
332
+ func createDefaultFirewallRule () error {
333
333
var err error
334
-
335
- name := "gce-pd-csi-e2e"
336
- myuuid := string (uuid .NewUUID ())
337
-
338
- // Create default filewall rule if it does not exist
339
334
if _ , err = computeService .Firewalls .Get (* project , defaultFirewallRule ).Do (); err != nil {
340
335
glog .Infof ("Default firewall rule %v does not exist, creating" , defaultFirewallRule )
341
336
f := & compute.Firewall {
342
337
Name : defaultFirewallRule ,
343
338
Allowed : []* compute.FirewallAllowed {
344
- & compute. FirewallAllowed {
339
+ {
345
340
IPProtocol : "tcp" ,
346
341
Ports : []string {"22" },
347
342
},
348
343
},
349
344
}
350
345
_ , err = computeService .Firewalls .Insert (* project , f ).Do ()
351
346
if err != nil {
352
- return "" , fmt .Errorf ("Failed to insert required default SSH Firewall Rule %v: %v" , defaultFirewallRule , err )
347
+ return fmt .Errorf ("Failed to insert required default SSH firewall Rule %v: %v" , defaultFirewallRule , err )
353
348
}
354
349
} else {
355
350
glog .Infof ("Default firewall rule %v already exists, skipping creation" , defaultFirewallRule )
356
351
}
352
+ return nil
353
+ }
354
+
355
+ // Provision a gce instance using image
356
+ func createInstance (serviceAccount string ) (string , error ) {
357
+ var err error
358
+
359
+ name := "gce-pd-csi-e2e"
360
+ myuuid := string (uuid .NewUUID ())
361
+
362
+ err = createDefaultFirewallRule ()
363
+ if err != nil {
364
+ return "" , fmt .Errorf ("Failed to create firewall rule: %v" , err )
365
+ }
357
366
358
367
glog .V (4 ).Infof ("Creating instance: %v" , name )
359
368
@@ -397,13 +406,6 @@ func createInstance(serviceAccount string) (string, error) {
397
406
return "" , err
398
407
}
399
408
i .Metadata = meta
400
- /*glog.V(4).Infof("JENKINS_GCE_SSH_PUBLIC_KEY_FILE set to %v, adding public key to Instance", pubkey)
401
- // If we're on CI add public SSH keys to the instance
402
- i.Metadata =
403
- err = addPubKeyToInstance(*project, *zone, i.Name, pubkey)
404
- if err != nil {
405
- return "", fmt.Errorf("could not add Jenkins public key %v to instance %v: %v", pubkey, i.Name, err)
406
- }*/
407
409
}
408
410
409
411
if _ , err := computeService .Instances .Get (* project , * zone , i .Name ).Do (); err != nil {
@@ -485,54 +487,6 @@ func generateMetadataWithPublicKey(pubKeyFile string) (*compute.Metadata, error)
485
487
return newMeta , nil
486
488
}
487
489
488
- func addPubKeyToInstance (project , zone , name , pubKeyFile string ) error {
489
- newKeys := ""
490
- i , err := computeService .Instances .Get (project , zone , name ).Do ()
491
- if err != nil {
492
- return err
493
- }
494
- fingerprint := i .Metadata .Fingerprint
495
- items := i .Metadata .Items
496
- for _ , item := range items {
497
- if item .Key == "ssh-keys" {
498
- glog .V (2 ).Infof ("Found existing ssh-keys, prepending to new key string" )
499
- newKeys += * item .Value
500
- break
501
- }
502
- }
503
- publicKeyByte , err := ioutil .ReadFile (pubKeyFile )
504
- if err != nil {
505
- return err
506
- }
507
-
508
- publicKey := string (publicKeyByte )
509
-
510
- // Take username and prepend it to the public key
511
- tokens := strings .Split (publicKey , " " )
512
- if len (tokens ) != 3 {
513
- return fmt .Errorf ("Public key not comprised of 3 parts, instead was: %v" , publicKey )
514
- }
515
- publicKey = strings .TrimSpace (tokens [2 ]) + ":" + publicKey
516
-
517
- newKeys = newKeys + publicKey
518
- glog .V (4 ).Infof ("New ssh-keys for instance %v: %v" , name , newKeys )
519
- newMeta := & compute.Metadata {
520
- Fingerprint : fingerprint ,
521
- Items : []* compute.MetadataItems {
522
- {
523
- Key : "ssh-keys" ,
524
- Value : & newKeys ,
525
- },
526
- },
527
- }
528
- _ , err = computeService .Instances .SetMetadata (project , zone , name , newMeta ).Do ()
529
- if err != nil {
530
- return err
531
- }
532
- return nil
533
-
534
- }
535
-
536
490
func getexternalIP (instance * compute.Instance ) string {
537
491
for i := range instance .NetworkInterfaces {
538
492
ni := instance .NetworkInterfaces [i ]
0 commit comments