/
instances_v2.go
160 lines (140 loc) · 5.78 KB
/
instances_v2.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2022 OnMetal 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 onmetal
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
computev1alpha1 "github.com/onmetal/onmetal-api/api/compute/v1alpha1"
networkingv1alpha1 "github.com/onmetal/onmetal-api/api/networking/v1alpha1"
)
type onmetalInstancesV2 struct {
targetClient client.Client
onmetalClient client.Client
onmetalNamespace string
clusterName string
}
func newOnmetalInstancesV2(targetClient client.Client, onmetalClient client.Client, namespace, clusterName string) cloudprovider.InstancesV2 {
return &onmetalInstancesV2{
targetClient: targetClient,
onmetalClient: onmetalClient,
onmetalNamespace: namespace,
clusterName: clusterName,
}
}
func (o *onmetalInstancesV2) InstanceExists(ctx context.Context, node *corev1.Node) (bool, error) {
if node == nil {
return false, nil
}
klog.V(4).InfoS("Checking if node exists", "Node", node.Name)
machine := &computev1alpha1.Machine{}
if err := o.onmetalClient.Get(ctx, client.ObjectKey{Namespace: o.onmetalNamespace, Name: node.Name}, machine); err != nil {
if apierrors.IsNotFound(err) {
return false, cloudprovider.InstanceNotFound
}
return false, fmt.Errorf("failed to get machine object for node %s: %w", node.Name, err)
}
klog.V(4).InfoS("Instance for node exists", "Node", node.Name, "Machine", client.ObjectKeyFromObject(machine))
return true, nil
}
func (o *onmetalInstancesV2) InstanceShutdown(ctx context.Context, node *corev1.Node) (bool, error) {
if node == nil {
return false, nil
}
klog.V(4).InfoS("Checking if instance is shut down", "Node", node.Name)
machine := &computev1alpha1.Machine{}
if err := o.onmetalClient.Get(ctx, client.ObjectKey{Namespace: o.onmetalNamespace, Name: node.Name}, machine); err != nil {
if apierrors.IsNotFound(err) {
return false, cloudprovider.InstanceNotFound
}
return false, fmt.Errorf("failed to get machine object for node %s: %w", node.Name, err)
}
nodeShutDownStatus := machine.Status.State == computev1alpha1.MachineStateShutdown
klog.V(4).InfoS("Instance shut down status", "NodeShutdown", nodeShutDownStatus)
return nodeShutDownStatus, nil
}
func (o *onmetalInstancesV2) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cloudprovider.InstanceMetadata, error) {
if node == nil {
return nil, nil
}
machine := &computev1alpha1.Machine{}
if err := o.onmetalClient.Get(ctx, client.ObjectKey{Namespace: o.onmetalNamespace, Name: node.Name}, machine); err != nil {
if apierrors.IsNotFound(err) {
return nil, cloudprovider.InstanceNotFound
}
return nil, fmt.Errorf("failed to get machine object for node %s: %w", node.Name, err)
}
//add label for clusterName to machine object
machineBase := machine.DeepCopy()
if machine.Labels == nil {
machine.Labels = make(map[string]string)
}
machine.Labels[LabelKeyClusterName] = o.clusterName
klog.V(2).InfoS("Adding cluster name label to Machine object", "Machine", client.ObjectKeyFromObject(machine), "Node", node.Name)
if err := o.onmetalClient.Patch(ctx, machine, client.MergeFrom(machineBase)); err != nil {
return nil, fmt.Errorf("failed to patch Machine %s for Node %s: %w", client.ObjectKeyFromObject(machine), node.Name, err)
}
for _, networkInterface := range machine.Spec.NetworkInterfaces {
nic := &networkingv1alpha1.NetworkInterface{}
nicName := fmt.Sprintf("%s-%s", machine.Name, networkInterface.Name)
if err := o.onmetalClient.Get(ctx, client.ObjectKey{Namespace: o.onmetalNamespace, Name: nicName}, nic); err != nil {
return nil, fmt.Errorf("failed to get network interface %s for machine %s: %w", client.ObjectKeyFromObject(nic), machine.Name, err)
}
// add label for clusterName to network interface of machine object
nicBase := nic.DeepCopy()
if nic.Labels == nil {
nic.Labels = make(map[string]string)
}
nic.Labels[LabelKeyClusterName] = o.clusterName
klog.V(2).InfoS("Adding cluster name label to NetworkInterface", "NetworkInterface", client.ObjectKeyFromObject(nic), "Node", node.Name, "Label", nic.Labels[LabelKeyClusterName])
if err := o.onmetalClient.Patch(ctx, nic, client.MergeFrom(nicBase)); err != nil {
return nil, fmt.Errorf("failed to patch NetworkInterface %s for Node %s: %w", client.ObjectKeyFromObject(nic), node.Name, err)
}
}
addresses := make([]corev1.NodeAddress, 0)
for _, iface := range machine.Status.NetworkInterfaces {
if iface.VirtualIP != nil {
addresses = append(addresses, corev1.NodeAddress{
Type: corev1.NodeExternalIP,
Address: iface.VirtualIP.String(),
})
}
for _, ip := range iface.IPs {
addresses = append(addresses, corev1.NodeAddress{
Type: corev1.NodeInternalIP,
Address: ip.String(),
})
}
}
providerID := node.Spec.ProviderID
if providerID == "" {
providerID = fmt.Sprintf("%s://%s/%s", ProviderName, o.onmetalNamespace, machine.Name)
}
zone := ""
if machine.Spec.MachinePoolRef != nil {
zone = machine.Spec.MachinePoolRef.Name
}
// TODO: handle region
return &cloudprovider.InstanceMetadata{
ProviderID: providerID,
InstanceType: machine.Spec.MachineClassRef.Name,
NodeAddresses: addresses,
Zone: zone,
Region: "",
}, nil
}