-
Notifications
You must be signed in to change notification settings - Fork 13
/
provider.go
338 lines (282 loc) · 9.98 KB
/
provider.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package aws
import (
"context"
"fmt"
"io"
"log"
"time"
"github.com/virtual-kubelet/aws-fargate/fargate"
"github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/node/api"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// FargateProvider implements the virtual-kubelet provider interface.
type FargateProvider struct {
resourceManager *manager.ResourceManager
nodeName string
operatingSystem string
internalIP string
daemonEndpointPort int32
// AWS resources.
region string
subnets []string
securityGroups []string
// Fargate resources.
cluster *fargate.Cluster
clusterName string
capacity capacity
assignPublicIPv4Address bool
executionRoleArn string
cloudWatchLogGroupName string
platformVersion string
lastTransitionTime time.Time
}
// Capacity represents the provisioned capacity on a Fargate cluster.
type capacity struct {
cpu string
memory string
storage string
pods string
}
var (
errNotImplemented = fmt.Errorf("not implemented by Fargate provider")
)
// NewFargateProvider creates a new Fargate provider.
func NewFargateProvider(
config string,
rm *manager.ResourceManager,
nodeName string,
operatingSystem string,
internalIP string,
daemonEndpointPort int32) (*FargateProvider, error) {
// Create the Fargate provider.
log.Println("Creating Fargate provider.")
p := FargateProvider{
resourceManager: rm,
nodeName: nodeName,
operatingSystem: operatingSystem,
internalIP: internalIP,
daemonEndpointPort: daemonEndpointPort,
}
// Read the Fargate provider configuration file.
err := p.loadConfigFile(config)
if err != nil {
err = fmt.Errorf("failed to load configuration file %s: %v", config, err)
return nil, err
}
log.Printf("Loaded provider configuration file %s.", config)
// Find or create the configured Fargate cluster.
clusterConfig := fargate.ClusterConfig{
Region: p.region,
Name: p.clusterName,
NodeName: nodeName,
Subnets: p.subnets,
SecurityGroups: p.securityGroups,
AssignPublicIPv4Address: p.assignPublicIPv4Address,
ExecutionRoleArn: p.executionRoleArn,
CloudWatchLogGroupName: p.cloudWatchLogGroupName,
PlatformVersion: p.platformVersion,
}
p.cluster, err = fargate.NewCluster(&clusterConfig)
if err != nil {
err = fmt.Errorf("failed to create Fargate cluster: %v", err)
return nil, err
}
p.lastTransitionTime = time.Now()
log.Printf("Created Fargate provider: %+v.", p)
return &p, nil
}
// CreatePod takes a Kubernetes Pod and deploys it within the Fargate provider.
func (p *FargateProvider) CreatePod(ctx context.Context, pod *corev1.Pod) error {
log.Printf("Received CreatePod request for %+v.\n", pod)
fgPod, err := fargate.NewPod(p.cluster, pod)
if err != nil {
log.Printf("Failed to create pod: %v.\n", err)
return err
}
err = fgPod.Start()
if err != nil {
log.Printf("Failed to start pod: %v.\n", err)
return err
}
return nil
}
// UpdatePod takes a Kubernetes Pod and updates it within the provider.
func (p *FargateProvider) UpdatePod(ctx context.Context, pod *corev1.Pod) error {
log.Printf("Received UpdatePod request for %s/%s.\n", pod.Namespace, pod.Name)
return errNotImplemented
}
// DeletePod takes a Kubernetes Pod and deletes it from the provider.
func (p *FargateProvider) DeletePod(ctx context.Context, pod *corev1.Pod) error {
log.Printf("Received DeletePod request for %s/%s.\n", pod.Namespace, pod.Name)
fgPod, err := p.cluster.GetPod(pod.Namespace, pod.Name)
if err != nil {
log.Printf("Failed to get pod: %v.\n", err)
return err
}
err = fgPod.Stop()
if err != nil {
log.Printf("Failed to stop pod: %v.\n", err)
return err
}
return nil
}
// GetPod retrieves a pod by name from the provider (can be cached).
func (p *FargateProvider) GetPod(ctx context.Context, namespace, name string) (*corev1.Pod, error) {
log.Printf("Received GetPod request for %s/%s.\n", namespace, name)
pod, err := p.cluster.GetPod(namespace, name)
if err != nil {
log.Printf("Failed to get pod: %v.\n", err)
return nil, err
}
spec, err := pod.GetSpec()
if err != nil {
log.Printf("Failed to get pod spec: %v.\n", err)
return nil, err
}
return spec, nil
}
// GetContainerLogs retrieves the logs of a container by name from the provider.
func (p *FargateProvider) GetContainerLogs(ctx context.Context, namespace, podName, containerName string, opts api.ContainerLogOpts) (io.ReadCloser, error) {
log.Printf("Received GetContainerLogs request for %s/%s/%s.\n", namespace, podName, containerName)
return p.cluster.GetContainerLogs(namespace, podName, containerName, opts)
}
// GetPodFullName retrieves the full pod name as defined in the provider context.
func (p *FargateProvider) GetPodFullName(namespace string, pod string) string {
return ""
}
// RunInContainer executes a command in a container in the pod, copying data
// between in/out/err and the container's stdin/stdout/stderr.
func (p *FargateProvider) RunInContainer(ctx context.Context, namespace, podName, containerName string, cmd []string, attach api.AttachIO) error {
return errNotImplemented
}
// GetPodStatus retrieves the status of a pod by name from the provider.
func (p *FargateProvider) GetPodStatus(ctx context.Context, namespace, name string) (*corev1.PodStatus, error) {
log.Printf("Received GetPodStatus request for %s/%s.\n", namespace, name)
pod, err := p.cluster.GetPod(namespace, name)
if err != nil {
log.Printf("Failed to get pod: %v.\n", err)
return nil, err
}
status := pod.GetStatus()
log.Printf("Responding to GetPodStatus: %+v.\n", status)
return &status, nil
}
// GetPods retrieves a list of all pods running on the provider (can be cached).
func (p *FargateProvider) GetPods(ctx context.Context) ([]*corev1.Pod, error) {
log.Println("Received GetPods request.")
pods, err := p.cluster.GetPods()
if err != nil {
log.Printf("Failed to get pods: %v.\n", err)
return nil, err
}
var result []*corev1.Pod
var podNames []string
for _, pod := range pods {
spec, err := pod.GetSpec()
if err != nil {
log.Printf("Failed to get pod spec: %v.\n", err)
continue
}
result = append(result, spec)
podNames = append(podNames, fmt.Sprintf("%s/%s", spec.Namespace, spec.Name))
}
log.Printf("Responding to GetPods: %+v.\n", podNames)
return result, nil
}
// Capacity returns a resource list with the capacity constraints of the provider.
func (p *FargateProvider) Capacity(ctx context.Context) corev1.ResourceList {
log.Println("Received Capacity request.")
return corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(p.capacity.cpu),
corev1.ResourceMemory: resource.MustParse(p.capacity.memory),
corev1.ResourceStorage: resource.MustParse(p.capacity.storage),
corev1.ResourcePods: resource.MustParse(p.capacity.pods),
}
}
// NodeConditions returns a list of conditions (Ready, OutOfDisk, etc), which is polled
// periodically to update the node status within Kubernetes.
func (p *FargateProvider) NodeConditions(ctx context.Context) []corev1.NodeCondition {
log.Println("Received NodeConditions request.")
lastHeartbeatTime := metav1.Now()
lastTransitionTime := metav1.NewTime(p.lastTransitionTime)
lastTransitionReason := "Fargate cluster is ready"
lastTransitionMessage := "ok"
// Return static thumbs-up values for all conditions.
return []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
LastHeartbeatTime: lastHeartbeatTime,
LastTransitionTime: lastTransitionTime,
Reason: lastTransitionReason,
Message: lastTransitionMessage,
},
{
Type: corev1.NodeOutOfDisk,
Status: corev1.ConditionFalse,
LastHeartbeatTime: lastHeartbeatTime,
LastTransitionTime: lastTransitionTime,
Reason: lastTransitionReason,
Message: lastTransitionMessage,
},
{
Type: corev1.NodeMemoryPressure,
Status: corev1.ConditionFalse,
LastHeartbeatTime: lastHeartbeatTime,
LastTransitionTime: lastTransitionTime,
Reason: lastTransitionReason,
Message: lastTransitionMessage,
},
{
Type: corev1.NodeDiskPressure,
Status: corev1.ConditionFalse,
LastHeartbeatTime: lastHeartbeatTime,
LastTransitionTime: lastTransitionTime,
Reason: lastTransitionReason,
Message: lastTransitionMessage,
},
{
Type: corev1.NodeNetworkUnavailable,
Status: corev1.ConditionFalse,
LastHeartbeatTime: lastHeartbeatTime,
LastTransitionTime: lastTransitionTime,
Reason: lastTransitionReason,
Message: lastTransitionMessage,
},
{
Type: "KubeletConfigOk",
Status: corev1.ConditionTrue,
LastHeartbeatTime: lastHeartbeatTime,
LastTransitionTime: lastTransitionTime,
Reason: lastTransitionReason,
Message: lastTransitionMessage,
},
}
}
// NodeAddresses returns a list of addresses for the node status within Kubernetes.
func (p *FargateProvider) NodeAddresses(ctx context.Context) []corev1.NodeAddress {
log.Println("Received NodeAddresses request.")
return []corev1.NodeAddress{
{
Type: corev1.NodeInternalIP,
Address: p.internalIP,
},
}
}
// NodeDaemonEndpoints returns NodeDaemonEndpoints for the node status within Kubernetes.
func (p *FargateProvider) NodeDaemonEndpoints(ctx context.Context) *corev1.NodeDaemonEndpoints {
log.Println("Received NodeDaemonEndpoints request.")
return &corev1.NodeDaemonEndpoints{
KubeletEndpoint: corev1.DaemonEndpoint{
Port: p.daemonEndpointPort,
},
}
}
// OperatingSystem returns the operating system the provider is for.
func (p *FargateProvider) OperatingSystem() string {
log.Println("Received OperatingSystem request.")
return p.operatingSystem
}