@@ -142,6 +142,160 @@ func TestSyncStatefulSetsAnnotations(t *testing.T) {
142142 }
143143}
144144
145+ func TestPodAnnotationsSync (t * testing.T ) {
146+ // testName := "test pod annotations"
147+ clusterName := "acid-test-cluster-2"
148+ namespace := "default"
149+ podAnnotation := "no-scale-down"
150+ podAnnotations := map [string ]string {"no-scale-down" : "true" }
151+
152+ ctrl := gomock .NewController (t )
153+ defer ctrl .Finish ()
154+ mockClient := mocks .NewMockHTTPClient (ctrl )
155+ client , _ := newFakeK8sAnnotationsClient ()
156+
157+ pg := acidv1.Postgresql {
158+ ObjectMeta : metav1.ObjectMeta {
159+ Name : clusterName ,
160+ Namespace : namespace ,
161+ },
162+ Spec : acidv1.PostgresSpec {
163+ Volume : acidv1.Volume {
164+ Size : "1Gi" ,
165+ },
166+ EnableConnectionPooler : boolToPointer (true ),
167+ EnableLogicalBackup : true ,
168+ EnableReplicaConnectionPooler : boolToPointer (true ),
169+ PodAnnotations : podAnnotations ,
170+ NumberOfInstances : 2 ,
171+ },
172+ }
173+
174+ var cluster = New (
175+ Config {
176+ OpConfig : config.Config {
177+ PatroniAPICheckInterval : time .Duration (1 ),
178+ PatroniAPICheckTimeout : time .Duration (5 ),
179+ PodManagementPolicy : "ordered_ready" ,
180+ ConnectionPooler : config.ConnectionPooler {
181+ ConnectionPoolerDefaultCPURequest : "100m" ,
182+ ConnectionPoolerDefaultCPULimit : "100m" ,
183+ ConnectionPoolerDefaultMemoryRequest : "100Mi" ,
184+ ConnectionPoolerDefaultMemoryLimit : "100Mi" ,
185+ NumberOfInstances : k8sutil .Int32ToPointer (1 ),
186+ },
187+ Resources : config.Resources {
188+ ClusterLabels : map [string ]string {"application" : "spilo" },
189+ ClusterNameLabel : "cluster-name" ,
190+ DefaultCPURequest : "300m" ,
191+ DefaultCPULimit : "300m" ,
192+ DefaultMemoryRequest : "300Mi" ,
193+ DefaultMemoryLimit : "300Mi" ,
194+ PodRoleLabel : "spilo-role" ,
195+ ResourceCheckInterval : time .Duration (3 ),
196+ ResourceCheckTimeout : time .Duration (10 ),
197+ },
198+ },
199+ }, client , pg , logger , eventRecorder )
200+
201+ configJson := `{"postgresql": {"parameters": {"log_min_duration_statement": 200, "max_connections": 50}}}, "ttl": 20}`
202+ response := http.Response {
203+ StatusCode : 200 ,
204+ Body : io .NopCloser (bytes .NewReader ([]byte (configJson ))),
205+ }
206+
207+ mockClient .EXPECT ().Do (gomock .Any ()).Return (& response , nil ).AnyTimes ()
208+ cluster .patroni = patroni .New (patroniLogger , mockClient )
209+ cluster .Name = clusterName
210+ cluster .Namespace = namespace
211+ clusterOptions := clusterLabelsOptions (cluster )
212+
213+ // create a statefulset
214+ _ , err := cluster .createStatefulSet ()
215+ assert .NoError (t , err )
216+ // create a pods
217+ podsList := createPods (cluster )
218+ for _ , pod := range podsList {
219+ _ , err = cluster .KubeClient .Pods (namespace ).Create (context .TODO (), & pod , metav1.CreateOptions {})
220+ assert .NoError (t , err )
221+ }
222+ // create connection pooler
223+ _ , err = cluster .createConnectionPooler (mockInstallLookupFunction )
224+ assert .NoError (t , err )
225+
226+ annotateResources (cluster )
227+ err = cluster .Sync (& cluster .Postgresql )
228+ assert .NoError (t , err )
229+
230+ // 1. PodAnnotations set
231+ stsList , err := cluster .KubeClient .StatefulSets (namespace ).List (context .TODO (), clusterOptions )
232+ assert .NoError (t , err )
233+ for _ , sts := range stsList .Items {
234+ assert .Contains (t , sts .Spec .Template .Annotations , podAnnotation )
235+ }
236+
237+ for _ , role := range []PostgresRole {Master , Replica } {
238+ deploy , err := cluster .KubeClient .Deployments (namespace ).Get (context .TODO (), cluster .connectionPoolerName (role ), metav1.GetOptions {})
239+ assert .NoError (t , err )
240+ assert .Contains (t , deploy .Spec .Template .Annotations , podAnnotation ,
241+ fmt .Sprintf ("pooler deployment pod template %s should contain annotation %s, found %#v" ,
242+ deploy .Name , podAnnotation , deploy .Spec .Template .Annotations ))
243+ assert .NoError (t , err )
244+ }
245+
246+ podList , err := cluster .KubeClient .Pods (namespace ).List (context .TODO (), clusterOptions )
247+ assert .NoError (t , err )
248+ for _ , pod := range podList .Items {
249+ assert .Contains (t , pod .Annotations , podAnnotation ,
250+ fmt .Sprintf ("pod %s should contain annotation %s, found %#v" , pod .Name , podAnnotation , pod .Annotations ))
251+ assert .NoError (t , err )
252+ }
253+
254+ cronJobList , err := cluster .KubeClient .CronJobs (namespace ).List (context .TODO (), clusterOptions )
255+ assert .NoError (t , err )
256+ for _ , cronJob := range cronJobList .Items {
257+ assert .Contains (t , cronJob .Annotations , podAnnotation ,
258+ fmt .Sprintf ("logical backup cron job's pod template should contain annotation %s, found %#v" ,
259+ podAnnotation , cronJob .Spec .JobTemplate .Spec .Template .Annotations ))
260+ }
261+
262+ // 2 PodAnnotations removed
263+ newSpec := cluster .Postgresql .DeepCopy ()
264+ newSpec .Spec .PodAnnotations = nil
265+ err = cluster .Sync (newSpec )
266+ assert .NoError (t , err )
267+
268+ stsList , err = cluster .KubeClient .StatefulSets (namespace ).List (context .TODO (), clusterOptions )
269+ assert .NoError (t , err )
270+ for _ , sts := range stsList .Items {
271+ assert .NotContains (t , sts .Spec .Template .Annotations , "no-scale-down" )
272+ }
273+
274+ for _ , role := range []PostgresRole {Master , Replica } {
275+ deploy , err := cluster .KubeClient .Deployments (namespace ).Get (context .TODO (), cluster .connectionPoolerName (role ), metav1.GetOptions {})
276+ assert .NoError (t , err )
277+ assert .NotContains (t , deploy .Spec .Template .Annotations , podAnnotation ,
278+ fmt .Sprintf ("pooler deployment pod template %s should not contain annotation %s, found %#v" ,
279+ deploy .Name , podAnnotation , deploy .Spec .Template .Annotations ))
280+ assert .NoError (t , err )
281+ }
282+
283+ podList , err = cluster .KubeClient .Pods (namespace ).List (context .TODO (), clusterOptions )
284+ assert .NoError (t , err )
285+ for _ , pod := range podList .Items {
286+ assert .NotContains (t , pod .Annotations , "no-scale-down" ,
287+ fmt .Sprintf ("pod %s should not contain annotation %s, found %#v" , pod .Name , podAnnotation , pod .Annotations ))
288+ }
289+
290+ cronJobList , err = cluster .KubeClient .CronJobs (namespace ).List (context .TODO (), clusterOptions )
291+ assert .NoError (t , err )
292+ for _ , cronJob := range cronJobList .Items {
293+ assert .NotContains (t , cronJob .Annotations , podAnnotation ,
294+ fmt .Sprintf ("logical backup cron job's pod template should not contain annotation %s, found %#v" ,
295+ podAnnotation , cronJob .Spec .JobTemplate .Spec .Template .Annotations ))
296+ }
297+ }
298+
145299func TestCheckAndSetGlobalPostgreSQLConfiguration (t * testing.T ) {
146300 testName := "test config comparison"
147301 client , _ := newFakeK8sSyncClient ()
0 commit comments