Skip to content

Commit bfae915

Browse files
committed
wip
Signed-off-by: Daniil Loktev <[email protected]>
1 parent 6cc4d6d commit bfae915

File tree

5 files changed

+226
-16
lines changed

5 files changed

+226
-16
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121

2222
corev1 "k8s.io/api/core/v1"
23+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"k8s.io/apimachinery/pkg/types"
2526
"k8s.io/utils/ptr"
@@ -30,6 +31,7 @@ import (
3031
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3132
podutil "github.com/deckhouse/virtualization-controller/pkg/common/pod"
3233
"github.com/deckhouse/virtualization-controller/pkg/common/provisioner"
34+
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
3335
)
3436

3537
type Bounder struct {
@@ -168,5 +170,16 @@ type PodNamer interface {
168170
}
169171

170172
func FindPod(ctx context.Context, client client.Client, name PodNamer) (*corev1.Pod, error) {
171-
return object.FetchObject(ctx, name.BounderPod(), client, &corev1.Pod{})
173+
pod, err := object.FetchObject(ctx, name.BounderPod(), client, &corev1.Pod{})
174+
if err == nil || !k8serrors.IsNotFound(err) {
175+
return pod, err
176+
}
177+
178+
// Try legacy naming for backward compatibility
179+
if gen, ok := name.(*supplements.Generator); ok {
180+
legacyGen := supplements.NewLegacyGenerator(gen.Prefix, gen.Name, gen.Namespace, gen.UID)
181+
return object.FetchObject(ctx, legacyGen.BounderPod(), client, &corev1.Pod{})
182+
}
183+
184+
return nil, err
172185
}

images/virtualization-artifact/pkg/controller/importer/importer_pod.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3535
podutil "github.com/deckhouse/virtualization-controller/pkg/common/pod"
3636
"github.com/deckhouse/virtualization-controller/pkg/common/provisioner"
37+
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
3738
)
3839

