generated from cobaltcore-dev/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Move InstanceHa Enabling/Disabling to own controller #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
internal/controller/hypervisor_instance_ha_controller.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| 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 controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/equality" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| k8sclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
| logger "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
|
||
| kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| HypervisorInstanceHaControllerName = "HypervisorInstanceHa" | ||
| ) | ||
|
|
||
| type HypervisorInstanceHaController struct { | ||
| k8sclient.Client | ||
| Scheme *runtime.Scheme | ||
| } | ||
|
|
||
| // +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors,verbs=get;list;watch;update;patch | ||
| // +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/status,verbs=get;list;watch;update;patch | ||
| func (r *HypervisorInstanceHaController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| log := logger.FromContext(ctx).WithName(req.Name) | ||
| ctx = logger.IntoContext(ctx, log) | ||
|
|
||
| hv := &kvmv1.Hypervisor{} | ||
| if err := r.Get(ctx, req.NamespacedName, hv); err != nil { | ||
| // ignore not found errors, could be deleted | ||
| return ctrl.Result{}, k8sclient.IgnoreNotFound(err) | ||
| } | ||
|
|
||
| onboardingCondition := meta.FindStatusCondition(hv.Status.Conditions, kvmv1.ConditionTypeOnboarding) | ||
|
|
||
| if onboardingCondition == nil { | ||
| return ctrl.Result{}, nil // Onboarding not started, so no hypervisor and nothing to do | ||
| } | ||
|
|
||
| old := hv.DeepCopy() | ||
|
|
||
| // Determine if HA should be disabled and the reason | ||
| evicted := meta.IsStatusConditionFalse(hv.Status.Conditions, kvmv1.ConditionTypeEvicting) | ||
| testing := onboardingCondition.Status == metav1.ConditionTrue && | ||
| onboardingCondition.Reason != kvmv1.ConditionReasonHandover // Onboarding still testing (not yet in Handover phase) | ||
| aborted := onboardingCondition.Status == metav1.ConditionFalse && | ||
| onboardingCondition.Reason == kvmv1.ConditionReasonAborted // Onboarding was aborted | ||
| shouldDisable := !hv.Spec.HighAvailability || // HA not requested | ||
| evicted || // HA not needed as it is empty | ||
| testing || // HA not needed for test VMs | ||
| aborted // HA not needed when onboarding aborted | ||
|
|
||
| if shouldDisable { | ||
| // Determine the reason based on why HA is being disabled | ||
| var reason string | ||
| var message string | ||
| switch { | ||
| case evicted: | ||
| reason = kvmv1.ConditionReasonHaEvicted | ||
| message = "HA disabled due to eviction" | ||
| case testing, aborted: | ||
| reason = kvmv1.ConditionReasonHaOnboarding | ||
| message = "HA disabled before onboarding" | ||
| default: | ||
| reason = kvmv1.ConditionReasonSucceeded | ||
| message = "HA disabled per spec" | ||
| } | ||
|
|
||
| if !meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{ | ||
| Type: kvmv1.ConditionTypeHaEnabled, | ||
| Status: metav1.ConditionFalse, | ||
| Message: message, | ||
| Reason: reason, | ||
| }) { | ||
| // Desired state already achieved | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| if err := disableInstanceHA(hv); err != nil { | ||
| meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{ | ||
| Type: kvmv1.ConditionTypeHaEnabled, | ||
| Status: metav1.ConditionUnknown, | ||
| Message: err.Error(), | ||
| Reason: kvmv1.ConditionReasonFailed, | ||
| }) | ||
|
|
||
| if patchErr := r.Status().Patch(ctx, hv, k8sclient.MergeFromWithOptions(old, k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(HypervisorInstanceHaControllerName)); patchErr != nil { | ||
| return ctrl.Result{}, errors.Join(err, patchErr) | ||
| } | ||
| return ctrl.Result{}, err | ||
| } | ||
| } else { | ||
| if !meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{ | ||
| Type: kvmv1.ConditionTypeHaEnabled, | ||
| Status: metav1.ConditionTrue, | ||
| Message: "HA is enabled", | ||
| Reason: kvmv1.ConditionReasonSucceeded, | ||
| }) { | ||
| // Desired state already achieved | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| if err := enableInstanceHA(hv); err != nil { | ||
| meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{ | ||
| Type: kvmv1.ConditionTypeHaEnabled, | ||
| Status: metav1.ConditionUnknown, | ||
| Message: err.Error(), | ||
| Reason: kvmv1.ConditionReasonFailed, | ||
| }) | ||
|
|
||
| if patchErr := r.Status().Patch(ctx, hv, k8sclient.MergeFromWithOptions(old, k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(HypervisorInstanceHaControllerName)); patchErr != nil { | ||
| return ctrl.Result{}, errors.Join(err, patchErr) | ||
| } | ||
| return ctrl.Result{}, err | ||
| } | ||
| } | ||
|
|
||
| if equality.Semantic.DeepEqual(hv, old) { | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| return ctrl.Result{}, r.Status().Patch(ctx, hv, k8sclient.MergeFromWithOptions(old, k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(HypervisorInstanceHaControllerName)) | ||
| } | ||
|
|
||
| // SetupWithManager sets up the controller with the Manager. | ||
| func (r *HypervisorInstanceHaController) SetupWithManager(mgr ctrl.Manager) error { | ||
| ctx := context.Background() | ||
| _ = logger.FromContext(ctx) | ||
|
|
||
| return ctrl.NewControllerManagedBy(mgr). | ||
| Named(HypervisorInstanceHaControllerName). | ||
| For(&kvmv1.Hypervisor{}). // trigger the r.Reconcile whenever a hypervisor is created/updated/deleted. | ||
| Complete(r) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not really helpful condition type. Either you want a status bool that reflects the HA service (is-state) of ha enabled or not, and/or you introduce a condition which reflects the communication to the HA service (e.g. HighAvailabilitySynced), which is false in case something gone wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow. The condition should do exactly that: It should reflect the current state of HA being enabled or not.
I could add that as well, but I was hoping that this controller would be only temporary, and that condition would then never get set, as the HA service would reflect itself it HA actually enabled, or not.