@@ -11,6 +11,8 @@ import (
11
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
12
"k8s.io/apimachinery/pkg/util/intstr"
13
13
"k8s.io/utils/pointer"
14
+
15
+ "github.com/openshift/operator-framework-olm/pkg/manifests"
14
16
)
15
17
16
18
func getReplicas (ha bool ) int32 {
@@ -61,61 +63,91 @@ func getTopologyModeFromInfra(infra *configv1.Infrastructure) bool {
61
63
}
62
64
63
65
// ensureCSV is responsible for ensuring the state of the @csv ClusterServiceVersion custom
66
+ // resource matches that of the codified defaults and high availability configurations, where
67
+ // codified defaults are defined by the csv returned by the manifests.NewPackageServerCSV
68
+ // function.
69
+ func ensureCSV (log logr.Logger , image string , csv * olmv1alpha1.ClusterServiceVersion , highlyAvailableMode bool ) (bool , error ) {
70
+ expectedCSV , err := manifests .NewPackageServerCSV (
71
+ manifests .WithName (csv .Name ),
72
+ manifests .WithNamespace (csv .Namespace ),
73
+ manifests .WithImage (image ),
74
+ )
75
+ if err != nil {
76
+ return false , err
77
+ }
78
+
79
+ ensureCSVHighAvailability (image , expectedCSV , highlyAvailableMode )
80
+
81
+ var modified bool
82
+
83
+ for k , v := range expectedCSV .GetLabels () {
84
+ if csv .GetLabels () == nil {
85
+ csv .SetLabels (make (map [string ]string ))
86
+ }
87
+ if vv , ok := csv .GetLabels ()[k ]; ! ok || vv != v {
88
+ log .Info ("setting expected label" , "key" , k , "value" , v )
89
+ csv .ObjectMeta .Labels [k ] = v
90
+ modified = true
91
+ }
92
+ }
93
+
94
+ for k , v := range expectedCSV .GetAnnotations () {
95
+ if csv .GetAnnotations () == nil {
96
+ csv .SetAnnotations (make (map [string ]string ))
97
+ }
98
+ if vv , ok := csv .GetAnnotations ()[k ]; ! ok || vv != v {
99
+ log .Info ("setting expected annotation" , "key" , k , "value" , v )
100
+ csv .ObjectMeta .Annotations [k ] = v
101
+ modified = true
102
+ }
103
+ }
104
+
105
+ if ! reflect .DeepEqual (expectedCSV .Spec , csv .Spec ) {
106
+ log .Info ("updating csv spec" )
107
+ csv .Spec = expectedCSV .Spec
108
+ modified = true
109
+ }
110
+
111
+ if modified {
112
+ log .V (3 ).Info ("csv has been modified" )
113
+ }
114
+
115
+ return modified , err
116
+ }
117
+
118
+ // ensureCSVHighAvailability is responsible for ensuring the state of the @csv ClusterServiceVersion custom
64
119
// resource matches the expected state based on any high availability expectations being exposed.
65
- func ensureCSV ( log logr. Logger , image string , csv * olmv1alpha1.ClusterServiceVersion , highlyAvailableMode bool ) bool {
120
+ func ensureCSVHighAvailability ( image string , csv * olmv1alpha1.ClusterServiceVersion , highlyAvailableMode bool ) {
66
121
var modified bool
67
122
68
123
deploymentSpecs := csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs
69
124
deployment := & deploymentSpecs [0 ].Spec
70
125
71
126
currentImage := deployment .Template .Spec .Containers [0 ].Image
72
127
if currentImage != image {
73
- log .Info ("updating the image" , "old" , currentImage , "new" , image )
74
128
deployment .Template .Spec .Containers [0 ].Image = image
75
129
modified = true
76
130
}
77
131
78
132
expectedReplicas := getReplicas (highlyAvailableMode )
79
133
if * deployment .Replicas != expectedReplicas {
80
- log .Info ("updating the replica count" , "old" , deployment .Replicas , "new" , expectedReplicas )
81
134
deployment .Replicas = pointer .Int32Ptr (expectedReplicas )
82
135
modified = true
83
136
}
84
137
85
138
expectedRolloutConfiguration := getRolloutStrategy (highlyAvailableMode )
86
139
if ! reflect .DeepEqual (deployment .Strategy .RollingUpdate , expectedRolloutConfiguration ) {
87
- log .Info ("updating the rollout strategy" )
88
140
deployment .Strategy .RollingUpdate = expectedRolloutConfiguration
89
141
modified = true
90
142
}
91
143
92
144
expectedAffinityConfiguration := getAntiAffinityConfig (highlyAvailableMode )
93
145
if ! reflect .DeepEqual (deployment .Template .Spec .Affinity , expectedAffinityConfiguration ) {
94
- log .Info ("updating the pod anti-affinity configuration" )
95
146
deployment .Template .Spec .Affinity = expectedAffinityConfiguration
96
147
modified = true
97
148
}
98
149
99
150
if modified {
100
- log .V (3 ).Info ("csv has been modified" )
101
151
csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs [0 ].Spec = * deployment
102
152
}
103
-
104
- return modified
105
- }
106
-
107
- func validateCSV (log logr.Logger , csv * olmv1alpha1.ClusterServiceVersion ) bool {
108
- deploymentSpecs := csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs
109
- if len (deploymentSpecs ) != 1 {
110
- log .Info ("csv contains more than one or zero nested deployment specs" )
111
- return false
112
- }
113
-
114
- deployment := & deploymentSpecs [0 ].Spec
115
- if len (deployment .Template .Spec .Containers ) != 1 {
116
- log .Info ("csv contains more than one container" )
117
- return false
118
- }
119
-
120
- return true
121
153
}
0 commit comments