-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathcloud_instance_group.go
135 lines (115 loc) · 3.56 KB
/
cloud_instance_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
/*
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 cloudinstances
import (
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
kopsapi "k8s.io/kops/pkg/apis/kops"
)
// CloudInstanceGroup is the cloud backing of InstanceGroup.
type CloudInstanceGroup struct {
// HumanName is a user-friendly name for the group
HumanName string
InstanceGroup *kopsapi.InstanceGroup
Ready []*CloudInstance
NeedUpdate []*CloudInstance
MinSize int
TargetSize int
MaxSize int
// Raw allows for the implementer to attach an object, for tracking additional state
Raw interface{}
}
// NewCloudInstance creates a new CloudInstance
func (c *CloudInstanceGroup) NewCloudInstance(instanceId string, status string, node *v1.Node) (*CloudInstance, error) {
if instanceId == "" {
return nil, fmt.Errorf("instance id for cloud instance member cannot be empty")
}
cm := &CloudInstance{
ID: instanceId,
CloudInstanceGroup: c,
}
if status == CloudInstanceStatusUpToDate {
c.Ready = append(c.Ready, cm)
} else {
c.NeedUpdate = append(c.NeedUpdate, cm)
}
cm.Status = status
if node != nil {
cm.Node = node
} else {
klog.V(8).Infof("unable to find node for instance: %s", instanceId)
}
return cm, nil
}
// Status returns a human-readable Status indicating whether an update is needed
func (c *CloudInstanceGroup) Status() string {
if len(c.NeedUpdate) == 0 {
return "Ready"
}
return "NeedsUpdate"
}
func (group *CloudInstanceGroup) AdjustNeedUpdate() {
if group.Ready != nil {
var newReady []*CloudInstance
for _, member := range group.Ready {
makeNotReady := false
if member.Node != nil && member.Node.Annotations != nil {
if _, ok := member.Node.Annotations["kops.k8s.io/needs-update"]; ok {
makeNotReady = true
}
}
if makeNotReady {
group.NeedUpdate = append(group.NeedUpdate, member)
member.Status = CloudInstanceStatusNeedsUpdate
} else {
newReady = append(newReady, member)
}
}
group.Ready = newReady
}
}
// GetNodeMap returns a list of nodes keyed by their external id
func GetNodeMap(nodes []v1.Node, cluster *kopsapi.Cluster) map[string]*v1.Node {
nodeMap := make(map[string]*v1.Node)
if cluster.Spec.GetCloudProvider() == kopsapi.CloudProviderAzure {
for i := range nodes {
node := &nodes[i]
vmName, err := toAzureVMName(node.Spec.ProviderID)
if err != nil {
klog.Errorf("ignoring node %q with malformed provider ID: %s", node.Name, err)
continue
}
nodeMap[vmName] = node
}
return nodeMap
}
for i := range nodes {
node := &nodes[i]
providerIDs := strings.Split(node.Spec.ProviderID, "/")
instanceID := providerIDs[len(providerIDs)-1]
nodeMap[instanceID] = node
}
return nodeMap
}
// toAzureVMName returns a VM name from the resource path stored in the provider ID.
func toAzureVMName(providerID string) (string, error) {
l := strings.Split(providerID, "/")
if len(l) != 13 {
return "", fmt.Errorf("unexpected form of resource path: %q", providerID)
}
vmssName := l[10]
idx := l[12]
return fmt.Sprintf("%s_%s", vmssName, idx), nil
}