-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
server_group.go
187 lines (159 loc) · 5.7 KB
/
server_group.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
/*
Copyright 2019 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 openstack
import (
"fmt"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/util/pkg/vfs"
)
func (c *openstackCloud) CreateServerGroup(opt servergroups.CreateOptsBuilder) (*servergroups.ServerGroup, error) {
return createServerGroup(c, opt)
}
func createServerGroup(c OpenstackCloud, opt servergroups.CreateOptsBuilder) (*servergroups.ServerGroup, error) {
var i *servergroups.ServerGroup
done, err := vfs.RetryWithBackoff(writeBackoff, func() (bool, error) {
v, err := servergroups.Create(c.ComputeClient(), opt).Extract()
if err != nil {
return false, fmt.Errorf("error creating server group: %v", err)
}
i = v
return true, nil
})
if err != nil {
return i, err
} else if done {
return i, nil
} else {
return i, wait.ErrWaitTimeout
}
}
func (c *openstackCloud) ListServerGroups(opts servergroups.ListOptsBuilder) ([]servergroups.ServerGroup, error) {
return listServerGroups(c, opts)
}
func listServerGroups(c OpenstackCloud, opts servergroups.ListOptsBuilder) ([]servergroups.ServerGroup, error) {
var sgs []servergroups.ServerGroup
done, err := vfs.RetryWithBackoff(readBackoff, func() (bool, error) {
allPages, err := servergroups.List(c.ComputeClient(), opts).AllPages()
if err != nil {
return false, fmt.Errorf("error listing server groups: %v", err)
}
r, err := servergroups.ExtractServerGroups(allPages)
if err != nil {
return false, fmt.Errorf("error extracting server groups from pages: %v", err)
}
sgs = r
return true, nil
})
if err != nil {
return sgs, err
} else if done {
return sgs, nil
} else {
return sgs, wait.ErrWaitTimeout
}
}
// matchInstanceGroup filters a list of instancegroups for recognized cloud groups
func matchInstanceGroup(name string, clusterName string, instancegroups []*kops.InstanceGroup) (*kops.InstanceGroup, error) {
var instancegroup *kops.InstanceGroup
for _, g := range instancegroups {
var groupName string
switch g.Spec.Role {
case kops.InstanceGroupRoleMaster, kops.InstanceGroupRoleNode, kops.InstanceGroupRoleBastion:
groupName = clusterName + "-" + g.ObjectMeta.Name
default:
klog.Warningf("Ignoring InstanceGroup of unknown role %q", g.Spec.Role)
continue
}
if name == groupName {
if instancegroup != nil {
return nil, fmt.Errorf("found multiple instance groups matching servergrp %q", groupName)
}
instancegroup = g
}
}
return instancegroup, nil
}
func osBuildCloudInstanceGroup(c OpenstackCloud, cluster *kops.Cluster, ig *kops.InstanceGroup, g *servergroups.ServerGroup, nodeMap map[string]*v1.Node) (*cloudinstances.CloudInstanceGroup, error) {
newLaunchConfigName := g.Name
cg := &cloudinstances.CloudInstanceGroup{
HumanName: newLaunchConfigName,
InstanceGroup: ig,
MinSize: int(fi.Int32Value(ig.Spec.MinSize)),
TargetSize: int(fi.Int32Value(ig.Spec.MinSize)), // TODO: Retrieve the target size from OpenStack?
MaxSize: int(fi.Int32Value(ig.Spec.MaxSize)),
Raw: g,
}
for _, i := range g.Members {
instanceId := i
if instanceId == "" {
klog.Warningf("ignoring instance with no instance id: %s", i)
continue
}
server, err := servers.Get(c.ComputeClient(), instanceId).Extract()
if err != nil {
return nil, fmt.Errorf("Failed to get instance group member: %v", err)
}
igObservedGeneration := server.Metadata[INSTANCE_GROUP_GENERATION]
clusterObservedGeneration := server.Metadata[CLUSTER_GENERATION]
observedName := fmt.Sprintf("%s-%s", clusterObservedGeneration, igObservedGeneration)
generationName := fmt.Sprintf("%d-%d", cluster.GetGeneration(), ig.Generation)
status := cloudinstances.CloudInstanceStatusUpToDate
if generationName != observedName {
status = cloudinstances.CloudInstanceStatusNeedsUpdate
}
cm, err := cg.NewCloudInstance(instanceId, status, nodeMap[instanceId])
if err != nil {
return nil, fmt.Errorf("error creating cloud instance group member: %v", err)
}
if server.Flavor["original_name"] != nil {
cm.MachineType = server.Flavor["original_name"].(string)
}
ip, err := GetServerFixedIP(server, server.Metadata[TagKopsNetwork])
if err != nil {
return nil, fmt.Errorf("error creating cloud instance group member: %v", err)
}
cm.PrivateIP = ip
cm.Roles = []string{server.Metadata["KopsRole"]}
cm.State = cloudinstances.State(server.Status)
}
return cg, nil
}
func (c *openstackCloud) DeleteServerGroup(groupID string) error {
return deleteServerGroup(c, groupID)
}
func deleteServerGroup(c OpenstackCloud, groupID string) error {
done, err := vfs.RetryWithBackoff(deleteBackoff, func() (bool, error) {
err := servergroups.Delete(c.ComputeClient(), groupID).ExtractErr()
if err != nil && !isNotFound(err) {
return false, fmt.Errorf("error deleting server group: %v", err)
}
if isNotFound(err) {
return true, nil
}
return false, nil
})
if err != nil {
return err
} else if done {
return nil
} else {
return wait.ErrWaitTimeout
}
}