/
packagerevisions_interface.go
277 lines (245 loc) · 8.96 KB
/
packagerevisions_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Copyright 2023 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 (
"fmt"
"regexp"
"strings"
"github.com/henderiw/apiserver-builder/pkg/builder/resource"
"github.com/kform-dev/pkg-server/apis/condition"
"github.com/kform-dev/pkg-server/apis/pkgid"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
const PackageRevisionPlural = "packagerevisions"
const PackageRevisionSingular = "packagerevision"
var _ resource.Object = &PackageRevision{}
var _ resource.ObjectList = &PackageRevisionList{}
var _ resource.ObjectWithStatusSubResource = &PackageRevision{}
func (PackageRevisionStatus) SubResourceName() string {
return fmt.Sprintf("%s/%s", PackageRevisionPlural, "status")
}
func (r PackageRevisionStatus) CopyTo(obj resource.ObjectWithStatusSubResource) {
pkgRev, ok := obj.(*PackageRevision)
if ok {
pkgRev.Status = r
}
}
func (r *PackageRevision) GetStatus() resource.StatusSubResource {
return r.Status
}
func (r *PackageRevision) GetSingularName() string {
return PackageRevisionSingular
}
func (PackageRevision) GetGroupVersionResource() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: SchemeGroupVersion.Group,
Version: SchemeGroupVersion.Version,
Resource: PackageRevisionPlural,
}
}
// IsStorageVersion returns true -- v1alpha1.Config is used as the internal version.
// IsStorageVersion implements resource.Object.
func (PackageRevision) IsStorageVersion() bool {
return true
}
// GetObjectMeta implements resource.Object
func (r *PackageRevision) GetObjectMeta() *metav1.ObjectMeta {
return &r.ObjectMeta
}
// NamespaceScoped returns true to indicate Fortune is a namespaced resource.
// NamespaceScoped implements resource.Object.
func (PackageRevision) NamespaceScoped() bool {
return true
}
// New implements resource.Object
func (PackageRevision) New() runtime.Object {
return &PackageRevision{}
}
// NewList implements resource.Object
func (PackageRevision) NewList() runtime.Object {
return &PackageRevisionList{}
}
// GetCondition returns the condition based on the condition kind
func (r *PackageRevision) GetCondition(t condition.ConditionType) condition.Condition {
return r.Status.GetCondition(t)
}
// HasCondition returns the if the condition is set
func (r *PackageRevision) HasCondition(t condition.ConditionType) bool {
return r.Status.HasCondition(t)
}
// SetConditions sets the conditions on the resource. it allows for 0, 1 or more conditions
// to be set at once
func (r *PackageRevision) SetConditions(c ...condition.Condition) {
r.Status.SetConditions(c...)
}
func (r *PackageRevision) ValidateRepository() error {
repoName := pkgid.GetRepoNameFromPkgRevName(r.GetName())
if repoName != r.Spec.PackageID.Repository {
return fmt.Errorf("the name of the %s, must start with the repo name as it is used in lookups for %s, got name: %s, spec: %s",
PackageRevisionSingular,
PackageRevisionResourcesSingular,
repoName,
r.Spec.PackageID.Repository,
)
}
return nil
}
func (r *PackageRevision) ValidateDiscoveryAnnotation() error {
a := r.GetAnnotations()
err := fmt.Errorf("discovery annotation not present")
if len(a) == 0 {
return err
}
if _, ok := a[DiscoveredPkgRevKey]; !ok {
return err
}
return nil
}
// GetListMeta returns the ListMeta
func (r *PackageRevisionList) GetListMeta() *metav1.ListMeta {
return &r.ListMeta
}
// BuildPackageRevision returns an PackageRevision from a client Object a Spec/Status
func BuildPackageRevision(meta metav1.ObjectMeta, spec PackageRevisionSpec, status PackageRevisionStatus) *PackageRevision {
return &PackageRevision{
TypeMeta: metav1.TypeMeta{
APIVersion: SchemeGroupVersion.Identifier(),
Kind: PackageRevisionKind,
},
ObjectMeta: meta,
Spec: spec,
Status: status,
}
}
// ValidateWorkspaceName validates WorkspaceName. It must:
// - be at least 1 and at most 63 characters long
// - contain only lowercase alphanumeric characters or '-'
// - start and end with an alphanumeric character.
//
// '/ ' should never be allowed, because we use '/' to
// delimit branch names (e.g. the 'drafts/' prefix).
func ValidateWorkspaceName(workspace string) error {
wn := string(workspace)
if len(wn) > 63 || len(wn) == 0 {
return fmt.Errorf("workspaceName %q must be at least 1 and at most 63 characters long", wn)
}
if strings.HasPrefix(wn, "-") || strings.HasSuffix(wn, "-") {
return fmt.Errorf("workspaceName %q must start and end with an alphanumeric character", wn)
}
match, err := regexp.MatchString(`^[a-z0-9-]+$`, wn)
if err != nil {
return err
}
if !match {
return fmt.Errorf("workspaceName %q must be comprised only of lowercase alphanumeric characters and '-'", wn)
}
return nil
}
func ValidateUpdateLifeCycle(newPkgRev, oldPkgRev *PackageRevision) error {
// validate lifecycle
switch oldPkgRev.Spec.Lifecycle {
case PackageRevisionLifecycleDraft:
switch newPkgRev.Spec.Lifecycle {
case PackageRevisionLifecycleDraft:
case PackageRevisionLifecycleProposed:
case PackageRevisionLifecycleDeletionProposed:
case PackageRevisionLifecyclePublished:
return fmt.Errorf("unsupported lifecycle transition %s -> %s", oldPkgRev.Spec.Lifecycle, newPkgRev.Spec.Lifecycle)
default:
return fmt.Errorf("unsupported lifecycle %s", newPkgRev.Spec.Lifecycle)
}
case PackageRevisionLifecycleProposed:
switch newPkgRev.Spec.Lifecycle {
case PackageRevisionLifecycleDraft:
case PackageRevisionLifecycleProposed:
case PackageRevisionLifecycleDeletionProposed:
case PackageRevisionLifecyclePublished:
default:
return fmt.Errorf("unsupported lifecycle %s", newPkgRev.Spec.Lifecycle)
}
case PackageRevisionLifecyclePublished:
switch newPkgRev.Spec.Lifecycle {
case PackageRevisionLifecycleDraft:
return fmt.Errorf("unsupported lifecycle transition %s -> %s", oldPkgRev.Spec.Lifecycle, newPkgRev.Spec.Lifecycle)
case PackageRevisionLifecycleProposed:
return fmt.Errorf("unsupported lifecycle transition %s -> %s", oldPkgRev.Spec.Lifecycle, newPkgRev.Spec.Lifecycle)
case PackageRevisionLifecycleDeletionProposed:
case PackageRevisionLifecyclePublished:
default:
return fmt.Errorf("unsupported lifecycle %s", newPkgRev.Spec.Lifecycle)
}
case PackageRevisionLifecycleDeletionProposed:
switch newPkgRev.Spec.Lifecycle {
case PackageRevisionLifecycleDraft:
return fmt.Errorf("unsupported lifecycle transition %s -> %s", oldPkgRev.Spec.Lifecycle, newPkgRev.Spec.Lifecycle)
case PackageRevisionLifecycleProposed:
return fmt.Errorf("unsupported lifecycle transition %s -> %s", oldPkgRev.Spec.Lifecycle, newPkgRev.Spec.Lifecycle)
case PackageRevisionLifecycleDeletionProposed:
case PackageRevisionLifecyclePublished:
default:
return fmt.Errorf("unsupported lifecycle %s", newPkgRev.Spec.Lifecycle)
}
default:
return fmt.Errorf("unsupported lifecycle %s", newPkgRev.Spec.Lifecycle)
}
return nil
}
// ConvertPackageRevisionsFieldSelector is the schema conversion function for normalizing the FieldSelector for PackageRevisions
func ConvertPackageRevisionsFieldSelector(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.packageID.target", "spec.packageID.repository", "spec.packageID.realm", "spec.packageID.package", "spec.packageID.workspace", "spec.packageID.revision", "spec.lifecycle":
return label, value, nil
default:
return "", "", fmt.Errorf("%q is not a known field selector", label)
}
}
func (r *PackageRevision) HasReadinessGate(cType condition.ConditionType) bool {
for _, readinessGate := range r.Spec.ReadinessGates {
if readinessGate.ConditionType == cType {
return true
}
}
return false
}
func (r *PackageRevision) NextReadinessGate(cType condition.ConditionType) condition.ConditionType {
idx := 0
for i, readinessGate := range r.Spec.ReadinessGates {
if readinessGate.ConditionType == cType {
idx = i
break
}
}
if idx < len(r.Spec.ReadinessGates)-1 {
return r.Spec.ReadinessGates[idx+1].ConditionType
}
return condition.ConditionTypeEnd
}
func (r *PackageRevision) GetPreviousCondition(cType condition.ConditionType) condition.Condition {
for i, readinessGate := range r.Spec.ReadinessGates {
if readinessGate.ConditionType == cType {
if i == 0 {
return condition.ConditionTrue()
} else {
return r.GetCondition(r.Spec.ReadinessGates[i-1].ConditionType)
}
}
}
// if this condition was not found we return false
return condition.ConditionFalse()
}