Skip to content

Commit 8ff9008

Browse files
committed
Only use Hypervisor CRD in onboarding
All the necessary information is now in the CRD, so we can stop polling the information from the node.
1 parent 94ce82b commit 8ff9008

File tree

3 files changed

+24
-62
lines changed

3 files changed

+24
-62
lines changed

internal/controller/node_eviction_label_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (r *NodeEvictionLabelReconciler) Reconcile(ctx context.Context, req ctrl.Re
107107
}
108108

109109
if value != "" {
110-
err = disableInstanceHA(node)
110+
err = disableInstanceHA(hv)
111111
if err != nil {
112112
return ctrl.Result{}, err
113113
}

internal/controller/onboarding_controller.go

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,6 @@ type OnboardingController struct {
7777
testNetworkClient *gophercloud.ServiceClient
7878
}
7979

80-
func getHypervisorAddress(node *corev1.Node) string {
81-
for _, addr := range node.Status.Addresses {
82-
if addr.Address == "" {
83-
continue
84-
}
85-
86-
if addr.Type == corev1.NodeHostName {
87-
return addr.Address
88-
}
89-
}
90-
91-
return ""
92-
}
93-
9480
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors,verbs=get;list;watch;patch
9581
func (r *OnboardingController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
9682
log := logger.FromContext(ctx).WithName(req.Name)
@@ -144,24 +130,14 @@ func (r *OnboardingController) Reconcile(ctx context.Context, req ctrl.Request)
144130
return ctrl.Result{}, r.Status().Update(ctx, hv)
145131
}
146132

147-
// TODO: cleanup node retrieval
148-
node := &corev1.Node{}
149-
if err := r.Get(ctx, types.NamespacedName{Name: hv.Name}, node); err != nil {
150-
if k8sclient.IgnoreNotFound(err) == nil {
151-
// Node not found, could be deleted
152-
return ctrl.Result{}, nil
153-
}
154-
return ctrl.Result{}, err
155-
}
156-
157133
switch status.Reason {
158134
case ConditionReasonInitial:
159135
return ctrl.Result{}, r.initialOnboarding(ctx, hv, computeHost)
160136
case ConditionReasonTesting:
161137
if hv.Spec.SkipTests {
162-
return r.completeOnboarding(ctx, computeHost, node, hv)
138+
return r.completeOnboarding(ctx, computeHost, hv)
163139
} else {
164-
return r.smokeTest(ctx, node, hv, computeHost)
140+
return r.smokeTest(ctx, hv, computeHost)
165141
}
166142
default:
167143
// Nothing to be done
@@ -170,16 +146,7 @@ func (r *OnboardingController) Reconcile(ctx context.Context, req ctrl.Request)
170146
}
171147

172148
func (r *OnboardingController) initialOnboarding(ctx context.Context, hv *kvmv1.Hypervisor, host string) error {
173-
node := &corev1.Node{}
174-
if err := r.Get(ctx, types.NamespacedName{Name: hv.Name}, node); err != nil {
175-
if k8sclient.IgnoreNotFound(err) == nil {
176-
// Node not found, could be deleted
177-
return nil
178-
}
179-
return err
180-
}
181-
182-
zone, found := node.Labels[corev1.LabelTopologyZone]
149+
zone, found := hv.Labels[corev1.LabelTopologyZone]
183150
if !found || zone == "" {
184151
return fmt.Errorf("cannot find availability-zone label %v on node", corev1.LabelTopologyZone)
185152
}
@@ -193,11 +160,9 @@ func (r *OnboardingController) initialOnboarding(ctx context.Context, hv *kvmv1.
193160
return fmt.Errorf("failed to agg to availability-zone aggregate %w", err)
194161
}
195162

196-
if _, found := node.Labels[corev1.LabelTopologyZone]; found {
197-
err = addToAggregate(ctx, r.computeClient, aggs, host, testAggregateName, "")
198-
if err != nil {
199-
return fmt.Errorf("failed to agg to test aggregate %w", err)
200-
}
163+
err = addToAggregate(ctx, r.computeClient, aggs, host, testAggregateName, "")
164+
if err != nil {
165+
return fmt.Errorf("failed to agg to test aggregate %w", err)
201166
}
202167

203168
// The service may be forced down previously due to an HA event,
@@ -220,10 +185,10 @@ func (r *OnboardingController) initialOnboarding(ctx context.Context, hv *kvmv1.
220185
return r.Status().Update(ctx, hv)
221186
}
222187

223-
func (r *OnboardingController) smokeTest(ctx context.Context, node *corev1.Node, hv *kvmv1.Hypervisor, host string) (ctrl.Result, error) {
188+
func (r *OnboardingController) smokeTest(ctx context.Context, hv *kvmv1.Hypervisor, host string) (ctrl.Result, error) {
224189
log := logger.FromContext(ctx)
225-
zone := node.Labels[corev1.LabelTopologyZone]
226-
server, err := r.createOrGetTestServer(ctx, zone, host, node.UID)
190+
zone := hv.Labels[corev1.LabelTopologyZone]
191+
server, err := r.createOrGetTestServer(ctx, zone, host, hv.UID)
227192
if err != nil {
228193
return ctrl.Result{}, fmt.Errorf("failed to create or get test instance %w", err)
229194
}
@@ -288,13 +253,13 @@ func (r *OnboardingController) smokeTest(ctx context.Context, node *corev1.Node,
288253
return ctrl.Result{RequeueAfter: defaultWaitTime}, nil
289254
}
290255

291-
return r.completeOnboarding(ctx, host, node, hv)
256+
return r.completeOnboarding(ctx, host, hv)
292257
default:
293258
return ctrl.Result{RequeueAfter: defaultWaitTime}, nil
294259
}
295260
}
296261

297-
func (r *OnboardingController) completeOnboarding(ctx context.Context, host string, node *corev1.Node, hv *kvmv1.Hypervisor) (ctrl.Result, error) {
262+
func (r *OnboardingController) completeOnboarding(ctx context.Context, host string, hv *kvmv1.Hypervisor) (ctrl.Result, error) {
298263
log := logger.FromContext(ctx)
299264

300265
serverPrefix := fmt.Sprintf("%v-%v", testPrefixName, host)
@@ -331,7 +296,7 @@ func (r *OnboardingController) completeOnboarding(ctx context.Context, host stri
331296
}
332297
log.Info("removed from test-aggregate", "name", testAggregateName)
333298

334-
err = enableInstanceHA(node)
299+
err = enableInstanceHA(hv)
335300
log.Info("enabled instance-ha")
336301
if err != nil {
337302
return ctrl.Result{}, err
@@ -356,12 +321,7 @@ func (r *OnboardingController) completeOnboarding(ctx context.Context, host stri
356321
}
357322

358323
func (r *OnboardingController) ensureNovaProperties(ctx context.Context, hv *kvmv1.Hypervisor) error {
359-
node := &corev1.Node{}
360-
if err := r.Get(ctx, types.NamespacedName{Name: hv.Name}, node); err != nil {
361-
return k8sclient.IgnoreNotFound(err)
362-
}
363-
364-
hypervisorAddress := getHypervisorAddress(node)
324+
hypervisorAddress := hv.Labels[corev1.LabelHostname]
365325
if hypervisorAddress == "" {
366326
return errRequeue
367327
}

internal/controller/utils.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
corev1 "k8s.io/api/core/v1"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3333
"sigs.k8s.io/controller-runtime/pkg/client"
34+
35+
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
3436
)
3537

3638
// setNodeLabels sets the labels on the node.
@@ -51,17 +53,17 @@ func InstanceHaUrl(region, zone, hostname string) string {
5153
return fmt.Sprintf("https://kvm-ha-service-%v.%v.cloud.sap/api/hypervisors/%v", zone, region, hostname)
5254
}
5355

54-
func updateInstanceHA(node *corev1.Node, data string, acceptedCodes []int) error {
55-
zone, found := node.Labels[corev1.LabelTopologyZone]
56+
func updateInstanceHA(hypervisor *kvmv1.Hypervisor, data string, acceptedCodes []int) error {
57+
zone, found := hypervisor.Labels[corev1.LabelTopologyZone]
5658
if !found {
5759
return fmt.Errorf("could not find label %v for node", corev1.LabelTopologyZone)
5860
}
59-
region, found := node.Labels[corev1.LabelTopologyRegion]
61+
region, found := hypervisor.Labels[corev1.LabelTopologyRegion]
6062
if !found {
6163
return fmt.Errorf("could not find label %v for node", corev1.LabelTopologyRegion)
6264
}
6365

64-
hostname, found := node.Labels[corev1.LabelHostname]
66+
hostname, found := hypervisor.Labels[corev1.LabelHostname]
6567
if !found {
6668
return fmt.Errorf("could not find label %v for node", corev1.LabelHostname)
6769
}
@@ -81,12 +83,12 @@ func updateInstanceHA(node *corev1.Node, data string, acceptedCodes []int) error
8183
return nil
8284
}
8385

84-
func enableInstanceHA(node *corev1.Node) error {
85-
return updateInstanceHA(node, `{"enabled": true}`, []int{http.StatusOK})
86+
func enableInstanceHA(hypervisor *kvmv1.Hypervisor) error {
87+
return updateInstanceHA(hypervisor, `{"enabled": true}`, []int{http.StatusOK})
8688
}
8789

88-
func disableInstanceHA(node *corev1.Node) error {
89-
return updateInstanceHA(node, `{"enabled": false}`, []int{http.StatusOK, http.StatusNotFound})
90+
func disableInstanceHA(hypervisor *kvmv1.Hypervisor) error {
91+
return updateInstanceHA(hypervisor, `{"enabled": false}`, []int{http.StatusOK, http.StatusNotFound})
9092
}
9193

9294
// IsNodeConditionTrue returns true when the conditionType is present and set to `corev1.ConditionTrue`

0 commit comments

Comments
 (0)