|
| 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 | + |
0 commit comments