/
helpers.go
337 lines (266 loc) · 10.3 KB
/
helpers.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Copyright 2022-2024 EscherCloud.
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 (
"errors"
"fmt"
"net"
"strings"
"time"
coreunikornv1 "github.com/eschercloudai/unikorn-core/pkg/apis/unikorn/v1alpha1"
"github.com/eschercloudai/unikorn-core/pkg/constants"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
)
var (
// ErrStatuscoreunikornv1.ConditionLookup is raised when a condition is not found in
// the resource status.
ErrStatusConditionLookup = errors.New("status condition not found")
// ErrMissingLabel is raised when an expected label is not present on
// a resource.
ErrMissingLabel = errors.New("expected label is missing")
// ErrApplicationLookup is raised when the named application is not
// present in an application bundle bundle.
ErrApplicationLookup = errors.New("failed to lookup an application")
)
// IPv4AddressSliceFromIPSlice is a simple converter from Go types
// to API types.
func IPv4AddressSliceFromIPSlice(in []net.IP) []IPv4Address {
out := make([]IPv4Address, len(in))
for i, ip := range in {
out[i] = IPv4Address{IP: ip}
}
return out
}
// Paused implements the ReconcilePauser interface.
func (c *Project) Paused() bool {
return c.Spec.Pause
}
// Paused implements the ReconcilePauser interface.
func (c *ControlPlane) Paused() bool {
return c.Spec.Pause
}
// Paused implements the ReconcilePauser interface.
func (c *KubernetesCluster) Paused() bool {
return c.Spec.Pause
}
// StatusConditionRead scans the status conditions for an existing condition whose type
// matches.
func (c *Project) StatusConditionRead(t coreunikornv1.ConditionType) (*coreunikornv1.Condition, error) {
return coreunikornv1.GetCondition(c.Status.Conditions, t)
}
// StatusConditionWrite either adds or updates a condition in the control plane status.
// If the condition, status and message match an existing condition the update is
// ignored.
func (c *Project) StatusConditionWrite(t coreunikornv1.ConditionType, status corev1.ConditionStatus, reason coreunikornv1.ConditionReason, message string) {
coreunikornv1.UpdateCondition(&c.Status.Conditions, t, status, reason, message)
}
// ResourceLabels generates a set of labels to uniquely identify the resource
// if it were to be placed in a single global namespace.
func (c *Project) ResourceLabels() (labels.Set, error) {
labels := labels.Set{
constants.KindLabel: constants.KindLabelValueProject,
constants.ProjectLabel: c.Name,
}
return labels, nil
}
// StatusConditionRead scans the status conditions for an existing condition whose type
// matches.
func (c *ControlPlane) StatusConditionRead(t coreunikornv1.ConditionType) (*coreunikornv1.Condition, error) {
return coreunikornv1.GetCondition(c.Status.Conditions, t)
}
// StatusConditionWrite either adds or updates a condition in the control plane status.
// If the condition, status and message match an existing condition the update is
// ignored.
func (c *ControlPlane) StatusConditionWrite(t coreunikornv1.ConditionType, status corev1.ConditionStatus, reason coreunikornv1.ConditionReason, message string) {
coreunikornv1.UpdateCondition(&c.Status.Conditions, t, status, reason, message)
}
// ResourceLabels generates a set of labels to uniquely identify the resource
// if it were to be placed in a single global namespace.
func (c *ControlPlane) ResourceLabels() (labels.Set, error) {
project, ok := c.Labels[constants.ProjectLabel]
if !ok {
return nil, ErrMissingLabel
}
labels := labels.Set{
constants.KindLabel: constants.KindLabelValueControlPlane,
constants.ProjectLabel: project,
constants.ControlPlaneLabel: c.Name,
}
return labels, nil
}
func (c ControlPlane) Entropy() []byte {
return []byte(c.UID)
}
func (c ControlPlane) UpgradeSpec() *ApplicationBundleAutoUpgradeSpec {
return c.Spec.ApplicationBundleAutoUpgrade
}
// StatusConditionRead scans the status conditions for an existing condition whose type
// matches.
func (c *KubernetesCluster) StatusConditionRead(t coreunikornv1.ConditionType) (*coreunikornv1.Condition, error) {
return coreunikornv1.GetCondition(c.Status.Conditions, t)
}
// StatusConditionWrite either adds or updates a condition in the cluster status.
// If the condition, status and message match an existing condition the update is
// ignored.
func (c *KubernetesCluster) StatusConditionWrite(t coreunikornv1.ConditionType, status corev1.ConditionStatus, reason coreunikornv1.ConditionReason, message string) {
coreunikornv1.UpdateCondition(&c.Status.Conditions, t, status, reason, message)
}
// ResourceLabels generates a set of labels to uniquely identify the resource
// if it were to be placed in a single global namespace.
func (c *KubernetesCluster) ResourceLabels() (labels.Set, error) {
project, ok := c.Labels[constants.ProjectLabel]
if !ok {
return nil, ErrMissingLabel
}
controlPlane, ok := c.Labels[constants.ControlPlaneLabel]
if !ok {
return nil, ErrMissingLabel
}
labels := labels.Set{
constants.KindLabel: constants.KindLabelValueKubernetesCluster,
constants.ProjectLabel: project,
constants.ControlPlaneLabel: controlPlane,
constants.KubernetesClusterLabel: c.Name,
}
return labels, nil
}
func (c KubernetesCluster) Entropy() []byte {
return []byte(c.UID)
}
func (c KubernetesCluster) UpgradeSpec() *ApplicationBundleAutoUpgradeSpec {
return c.Spec.ApplicationBundleAutoUpgrade
}
// AutoscalingEnabled indicates whether cluster autoscaling is enabled for the cluster.
func (c *KubernetesCluster) AutoscalingEnabled() bool {
return c.Spec.Features != nil && c.Spec.Features.Autoscaling != nil && *c.Spec.Features.Autoscaling
}
// IngressEnabled indicates whether an ingress controller is required.
func (c *KubernetesCluster) IngressEnabled() bool {
return c.Spec.Features != nil && c.Spec.Features.Ingress != nil && *c.Spec.Features.Ingress
}
// CertManagerEnabled indicates whether cert-manager is required.
func (c *KubernetesCluster) CertManagerEnabled() bool {
return c.Spec.Features != nil && c.Spec.Features.CertManager != nil && *c.Spec.Features.CertManager
}
// KubernetesDashboardEnabled indicates whether the Kubernetes dashboard is required.
func (c *KubernetesCluster) KubernetesDashboardEnabled() bool {
return c.Spec.Features != nil && c.Spec.Features.KubernetesDashboard != nil && *c.Spec.Features.KubernetesDashboard
}
// FileStorageEnabled indicates whether a POSIX file storage CSI is required.
func (c *KubernetesCluster) FileStorageEnabled() bool {
return c.Spec.Features != nil && c.Spec.Features.FileStorage != nil && *c.Spec.Features.FileStorage
}
// PrometheusEnabled indicates whether the Prometheus Operator is required.
func (c *KubernetesCluster) PrometheusEnabled() bool {
return c.Spec.Features != nil && c.Spec.Features.Prometheus != nil && *c.Spec.Features.Prometheus
}
// NvidiaOperatorEnabled indicates whether to install the Nvidia GPU operator.
func (c *KubernetesCluster) NvidiaOperatorEnabled() bool {
return c.Spec.Features != nil && c.Spec.Features.NvidiaOperator != nil && *c.Spec.Features.NvidiaOperator
}
func CompareControlPlane(a, b ControlPlane) int {
return strings.Compare(a.Name, b.Name)
}
func CompareKubernetesCluster(a, b KubernetesCluster) int {
return strings.Compare(a.Name, b.Name)
}
func CompareControlPlaneApplicationBundle(a, b ControlPlaneApplicationBundle) int {
// TODO: while this works now, it won't unless we parse and compare as
// a semantic version.
return strings.Compare(*a.Spec.Version, *b.Spec.Version)
}
func CompareKubernetesClusterApplicationBundle(a, b KubernetesClusterApplicationBundle) int {
// TODO: while this works now, it won't unless we parse and compare as
// a semantic version.
return strings.Compare(*a.Spec.Version, *b.Spec.Version)
}
// Get retrieves the named bundle.
func (l ControlPlaneApplicationBundleList) Get(name string) *ControlPlaneApplicationBundle {
for i := range l.Items {
if l.Items[i].Name == name {
return &l.Items[i]
}
}
return nil
}
func (l KubernetesClusterApplicationBundleList) Get(name string) *KubernetesClusterApplicationBundle {
for i := range l.Items {
if l.Items[i].Name == name {
return &l.Items[i]
}
}
return nil
}
// Upgradable returns a new list of bundles that are "stable" e.g. not end of life and
// not a preview.
func (l ControlPlaneApplicationBundleList) Upgradable() *ControlPlaneApplicationBundleList {
result := &ControlPlaneApplicationBundleList{}
for _, bundle := range l.Items {
if bundle.Spec.Preview != nil && *bundle.Spec.Preview {
continue
}
if bundle.Spec.EndOfLife != nil {
continue
}
result.Items = append(result.Items, bundle)
}
return result
}
func (l KubernetesClusterApplicationBundleList) Upgradable() *KubernetesClusterApplicationBundleList {
result := &KubernetesClusterApplicationBundleList{}
for _, bundle := range l.Items {
if bundle.Spec.Preview != nil && *bundle.Spec.Preview {
continue
}
if bundle.Spec.EndOfLife != nil {
continue
}
result.Items = append(result.Items, bundle)
}
return result
}
func (s ApplicationBundleSpec) GetApplication(name string) (*coreunikornv1.ApplicationReference, error) {
for i := range s.Applications {
if *s.Applications[i].Name == name {
return s.Applications[i].Reference, nil
}
}
return nil, fmt.Errorf("%w: %s", ErrApplicationLookup, name)
}
// Weekdays returns the days of the week that are set in the spec.
func (s ApplicationBundleAutoUpgradeWeekDaySpec) Weekdays() []time.Weekday {
var result []time.Weekday
if s.Sunday != nil {
result = append(result, time.Sunday)
}
if s.Monday != nil {
result = append(result, time.Monday)
}
if s.Tuesday != nil {
result = append(result, time.Tuesday)
}
if s.Wednesday != nil {
result = append(result, time.Wednesday)
}
if s.Thursday != nil {
result = append(result, time.Thursday)
}
if s.Friday != nil {
result = append(result, time.Friday)
}
if s.Saturday != nil {
result = append(result, time.Saturday)
}
return result
}