-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
nodespecer.go
214 lines (168 loc) · 4.93 KB
/
nodespecer.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package agent
import (
"context"
"errors"
"fmt"
"github.com/cilium/cilium/daemon/k8s"
"github.com/cilium/cilium/pkg/hive"
"github.com/cilium/cilium/pkg/hive/cell"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/k8s/resource"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/workerpool"
)
// nodeSpecer is an abstraction which allows us to mock/fake out the local node resource during testing.
type nodeSpecer interface {
Annotations() (map[string]string, error)
Labels() (map[string]string, error)
PodCIDRs() ([]string, error)
}
type localNodeStoreSpecerParams struct {
cell.In
Lifecycle hive.Lifecycle
Config *option.DaemonConfig
NodeResource k8s.LocalNodeResource
CiliumNodeResource k8s.LocalCiliumNodeResource
Signaler Signaler
}
// NewNodeSpecer constructs a new nodeSpecer and registers it in the hive lifecycle
func NewNodeSpecer(params localNodeStoreSpecerParams) (nodeSpecer, error) {
if !params.Config.BGPControlPlaneEnabled() {
return nil, nil
}
switch params.Config.IPAM {
case ipamOption.IPAMClusterPoolV2, ipamOption.IPAMClusterPool:
cns := &ciliumNodeSpecer{
nodeResource: params.CiliumNodeResource,
signaler: params.Signaler,
}
params.Lifecycle.Append(cns)
return cns, nil
case ipamOption.IPAMKubernetes:
kns := &kubernetesNodeSpecer{
nodeResource: params.NodeResource,
signaler: params.Signaler,
}
params.Lifecycle.Append(kns)
return kns, nil
default:
return nil, fmt.Errorf("BGP Control Plane doesn't support current IPAM-mode: '%s'", params.Config.IPAM)
}
}
type kubernetesNodeSpecer struct {
nodeResource k8s.LocalNodeResource
currentNode *slim_corev1.Node
workerpool *workerpool.WorkerPool
signaler Signaler
}
func (s *kubernetesNodeSpecer) Start(_ hive.HookContext) error {
s.workerpool = workerpool.New(1)
s.workerpool.Submit("kubernetes-node-specer-run", s.run)
return nil
}
func (s *kubernetesNodeSpecer) Stop(ctx hive.HookContext) error {
doneChan := make(chan struct{})
go func() {
s.workerpool.Close()
close(doneChan)
}()
select {
case <-doneChan:
case <-ctx.Done():
return ctx.Err()
}
return nil
}
func (s *kubernetesNodeSpecer) run(ctx context.Context) error {
for event := range s.nodeResource.Events(ctx) {
if event.Kind == resource.Upsert && event.Object != nil {
s.currentNode = event.Object
s.signaler.Event(struct{}{})
}
event.Done(nil)
}
return nil
}
func (s *kubernetesNodeSpecer) Annotations() (map[string]string, error) {
if s.currentNode == nil {
return nil, errors.New("annotations of current node not yet available")
}
return s.currentNode.Annotations, nil
}
func (s *kubernetesNodeSpecer) Labels() (map[string]string, error) {
if s.currentNode == nil {
return nil, errors.New("labels of current node not yet available")
}
return s.currentNode.Labels, nil
}
func (s *kubernetesNodeSpecer) PodCIDRs() ([]string, error) {
if s.currentNode == nil {
return nil, errors.New("pod ciders of current node not yet available")
}
if s.currentNode.Spec.PodCIDRs != nil {
return s.currentNode.Spec.PodCIDRs, nil
}
if s.currentNode.Spec.PodCIDR != "" {
return []string{s.currentNode.Spec.PodCIDR}, nil
}
return []string{}, nil
}
type ciliumNodeSpecer struct {
nodeResource k8s.LocalCiliumNodeResource
currentNode *ciliumv2.CiliumNode
workerpool *workerpool.WorkerPool
signaler Signaler
}
func (s *ciliumNodeSpecer) Start(_ hive.HookContext) error {
s.workerpool = workerpool.New(1)
s.workerpool.Submit("cilium-node-specer-run", s.run)
return nil
}
func (s *ciliumNodeSpecer) Stop(ctx hive.HookContext) error {
doneChan := make(chan struct{})
go func() {
s.workerpool.Close()
close(doneChan)
}()
select {
case <-doneChan:
case <-ctx.Done():
return ctx.Err()
}
return nil
}
func (s *ciliumNodeSpecer) run(ctx context.Context) error {
for event := range s.nodeResource.Events(ctx) {
if event.Kind == resource.Upsert && event.Object != nil {
s.currentNode = event.Object
s.signaler.Event(struct{}{})
}
event.Done(nil)
}
return nil
}
func (s *ciliumNodeSpecer) Annotations() (map[string]string, error) {
if s.currentNode == nil {
return nil, errors.New("annotations of current node not yet available")
}
return s.currentNode.Annotations, nil
}
func (s *ciliumNodeSpecer) Labels() (map[string]string, error) {
if s.currentNode == nil {
return nil, errors.New("labels of current node not yet available")
}
return s.currentNode.Labels, nil
}
func (s *ciliumNodeSpecer) PodCIDRs() ([]string, error) {
if s.currentNode == nil {
return nil, errors.New("pod ciders of current node not yet available")
}
if s.currentNode.Spec.IPAM.PodCIDRs != nil {
return s.currentNode.Spec.IPAM.PodCIDRs, nil
}
return []string{}, nil
}