-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathkube.go
55 lines (45 loc) · 1.51 KB
/
kube.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
package controller
import (
"context"
"fmt"
"strings"
"github.com/tinkerbell/rufio/api/v1alpha1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// resolveAuthSecretRef Gets the Secret from the SecretReference.
// Returns the username and password encoded in the Secret.
func resolveAuthSecretRef(ctx context.Context, c client.Client, secretRef v1.SecretReference) (string, string, error) {
secret := &v1.Secret{}
key := types.NamespacedName{Namespace: secretRef.Namespace, Name: secretRef.Name}
if err := c.Get(ctx, key, secret); err != nil {
if apierrors.IsNotFound(err) {
return "", "", fmt.Errorf("secret %s not found: %w", key, err)
}
return "", "", fmt.Errorf("failed to retrieve secret %s : %w", secretRef, err)
}
username, ok := secret.Data["username"]
if !ok {
return "", "", fmt.Errorf("'username' required in Machine secret")
}
password, ok := secret.Data["password"]
if !ok {
return "", "", fmt.Errorf("'password' required in Machine secret")
}
return string(username), string(password), nil
}
// toPowerState takes a raw BMC power state response and converts it to a v1alpha1.PowerState.
func toPowerState(state string) v1alpha1.PowerState {
// Normalize the response string for comparison.
state = strings.ToLower(state)
switch {
case strings.Contains(state, "on"):
return v1alpha1.On
case strings.Contains(state, "off"):
return v1alpha1.Off
default:
return v1alpha1.Unknown
}
}