-
Notifications
You must be signed in to change notification settings - Fork 0
/
extcommentry_interface.go
253 lines (213 loc) · 6.63 KB
/
extcommentry_interface.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
/*
Copyright 2024 Nokia.
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 v1alpha1
import (
"crypto/sha1"
"encoding/json"
"fmt"
strings "strings"
"github.com/henderiw/apiserver-builder/pkg/builder/resource"
"github.com/henderiw/store"
"github.com/kuidio/kuid/apis/backend"
commonv1alpha1 "github.com/kuidio/kuid/apis/common/v1alpha1"
conditionv1alpha1 "github.com/kuidio/kuid/apis/condition/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)
const EXTCOMMEntryPlural = "extcommentries"
const EXTCOMMEntrySingular = "extcommentry"
// +k8s:deepcopy-gen=false
var _ resource.Object = &EXTCOMMEntry{}
var _ resource.ObjectList = &EXTCOMMEntryList{}
// GetListMeta returns the ListMeta
func (r *EXTCOMMEntryList) GetListMeta() *metav1.ListMeta {
return &r.ListMeta
}
func (r *EXTCOMMEntryList) GetItems() []backend.Object {
entries := make([]backend.Object, 0, len(r.Items))
for _, entry := range r.Items {
entries = append(entries, &entry)
}
return entries
}
func (r *EXTCOMMEntry) GetSingularName() string {
return EXTCOMMEntrySingular
}
func (EXTCOMMEntry) GetGroupVersionResource() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: SchemeGroupVersion.Group,
Version: SchemeGroupVersion.Version,
Resource: EXTCOMMEntryPlural,
}
}
// IsStorageVersion returns true -- v1alpha1.Config is used as the internal version.
// IsStorageVersion implements resource.Object.
func (EXTCOMMEntry) IsStorageVersion() bool {
return true
}
// GetObjectMeta implements resource.Object
func (r *EXTCOMMEntry) GetObjectMeta() *metav1.ObjectMeta {
return &r.ObjectMeta
}
// NamespaceScoped returns true to indicate Fortune is a namespaced resource.
// NamespaceScoped implements resource.Object.
func (EXTCOMMEntry) NamespaceScoped() bool {
return true
}
// New implements resource.Object
func (EXTCOMMEntry) New() runtime.Object {
return &EXTCOMMEntry{}
}
// NewList implements resource.Object
func (EXTCOMMEntry) NewList() runtime.Object {
return &EXTCOMMEntryList{}
}
// GetCondition returns the condition based on the condition kind
func (r *EXTCOMMEntry) GetCondition(t conditionv1alpha1.ConditionType) conditionv1alpha1.Condition {
return r.Status.GetCondition(t)
}
// SetConditions sets the conditions on the resource. it allows for 0, 1 or more conditions
// to be set at once
func (r *EXTCOMMEntry) SetConditions(c ...conditionv1alpha1.Condition) {
r.Status.SetConditions(c...)
}
// EXTCOMMEntryConvertFieldSelector is the schema conversion function for normalizing the FieldSelector for EXTCOMMEntry
func EXTCOMMEntryConvertFieldSelector(label, value string) (internalLabel, internalValue string, err error) {
switch label {
case "metadata.name":
return label, value, nil
case "metadata.namespace":
return label, value, nil
case "spec.index":
return label, value, nil
default:
return "", "", fmt.Errorf("%q is not a known field selector", label)
}
}
func (r *EXTCOMMEntry) CalculateHash() ([sha1.Size]byte, error) {
// Convert the struct to JSON
jsonData, err := json.Marshal(r)
if err != nil {
return [sha1.Size]byte{}, err
}
// Calculate SHA-1 hash
return sha1.Sum(jsonData), nil
}
func (r *EXTCOMMEntry) GetNamespacedName() types.NamespacedName {
return types.NamespacedName{
Namespace: r.GetNamespace(),
Name: r.GetName(),
}
}
func (r *EXTCOMMEntry) GetKey() store.Key {
return store.KeyFromNSN(types.NamespacedName{Namespace: r.Namespace, Name: r.Spec.Index})
}
func (r *EXTCOMMEntry) GetIndex() string {
return r.Spec.Index
}
func (r *EXTCOMMEntry) GetOwnerReference() *commonv1alpha1.OwnerReference {
return r.Spec.Owner
}
func (r *EXTCOMMEntry) GetClaimName() string {
return r.Spec.Claim
}
func (r *EXTCOMMEntry) GetClaimType() backend.ClaimType {
return r.Spec.ClaimType
}
func (r *EXTCOMMEntry) GetOwnerGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: r.Spec.Owner.Group,
Version: r.Spec.Owner.Version,
Kind: r.Spec.Owner.Kind,
}
}
func (r *EXTCOMMEntry) GetOwnerNSN() types.NamespacedName {
return types.NamespacedName{
Namespace: r.Spec.Owner.Namespace,
Name: r.Spec.Owner.Name,
}
}
func (r *EXTCOMMEntry) GetSpec() any {
return r.Spec
}
func (r *EXTCOMMEntry) GetSpecID() string {
return r.Spec.ID
}
func (r *EXTCOMMEntry) SetSpec(s any) {
if spec, ok := s.(EXTCOMMEntrySpec); ok {
r.Spec = spec
}
}
func GetEXTCOMMEntry(k store.Key, vrange, id string, labels map[string]string) backend.EntryObject {
//log := log.FromContext(ctx)
index := k.Name
ns := k.Namespace
spec := &EXTCOMMEntrySpec{
Index: index,
ClaimType: backend.GetClaimTypeFromString(labels[backend.KuidClaimTypeKey]),
Claim: labels[backend.KuidClaimNameKey],
ID: id,
Owner: &commonv1alpha1.OwnerReference{
Group: labels[backend.KuidOwnerGroupKey],
Version: labels[backend.KuidOwnerVersionKey],
Kind: labels[backend.KuidOwnerKindKey],
Namespace: labels[backend.KuidOwnerNamespaceKey],
Name: labels[backend.KuidOwnerNameKey],
},
}
// filter the system defined labels from the labels to prepare for the user defined labels
udLabels := map[string]string{}
for k, v := range labels {
if !backend.BackendSystemKeys.Has(k) {
udLabels[k] = v
}
}
spec.UserDefinedLabels.Labels = udLabels
status := &EXTCOMMEntryStatus{}
status.SetConditions(conditionv1alpha1.Ready())
id = strings.ReplaceAll(id, "/", "-")
name := fmt.Sprintf("%s.%s", index, id)
if vrange != "" {
name = fmt.Sprintf("%s.%s", vrange, id)
}
return BuildEXTCOMMEntry(
metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
spec,
status,
)
}
// BuildEXTCOMMEntry returns a reource from a client Object a Spec/Status
func BuildEXTCOMMEntry(meta metav1.ObjectMeta, spec *EXTCOMMEntrySpec, status *EXTCOMMEntryStatus) *EXTCOMMEntry {
aspec := EXTCOMMEntrySpec{}
if spec != nil {
aspec = *spec
}
astatus := EXTCOMMEntryStatus{}
if status != nil {
astatus = *status
}
return &EXTCOMMEntry{
TypeMeta: metav1.TypeMeta{
APIVersion: SchemeGroupVersion.Identifier(),
Kind: EXTCOMMEntryKind,
},
ObjectMeta: meta,
Spec: aspec,
Status: astatus,
}
}