forked from kubernetes-sigs/cluster-api-provider-vsphere
/
instancestatus.go
121 lines (98 loc) · 4.46 KB
/
instancestatus.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
/*
Copyright 2018 The Kubernetes Authors.
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 vsphere
import (
"bytes"
"fmt"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/schema"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/util"
)
// Long term, we should retrieve the current status by asking k8s, gce etc. for all the needed info.
// For now, it is stored in the matching CRD under an annotation. This is similar to
// the spec and status concept where the machine CRD is the instance spec and the annotation is the instance status.
const InstanceStatusAnnotationKey = "instance-status"
type instanceStatus *clusterv1.Machine
// Get the status of the instance identified by the given machine
func (vc *VsphereClient) instanceStatus(machine *clusterv1.Machine) (instanceStatus, error) {
currentMachine, err := util.GetMachineIfExists(vc.controllerClient, machine.Namespace, machine.ObjectMeta.Name)
//currentMachine, err := util.GetMachineIfExists(vc.clusterV1alpha1.Machines(machine.Namespace), machine.Namespace, machine.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 vc.machineInstanceStatus(currentMachine)
}
// Sets the status of the instance identified by the given machine to the given machine
func (vc *VsphereClient) updateInstanceStatus(machine *clusterv1.Machine) error {
status := instanceStatus(machine)
currentMachine, err := util.GetMachineIfExists(vc.controllerClient, machine.Namespace, machine.ObjectMeta.Name)
//currentMachine, err := util.GetMachineIfExists(vc.clusterV1alpha1.Machines(machine.Namespace), 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)
}
m, err := vc.setMachineInstanceStatus(currentMachine, status)
if err != nil {
return err
}
_, err = vc.clusterV1alpha1.Machines(m.Namespace).Update(m)
return err
}
// Gets the state of the instance stored on the given machine CRD
func (vc *VsphereClient) machineInstanceStatus(machine *clusterv1.Machine) (instanceStatus, error) {
if machine.ObjectMeta.Annotations == nil {
// No state
return nil, nil
}
a := machine.ObjectMeta.Annotations[InstanceStatusAnnotationKey]
if a == "" {
// No state
return nil, nil
}
//TODO: Remove the old version if nil works
serializer := json.NewSerializer(json.DefaultMetaFactory, nil, nil, false)
//serializer := json.NewSerializer(json.DefaultMetaFactory, vc.scheme, vc.scheme, false)
var status clusterv1.Machine
_, _, err := serializer.Decode([]byte(a), &schema.GroupVersionKind{Group: "", Version: "cluster.k8s.io/v1alpha1", Kind: "Machine"}, &status)
if err != nil {
return nil, fmt.Errorf("decoding failure: %v", err)
}
return instanceStatus(&status), nil
}
// Applies the state of an instance onto a given machine CRD
func (vc *VsphereClient) setMachineInstanceStatus(machine *clusterv1.Machine, status instanceStatus) (*clusterv1.Machine, error) {
// Avoid status within status within status ...
status.ObjectMeta.Annotations[InstanceStatusAnnotationKey] = ""
//TODO: Remove the old version if nil works
serializer := json.NewSerializer(json.DefaultMetaFactory, nil, nil, false)
//serializer := json.NewSerializer(json.DefaultMetaFactory, vc.scheme, vc.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[InstanceStatusAnnotationKey] = buff.String()
return machine, nil
}