/
machines.go
67 lines (56 loc) · 2.15 KB
/
machines.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
package utils
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
mapiv1 "sigs.k8s.io/cluster-api/pkg/apis/machine/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetMachine get a machine by its name from the default machine API namespace.
func GetMachine(c client.Client, machineName string) (*mapiv1.Machine, error) {
machine := &mapiv1.Machine{}
if err := c.Get(context.TODO(), client.ObjectKey{Namespace: NamespaceOpenShiftMachineAPI, Name: machineName}, machine); err != nil {
return nil, fmt.Errorf("error querying api for machine object: %v", err)
}
return machine, nil
}
// GetMachineFromNode returns the machine referenced by the "controllernode.MachineAnnotationKey" annotation in the given node
func GetMachineFromNode(c client.Client, node *corev1.Node) (*mapiv1.Machine, error) {
machineNamespaceKey, ok := node.Annotations[MachineAnnotationKey]
if !ok {
return nil, fmt.Errorf("node %q does not have a MachineAnnotationKey %q", node.Name, MachineAnnotationKey)
}
namespace, machineName, err := cache.SplitMetaNamespaceKey(machineNamespaceKey)
if err != nil {
return nil, fmt.Errorf("machine annotation format is incorrect %v: %v", machineNamespaceKey, err)
}
if namespace != NamespaceOpenShiftMachineAPI {
return nil, fmt.Errorf("Machine %q is forbidden to live outside of default %v namespace", machineNamespaceKey, NamespaceOpenShiftMachineAPI)
}
machine, err := GetMachine(c, machineName)
if err != nil {
return nil, fmt.Errorf("error querying api for machine object: %v", err)
}
return machine, nil
}
// GetMachinesSetByMachine retruns machine owner machine set
func GetMachinesSetByMachine(machine *mapiv1.Machine) (*mapiv1.MachineSet, error) {
c, err := LoadClient()
if err != nil {
return nil, err
}
if len(machine.OwnerReferences) == 0 {
return nil, fmt.Errorf("machine %s does not have owner controller", machine.Name)
}
machineSet := &mapiv1.MachineSet{}
key := client.ObjectKey{
Namespace: NamespaceOpenShiftMachineAPI,
Name: machine.OwnerReferences[0].Name,
}
err = c.Get(context.TODO(), key, machineSet)
if err != nil {
return nil, err
}
return machineSet, nil
}