-
Notifications
You must be signed in to change notification settings - Fork 2
feat(vm): add maintenance #1316
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
Show all changes
44 commits
Select commit
Hold shift + click to select a range
4476a33
add maintenance
yaroslavborbat b26c7be
add maintenance
yaroslavborbat 97f5222
add maintenance
loktev-d 957d57c
fixes
loktev-d 312c855
add annotation for testing
loktev-d def0204
fixes
loktev-d 43b0042
remove vmop
loktev-d bb19ec0
fixes
loktev-d 0b0c4d8
fixes
loktev-d 4a22538
fixes
loktev-d 625f874
fixes
loktev-d fecf8b0
fixes
loktev-d 651942a
fixes
loktev-d 6184931
fixes
loktev-d 13da17c
fixes
loktev-d c0abc7b
fixes
loktev-d bbc4fbd
add check for syncPowerState
loktev-d 3cafbe2
fixes
loktev-d f8ccf17
fixes
loktev-d 6f0dc0c
fixes
loktev-d 31e11db
fixes
loktev-d c3b7abb
fixes
loktev-d 6d6bcf7
fix linter errors
loktev-d f6ef9f3
fixes
loktev-d 074a805
fixes
loktev-d b2917b6
add comments
loktev-d 8256de6
add annotation for testing
loktev-d 12a30fe
Merge branch 'main' into feat/vm/maitance-mode
loktev-d eb1de7d
fixes
loktev-d 6ed6694
fixes
loktev-d ad1f841
fixes
loktev-d 6c3d8a1
fixes
loktev-d 013bceb
fixes
loktev-d 888f4c6
remove annotation
loktev-d 8eda7c5
fixes
loktev-d a2cd477
fixes
loktev-d ee6553e
Merge branch 'main' into feat/vm/maitance-mode
loktev-d ed14f1e
remove annotation
loktev-d a9993c0
fixes
loktev-d 9bc3856
Merge branch 'main' into feat/vm/maitance-mode
loktev-d a0bc1aa
Merge branch 'main' into feat/vm/maitance-mode
loktev-d 251798a
fixes
loktev-d 8c9adc2
fixes
loktev-d 0cf4ee3
Merge branch 'main' into feat/vm/maitance-mode
loktev-d 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
115 changes: 115 additions & 0 deletions
115
images/virtualization-artifact/pkg/controller/vm/internal/maintenance.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,115 @@ | ||
/* | ||
Copyright 2025 Flant JSC | ||
|
||
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 internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/deckhouse/virtualization-controller/pkg/common/object" | ||
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions" | ||
"github.com/deckhouse/virtualization-controller/pkg/controller/reconciler" | ||
"github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal/state" | ||
"github.com/deckhouse/virtualization-controller/pkg/logger" | ||
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" | ||
) | ||
|
||
const nameMaintenanceHandler = "MaintenanceHandler" | ||
|
||
type MaintenanceHandler struct { | ||
client client.Client | ||
} | ||
|
||
func NewMaintenanceHandler(client client.Client) *MaintenanceHandler { | ||
return &MaintenanceHandler{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (h *MaintenanceHandler) Handle(ctx context.Context, s state.VirtualMachineState) (reconcile.Result, error) { | ||
log := logger.FromContext(ctx) | ||
|
||
if s.VirtualMachine().IsEmpty() { | ||
return reconcile.Result{}, nil | ||
} | ||
changed := s.VirtualMachine().Changed() | ||
|
||
maintenance, _ := conditions.GetCondition(vmcondition.TypeMaintenance, changed.Status.Conditions) | ||
|
||
if maintenance.Status == metav1.ConditionFalse { | ||
conditions.RemoveCondition(vmcondition.TypeMaintenance, &changed.Status.Conditions) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
if maintenance.Status != metav1.ConditionTrue { | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
log.Info("Reconcile observe a VirtualMachine in maintenance mode") | ||
|
||
kvvmi, err := s.KVVMI(ctx) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("get KVVMI: %w", err) | ||
} | ||
|
||
// If VM is not stopped yet, wait for it to stop (SyncPowerStateHandler will handle stopping) | ||
if kvvmi != nil { | ||
log.Info("VM is still running, waiting for shutdown in maintenance mode") | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Hide all other conditions when in maintenance mode | ||
changed.Status.Conditions = []metav1.Condition{maintenance} | ||
|
||
log.Info("VM is stopped, cleaning up resources if any for maintenance mode") | ||
|
||
kvvm, err := s.KVVM(ctx) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("%w: get KVVM: %w", reconciler.ErrStopHandlerChain, err) | ||
} | ||
if kvvm != nil { | ||
log.Info("Deleting KVVM for maintenance mode") | ||
err = object.CleanupObject(ctx, h.client, kvvm) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("%w: delete KVVM: %w", reconciler.ErrStopHandlerChain, err) | ||
} | ||
} | ||
|
||
pods, err := s.Pods(ctx) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("%w: get pods: %w", reconciler.ErrStopHandlerChain, err) | ||
} | ||
if pods != nil && len(pods.Items) > 0 { | ||
log.Info("Deleting pods for maintenance mode") | ||
for i := range pods.Items { | ||
err = object.CleanupObject(ctx, h.client, &pods.Items[i]) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("%w: delete pod: %w", reconciler.ErrStopHandlerChain, err) | ||
} | ||
} | ||
} | ||
|
||
return reconcile.Result{}, reconciler.ErrStopHandlerChain | ||
} | ||
|
||
func (h *MaintenanceHandler) Name() string { | ||
return nameMaintenanceHandler | ||
} |
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
54 changes: 54 additions & 0 deletions
54
...es/virtualization-artifact/pkg/controller/vm/internal/validators/maintenance_validator.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,54 @@ | ||
/* | ||
Copyright 2025 Flant JSC | ||
|
||
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 validators | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions" | ||
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" | ||
) | ||
|
||
type MaintenanceValidator struct{} | ||
|
||
func NewMaintenanceValidator() *MaintenanceValidator { | ||
return &MaintenanceValidator{} | ||
} | ||
|
||
func (v *MaintenanceValidator) ValidateCreate(_ context.Context, _ *virtv2.VirtualMachine) (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
func (v *MaintenanceValidator) ValidateUpdate(_ context.Context, oldVM, newVM *virtv2.VirtualMachine) (admission.Warnings, error) { | ||
maintenance, _ := conditions.GetCondition(vmcondition.TypeMaintenance, oldVM.Status.Conditions) | ||
|
||
if maintenance.Status != metav1.ConditionTrue { | ||
return nil, nil | ||
} | ||
|
||
if !reflect.DeepEqual(oldVM.Spec, newVM.Spec) { | ||
return nil, fmt.Errorf("spec changes are not allowed while VirtualMachine is in maintenance mode") | ||
} | ||
|
||
return nil, nil | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.