@@ -19,6 +19,7 @@ package gce
19
19
import (
20
20
"fmt"
21
21
"io"
22
+ "strings"
22
23
"sync"
23
24
"time"
24
25
@@ -40,9 +41,14 @@ const (
40
41
operationPollInterval = 100 * time .Millisecond
41
42
)
42
43
44
+ type migInformation struct {
45
+ config * config.MigConfig
46
+ basename string
47
+ }
48
+
43
49
// GceManager is handles gce communication and data caching.
44
50
type GceManager struct {
45
- migs []* config. MigConfig
51
+ migs []* migInformation
46
52
service * gce.Service
47
53
migCache map [config.InstanceConfig ]* config.MigConfig
48
54
cacheMutex sync.Mutex
@@ -75,8 +81,15 @@ func CreateGceManager(migs []*config.MigConfig, configReader io.Reader) (*GceMan
75
81
return nil , err
76
82
}
77
83
84
+ migInfos := make ([]* migInformation , 0 , len (migs ))
85
+ for _ , mig := range migs {
86
+ migInfos = append (migInfos , & migInformation {
87
+ config : mig ,
88
+ })
89
+ }
90
+
78
91
manager := & GceManager {
79
- migs : migs ,
92
+ migs : migInfos ,
80
93
service : gceService ,
81
94
migCache : map [config.InstanceConfig ]* config.MigConfig {},
82
95
}
@@ -165,13 +178,22 @@ func (m *GceManager) GetMigForInstance(instance *config.InstanceConfig) (*config
165
178
if mig , found := m .migCache [* instance ]; found {
166
179
return mig , nil
167
180
}
168
- if err := m .regenerateCache (); err != nil {
169
- return nil , fmt .Errorf ("Error while looking for MIG for instance %+v, error: %v" , * instance , err )
170
- }
171
- if mig , found := m .migCache [* instance ]; found {
172
- return mig , nil
181
+
182
+ for _ , mig := range m .migs {
183
+ if mig .config .Project == instance .Project &&
184
+ mig .config .Zone == instance .Zone &&
185
+ strings .HasPrefix (instance .Name , mig .basename ) {
186
+ if err := m .regenerateCache (); err != nil {
187
+ return nil , fmt .Errorf ("Error while looking for MIG for instance %+v, error: %v" , * instance , err )
188
+ }
189
+ if mig , found := m .migCache [* instance ]; found {
190
+ return mig , nil
191
+ }
192
+ return nil , fmt .Errorf ("Instance %+v does not belong to any configured MIG" , * instance )
193
+ }
173
194
}
174
- return nil , fmt .Errorf ("Instance %+v does not belong to any known MIG" , * instance )
195
+ // Instance doesn't belong to any configured mig.
196
+ return nil , nil
175
197
}
176
198
177
199
func (m * GceManager ) regenerateCacheIgnoreError () {
@@ -185,8 +207,16 @@ func (m *GceManager) regenerateCacheIgnoreError() {
185
207
func (m * GceManager ) regenerateCache () error {
186
208
newMigCache := map [config.InstanceConfig ]* config.MigConfig {}
187
209
188
- for _ , mig := range m .migs {
210
+ for _ , migInfo := range m .migs {
211
+ mig := migInfo .config
189
212
glog .V (4 ).Infof ("Regenerating MIG information for %s %s %s" , mig .Project , mig .Zone , mig .Name )
213
+
214
+ instanceGroupManager , err := m .service .InstanceGroupManagers .Get (mig .Project , mig .Zone , mig .Name ).Do ()
215
+ if err != nil {
216
+ return err
217
+ }
218
+ migInfo .basename = instanceGroupManager .BaseInstanceName
219
+
190
220
instances , err := m .service .InstanceGroupManagers .ListManagedInstances (mig .Project , mig .Zone , mig .Name ).Do ()
191
221
if err != nil {
192
222
glog .V (4 ).Infof ("Failed MIG info request for %s %s %s: %v" , mig .Project , mig .Zone , mig .Name , err )
0 commit comments