@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"net/http"
24
24
"os"
25
+ "os/exec"
25
26
"strings"
26
27
"time"
27
28
@@ -44,12 +45,25 @@ const (
44
45
timestampFormat = "20060102T150405"
45
46
)
46
47
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
+
47
58
type 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
53
67
54
68
// External IP is filled in after instance creation
55
69
externalIP string
@@ -69,19 +83,22 @@ func (i *InstanceInfo) GetNodeID() string {
69
83
return common .CreateNodeID (i .project , i .zone , i .name )
70
84
}
71
85
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 ) {
73
87
return & InstanceInfo {
74
- project : project ,
75
- architecture : instanceArchitecture ,
76
- zone : instanceZone ,
88
+ project : config . Project ,
89
+ architecture : config . Architecture ,
90
+ zone : zone ,
77
91
name : name ,
78
- machineType : machineType ,
92
+ machineType : config .MachineType ,
93
+ cloudtopHost : config .CloudtopHost ,
94
+ serviceAccount : config .ServiceAccount ,
95
+ imageURL : config .ImageURL ,
79
96
computeService : cs ,
80
97
}, nil
81
98
}
82
99
83
100
// Provision a gce instance using image
84
- func (i * InstanceInfo ) CreateOrGetInstance (imageURL , serviceAccount string ) error {
101
+ func (i * InstanceInfo ) CreateOrGetInstance () error {
85
102
var err error
86
103
var instance * compute.Instance
87
104
klog .V (4 ).Infof ("Creating instance: %v" , i .name )
@@ -112,14 +129,14 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
112
129
Type : "PERSISTENT" ,
113
130
InitializeParams : & compute.AttachedDiskInitializeParams {
114
131
DiskName : "my-root-pd-" + myuuid ,
115
- SourceImage : imageURL ,
132
+ SourceImage : i . imageURL ,
116
133
},
117
134
},
118
135
},
119
136
}
120
137
121
138
saObj := & compute.ServiceAccount {
122
- Email : serviceAccount ,
139
+ Email : i . serviceAccount ,
123
140
Scopes : []string {"https://www.googleapis.com/auth/cloud-platform" },
124
141
}
125
142
newInst .ServiceAccounts = []* compute.ServiceAccount {saObj }
@@ -187,6 +204,15 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
187
204
return false , nil
188
205
}
189
206
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
+
190
216
externalIP := getexternalIP (instance )
191
217
if len (externalIP ) > 0 {
192
218
i .externalIP = externalIP
0 commit comments