@@ -22,6 +22,7 @@ import (
2222 "fmt"
2323 "net/http"
2424 "os"
25+ "os/exec"
2526 "strings"
2627 "time"
2728
@@ -44,12 +45,25 @@ const (
4445 timestampFormat = "20060102T150405"
4546)
4647
48+ // InstanceConfig is the common bundle of options used for instance creation.
49+ type InstanceConfig struct {
50+ Project string
51+ Architecture string
52+ MachineType string
53+ ServiceAccount string
54+ ImageURL string
55+ CloudtopHost bool
56+ }
57+
4758type InstanceInfo struct {
48- project string
49- architecture string
50- zone string
51- name string
52- machineType string
59+ project string
60+ architecture string
61+ zone string
62+ name string
63+ machineType string
64+ serviceAccount string
65+ imageURL string
66+ cloudtopHost bool
5367
5468 // External IP is filled in after instance creation
5569 externalIP string
@@ -69,19 +83,22 @@ func (i *InstanceInfo) GetNodeID() string {
6983 return common .CreateNodeID (i .project , i .zone , i .name )
7084}
7185
72- func CreateInstanceInfo (project , instanceArchitecture , instanceZone , name , machineType string , cs * compute.Service ) (* InstanceInfo , error ) {
86+ func CreateInstanceInfo (config * InstanceConfig , zone , name string , cs * compute.Service ) (* InstanceInfo , error ) {
7387 return & InstanceInfo {
74- project : project ,
75- architecture : instanceArchitecture ,
76- zone : instanceZone ,
88+ project : config . Project ,
89+ architecture : config . Architecture ,
90+ zone : zone ,
7791 name : name ,
78- machineType : machineType ,
92+ machineType : config .MachineType ,
93+ cloudtopHost : config .CloudtopHost ,
94+ serviceAccount : config .ServiceAccount ,
95+ imageURL : config .ImageURL ,
7996 computeService : cs ,
8097 }, nil
8198}
8299
83100// Provision a gce instance using image
84- func (i * InstanceInfo ) CreateOrGetInstance (imageURL , serviceAccount string ) error {
101+ func (i * InstanceInfo ) CreateOrGetInstance () error {
85102 var err error
86103 var instance * compute.Instance
87104 klog .V (4 ).Infof ("Creating instance: %v" , i .name )
@@ -112,14 +129,14 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
112129 Type : "PERSISTENT" ,
113130 InitializeParams : & compute.AttachedDiskInitializeParams {
114131 DiskName : "my-root-pd-" + myuuid ,
115- SourceImage : imageURL ,
132+ SourceImage : i . imageURL ,
116133 },
117134 },
118135 },
119136 }
120137
121138 saObj := & compute.ServiceAccount {
122- Email : serviceAccount ,
139+ Email : i . serviceAccount ,
123140 Scopes : []string {"https://www.googleapis.com/auth/cloud-platform" },
124141 }
125142 newInst .ServiceAccounts = []* compute.ServiceAccount {saObj }
@@ -187,6 +204,15 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
187204 return false , nil
188205 }
189206
207+ if i .cloudtopHost {
208+ output , err := exec .Command ("gcloud" , "compute" , "ssh" , i .name , "--zone" , i .zone , "--project" , i .project , "--" , "-o" , "ProxyCommand=corp-ssh-helper %h %p" , "--" , "echo" ).CombinedOutput ()
209+ if err != nil {
210+ klog .Errorf ("Failed to bootstrap ssh (%v): %s" , err , string (output ))
211+ return false , nil
212+ }
213+ klog .V (4 ).Infof ("Bootstrapped cloudtop ssh for instance %v" , i .name )
214+ }
215+
190216 externalIP := getexternalIP (instance )
191217 if len (externalIP ) > 0 {
192218 i .externalIP = externalIP
0 commit comments