This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
controller.go
148 lines (131 loc) · 4.05 KB
/
controller.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
package fake_kubelet
import (
"context"
"fmt"
"strings"
"text/template"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"
)
var (
startTime = time.Now().Format(time.RFC3339)
funcMap = template.FuncMap{
"Now": func() string {
return time.Now().Format(time.RFC3339)
},
"StartTime": func() string {
return startTime
},
"YAML": func(s interface{}, indent ...int) (string, error) {
d, err := yaml.Marshal(s)
if err != nil {
return "", err
}
data := string(d)
if len(indent) == 1 && indent[0] > 0 {
pad := strings.Repeat(" ", indent[0]*2)
data = strings.Replace("\n"+data, "\n", "\n"+pad, -1)
}
return data, nil
},
}
)
// Controller is a fake kubelet implementation that can be used to test
type Controller struct {
nodes *NodeController
pods *PodController
}
type Config struct {
ClientSet kubernetes.Interface
TakeOverAll bool
TakeOverLabelsSelector string
PodCustomStatusAnnotationSelector string
CIDR string
NodeIP string
Logger Logger
PodStatusTemplate string
NodeTemplate string
NodeInitializationTemplate string
NodeHeartbeatTemplate string
}
type Logger interface {
Printf(format string, v ...interface{})
}
// NewController creates a new fake kubelet controller
func NewController(conf Config) (*Controller, error) {
var nodeSelectorFunc func(node *corev1.Node) bool
var nodeLabelSelector string
if conf.TakeOverAll {
nodeSelectorFunc = func(node *corev1.Node) bool {
return true
}
} else if conf.TakeOverLabelsSelector != "" {
selector, err := labels.Parse(conf.TakeOverLabelsSelector)
if err != nil {
return nil, err
}
nodeSelectorFunc = func(node *corev1.Node) bool {
return selector.Matches(labels.Set(node.Labels))
}
nodeLabelSelector = selector.String()
}
var lockPodsOnNodeFunc func(ctx context.Context, nodeName string) error
nodes, err := NewNodeController(NodeControllerConfig{
ClientSet: conf.ClientSet,
NodeIP: conf.NodeIP,
NodeSelectorFunc: nodeSelectorFunc,
NodeLabelSelector: nodeLabelSelector,
LockPodsOnNodeFunc: func(nodeName string) error {
return lockPodsOnNodeFunc(context.Background(), nodeName)
},
NodeTemplate: conf.NodeTemplate,
NodeInitializationTemplate: conf.NodeInitializationTemplate,
NodeHeartbeatTemplate: conf.NodeHeartbeatTemplate,
NodeHeartbeatInterval: 30 * time.Second,
NodeHeartbeatParallelism: 16,
LockNodeParallelism: 16,
Logger: conf.Logger,
FuncMap: funcMap,
})
if err != nil {
return nil, fmt.Errorf("failed to create nodes controller: %v", err)
}
pods, err := NewPodController(PodControllerConfig{
ClientSet: conf.ClientSet,
NodeIP: conf.NodeIP,
CIDR: conf.CIDR,
PodCustomStatusAnnotationSelector: conf.PodCustomStatusAnnotationSelector,
PodStatusTemplate: conf.PodStatusTemplate,
LockPodParallelism: 16,
DeletePodParallelism: 16,
NodeHasFunc: nodes.Has, // just handle pods that are on nodes we have
Logger: conf.Logger,
FuncMap: funcMap,
})
if err != nil {
return nil, fmt.Errorf("failed to create pods controller: %v", err)
}
lockPodsOnNodeFunc = pods.LockPodsOnNode
n := &Controller{
pods: pods,
nodes: nodes,
}
return n, nil
}
func (c *Controller) Start(ctx context.Context) error {
err := c.pods.Start(ctx)
if err != nil {
return fmt.Errorf("failed to start pods controller: %v", err)
}
err = c.nodes.Start(ctx)
if err != nil {
return fmt.Errorf("failed to start nodes controller: %v", err)
}
return nil
}
func (c *Controller) CreateNode(ctx context.Context, nodeName string) error {
return c.nodes.CreateNode(ctx, nodeName)
}