This repository has been archived by the owner on Jul 14, 2019. It is now read-only.
/
actuator_status.go
131 lines (105 loc) · 4.06 KB
/
actuator_status.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
package machine
import (
"bytes"
"fmt"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/util"
)
type MachineStatus *clusterv1.Machine
type AnnotationKey string
const (
InstanceStatus AnnotationKey = "instance-status"
Name AnnotationKey = "machine-name"
)
// Get the status of the instance identified by the given machine
func (a *Actuator) status(m *clusterv1.Machine) (MachineStatus, error) {
if a.v1Alpha1Client == nil {
return nil, nil
}
currentMachine, err := util.GetMachineIfExists(a.v1Alpha1Client.Machines(m.Namespace), m.ObjectMeta.Name)
if err != nil {
return nil, err
}
if currentMachine == nil {
// The current status no longer exists because the matching CRD has been deleted (or does not exist yet ie. bootstrapping)
return nil, nil
}
return a.machineStatus(currentMachine)
}
// Gets the state of the instance stored on the given machine CRD
func (a *Actuator) machineStatus(m *clusterv1.Machine) (MachineStatus, error) {
if m.ObjectMeta.Annotations == nil {
return nil, nil
}
annot := m.ObjectMeta.Annotations[string(InstanceStatus)]
if annot == "" {
glog.Infof("machineStatus: instance-status was empty %s", m.ObjectMeta.Name)
return nil, nil
}
serializer := json.NewSerializer(json.DefaultMetaFactory, a.scheme, a.scheme, false)
var status clusterv1.Machine
_, _, err := serializer.Decode([]byte(annot), &schema.GroupVersionKind{Group: "", Version: "cluster.k8s.io/v1alpha1", Kind: "Machine"}, &status)
if err != nil {
return nil, fmt.Errorf("decoding failure: %v", err)
}
glog.Infof("machineStatus: instance-status was decoded successfully %s", m.ObjectMeta.Name)
return MachineStatus(&status), nil
}
// Sets the status of the instance identified by the given machine to the given machine
func (a *Actuator) updateStatus(machine *clusterv1.Machine) error {
if a.v1Alpha1Client == nil {
return nil
}
status := MachineStatus(machine)
currentMachine, err := util.GetMachineIfExists(a.v1Alpha1Client.Machines(machine.Namespace), machine.ObjectMeta.Name)
if err != nil {
return err
}
if currentMachine == nil {
// The current status no longer exists because the matching CRD has been deleted.
return fmt.Errorf("Machine has already been deleted. Cannot update current instance status for machine %v", machine.ObjectMeta.Name)
}
currentMachine.ObjectMeta.Annotations = machine.ObjectMeta.Annotations
m, err := a.setMachineStatus(currentMachine, status)
if err != nil {
glog.Infof("updateStatus: failed to set instance-status for machine %s", m.ObjectMeta.Name)
return err
}
glog.Infof("setMachineStatus: successfully set instance-status for machine %s", m.ObjectMeta.Name)
_, err = a.v1Alpha1Client.Machines(m.Namespace).Update(m)
return err
}
// Applies the state of an instance onto a given machine CRD
func (a *Actuator) setMachineStatus(machine *clusterv1.Machine, status MachineStatus) (*clusterv1.Machine, error) {
// Avoid status within status within status ...
status.ObjectMeta.Annotations[string(InstanceStatus)] = ""
serializer := json.NewSerializer(json.DefaultMetaFactory, a.scheme, a.scheme, false)
b := []byte{}
buff := bytes.NewBuffer(b)
err := serializer.Encode((*clusterv1.Machine)(status), buff)
if err != nil {
return nil, fmt.Errorf("encoding failure: %v", err)
}
if machine.ObjectMeta.Annotations == nil {
machine.ObjectMeta.Annotations = make(map[string]string)
}
machine.ObjectMeta.Annotations[string(InstanceStatus)] = buff.String()
glog.Infof("setMachineStatus: encoded instance-status %s.", buff.String())
return machine, nil
}
func (a *Actuator) updateAnnotations(cluster *clusterv1.Cluster, machine *clusterv1.Machine) error {
if a.v1Alpha1Client == nil {
return nil
}
name := machine.ObjectMeta.Name
annotations := machine.ObjectMeta.Annotations
if annotations == nil {
annotations = make(map[string]string)
}
annotations[string(Name)] = name
machine.ObjectMeta.Annotations = annotations
return a.updateStatus(machine)
}