/
manager.go
154 lines (130 loc) · 4.81 KB
/
manager.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
// Copyright 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 pod
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider/branch/trunk"
"github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/utils"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type Manager interface {
CreateAndWaitTillPodIsRunning(context context.Context, pod *v1.Pod) (*v1.Pod, error)
CreateAndWaitTillPodIsCompleted(context context.Context, pod *v1.Pod) (*v1.Pod, error)
DeleteAndWaitTillPodIsDeleted(context context.Context, pod *v1.Pod) error
GetENIDetailsFromPodAnnotation(podAnnotation map[string]string) ([]*trunk.ENIDetails, error)
GetPodsWithLabel(context context.Context, namespace string, labelKey string, labelValue string) ([]v1.Pod, error)
PatchPod(context context.Context, oldPod *v1.Pod, newPod *v1.Pod) error
}
type defaultManager struct {
k8sClient client.Client
}
func NewManager(k8sClient client.Client) Manager {
return &defaultManager{k8sClient: k8sClient}
}
func (d *defaultManager) CreateAndWaitTillPodIsRunning(context context.Context, pod *v1.Pod) (*v1.Pod, error) {
err := d.k8sClient.Create(context, pod)
if err != nil {
return nil, err
}
// Allow the cache to sync, without the interval cache may be stale and return an error
time.Sleep(utils.PollIntervalShort)
updatedPod := &v1.Pod{}
err = wait.PollImmediateUntil(utils.PollIntervalShort, func() (done bool, err error) {
err = d.k8sClient.Get(context, utils.NamespacedName(pod), updatedPod)
if err != nil {
return true, err
}
return isPodReady(updatedPod), nil
}, context.Done())
return updatedPod, err
}
func (d *defaultManager) CreateAndWaitTillPodIsCompleted(context context.Context, pod *v1.Pod) (*v1.Pod, error) {
err := d.k8sClient.Create(context, pod)
if err != nil {
return nil, err
}
// Allow the cache to sync, without the interval cache may be stale and return an error
time.Sleep(utils.PollIntervalShort)
updatedPod := &v1.Pod{}
err = wait.PollImmediateUntil(utils.PollIntervalShort, func() (done bool, err error) {
err = d.k8sClient.Get(context, utils.NamespacedName(pod), updatedPod)
if err != nil {
return true, err
}
if isPodCompleted(updatedPod) {
return true, nil
}
if isPodFailed(updatedPod) {
return true, fmt.Errorf("pod failed to start")
}
return false, nil
}, context.Done())
return updatedPod, err
}
func (d *defaultManager) GetPodsWithLabel(context context.Context, namespace string,
labelKey string, labelValue string) ([]v1.Pod, error) {
podList := &v1.PodList{}
err := d.k8sClient.List(context, podList, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{labelKey: labelValue}),
Namespace: namespace,
})
return podList.Items, err
}
func (d *defaultManager) DeleteAndWaitTillPodIsDeleted(context context.Context, pod *v1.Pod) error {
err := d.k8sClient.Delete(context, pod)
if err != nil {
return err
}
observedPod := &v1.Pod{}
return wait.PollImmediateUntil(utils.PollIntervalShort, func() (done bool, err error) {
err = d.k8sClient.Get(context, utils.NamespacedName(pod), observedPod)
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}, context.Done())
}
func (d *defaultManager) GetENIDetailsFromPodAnnotation(podAnnotation map[string]string) ([]*trunk.ENIDetails, error) {
branchDetails, hasAnnotation := podAnnotation[config.ResourceNamePodENI]
if !hasAnnotation {
return nil, fmt.Errorf("failed to find annotation on pod %v", podAnnotation)
}
eniDetails := []*trunk.ENIDetails{}
json.Unmarshal([]byte(branchDetails), &eniDetails)
return eniDetails, nil
}
func (d *defaultManager) PatchPod(context context.Context, oldPod *v1.Pod, newPod *v1.Pod) error {
return d.k8sClient.Patch(context, newPod, client.MergeFrom(oldPod))
}
func isPodReady(pod *v1.Pod) bool {
for _, condition := range pod.Status.Conditions {
if condition.Status == v1.ConditionTrue && condition.Type == v1.PodReady {
return true
}
}
return false
}
func isPodCompleted(pod *v1.Pod) bool {
return pod.Status.Phase == v1.PodSucceeded
}
func isPodFailed(pod *v1.Pod) bool {
return pod.Status.Phase == v1.PodFailed
}