generated from kubernetes/kubernetes-template-project
/
client.go
150 lines (126 loc) · 4.88 KB
/
client.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
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 testing
import (
"context"
"fmt"
"strings"
"sync"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/pkg/controller/core/indexer"
)
func NewFakeClient(objs ...client.Object) client.Client {
return NewClientBuilder().WithObjects(objs...).WithStatusSubresource(objs...).Build()
}
func NewFakeClientSSAAsSM(objs ...client.Object) client.Client {
return NewClientBuilder().WithObjects(objs...).WithStatusSubresource(objs...).WithInterceptorFuncs(interceptor.Funcs{SubResourcePatch: TreatSSAAsStrategicMerge}).Build()
}
func NewClientBuilder(addToSchemes ...func(s *runtime.Scheme) error) *fake.ClientBuilder {
scheme := runtime.NewScheme()
if err := clientgoscheme.AddToScheme(scheme); err != nil {
panic(err)
}
if err := kueue.AddToScheme(scheme); err != nil {
panic(err)
}
for i := range addToSchemes {
if err := addToSchemes[i](scheme); err != nil {
panic(err)
}
}
return fake.NewClientBuilder().WithScheme(scheme).
WithIndex(&kueue.LocalQueue{}, indexer.QueueClusterQueueKey, indexer.IndexQueueClusterQueue).
WithIndex(&kueue.Workload{}, indexer.WorkloadQueueKey, indexer.IndexWorkloadQueue).
WithIndex(&kueue.Workload{}, indexer.WorkloadClusterQueueKey, indexer.IndexWorkloadClusterQueue).
WithIndex(&kueue.Workload{}, indexer.OwnerReferenceUID, indexer.IndexOwnerUID)
}
type builderIndexer struct {
*fake.ClientBuilder
}
func (b *builderIndexer) IndexField(_ context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
b.ClientBuilder = b.ClientBuilder.WithIndex(obj, field, extractValue)
return nil
}
func AsIndexer(builder *fake.ClientBuilder) client.FieldIndexer {
return &builderIndexer{ClientBuilder: builder}
}
type EventRecord struct {
Key types.NamespacedName
EventType string
Reason string
Message string
// add annotations if ever needed
}
type EventRecorder struct {
lock sync.Mutex
RecordedEvents []EventRecord
}
func SortEvents(ei, ej EventRecord) bool {
if cmp := strings.Compare(ei.Key.String(), ej.Key.String()); cmp != 0 {
return cmp < 0
}
if cmp := strings.Compare(ei.EventType, ej.EventType); cmp != 0 {
return cmp < 0
}
if cmp := strings.Compare(ei.Reason, ej.Reason); cmp != 0 {
return cmp < 0
}
if cmp := strings.Compare(ei.Message, ej.Message); cmp != 0 {
return cmp < 0
}
return false
}
func (tr *EventRecorder) Event(object runtime.Object, eventType, reason, message string) {
tr.Eventf(object, eventType, reason, message)
}
func (tr *EventRecorder) Eventf(object runtime.Object, eventType, reason, messageFmt string, args ...interface{}) {
tr.AnnotatedEventf(object, nil, eventType, reason, messageFmt, args...)
}
func (tr *EventRecorder) AnnotatedEventf(targetObject runtime.Object, _ map[string]string, eventType, reason, messageFmt string, args ...interface{}) {
tr.lock.Lock()
defer tr.lock.Unlock()
key := types.NamespacedName{}
if cObj, isCObj := targetObject.(client.Object); isCObj {
key = client.ObjectKeyFromObject(cObj)
}
tr.RecordedEvents = append(tr.RecordedEvents, EventRecord{
Key: key,
EventType: eventType,
Reason: reason,
Message: fmt.Sprintf(messageFmt, args...),
})
}
type ssaPatchAsStrategicMerge struct {
client.Patch
}
func (*ssaPatchAsStrategicMerge) Type() types.PatchType {
return types.StrategicMergePatchType
}
func wrapSSAPatch(patch client.Patch) client.Patch {
if patch.Type() == types.ApplyPatchType {
return &ssaPatchAsStrategicMerge{Patch: patch}
}
return patch
}
// TreatSSAAsStrategicMerge - can be used as a SubResourcePatch interceptor function to treat SSA patches as StrategicMergePatchType.
// Note: By doing so the values set in the patch will be updated but the call will have no knowledge of FieldManagement when it
// comes to detecting conflicts between managers or removing fields that are missing from the patch.
func TreatSSAAsStrategicMerge(ctx context.Context, clnt client.Client, SubResourceName string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
return clnt.SubResource(SubResourceName).Patch(ctx, obj, wrapSSAPatch(patch), opts...)
}