-
Notifications
You must be signed in to change notification settings - Fork 136
/
sync_cluster_context.go
181 lines (145 loc) · 5.37 KB
/
sync_cluster_context.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
package fake
import (
"errors"
"fmt"
v1 "k8s.io/api/core/v1"
discovery "k8s.io/api/discovery/v1"
networking "k8s.io/api/networking/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/kubelet/pkg/apis/stats/v1alpha1"
"github.com/armadaproject/armada/internal/common/armadacontext"
"github.com/armadaproject/armada/internal/common/util"
"github.com/armadaproject/armada/internal/executor/domain"
)
type SyncFakeClusterContext struct {
Pods map[string]*v1.Pod
AnnotationsAdded map[string]map[string]string
podEventHandlers []*cache.ResourceEventHandlerFuncs
clusterEventHandlers []*cache.ResourceEventHandlerFuncs
}
func NewSyncFakeClusterContext() *SyncFakeClusterContext {
c := &SyncFakeClusterContext{Pods: map[string]*v1.Pod{}, AnnotationsAdded: map[string]map[string]string{}}
return c
}
func (*SyncFakeClusterContext) Stop() {}
func (c *SyncFakeClusterContext) AddPodEventHandler(handler cache.ResourceEventHandlerFuncs) {
c.podEventHandlers = append(c.podEventHandlers, &handler)
}
func (c *SyncFakeClusterContext) AddClusterEventEventHandler(handler cache.ResourceEventHandlerFuncs) {
c.clusterEventHandlers = append(c.clusterEventHandlers, &handler)
}
func (c *SyncFakeClusterContext) GetBatchPods() ([]*v1.Pod, error) {
pods := make([]*v1.Pod, 0, len(c.Pods))
for _, p := range c.Pods {
pods = append(pods, p.DeepCopy())
}
return pods, nil
}
func (c *SyncFakeClusterContext) GetAllPods() ([]*v1.Pod, error) {
return c.GetBatchPods()
}
func (c *SyncFakeClusterContext) GetActiveBatchPods() ([]*v1.Pod, error) {
return c.GetBatchPods()
}
func (c *SyncFakeClusterContext) GetNodes() ([]*v1.Node, error) {
return make([]*v1.Node, 0), nil
}
func (c *SyncFakeClusterContext) GetNode(nodeName string) (*v1.Node, error) {
return nil, errors.New("GetNode Not implemented")
}
func (c *SyncFakeClusterContext) GetPodEvents(pod *v1.Pod) ([]*v1.Event, error) {
return []*v1.Event{}, nil
}
func (c *SyncFakeClusterContext) SubmitService(service *v1.Service) (*v1.Service, error) {
return nil, fmt.Errorf("Services not implemented in SyncFakeClusterContext")
}
func (c *SyncFakeClusterContext) GetServices(pod *v1.Pod) ([]*v1.Service, error) {
return nil, fmt.Errorf("Services not implemented in SyncFakeClusterContext")
}
func (c *SyncFakeClusterContext) DeleteService(service *v1.Service) error {
return fmt.Errorf("Services not implemented in SyncFakeClusterContext")
}
func (c *SyncFakeClusterContext) SubmitIngress(ingress *networking.Ingress) (*networking.Ingress, error) {
return nil, fmt.Errorf("Ingresses not implemented in SyncFakeClusterContext")
}
func (c *SyncFakeClusterContext) GetIngresses(pod *v1.Pod) ([]*networking.Ingress, error) {
return nil, fmt.Errorf("Ingresses not implemented in SyncFakeClusterContext")
}
func (c *SyncFakeClusterContext) GetEndpointSlices(namespace string, labelName string, labelValue string) ([]*discovery.EndpointSlice, error) {
return nil, fmt.Errorf("EndpointSlices not implemented in SyncFakeClusterContext")
}
func (c *SyncFakeClusterContext) DeleteIngress(ingress *networking.Ingress) error {
return fmt.Errorf("Ingresses not implemented in SyncFakeClusterContext")
}
func (c *SyncFakeClusterContext) SubmitPod(pod *v1.Pod, owner string, ownerGroups []string) (*v1.Pod, error) {
c.Pods[pod.Labels[domain.JobId]] = pod
return pod, nil
}
func (c *SyncFakeClusterContext) AddAnnotation(pod *v1.Pod, annotations map[string]string) error {
pod.Annotations = util.MergeMaps(pod.Annotations, annotations)
if c.AnnotationsAdded[pod.Labels[domain.JobId]] == nil {
c.AnnotationsAdded[pod.Labels[domain.JobId]] = map[string]string{}
}
c.AnnotationsAdded[pod.Labels[domain.JobId]] = util.MergeMaps(c.AnnotationsAdded[pod.Labels[domain.JobId]], annotations)
return nil
}
func (c *SyncFakeClusterContext) AddClusterEventAnnotation(event *v1.Event, annotations map[string]string) error {
return nil
}
func (c *SyncFakeClusterContext) DeletePodWithCondition(pod *v1.Pod, condition func(pod *v1.Pod) bool, pessimistic bool) error {
if condition(pod) {
c.DeletePods([]*v1.Pod{pod})
}
return nil
}
func (c *SyncFakeClusterContext) DeletePods(pods []*v1.Pod) {
for _, p := range pods {
delete(c.Pods, p.Labels[domain.JobId])
}
}
func (c *SyncFakeClusterContext) GetClusterId() string {
return "cluster-id-1"
}
func (c *SyncFakeClusterContext) GetClusterPool() string {
return "pool"
}
func (c *SyncFakeClusterContext) GetNodeStatsSummary(ctx *armadacontext.Context, node *v1.Node) (*v1alpha1.Summary, error) {
return &v1alpha1.Summary{}, nil
}
func (c *SyncFakeClusterContext) SimulateDeletionEvent(pod *v1.Pod) {
for _, h := range c.podEventHandlers {
if h.DeleteFunc != nil {
h.DeleteFunc(pod)
}
}
}
func (c *SyncFakeClusterContext) SimulatePodAddEvent(pod *v1.Pod) {
for _, h := range c.podEventHandlers {
if h.AddFunc != nil {
h.AddFunc(pod)
}
}
}
func (c *SyncFakeClusterContext) SimulateClusterAddEvent(clusterEvent *v1.Event) {
for _, h := range c.clusterEventHandlers {
if h.AddFunc != nil {
h.AddFunc(clusterEvent)
}
}
}
type FakeClusterIdentity struct {
clusterId string
clusterPool string
}
func NewFakeClusterIdentity(clusterId string, clusterPool string) *FakeClusterIdentity {
return &FakeClusterIdentity{
clusterId: clusterId,
clusterPool: clusterPool,
}
}
func (f *FakeClusterIdentity) GetClusterId() string {
return f.clusterId
}
func (f *FakeClusterIdentity) GetClusterPool() string {
return f.clusterPool
}