Skip to content

Commit e20f167

Browse files
authored
fix(vm): watch to provisioning secrets (#1820)
Signed-off-by: Valeriy Khorunzhin <[email protected]>
1 parent 9eec71f commit e20f167

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

images/virtualization-artifact/pkg/controller/indexer/indexer.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const (
5959
IndexFieldVMMACLeaseByVMMAC = "spec.virtualMachineMACAddressRef.Name"
6060

6161
IndexFieldVMIPLeaseByVMIP = "spec.virtualMachineIPAddressRef"
62+
63+
IndexFieldVMByProvisioningSecret = "spec.provisioning.secretRef"
6264
)
6365

6466
var IndexGetters = []IndexGetter{
@@ -67,6 +69,7 @@ var IndexGetters = []IndexGetter{
6769
IndexVMByVI,
6870
IndexVMByCVI,
6971
IndexVMByNode,
72+
IndexVMByProvisioningSecret,
7073
IndexVMSnapshotByVM,
7174
IndexVMSnapshotByVDSnapshot,
7275
IndexVMRestoreByVMSnapshot,
@@ -134,6 +137,25 @@ func IndexVMByNode() (obj client.Object, field string, extractValue client.Index
134137
}
135138
}
136139

140+
func IndexVMByProvisioningSecret() (obj client.Object, field string, extractValue client.IndexerFunc) {
141+
return &v1alpha2.VirtualMachine{}, IndexFieldVMByProvisioningSecret, func(object client.Object) []string {
142+
vm, ok := object.(*v1alpha2.VirtualMachine)
143+
if !ok || vm == nil || vm.Spec.Provisioning == nil {
144+
return nil
145+
}
146+
147+
var secrets []string
148+
if vm.Spec.Provisioning.UserDataRef != nil && vm.Spec.Provisioning.UserDataRef.Kind == v1alpha2.UserDataRefKindSecret {
149+
secrets = append(secrets, vm.Spec.Provisioning.UserDataRef.Name)
150+
}
151+
if vm.Spec.Provisioning.SysprepRef != nil && vm.Spec.Provisioning.SysprepRef.Kind == v1alpha2.SysprepRefKindSecret {
152+
secrets = append(secrets, vm.Spec.Provisioning.SysprepRef.Name)
153+
}
154+
155+
return secrets
156+
}
157+
}
158+
137159
func getBlockDeviceNamesByKind(obj client.Object, kind v1alpha2.BlockDeviceKind) []string {
138160
vm, ok := obj.(*v1alpha2.VirtualMachine)
139161
if !ok || vm == nil {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

images/virtualization-artifact/pkg/controller/vm/vm_reconciler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr
7373
watcher.NewVirtualMachineSnapshotWatcher(),
7474
watcher.NewVMOPWatcher(),
7575
watcher.NewVMMACWatcher(),
76+
watcher.NewSecretWatcher(mgr.GetClient()),
7677
} {
7778
err := w.Watch(mgr, ctr)
7879
if err != nil {

0 commit comments

Comments
 (0)