|
| 1 | +/* |
| 2 | +SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package controller |
| 19 | + |
| 20 | +// This controller only takes care of enabling or disabling the compute |
| 21 | +// service depending on the hypervisor spec Maintenance field |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + |
| 27 | + "k8s.io/apimachinery/pkg/api/meta" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/runtime" |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + k8sclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + logger "sigs.k8s.io/controller-runtime/pkg/log" |
| 33 | + |
| 34 | + "github.com/gophercloud/gophercloud/v2" |
| 35 | + "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/services" |
| 36 | + |
| 37 | + kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1" |
| 38 | + "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack" |
| 39 | +) |
| 40 | + |
| 41 | +const ( |
| 42 | + HypervisorMaintenanceControllerName = "HypervisorMaintenanceController" |
| 43 | +) |
| 44 | + |
| 45 | +type HypervisorMaintenanceController struct { |
| 46 | + k8sclient.Client |
| 47 | + Scheme *runtime.Scheme |
| 48 | + computeClient *gophercloud.ServiceClient |
| 49 | +} |
| 50 | + |
| 51 | +// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors,verbs=get;list;watch |
| 52 | +// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/status,verbs=get;list;watch;create;update;patch;delete |
| 53 | + |
| 54 | +func (hec *HypervisorMaintenanceController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 55 | + hv := &kvmv1.Hypervisor{} |
| 56 | + if err := hec.Get(ctx, req.NamespacedName, hv); err != nil { |
| 57 | + // OnboardingReconciler not found errors, could be deleted |
| 58 | + return ctrl.Result{}, k8sclient.IgnoreNotFound(err) |
| 59 | + } |
| 60 | + |
| 61 | + // is onboarding completed? |
| 62 | + if !meta.IsStatusConditionFalse(hv.Status.Conditions, ConditionTypeOnboarding) { |
| 63 | + return ctrl.Result{}, nil |
| 64 | + } |
| 65 | + |
| 66 | + // ensure serviceId is set |
| 67 | + if hv.Status.ServiceID == "" { |
| 68 | + return ctrl.Result{}, nil |
| 69 | + } |
| 70 | + |
| 71 | + log := logger.FromContext(ctx). |
| 72 | + WithName("HypervisorService") |
| 73 | + ctx = logger.IntoContext(ctx, log) |
| 74 | + |
| 75 | + changed, err := hec.handleSpecMaintenance(ctx, hv) |
| 76 | + if err != nil { |
| 77 | + return ctrl.Result{}, err |
| 78 | + } |
| 79 | + |
| 80 | + if changed { |
| 81 | + return ctrl.Result{}, hec.Status().Update(ctx, hv) |
| 82 | + } else { |
| 83 | + return ctrl.Result{}, nil |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func (hec *HypervisorMaintenanceController) handleSpecMaintenance(ctx context.Context, hv *kvmv1.Hypervisor) (bool, error) { |
| 88 | + log := logger.FromContext(ctx) |
| 89 | + serviceId := hv.Status.ServiceID |
| 90 | + |
| 91 | + switch hv.Spec.Maintenance { |
| 92 | + case "": // Enable the compute service (in case we haven't done so already) |
| 93 | + if !meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{ |
| 94 | + Type: kvmv1.ConditionTypeHypervisorDisabled, |
| 95 | + Status: metav1.ConditionFalse, |
| 96 | + Message: "Hypervisor enabled", |
| 97 | + Reason: kvmv1.ConditionReasonSucceeded, |
| 98 | + }) { |
| 99 | + // Spec matches status |
| 100 | + return false, nil |
| 101 | + } |
| 102 | + // We need to enable the host as per spec |
| 103 | + enableService := services.UpdateOpts{Status: services.ServiceEnabled} |
| 104 | + log.Info("Enabling hypervisor", "id", serviceId) |
| 105 | + _, err := services.Update(ctx, hec.computeClient, serviceId, enableService).Extract() |
| 106 | + if err != nil { |
| 107 | + return false, fmt.Errorf("failed to enable hypervisor due to %w", err) |
| 108 | + } |
| 109 | + case "manual", "auto", "ha": // Disable the compute service |
| 110 | + if !meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{ |
| 111 | + Type: kvmv1.ConditionTypeHypervisorDisabled, |
| 112 | + Status: metav1.ConditionTrue, |
| 113 | + Message: "Hypervisor disabled", |
| 114 | + Reason: kvmv1.ConditionReasonSucceeded, |
| 115 | + }) { |
| 116 | + // Spec matches status |
| 117 | + return false, nil |
| 118 | + } |
| 119 | + |
| 120 | + // We need to disable the host as per spec |
| 121 | + enableService := services.UpdateOpts{Status: services.ServiceDisabled, DisabledReason: ""} |
| 122 | + log.Info("Disabling hypervisor", "id", serviceId) |
| 123 | + _, err := services.Update(ctx, hec.computeClient, serviceId, enableService).Extract() |
| 124 | + if err != nil { |
| 125 | + return false, fmt.Errorf("failed to disable hypervisor due to %w", err) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return true, nil |
| 130 | +} |
| 131 | + |
| 132 | +// SetupWithManager sets up the controller with the Manager. |
| 133 | +func (hec *HypervisorMaintenanceController) SetupWithManager(mgr ctrl.Manager) error { |
| 134 | + ctx := context.Background() |
| 135 | + _ = logger.FromContext(ctx) |
| 136 | + |
| 137 | + var err error |
| 138 | + if hec.computeClient, err = openstack.GetServiceClient(ctx, "compute", nil); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + hec.computeClient.Microversion = "2.90" // Xena (or later) |
| 142 | + |
| 143 | + return ctrl.NewControllerManagedBy(mgr). |
| 144 | + Named(HypervisorMaintenanceControllerName). |
| 145 | + For(&kvmv1.Hypervisor{}). |
| 146 | + Complete(hec) |
| 147 | +} |
0 commit comments