• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubevirt / containerized-data-importer / #5728

24 Dec 2025 09:03AM UTC coverage: 49.419% (-9.3%) from 58.699%
#5728

Pull #3993

travis-ci

Acedus
build: bump linters version & make format

Signed-off-by: Adi Aloni <aaloni@redhat.com>
Pull Request #3993: Build: bump to go 1.24.0

14598 of 29539 relevant lines covered (49.42%)

0.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/operator/api.go
1
/*
2
Copyright 2018 The CDI Authors.
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 operator
18

19
import (
20
        "context"
21
        "fmt"
22

23
        corev1 "k8s.io/api/core/v1"
24
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
        "k8s.io/apimachinery/pkg/types"
26
        "k8s.io/client-go/kubernetes"
27
        "k8s.io/klog/v2"
28

29
        "sigs.k8s.io/controller-runtime/pkg/client"
30

31
        cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
32
        cc "kubevirt.io/containerized-data-importer/pkg/controller/common"
33
        "kubevirt.io/containerized-data-importer/pkg/operator/resources/cert"
34
        "kubevirt.io/containerized-data-importer/pkg/util"
35
)
36

37
const (
38
        // ConfigMapName is the name of the CDI Operator config map
39
        // used to determine which CDI instance is "active"
40
        // and maybe other stuff some day in the future
41
        ConfigMapName = "cdi-config"
42
)
43

44
// SetOwnerRuntime makes the current "active" CDI CR the owner of the object using runtime lib client
45
func SetOwnerRuntime(client client.Client, object metav1.Object) error {
×
46
        namespace := util.GetNamespace()
×
47
        configMap := &corev1.ConfigMap{}
×
48
        if err := client.Get(context.TODO(), types.NamespacedName{Name: ConfigMapName, Namespace: namespace}, configMap); err != nil {
×
49
                klog.Warningf("ConfigMap %s does not exist, so not assigning owner", ConfigMapName)
×
50
                return nil
×
51
        }
×
52
        return SetConfigAsOwner(configMap, object)
×
53
}
54

55
// SetOwner makes the current "active" CDI CR the owner of the object
56
func SetOwner(client kubernetes.Interface, object metav1.Object) error {
×
57
        namespace := util.GetNamespace()
×
58
        configMap, err := client.CoreV1().ConfigMaps(namespace).Get(context.TODO(), ConfigMapName, metav1.GetOptions{})
×
59
        if err != nil {
×
60
                klog.Warningf("ConfigMap %s does not exist, so not assigning owner", ConfigMapName)
×
61
                return nil
×
62
        }
×
63
        return SetConfigAsOwner(configMap, object)
×
64
}
65

66
// SetConfigAsOwner sets the passed in config map as owner of the object
67
func SetConfigAsOwner(configMap *corev1.ConfigMap, object metav1.Object) error {
×
68
        configMapOwner := getController(configMap.GetOwnerReferences())
×
69

×
70
        if configMapOwner == nil {
×
71
                return fmt.Errorf("configmap has no owner")
×
72
        }
×
73

74
        for _, o := range object.GetOwnerReferences() {
×
75
                if o.Controller != nil && *o.Controller {
×
76
                        if o.UID == configMapOwner.UID {
×
77
                                // already set to current obj
×
78
                                return nil
×
79
                        }
×
80

81
                        return fmt.Errorf("object %+v already owned by %+v", object, o)
×
82
                }
83
        }
84

85
        object.SetOwnerReferences(append(object.GetOwnerReferences(), *configMapOwner))
×
86

×
87
        return nil
×
88
}
89

90
// GetCertConfigWithDefaults returns the CDI cert config with default values when not set
91
func GetCertConfigWithDefaults(ctx context.Context, c client.Client) (*cdiv1.CDICertConfig, error) {
×
92
        cdi, err := cc.GetActiveCDI(ctx, c)
×
93
        if err != nil {
×
94
                return nil, err
×
95
        }
×
96

97
        certConfig := cdi.Spec.CertConfig
×
98
        if certConfig == nil {
×
99
                certConfig = &cdiv1.CDICertConfig{}
×
100
        }
×
101

102
        if certConfig.CA == nil || certConfig.CA.Duration == nil || certConfig.CA.RenewBefore == nil {
×
103
                certConfig.CA = &cdiv1.CertConfig{
×
104
                        Duration:    &metav1.Duration{Duration: cert.SignerLifetime},
×
105
                        RenewBefore: &metav1.Duration{Duration: cert.SignerLifetime - cert.SignerRefresh},
×
106
                }
×
107
        }
×
108

109
        if certConfig.Server == nil || certConfig.Server.Duration == nil || certConfig.Server.RenewBefore == nil {
×
110
                certConfig.Server = &cdiv1.CertConfig{
×
111
                        Duration:    &metav1.Duration{Duration: cert.ServerLifetime},
×
112
                        RenewBefore: &metav1.Duration{Duration: cert.ServerLifetime - cert.ServerRefresh},
×
113
                }
×
114
        }
×
115

116
        if certConfig.Client == nil || certConfig.Client.Duration == nil || certConfig.Client.RenewBefore == nil {
×
117
                certConfig.Client = &cdiv1.CertConfig{
×
118
                        Duration:    &metav1.Duration{Duration: cert.ClientLifetime},
×
119
                        RenewBefore: &metav1.Duration{Duration: cert.ClientLifetime - cert.ClientRefresh},
×
120
                }
×
121
        }
×
122

123
        return certConfig, nil
×
124
}
125

126
func getController(owners []metav1.OwnerReference) *metav1.OwnerReference {
×
127
        for _, owner := range owners {
×
128
                if owner.Controller != nil && *owner.Controller {
×
129
                        return &owner
×
130
                }
×
131
        }
132
        return nil
×
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc