-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgetOvercommitClass.go
More file actions
68 lines (54 loc) · 1.98 KB
/
getOvercommitClass.go
File metadata and controls
68 lines (54 loc) · 1.98 KB
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
// SPDX-FileCopyrightText: 2025 2025 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.)
// SPDX-FileContributor: enriqueavi@inditex.com
//
// SPDX-License-Identifier: Apache-2.0
package utils
import (
"context"
"errors"
"fmt"
overcommit "github.com/InditexTech/k8s-overcommit-operator/api/v1alphav1"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
var podlog = logf.Log.WithName("utils")
func GetOvercommitClassSpec(ctx context.Context, name string, k8sClient client.Client) (*overcommit.OvercommitClassSpec, error) {
// Validate the parameters
if name == "" {
return nil, errors.New("name parameter cannot be empty")
}
if k8sClient == nil {
return nil, errors.New("client parameter cannot be nil")
}
// Create a new OvercommitClass object
var overcommitClass overcommit.OvercommitClass
// Search for the OvercommitClass with the name "cluster"
err := k8sClient.Get(ctx, client.ObjectKey{
Name: name,
}, &overcommitClass)
if err != nil {
podlog.Error(err, "Error getting the CR", "name", name)
return nil, fmt.Errorf("error getting OvercommitClass with name '%s': %w", name, err)
}
podlog.Info("OvercommitClass found", "name", name)
// Return the overcommit label
return &overcommitClass.Spec, nil
}
func GetDefaultSpec(ctx context.Context, k8sClient client.Client) (*overcommit.OvercommitClassSpec, error) {
if k8sClient == nil {
return nil, errors.New("client parameter cannot be nil")
}
// List all OvercommitClass
var overcommitClasses overcommit.OvercommitClassList
if err := k8sClient.List(ctx, &overcommitClasses); err != nil {
return nil, fmt.Errorf("error listing OvercommitClass: %w", err)
}
// Find the default OvercommitClass
for _, overcommitClass := range overcommitClasses.Items {
if overcommitClass.Spec.IsDefault {
podlog.Info("Default OvercommitClass found", "name", overcommitClass.Name)
return &overcommitClass.Spec, nil
}
}
return nil, errors.New("no OvercommitClass with isDefault: true found")
}