diff --git a/pkg/virt-operator/install-strategy/strategy.go b/pkg/virt-operator/install-strategy/strategy.go index e66785ce13fb..052f9bb95527 100644 --- a/pkg/virt-operator/install-strategy/strategy.go +++ b/pkg/virt-operator/install-strategy/strategy.go @@ -267,6 +267,22 @@ func GenerateCurrentInstallStrategy(namespace string, return strategy, nil } +func mostRecentConfigMap(configMaps []*corev1.ConfigMap) *corev1.ConfigMap { + var configMap *corev1.ConfigMap + // choose the most recent configmap if multiple match. + mostRecentTime := metav1.Time{} + for _, config := range configMaps { + if configMap == nil { + configMap = config + mostRecentTime = config.ObjectMeta.CreationTimestamp + } else if mostRecentTime.Before(&config.ObjectMeta.CreationTimestamp) { + configMap = config + mostRecentTime = config.ObjectMeta.CreationTimestamp + } + } + return configMap +} + func LoadInstallStrategyFromCache(stores util.Stores, namespace string, imageTag string, imageRegistry string) (*InstallStrategy, error) { var configMap *corev1.ConfigMap var matchingConfigMaps []*corev1.ConfigMap @@ -292,16 +308,7 @@ func LoadInstallStrategyFromCache(stores util.Stores, namespace string, imageTag } // choose the most recent configmap if multiple match. - mostRecentTime := metav1.Time{} - for _, config := range matchingConfigMaps { - if configMap == nil { - configMap = config - mostRecentTime = config.ObjectMeta.CreationTimestamp - } else if mostRecentTime.Before(&config.ObjectMeta.CreationTimestamp) { - configMap = config - mostRecentTime = config.ObjectMeta.CreationTimestamp - } - } + configMap = mostRecentConfigMap(matchingConfigMaps) data, ok := configMap.Data["manifests"] if !ok { diff --git a/pkg/virt-operator/install-strategy/strategy_test.go b/pkg/virt-operator/install-strategy/strategy_test.go index 1426b074baf0..33e1b06e6bcb 100644 --- a/pkg/virt-operator/install-strategy/strategy_test.go +++ b/pkg/virt-operator/install-strategy/strategy_test.go @@ -31,6 +31,7 @@ import ( corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" extv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) var _ = Describe("Install Strategy", func() { @@ -154,4 +155,32 @@ var _ = Describe("Install Strategy", func() { } }) }) + + Context("should match", func() { + It("the most recent install strategy.", func() { + var configMaps []*corev1.ConfigMap + + configMaps = append(configMaps, &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test1", + CreationTimestamp: metav1.Time{}, + }, + }) + configMaps = append(configMaps, &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test2", + CreationTimestamp: metav1.Now(), + }, + }) + configMaps = append(configMaps, &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test3", + CreationTimestamp: metav1.Time{}, + }, + }) + + configMap := mostRecentConfigMap(configMaps) + Expect(configMap.Name).To(Equal("test2")) + }) + }) })