-
Notifications
You must be signed in to change notification settings - Fork 4
fix(vi): handle vi default storage class from module config #902
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ import ( | |
| "github.com/deckhouse/virtualization-controller/pkg/common/pointer" | ||
| "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" | ||
| "github.com/deckhouse/virtualization-controller/pkg/controller/kvbuilder" | ||
| mcapi "github.com/deckhouse/virtualization-controller/pkg/controller/moduleconfig/api" | ||
| "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" | ||
| "github.com/deckhouse/virtualization-controller/pkg/dvcr" | ||
| virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
|
|
@@ -608,8 +609,32 @@ func (s DiskService) GetStorageClass(ctx context.Context, storageClassName *stri | |
| } | ||
|
|
||
| func (s DiskService) GetDefaultStorageClass(ctx context.Context) (*storev1.StorageClass, error) { | ||
| var ( | ||
| moduleConfigViDefaultStorageClass string | ||
| moduleConfig mcapi.ModuleConfig | ||
| moduleConfigName = "virtualization" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can it change? |
||
| ) | ||
| err := s.client.Get(ctx, types.NamespacedName{Name: moduleConfigName}, &moduleConfig, &client.GetOptions{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if virtualImages, ok := moduleConfig.Spec.Settings["virtualImages"].(map[string]interface{}); ok { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not hardcode please, use constant not string literal (set it if needed), next line too
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "virtualImages" var seems like slice of VI, use virtualImagesSetting or something |
||
| if defaultClass, ok := virtualImages["defaultStorageClassName"].(string); ok { | ||
| moduleConfigViDefaultStorageClass = defaultClass | ||
| } | ||
| } | ||
|
|
||
| if moduleConfigViDefaultStorageClass != "" { | ||
| moduleConfigViDefaultStorageClassObj, err := s.getStorageClass(ctx, moduleConfigViDefaultStorageClass) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return moduleConfigViDefaultStorageClassObj, nil | ||
| } | ||
|
|
||
| var scs storev1.StorageClassList | ||
| err := s.client.List(ctx, &scs, &client.ListOptions{}) | ||
| err = s.client.List(ctx, &scs, &client.ListOptions{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| 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 watcher | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "k8s.io/apimachinery/pkg/fields" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| "sigs.k8s.io/controller-runtime/pkg/source" | ||
|
|
||
| "github.com/deckhouse/virtualization-controller/pkg/controller/indexer" | ||
| mcapi "github.com/deckhouse/virtualization-controller/pkg/controller/moduleconfig/api" | ||
| virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
| "github.com/deckhouse/virtualization/api/core/v1alpha2/vicondition" | ||
| ) | ||
|
|
||
| type ModuleConfigWatcher struct { | ||
| client client.Client | ||
| logger *slog.Logger | ||
| } | ||
|
|
||
| func NewModuleConfigWatcher(client client.Client) *StorageClassWatcher { | ||
| return &StorageClassWatcher{ | ||
| client: client, | ||
| logger: slog.Default().With("watcher", "moduleconfig"), | ||
| } | ||
| } | ||
|
|
||
| func (w ModuleConfigWatcher) Watch(mgr manager.Manager, ctr controller.Controller) error { | ||
| return ctr.Watch( | ||
| source.Kind(mgr.GetCache(), &mcapi.ModuleConfig{}), | ||
| handler.EnqueueRequestsFromMapFunc(w.enqueueRequests), | ||
| predicate.Funcs{ | ||
| CreateFunc: func(event event.CreateEvent) bool { return true }, | ||
| DeleteFunc: func(event event.DeleteEvent) bool { return true }, | ||
| UpdateFunc: func(event event.UpdateEvent) bool { | ||
| oldMc, oldOk := event.ObjectOld.(*mcapi.ModuleConfig) | ||
| newMc, newOk := event.ObjectNew.(*mcapi.ModuleConfig) | ||
| if !oldOk || !newOk { | ||
| return false | ||
| } | ||
| var ( | ||
| oldViDefaultSc string | ||
| newViDefaultSc string | ||
| ) | ||
| if virtualImages, ok := oldMc.Spec.Settings["virtualImages"].(map[string]interface{}); ok { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use consts, not string literals |
||
| if defaultClass, ok := virtualImages["defaultStorageClassName"].(string); ok { | ||
| oldViDefaultSc = defaultClass | ||
| } | ||
| } | ||
| if virtualImages, ok := newMc.Spec.Settings["virtualImages"].(map[string]interface{}); ok { | ||
| if defaultClass, ok := virtualImages["defaultStorageClassName"].(string); ok { | ||
| oldViDefaultSc = defaultClass | ||
| } | ||
| } | ||
| return oldViDefaultSc != newViDefaultSc | ||
| }, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| func (w ModuleConfigWatcher) enqueueRequests(ctx context.Context, object client.Object) []reconcile.Request { | ||
| var vis virtv2.VirtualImageList | ||
| err := w.client.List(ctx, &vis, &client.ListOptions{ | ||
| FieldSelector: fields.OneTermEqualSelector(indexer.IndexFieldVIByNotReadyStorageClass, string(vicondition.StorageClassReadyType)), | ||
| }) | ||
| if err != nil { | ||
| w.logger.Error(fmt.Sprintf("failed to list virtual images: %s", err)) | ||
| return []reconcile.Request{} | ||
| } | ||
|
|
||
| var requests []reconcile.Request | ||
| for _, vi := range vis.Items { | ||
| requests = append(requests, reconcile.Request{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Name: vi.Name, | ||
| Namespace: vi.Namespace, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| return requests | ||
| } | ||
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.
Why with this name you indexing by sc ready? And ignore non pvc VI please.