-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
dump.go
115 lines (93 loc) · 3.13 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gce
import (
"context"
"fmt"
"sync"
compute "google.golang.org/api/compute/v1"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/resources"
gce "k8s.io/kops/upup/pkg/fi/cloudup/gce"
)
// dumpState holds state for use during a GCE dump operation
type dumpState struct {
// cloud is the a reference to the GCE cloud we are dumping
cloud gce.GCECloud
// mutex protects the follow resources
mutex sync.Mutex
// instances is a cache of instances by zone
instances map[string]map[string]*compute.Instance
}
// DumpManagedInstance is responsible for dumping a resource for a ManagedInstance
func DumpManagedInstance(op *resources.DumpOperation, r *resources.Resource) error {
instance := r.Obj.(*compute.ManagedInstance)
u, err := gce.ParseGoogleCloudURL(instance.Instance)
if err != nil {
return fmt.Errorf("unable to parse instance url %q", instance.Instance)
}
// Fetch instance details
instanceMap, err := getDumpState(op).getInstances(op.Context, u.Zone)
if err != nil {
return err
}
i := &resources.Instance{
Name: u.Name,
}
instanceDetails := instanceMap[u.Name]
if instanceDetails == nil {
klog.Warningf("instance %q not found", instance.Instance)
} else {
for _, ni := range instanceDetails.NetworkInterfaces {
for _, ac := range ni.AccessConfigs {
if ac.NatIP != "" {
i.PublicAddresses = append(i.PublicAddresses, ac.NatIP)
}
}
}
}
op.Dump.Instances = append(op.Dump.Instances, i)
// Unclear if we should include the instance details in the dump - assume YAGNI until someone needs it
// dump.Resources = append(dump.Resources, instanceDetails)
return nil
}
// getDumpState gets the dumpState from the dump context, or creates one if not yet initialized
func getDumpState(dumpContext *resources.DumpOperation) *dumpState {
if dumpContext.CloudState == nil {
dumpContext.CloudState = &dumpState{
cloud: dumpContext.Cloud.(gce.GCECloud),
}
}
return dumpContext.CloudState.(*dumpState)
}
// getInstances retrieves the list of instances from the cloud, using a cached copy if possible
func (s *dumpState) getInstances(ctx context.Context, zone string) (map[string]*compute.Instance, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.instances == nil {
s.instances = make(map[string]map[string]*compute.Instance)
}
if s.instances[zone] != nil {
return s.instances[zone], nil
}
l, err := s.cloud.Compute().Instances().List(ctx, s.cloud.Project(), zone)
if err != nil {
return nil, err
}
instances := make(map[string]*compute.Instance)
for _, i := range l {
instances[i.Name] = i
}
s.instances[zone] = instances
return instances, nil
}