|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package watcher |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "log/slog" |
| 23 | + |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + "k8s.io/apimachinery/pkg/fields" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 35 | + |
| 36 | + "github.com/deckhouse/virtualization-controller/pkg/controller/indexer" |
| 37 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 38 | +) |
| 39 | + |
| 40 | +func NewSecretWatcher(client client.Client) *SecretWatcher { |
| 41 | + return &SecretWatcher{ |
| 42 | + client: client, |
| 43 | + logger: slog.Default().With("watcher", "secret"), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +type SecretWatcher struct { |
| 48 | + client client.Client |
| 49 | + logger *slog.Logger |
| 50 | +} |
| 51 | + |
| 52 | +func (w *SecretWatcher) Watch(mgr manager.Manager, ctr controller.Controller) error { |
| 53 | + if err := ctr.Watch( |
| 54 | + source.Kind( |
| 55 | + mgr.GetCache(), |
| 56 | + &corev1.Secret{}, |
| 57 | + handler.TypedEnqueueRequestsFromMapFunc(w.enqueue), |
| 58 | + predicate.TypedFuncs[*corev1.Secret]{ |
| 59 | + DeleteFunc: func(e event.TypedDeleteEvent[*corev1.Secret]) bool { return false }, |
| 60 | + }, |
| 61 | + ), |
| 62 | + ); err != nil { |
| 63 | + return fmt.Errorf("error setting watch on Secret: %w", err) |
| 64 | + } |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func (w *SecretWatcher) enqueue(ctx context.Context, secret *corev1.Secret) []reconcile.Request { |
| 69 | + var vms v1alpha2.VirtualMachineList |
| 70 | + err := w.client.List(ctx, &vms, &client.ListOptions{ |
| 71 | + Namespace: secret.Namespace, |
| 72 | + FieldSelector: fields.OneTermEqualSelector(indexer.IndexFieldVMByProvisioningSecret, secret.Name), |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + w.logger.Error(fmt.Sprintf("failed to list virtual machines: %v", err)) |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + var result []reconcile.Request |
| 80 | + for _, vm := range vms.Items { |
| 81 | + result = append(result, reconcile.Request{ |
| 82 | + NamespacedName: types.NamespacedName{ |
| 83 | + Name: vm.GetName(), |
| 84 | + Namespace: vm.GetNamespace(), |
| 85 | + }, |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + return result |
| 90 | +} |
0 commit comments