3940
const (
@@ -411,5 +412,16 @@ type PodNamer interface {
411412
}
412413

413414
func FindPod(ctx context.Context, client client.Client, name PodNamer) (*corev1.Pod, error) {
414-
return object.FetchObject(ctx, name.ImporterPod(), client, &corev1.Pod{})
415+
pod, err := object.FetchObject(ctx, name.ImporterPod(), client, &corev1.Pod{})
416+
if err == nil || !k8serrors.IsNotFound(err) {
417+
return pod, err
418+
}
419+
420+
// Try legacy naming for backward compatibility
421+
if gen, ok := name.(*supplements.Generator); ok {
422+
legacyGen := supplements.NewLegacyGenerator(gen.Prefix, gen.Name, gen.Namespace, gen.UID)
423+
return object.FetchObject(ctx, legacyGen.ImporterPod(), client, &corev1.Pod{})
424+
}
425+
426+
return nil, err
415427
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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 supplements
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
corev1 "k8s.io/api/core/v1"
24+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
25+
"k8s.io/apimachinery/pkg/types"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
)
28+
29+
// LegacyGenerator generates names in the old format for backward compatibility
30+
type LegacyGenerator struct {
31+
*Generator
32+
}
33+
34+
func NewLegacyGenerator(prefix, name, namespace string, uid types.UID) *LegacyGenerator {
35+
return &LegacyGenerator{
36+
Generator: NewGenerator(prefix, name, namespace, uid),
37+
}
38+
}
39+
40+
// DVCRAuthSecret returns old format name for auth Secret copy.
41+
func (g *LegacyGenerator) DVCRAuthSecret() types.NamespacedName {
42+
name := fmt.Sprintf("%s-dvcr-auth-%s", g.Prefix, g.Name)
43+
return g.shortenNamespaced(name)
44+
}
45+
46+
// DVCRAuthSecretForDV returns old format name for auth Secret copy
47+
// compatible with DataVolume: with accessKeyId and secretKey fields.
48+
func (g *LegacyGenerator) DVCRAuthSecretForDV() types.NamespacedName {
49+
name := fmt.Sprintf("%s-dvcr-auth-dv-%s", g.Prefix, g.Name)
50+
return g.shortenNamespaced(name)
51+
}
52+
53+
// DVCRCABundleConfigMapForDV returns old format name for ConfigMap with ca.crt.
54+
func (g *LegacyGenerator) DVCRCABundleConfigMapForDV() types.NamespacedName {
55+
name := fmt.Sprintf("%s-dvcr-ca-dv-%s", g.Prefix, g.Name)
56+
return g.shortenNamespaced(name)
57+
}
58+
59+
// CABundleConfigMap returns old format name for ConfigMap which contains caBundle from dataSource.
60+
func (g *LegacyGenerator) CABundleConfigMap() types.NamespacedName {
61+
name := fmt.Sprintf("%s-ca-%s", g.Prefix, g.Name)
62+
return g.shortenNamespaced(name)
63+
}
64+
65+
// ImagePullSecret returns old format name for image pull secret for the containerImage dataSource.
66+
func (g *LegacyGenerator) ImagePullSecret() types.NamespacedName {
67+
name := fmt.Sprintf("%s-pull-image-%s", g.Prefix, g.Name)
68+
return g.shortenNamespaced(name)
69+
}
70+
71+
// ImporterPod generates old format name for importer Pod.
72+
func (g *LegacyGenerator) ImporterPod() types.NamespacedName {
73+
name := fmt.Sprintf("%s-importer-%s", g.Prefix, g.Name)
74+
return g.shortenNamespaced(name)
75+
}
76+
77+
// BounderPod generates old format name for bounder Pod.
78+
func (g *LegacyGenerator) BounderPod() types.NamespacedName {
79+
name := fmt.Sprintf("%s-bounder-%s", g.Prefix, g.Name)
80+
return g.shortenNamespaced(name)
81+
}
82+
83+
// UploaderPod generates old format name for uploader Pod.
84+
func (g *LegacyGenerator) UploaderPod() types.NamespacedName {
85+
name := fmt.Sprintf("%s-uploader-%s", g.Prefix, g.Name)
86+
return g.shortenNamespaced(name)
87+
}
88+
89+
// UploaderService generates old format name for uploader Service.
90+
func (g *LegacyGenerator) UploaderService() types.NamespacedName {
91+
name := fmt.Sprintf("%s-uploader-svc-%s", g.Prefix, string(g.UID))
92+
return g.shortenNamespaced(name)
93+
}
94+
95+
// UploaderIngress generates old format name for uploader Ingress.
96+
func (g *LegacyGenerator) UploaderIngress() types.NamespacedName {
97+
name := fmt.Sprintf("%s-uploader-ingress-%s", g.Prefix, string(g.UID))
98+
return g.shortenNamespaced(name)
99+
}
100+
101+
// UploaderTLSSecretForIngress generates old format name for uploader tls secret.
102+
func (g *LegacyGenerator) UploaderTLSSecretForIngress() types.NamespacedName {
103+
name := fmt.Sprintf("%s-uploader-tls-ing-%s", g.Prefix, g.Name)
104+
return g.shortenNamespaced(name)
105+
}
106+
107+
// DataVolume generates old format name for underlying DataVolume.
108+
// DataVolume is always one for vmd/vmi, so prefix is used.
109+
func (g *LegacyGenerator) DataVolume() types.NamespacedName {
110+
dvName := fmt.Sprintf("%s-%s-%s", g.Prefix, g.Name, string(g.UID))
111+
return g.shortenNamespaced(dvName)
112+
}
113+
114+
func (g *LegacyGenerator) PersistentVolumeClaim() types.NamespacedName {
115+
return g.DataVolume()
116+
}
117+
118+
// NetworkPolicy generates old format name for NetworkPolicy.
119+
func (g *LegacyGenerator) NetworkPolicy() types.NamespacedName {
120+
// Old network policies used DataVolume/Pod names directly
121+
return g.DataVolume()
122+
}
123+
124+
// FindResourceWithFallback attempts to find a resource with new naming,
125+
// falling back to old naming if not found
126+
func FindResourceWithFallback[T client.Object](ctx context.Context, c client.Client, newName, oldName types.NamespacedName, obj T) error {
127+
// Try new name first
128+
err := c.Get(ctx, newName, obj)
129+
if err == nil {
130+
return nil
131+
}
132+
133+
if !k8serrors.IsNotFound(err) {
134+
return err
135+
}
136+
137+
// Fallback to old name
138+
return c.Get(ctx, oldName, obj)
139+
}
140+
141+
// GetPodWithFallback attempts to find a pod with new naming,
142+
// falling back to old naming if not found
143+
func GetPodWithFallback(ctx context.Context, c client.Client, gen *Generator) (*corev1.Pod, error) {
144+
pod := &corev1.Pod{}
145+
legacyGen := NewLegacyGenerator(gen.Prefix, gen.Name, gen.Namespace, gen.UID)
146+
147+
// Try to find importer pod
148+
err := FindResourceWithFallback(ctx, c, gen.ImporterPod(), legacyGen.ImporterPod(), pod)
149+
if err == nil {
150+
return pod, nil
151+
}
152+
153+
// Try to find uploader pod
154+
err = FindResourceWithFallback(ctx, c, gen.UploaderPod(), legacyGen.UploaderPod(), pod)
155+
if err == nil {
156+
return pod, nil
157+
}
158+
159+
// Try to find bounder pod
160+
err = FindResourceWithFallback(ctx, c, gen.BounderPod(), legacyGen.BounderPod(), pod)
161+
if err == nil {
162+
return pod, nil
163+
}
164+
165+
return nil, err
166+
}
167+

images/virtualization-artifact/pkg/controller/supplements/generator.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,82 +43,88 @@ func NewGenerator(prefix, name, namespace string, uid types.UID) *Generator {
4343

4444
// DVCRAuthSecret returns name and namespace for auth Secret copy.
4545
func (g *Generator) DVCRAuthSecret() types.NamespacedName {
46-
name := fmt.Sprintf("%s-dvcr-auth-%s", g.Prefix, g.Name)
46+
name := fmt.Sprintf("d8v-%s-dvcr-auth-%s-%s", g.Prefix, g.Name, g.UID)
4747
return g.shortenNamespaced(name)
4848
}
4949

5050
// DVCRAuthSecretForDV returns name and namespace for auth Secret copy
5151
// compatible with DataVolume: with accessKeyId and secretKey fields.
5252
func (g *Generator) DVCRAuthSecretForDV() types.NamespacedName {
53-
name := fmt.Sprintf("%s-dvcr-auth-dv-%s", g.Prefix, g.Name)
53+
name := fmt.Sprintf("d8v-%s-dvcr-auth-%s-%s", g.Prefix, g.Name, g.UID)
5454
return g.shortenNamespaced(name)
5555
}
5656

5757
// DVCRCABundleConfigMapForDV returns name and namespace for ConfigMap with ca.crt.
5858
func (g *Generator) DVCRCABundleConfigMapForDV() types.NamespacedName {
59-
name := fmt.Sprintf("%s-dvcr-ca-dv-%s", g.Prefix, g.Name)
59+
name := fmt.Sprintf("d8v-%s-dvcr-ca-%s-%s", g.Prefix, g.Name, g.UID)
6060
return g.shortenNamespaced(name)
6161
}
6262

6363
// CABundleConfigMap returns name and namespace for ConfigMap which contains caBundle from dataSource.
6464
func (g *Generator) CABundleConfigMap() types.NamespacedName {
65-
name := fmt.Sprintf("%s-ca-%s", g.Prefix, g.Name)
65+
name := fmt.Sprintf("d8v-%s-ca-%s-%s", g.Prefix, g.Name, g.UID)
6666
return g.shortenNamespaced(name)
6767
}
6868

6969
// ImagePullSecret returns name and namespace for image pull secret for the containerImage dataSource.
7070
func (g *Generator) ImagePullSecret() types.NamespacedName {
71-
name := fmt.Sprintf("%s-pull-image-%s", g.Prefix, g.Name)
71+
name := fmt.Sprintf("d8v-%s-pull-image-%s-%s", g.Prefix, g.Name, g.UID)
7272
return g.shortenNamespaced(name)
7373
}
7474

7575
// ImporterPod generates name for importer Pod.
7676
func (g *Generator) ImporterPod() types.NamespacedName {
77-
name := fmt.Sprintf("%s-importer-%s", g.Prefix, g.Name)
77+
name := fmt.Sprintf("d8v-%s-importer-%s-%s", g.Prefix, g.Name, g.UID)
7878
return g.shortenNamespaced(name)
7979
}
8080

81-
// ImporterPod generates name for importer Pod.
81+
// BounderPod generates name for bounder Pod.
8282
func (g *Generator) BounderPod() types.NamespacedName {
83-
name := fmt.Sprintf("%s-bounder-%s", g.Prefix, g.Name)
83+
name := fmt.Sprintf("d8v-%s-bounder-%s-%s", g.Prefix, g.Name, g.UID)
8484
return g.shortenNamespaced(name)
8585
}
8686

8787
// UploaderPod generates name for uploader Pod.
8888
func (g *Generator) UploaderPod() types.NamespacedName {
89-
name := fmt.Sprintf("%s-uploader-%s", g.Prefix, g.Name)
89+
name := fmt.Sprintf("d8v-%s-uploader-%s-%s", g.Prefix, g.Name, g.UID)
9090
return g.shortenNamespaced(name)
9191
}
9292

9393
// UploaderService generates name for uploader Service.
9494
func (g *Generator) UploaderService() types.NamespacedName {
95-
name := fmt.Sprintf("%s-uploader-svc-%s", g.Prefix, g.UID)
95+
name := fmt.Sprintf("d8v-%s-%s-%s", g.Prefix, g.Name, g.UID)
9696
return g.shortenNamespaced(name)
9797
}
9898

9999
// UploaderIngress generates name for uploader Ingress.
100100
func (g *Generator) UploaderIngress() types.NamespacedName {
101-
name := fmt.Sprintf("%s-uploader-ingress-%s", g.Prefix, g.UID)
101+
name := fmt.Sprintf("d8v-%s-%s-%s", g.Prefix, g.Name, g.UID)
102102
return g.shortenNamespaced(name)
103103
}
104104

105105
// UploaderTLSSecretForIngress generates name for uploader tls secret.
106106
func (g *Generator) UploaderTLSSecretForIngress() types.NamespacedName {
107-
name := fmt.Sprintf("%s-uploader-tls-ing-%s", g.Prefix, g.Name)
107+
name := fmt.Sprintf("d8v-%s-tls-%s-%s", g.Prefix, g.Name, g.UID)
108108
return g.shortenNamespaced(name)
109109
}
110110

111111
// DataVolume generates name for underlying DataVolume.
112112
// DataVolume is always one for vmd/vmi, so prefix is used.
113113
func (g *Generator) DataVolume() types.NamespacedName {
114-
dvName := fmt.Sprintf("%s-%s-%s", g.Prefix, g.Name, g.UID)
114+
dvName := fmt.Sprintf("d8v-%s-%s-%s", g.Prefix, g.Name, g.UID)
115115
return g.shortenNamespaced(dvName)
116116
}
117117

118118
func (g *Generator) PersistentVolumeClaim() types.NamespacedName {
119119
return g.DataVolume()
120120
}
121121

122+
// NetworkPolicy generates name for NetworkPolicy.
123+
func (g *Generator) NetworkPolicy() types.NamespacedName {
124+
name := fmt.Sprintf("d8v-%s-%s-%s", g.Prefix, g.Name, g.UID)
125+
return g.shortenNamespaced(name)
126+
}
127+
122128
func (g *Generator) shortenNamespaced(name string) types.NamespacedName {
123129
return types.NamespacedName{
124130
Name: strings.ShortenString(name, kvalidation.DNS1123SubdomainMaxLength),

images/virtualization-artifact/pkg/controller/uploader/uploader_pod.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3434
podutil "github.com/deckhouse/virtualization-controller/pkg/common/pod"
3535
"github.com/deckhouse/virtualization-controller/pkg/common/provisioner"
36+
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
3637
)
3738

3839
const (
@@ -219,5 +220,16 @@ type PodNamer interface {
219220
}
220221

221222
func FindPod(ctx context.Context, client client.Client, name PodNamer) (*corev1.Pod, error) {
222-
return object.FetchObject(ctx, name.UploaderPod(), client, &corev1.Pod{})
223+
pod, err := object.FetchObject(ctx, name.UploaderPod(), client, &corev1.Pod{})
224+
if err == nil || !k8serrors.IsNotFound(err) {
225+
return pod, err
226+
}
227+
228+
// Try legacy naming for backward compatibility
229+
if gen, ok := name.(*supplements.Generator); ok {
230+
legacyGen := supplements.NewLegacyGenerator(gen.Prefix, gen.Name, gen.Namespace, gen.UID)
231+
return object.FetchObject(ctx, legacyGen.UploaderPod(), client, &corev1.Pod{})
232+
}
233+
234+
return nil, err
223235
}

0 commit comments

Comments
 (0)