-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathidentify.go
148 lines (123 loc) · 4.09 KB
/
identify.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
/*
Copyright 2020 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 azure
import (
"context"
"fmt"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
corev1 "k8s.io/api/core/v1"
expirationcache "k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/nodeidentity"
)
const (
// InstanceGroupNameTag is the key of the tag used to identify an
// instance group that VM ScaleSet belongs.
InstanceGroupNameTag = "kops.k8s.io_instancegroup"
// ClusterNodeTemplateLabel is the prefix used on node labels when copying to cloud tags.
ClusterNodeTemplateLabel = "k8s.io_cluster_node-template_label_"
// cacheTTL is the expiration time of nodeidentity.Info cache.
cacheTTL = 60 * time.Minute
)
type vmssGetter interface {
getVMScaleSet(ctx context.Context, vmssName string) (compute.VirtualMachineScaleSet, error)
}
var _ vmssGetter = &client{}
// nodeIdentifier identifies a node from Azure VM.
type nodeIdentifier struct {
vmssGetter vmssGetter
// cache is a cache of nodeidentity.Info
cache expirationcache.Store
// cacheEnabled indicates if caching should be used
cacheEnabled bool
}
var _ nodeidentity.Identifier = &nodeIdentifier{}
// New creates and returns a a node identifier for Nodes running on Azure.
func New(cacheNodeidentityInfo bool) (nodeidentity.Identifier, error) {
client, err := newClient()
if err != nil {
return nil, err
}
return &nodeIdentifier{
vmssGetter: client,
cache: expirationcache.NewTTLStore(stringKeyFunc, cacheTTL),
cacheEnabled: cacheNodeidentityInfo,
}, nil
}
// IdentifyNode queries Azure for the node identity information.
func (i *nodeIdentifier) IdentifyNode(ctx context.Context, node *corev1.Node) (*nodeidentity.Info, error) {
providerID := node.Spec.ProviderID
if providerID == "" {
return nil, fmt.Errorf("providerID was not set for node %s", node.Name)
}
vmssName, err := getVMSSNameFromProviderID(providerID)
if err != nil {
return nil, fmt.Errorf("error on extracting VM ScaleSet name: %s", err)
}
// If caching is enabled try pulling nodeidentity.Info from cache before
// doing a EC2 API call.
if i.cacheEnabled {
obj, exists, err := i.cache.GetByKey(vmssName)
if err != nil {
klog.Warningf("Nodeidentity info cache lookup failure: %v", err)
}
if exists {
return obj.(*nodeidentity.Info), nil
}
}
vmss, err := i.vmssGetter.getVMScaleSet(ctx, vmssName)
if err != nil {
return nil, fmt.Errorf("error on getting VM ScaleSet: %s", err)
}
labels := map[string]string{}
// TODO(kenji): Populate labels
info := &nodeidentity.Info{
InstanceID: vmssName,
Labels: labels,
}
for k, v := range vmss.Tags {
if !strings.HasPrefix(k, ClusterNodeTemplateLabel) {
continue
}
l := strings.SplitN(*v, "=", 2)
if len(l) <= 1 {
return nil, fmt.Errorf("malformed tag value %s", *v)
}
labels[l[0]] = l[1]
}
// If caching is enabled add the nodeidentity.Info to cache.
if i.cacheEnabled {
err = i.cache.Add(info)
if err != nil {
klog.Warningf("Failed to add node identity info to cache: %v", err)
}
}
return info, nil
}
// stringKeyFunc is a string as cache key function
func stringKeyFunc(obj interface{}) (string, error) {
key := obj.(*nodeidentity.Info).InstanceID
return key, nil
}
func getVMSSNameFromProviderID(providerID string) (string, error) {
if !strings.HasPrefix(providerID, "azure://") {
return "", fmt.Errorf("providerID %q not recognized", providerID)
}
l := strings.Split(strings.TrimPrefix(providerID, "azure://"), "/")
if len(l) != 11 {
return "", fmt.Errorf("unexpected format of providerID %q", providerID)
}
return l[len(l)-3], nil
}