-
Notifications
You must be signed in to change notification settings - Fork 20
/
k8s.go
429 lines (380 loc) · 13 KB
/
k8s.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright 2020 IBM Corp.
//
// 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 utils
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"reflect"
emperrors "emperror.dev/errors"
"github.com/gotidy/ptr"
"github.com/imdario/mergo"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
k8yaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/redhat-marketplace/redhat-marketplace-operator/v2/apis/marketplace/common"
marketplacev1alpha1 "github.com/redhat-marketplace/redhat-marketplace-operator/v2/apis/marketplace/v1alpha1"
"github.com/redhat-marketplace/redhat-marketplace-operator/v2/pkg/utils/operrors"
appsv1 "k8s.io/api/apps/v1"
)
type PersistentVolume struct {
*metav1.ObjectMeta
StorageClass *string
StorageSize *resource.Quantity
AccessMode *corev1.PersistentVolumeAccessMode
}
func NewPersistentVolumeClaim(values PersistentVolume) (corev1.PersistentVolumeClaim, error) {
// set some defaults
quantity := resource.MustParse("20Gi")
accessMode := corev1.ReadWriteOnce
defaults := PersistentVolume{
ObjectMeta: &metav1.ObjectMeta{},
StorageClass: ptr.String(""),
AccessMode: &accessMode,
StorageSize: &quantity,
}
// merge values from pv into values
if err := mergo.Merge(&values, defaults); err != nil {
return corev1.PersistentVolumeClaim{}, err
}
return corev1.PersistentVolumeClaim{
ObjectMeta: *values.ObjectMeta,
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{
*values.AccessMode,
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"storage": *values.StorageSize,
},
},
StorageClassName: values.StorageClass,
},
}, nil
}
// GetPodNames returns the pod names of the array of pods passed in
func GetPodNames(pods []corev1.Pod) []string {
var podNames []string
for _, pod := range pods {
podNames = append(podNames, pod.Name)
}
return podNames
}
// GetDefaultStorageClass attempts to return the default storage class
// of the cluster and errors if it cannot be found
func GetDefaultStorageClass(client client.Client) (string, error) {
storageList := &storagev1.StorageClassList{}
if err := client.List(context.TODO(), storageList); err != nil {
return "", err
}
defaultStorageOptions := []string{}
for _, storageClass := range storageList.Items {
if storageClass.Annotations["storageclass.kubernetes.io/is-default-class"] == "true" ||
storageClass.Annotations["storageclass.beta.kubernetes.io/is-default-class"] == "true" {
defaultStorageOptions = append(defaultStorageOptions, storageClass.Name)
}
}
if len(defaultStorageOptions) == 0 {
return "", emperrors.WithStack(operrors.DefaultStorageClassNotFound)
}
if len(defaultStorageOptions) > 1 {
return "", emperrors.WithStack(operrors.MultipleDefaultStorageClassFound)
}
return defaultStorageOptions[0], nil
}
// MakeProbe creates a probe with the specified path and prot
func MakeProbe(path string, port, initialDelaySeconds, timeoutSeconds int32) *corev1.Probe {
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: path,
Port: intstr.FromInt(int(port)),
},
},
InitialDelaySeconds: initialDelaySeconds,
TimeoutSeconds: timeoutSeconds,
}
}
// BuildMarketplaceConfigCR returns a new MarketplaceConfig
func BuildMarketplaceConfigCR(namespace, customerID string) *marketplacev1alpha1.MarketplaceConfig {
return &marketplacev1alpha1.MarketplaceConfig{
ObjectMeta: metav1.ObjectMeta{
Name: MARKETPLACECONFIG_NAME,
Namespace: namespace,
},
Spec: marketplacev1alpha1.MarketplaceConfigSpec{
RhmAccountID: customerID,
},
}
}
// BuildNewOpSrc returns a new Operator Source
func BuildNewOpSrc() *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "operators.coreos.com/v1",
"kind": "OperatorSource",
"metadata": map[string]interface{}{
"namespace": OPERATOR_MKTPLACE_NS,
"name": OPSRC_NAME,
},
"spec": map[string]interface{}{
"DisplayName": "Red Hat Marketplace",
"Endpoint": "https://quay.io/cnr",
"Publisher": "Red Hat Marketplace",
"RegistryNamespace": "redhat-marketplace",
"Type": "appregistry",
},
},
}
}
// BuildNewIBMCatalogSrc returns a new IBM Catalog Source
func BuildNewIBMCatalogSrc() *operatorsv1alpha1.CatalogSource {
catalogSrc := &operatorsv1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: IBM_CATALOGSRC_NAME,
// Must always be openshift-marketplace
Namespace: OPERATOR_MKTPLACE_NS,
},
Spec: operatorsv1alpha1.CatalogSourceSpec{
DisplayName: "IBM Operator Catalog",
Publisher: "IBM",
SourceType: "grpc",
Image: "icr.io/cpopen/ibm-operator-catalog",
UpdateStrategy: &operatorsv1alpha1.UpdateStrategy{RegistryPoll: &operatorsv1alpha1.RegistryPoll{RawInterval: "45m"}},
},
}
return catalogSrc
}
// BuildNewOpencloudCatalogSrc returns a new Opencloud Catalog Source
func BuildNewOpencloudCatalogSrc() *operatorsv1alpha1.CatalogSource {
catalogSrc := &operatorsv1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: OPENCLOUD_CATALOGSRC_NAME,
// Must always be openshift-marketplace
Namespace: OPERATOR_MKTPLACE_NS,
},
Spec: operatorsv1alpha1.CatalogSourceSpec{
DisplayName: "IBMCS Operators",
Publisher: "IBM",
SourceType: "grpc",
Image: "icr.io/cpopen/ibm-common-service-catalog",
UpdateStrategy: &operatorsv1alpha1.UpdateStrategy{RegistryPoll: &operatorsv1alpha1.RegistryPoll{RawInterval: "45m"}},
},
}
return catalogSrc
}
// BuildRazeeCrd returns a RazeeDeployment cr with default values
func BuildRazeeCr(namespace, clusterUUID string, deploySecretName *string, features *common.Features, installIBMCatalogSource *bool) *marketplacev1alpha1.RazeeDeployment {
cr := &marketplacev1alpha1.RazeeDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: RAZEE_NAME,
Namespace: namespace,
},
Spec: marketplacev1alpha1.RazeeDeploymentSpec{
Enabled: true,
ClusterUUID: clusterUUID,
DeploySecretName: deploySecretName,
Features: features.DeepCopy(),
InstallIBMCatalogSource: installIBMCatalogSource,
},
}
return cr
}
func NewMeterDefinitionCatalogConfig(
enabled bool,
) *common.MeterDefinitionCatalogServerConfig {
return &common.MeterDefinitionCatalogServerConfig{
DeployMeterDefinitionCatalogServer: enabled,
SyncCommunityMeterDefinitions: enabled,
SyncSystemMeterDefinitions: false, //always making this false for now
}
}
func UpdateMeterDefinitionCatalogConfig(
base *marketplacev1alpha1.MeterBase,
config *common.MeterDefinitionCatalogServerConfig,
) bool {
if config == nil {
return false
}
if base.Spec.MeterdefinitionCatalogServerConfig == nil {
base.Spec.MeterdefinitionCatalogServerConfig = config
return true
}
baseConfig := *base.Spec.MeterdefinitionCatalogServerConfig
return !reflect.DeepEqual(baseConfig, *config)
}
// BuildMeterBaseCr returns a MeterBase cr with default values
func BuildMeterBaseCr(namespace string, deployMdefCatalogServer bool) *marketplacev1alpha1.MeterBase {
cr := &marketplacev1alpha1.MeterBase{
ObjectMeta: metav1.ObjectMeta{
Name: METERBASE_NAME,
Namespace: namespace,
},
Spec: marketplacev1alpha1.MeterBaseSpec{
Enabled: true,
Prometheus: &marketplacev1alpha1.PrometheusSpec{
Storage: marketplacev1alpha1.StorageSpec{
Size: resource.MustParse("20Gi"),
},
},
MeterdefinitionCatalogServerConfig: NewMeterDefinitionCatalogConfig(deployMdefCatalogServer),
},
}
return cr
}
func LoadYAML(filename string, i interface{}) (interface{}, error) {
dat, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
dec := k8yaml.NewYAMLOrJSONDecoder(bytes.NewReader(dat), 1000)
var genericTypeVal interface{}
switch v := i.(type) {
case corev1.ConfigMap:
genericTypeVal = &corev1.ConfigMap{}
case monitoringv1.Prometheus:
genericTypeVal = &monitoringv1.Prometheus{}
default:
return nil, fmt.Errorf("type not recognized %T", v)
}
if err := dec.Decode(&genericTypeVal); err != nil {
return nil, err
}
return genericTypeVal, nil
}
// filterByNamespace returns the client.Object filtered by namespaces ListOptions
func FilterByNamespace(obj client.ObjectList, namespaces []corev1.Namespace, rClient client.Client, options ...client.ListOption) error {
var err error
var listOpts []client.ListOption
for _, opt := range options {
listOpts = append(listOpts, opt)
}
if len(namespaces) == 0 {
// if no namespaces are passed, return resources across all namespaces
listOpts = append(listOpts, client.InNamespace(""))
return getResources(obj, listOpts, rClient)
}
if len(namespaces) == 1 {
//if passed a single namespace, return resources across that namespace
listOpts = append(listOpts, client.InNamespace(namespaces[0].ObjectMeta.Name))
err = getResources(obj, listOpts, rClient)
return err
}
//if more than one namespaces is passed, loop through and append the resources
switch listType := obj.(type) {
case *corev1.PodList:
for _, ns := range namespaces {
temp := &corev1.PodList{}
listOpts = append(listOpts, client.InNamespace(ns.ObjectMeta.Name))
err = getResources(temp, listOpts, rClient)
for _, i := range temp.Items {
listType.Items = append(listType.Items, i)
}
}
case *monitoringv1.ServiceMonitorList:
for _, ns := range namespaces {
temp := &monitoringv1.ServiceMonitorList{}
listOpts = append(listOpts, client.InNamespace(ns.ObjectMeta.Name))
err = getResources(temp, listOpts, rClient)
for _, i := range temp.Items {
listType.Items = append(listType.Items, i)
}
}
case *corev1.ServiceList:
for _, ns := range namespaces {
temp := &corev1.ServiceList{}
listOpts = append(listOpts, client.InNamespace(ns.ObjectMeta.Name))
err = getResources(temp, listOpts, rClient)
for _, i := range temp.Items {
listType.Items = append(listType.Items, i)
}
}
default:
err = errors.New("type is not supported for filter aggregation")
}
return err
}
// getResources() is a helper function for FilterByNamespace(), it returns a the client.Object filled with resources from the requested namespaces
// the namespaces are preset in listOpts
func getResources(obj client.ObjectList, listOpts []client.ListOption, rClient client.Client) error {
err := rClient.List(context.TODO(), obj, listOpts...)
if err != nil {
return err
}
return nil
}
var eq = conversion.EqualitiesOrDie(
func(a, b resource.Quantity) bool {
// Ignore formatting, only care that numeric value stayed the same.
// TODO: if we decide it's important, it should be safe to start comparing the format.
//
// Uninitialized quantities are equivalent to 0 quantities.
return a.Cmp(b) == 0
},
func(a, b metav1.MicroTime) bool {
return a.UTC() == b.UTC()
},
func(a, b metav1.Time) bool {
return a.UTC() == b.UTC()
},
func(a, b labels.Selector) bool {
return a.String() == b.String()
},
func(a, b fields.Selector) bool {
return a.String() == b.String()
},
func(a, b corev1.ObjectFieldSelector) bool {
if a.APIVersion == "" {
a.APIVersion = "v1"
}
if b.APIVersion == "" {
b.APIVersion = "v1"
}
return a.APIVersion == b.APIVersion && a.FieldPath == b.FieldPath
},
)
func DeploymentNeedsUpdate(original, updated appsv1.Deployment) (bool, string) {
if !eq.DeepEqual(updated.Labels, original.Labels) {
return true, "labels"
}
if !eq.DeepEqual(updated.Annotations, original.Annotations) {
return true, "annotations"
}
if !eq.DeepEqual(updated.Spec.Strategy, original.Spec.Strategy) {
return true, "strategy"
}
if !eq.DeepEqual(updated.ObjectMeta.OwnerReferences, original.ObjectMeta.OwnerReferences) {
return true, "owner"
}
if !eq.DeepEqual(original.Spec.Template.Spec.Volumes, updated.Spec.Template.Spec.Volumes) {
return true, "spec.volumes"
}
if !eq.DeepEqual(original.Spec.Template.Spec.Containers, updated.Spec.Template.Spec.Containers) {
return true, "spec.containers"
}
//return !eq.DeepEqual(original.Spec, updated.Spec), "spec"
return false, ""
}