-
Notifications
You must be signed in to change notification settings - Fork 748
/
eniconfig.go
227 lines (190 loc) · 6.67 KB
/
eniconfig.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 eniconfig handles eniconfig CRD
package eniconfig
import (
"context"
"os"
"runtime"
"sync"
"time"
"github.com/operator-framework/operator-sdk/pkg/sdk"
"github.com/pkg/errors"
"github.com/aws/amazon-vpc-cni-k8s/pkg/apis/crd/v1alpha1"
corev1 "k8s.io/api/core/v1"
"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
sdkVersion "github.com/operator-framework/operator-sdk/version"
log "github.com/cihub/seelog"
)
const (
defaultEniConfigAnnotationDef = "k8s.amazonaws.com/eniConfig"
defaultEniConfigLabelDef = "k8s.amazonaws.com/eniConfig"
eniConfigDefault = "default"
// when "ENI_CONFIG_LABEL_DEF is defined, ENIConfigController will use that label key to
// search if is setting value for eniConfigLabelDef
// Example:
// Node has set label k8s.amazonaws.com/eniConfigCustom=customConfig
// We can get that value in controller by setting environmental variable ENI_CONFIG_LABEL_DEF
// ENI_CONFIG_LABEL_DEF=k8s.amazonaws.com/eniConfigOverride
// This will set eniConfigLabelDef to eniConfigOverride
envEniConfigAnnotationDef = "ENI_CONFIG_ANNOTATION_DEF"
envEniConfigLabelDef = "ENI_CONFIG_LABEL_DEF"
)
type ENIConfig interface {
MyENIConfig() (*v1alpha1.ENIConfigSpec, error)
Getter() *ENIConfigInfo
}
var ErrNoENIConfig = errors.New("eniconfig: eniconfig is not available")
// ENIConfigController defines global context for ENIConfig controller
type ENIConfigController struct {
eni map[string]*v1alpha1.ENIConfigSpec
myENI string
eniLock sync.RWMutex
myNodeName string
eniConfigAnnotationDef string
eniConfigLabelDef string
}
// ENIConfigInfo returns locally cached ENIConfigs
type ENIConfigInfo struct {
ENI map[string]v1alpha1.ENIConfigSpec
MyENI string
EniConfigAnnotationDef string
EniConfigLabelDef string
}
// NewENIConfigController creates a new ENIConfig controller
func NewENIConfigController() *ENIConfigController {
return &ENIConfigController{
myNodeName: os.Getenv("MY_NODE_NAME"),
eni: make(map[string]*v1alpha1.ENIConfigSpec),
myENI: eniConfigDefault,
eniConfigAnnotationDef: getEniConfigAnnotationDef(),
eniConfigLabelDef: getEniConfigLabelDef(),
}
}
// NewHandler creates a new handler for sdk
func NewHandler(controller *ENIConfigController) sdk.Handler {
return &Handler{controller: controller}
}
// Handler stores the ENIConfigController
type Handler struct {
controller *ENIConfigController
}
// Handle handles ENIConfig updates from API Server and store them in local cache
func (h *Handler) Handle(ctx context.Context, event sdk.Event) error {
switch o := event.Object.(type) {
case *v1alpha1.ENIConfig:
eniConfigName := o.GetName()
if event.Deleted {
log.Infof("Deleting ENIConfig: %s", eniConfigName)
h.controller.eniLock.Lock()
defer h.controller.eniLock.Unlock()
delete(h.controller.eni, eniConfigName)
return nil
}
curENIConfig := o.DeepCopy()
log.Infof("Handle ENIConfig Add/Update: %s, %v, %s", eniConfigName, curENIConfig.Spec.SecurityGroups, curENIConfig.Spec.Subnet)
h.controller.eniLock.Lock()
defer h.controller.eniLock.Unlock()
h.controller.eni[eniConfigName] = &curENIConfig.Spec
case *corev1.Node:
log.Infof("Handle corev1.Node: %s, %v, %v", o.GetName(), o.GetAnnotations(), o.GetLabels())
// Get annotations if not found get labels if not found fallback use default
if h.controller.myNodeName == o.GetName() {
val, ok := o.GetAnnotations()[h.controller.eniConfigAnnotationDef]
if !ok {
val, ok = o.GetLabels()[h.controller.eniConfigLabelDef]
if !ok {
val = eniConfigDefault
}
}
h.controller.eniLock.Lock()
defer h.controller.eniLock.Unlock()
h.controller.myENI = val
log.Infof("Setting myENI to: %s", val)
}
}
return nil
}
func printVersion() {
log.Infof("Go Version: %s", runtime.Version())
log.Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
log.Infof("operator-sdk Version: %v", sdkVersion.Version)
}
// Start kicks off ENIConfig controller
func (eniCfg *ENIConfigController) Start() {
printVersion()
sdk.ExposeMetricsPort()
resource := "crd.k8s.amazonaws.com/v1alpha1"
kind := "ENIConfig"
namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
log.Errorf("failed to get watch namespace: %v", err)
}
resyncPeriod := time.Second * 5
log.Infof("Watching %s, %s, %s, every %d s", resource, kind, namespace, resyncPeriod.Seconds())
sdk.Watch(resource, kind, namespace, resyncPeriod)
sdk.Watch("/v1", "Node", corev1.NamespaceAll, resyncPeriod)
sdk.Handle(NewHandler(eniCfg))
sdk.Run(context.TODO())
}
func (eniCfg *ENIConfigController) Getter() *ENIConfigInfo {
output := &ENIConfigInfo{
ENI: make(map[string]v1alpha1.ENIConfigSpec),
}
eniCfg.eniLock.Lock()
defer eniCfg.eniLock.Unlock()
output.MyENI = eniCfg.myENI
output.EniConfigAnnotationDef = getEniConfigAnnotationDef()
output.EniConfigLabelDef = getEniConfigLabelDef()
for name, val := range eniCfg.eni {
output.ENI[name] = *val
}
return output
}
// MyENIConfig returns the security
func (eniCfg *ENIConfigController) MyENIConfig() (*v1alpha1.ENIConfigSpec, error) {
eniCfg.eniLock.Lock()
defer eniCfg.eniLock.Unlock()
myENIConfig, ok := eniCfg.eni[eniCfg.myENI]
if ok {
return &v1alpha1.ENIConfigSpec{
SecurityGroups: myENIConfig.SecurityGroups,
Subnet: myENIConfig.Subnet,
}, nil
}
return nil, ErrNoENIConfig
}
// getEniConfigAnnotationDef returns eniConfigAnnotation
func getEniConfigAnnotationDef() string {
inputStr, found := os.LookupEnv(envEniConfigAnnotationDef)
if !found {
return defaultEniConfigAnnotationDef
}
if len(inputStr) > 0 {
log.Debugf("Using ENI_CONFIG_ANNOTATION_DEF %v", inputStr)
return inputStr
}
return defaultEniConfigAnnotationDef
}
// getEniConfigLabelDef returns eniConfigLabel name
func getEniConfigLabelDef() string {
inputStr, found := os.LookupEnv(envEniConfigLabelDef)
if !found {
return defaultEniConfigLabelDef
}
if len(inputStr) > 0 {
log.Debugf("Using ENI_CONFIG_LABEL_DEF %v", inputStr)
return inputStr
}
return defaultEniConfigLabelDef
}