forked from kubernetes/kops
/
resources.go
193 lines (157 loc) · 5.43 KB
/
resources.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright 2016 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 spotinst
import (
"context"
"fmt"
"strings"
"time"
"github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/pkg/resources"
"k8s.io/kops/upup/pkg/fi"
)
// ListGroups returns a list of all Elastigroups as Resource objects.
func ListGroups(svc Service, clusterName string) ([]*resources.Resource, error) {
glog.V(2).Info("Listing all Elastigroups")
groups, err := svc.List(context.Background())
if err != nil {
return nil, err
}
var resourceTrackers []*resources.Resource
for _, group := range groups {
if strings.HasSuffix(group.Name(), clusterName) {
resource := &resources.Resource{
ID: group.Id(),
Name: group.Name(),
Obj: group,
Deleter: deleter(svc, group),
Dumper: dumper,
}
resourceTrackers = append(resourceTrackers, resource)
}
}
return resourceTrackers, nil
}
// DeleteGroup deletes an existing Elastigroup.
func DeleteGroup(svc Service, group *cloudinstances.CloudInstanceGroup) error {
glog.V(2).Infof("Deleting Elastigroup %q", group.HumanName)
return svc.Delete(
context.Background(),
group.Raw.(Elastigroup).Id())
}
// DeleteInstance removes an instance from its Elastigroup.
func DeleteInstance(svc Service, instance *cloudinstances.CloudInstanceGroupMember) error {
glog.V(2).Infof("Detaching instance %q from Elastigroup", instance.ID)
return svc.Detach(
context.Background(),
instance.CloudInstanceGroup.Raw.(Elastigroup).Id(),
[]string{instance.ID})
}
// GetCloudGroups returns a list of Elastigroups as CloudInstanceGroup objects.
func GetCloudGroups(svc Service, cluster *kops.Cluster, instancegroups []*kops.InstanceGroup,
warnUnmatched bool, nodes []v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
glog.V(2).Info("Listing all Elastigroups")
groups, err := svc.List(context.Background())
if err != nil {
return nil, err
}
instanceGroups := make(map[string]*cloudinstances.CloudInstanceGroup)
nodeMap := cloudinstances.GetNodeMap(nodes, cluster)
for _, group := range groups {
// Find matching instance group.
var instancegroup *kops.InstanceGroup
for _, ig := range instancegroups {
name := getGroupNameByRole(cluster, ig)
if name == "" {
continue
}
if name == group.Name() {
if instancegroup != nil {
return nil, fmt.Errorf("spotinst: found multiple instance groups matching group %q", group.Name())
}
instancegroup = ig
}
}
if instancegroup == nil {
if warnUnmatched {
glog.V(2).Infof("Found group with no corresponding instance group %q", group.Name())
}
continue
}
// Build the instance group.
ig, err := buildCloudInstanceGroup(svc, instancegroup, group, nodeMap)
if err != nil {
return nil, fmt.Errorf("spotinst: failed to build instance group: %v", err)
}
instanceGroups[instancegroup.ObjectMeta.Name] = ig
}
return instanceGroups, nil
}
func getGroupNameByRole(cluster *kops.Cluster, ig *kops.InstanceGroup) string {
var groupName string
switch ig.Spec.Role {
case kops.InstanceGroupRoleMaster:
groupName = ig.ObjectMeta.Name + ".masters." + cluster.ObjectMeta.Name
case kops.InstanceGroupRoleNode:
groupName = ig.ObjectMeta.Name + "." + cluster.ObjectMeta.Name
case kops.InstanceGroupRoleBastion:
groupName = ig.ObjectMeta.Name + "." + cluster.ObjectMeta.Name
default:
glog.Warningf("Ignoring InstanceGroup of unknown role %q", ig.Spec.Role)
}
return groupName
}
func buildCloudInstanceGroup(svc Service, ig *kops.InstanceGroup, group Elastigroup,
nodeMap map[string]*v1.Node) (*cloudinstances.CloudInstanceGroup, error) {
instances, err := svc.Instances(context.Background(), group.Id())
if err != nil {
return nil, err
}
instanceGroup := &cloudinstances.CloudInstanceGroup{
HumanName: group.Name(),
InstanceGroup: ig,
MinSize: group.MinSize(),
MaxSize: group.MaxSize(),
Raw: group,
}
currentName := group.Name()
newName := fmt.Sprintf("%s:%d", group.Name(), time.Now().Nanosecond())
for _, instance := range instances {
if instance.Id() == "" {
glog.Warningf("Ignoring instance with no ID: %v", instance)
continue
}
if err := instanceGroup.NewCloudInstanceGroupMember(
instance.Id(), currentName, newName, nodeMap); err != nil {
return nil, fmt.Errorf("spotinst: error creating cloud instance group member: %v", err)
}
}
return instanceGroup, nil
}
func deleter(svc Service, group Elastigroup) func(fi.Cloud, *resources.Resource) error {
return func(cloud fi.Cloud, resource *resources.Resource) error {
glog.V(2).Infof("Deleting Elastigroup %q", group.Id())
return svc.Delete(context.Background(), group.Id())
}
}
func dumper(op *resources.DumpOperation, resource *resources.Resource) error {
data := make(map[string]interface{})
data["id"] = resource.ID
data["type"] = resource.Type
data["raw"] = resource.Obj
op.Dump.Resources = append(op.Dump.Resources, data)
return nil
}