@@ -354,6 +354,19 @@ func (c *Controller) onUpdate(oldObj, newObj interface{}) {
354
354
}
355
355
}
356
356
357
+ // check to see if any of the custom labels have been modified
358
+ if ! reflect .DeepEqual (util .GetCustomLabels (oldcluster ), util .GetCustomLabels (newcluster )) {
359
+ // update the custom labels on all of the managed objects at are not the
360
+ // Postgres cluster deployments
361
+ if err := updateLabels (c , oldcluster , newcluster ); err != nil {
362
+ log .Error (err )
363
+ return
364
+ }
365
+
366
+ // append the PostgreSQL specific functions as part of a rolling update
367
+ rollingUpdateFuncs = append (rollingUpdateFuncs , clusteroperator .UpdateLabels )
368
+ }
369
+
357
370
// check to see if any tolerations have been modified
358
371
if ! reflect .DeepEqual (oldcluster .Spec .Tolerations , newcluster .Spec .Tolerations ) {
359
372
rollingUpdateFuncs = append (rollingUpdateFuncs , clusteroperator .UpdateTolerations )
@@ -639,6 +652,237 @@ func updateBackrestS3(c *Controller, cluster *crv1.Pgcluster) error {
639
652
return nil
640
653
}
641
654
655
+ // updateLabels updates the custom labels on all of the managed objects *except*
656
+ // the Postgres instances themselves, i.e. the deployment templates
657
+ func updateLabels (c * Controller , oldCluster * crv1.Pgcluster , newCluster * crv1.Pgcluster ) error {
658
+ // we need to figure out which labels need to be removed from the list
659
+ labelsToRemove := make ([]string , 0 )
660
+ labels := util .GetCustomLabels (newCluster )
661
+
662
+ for old := range util .GetCustomLabels (oldCluster ) {
663
+ if _ , ok := labels [old ]; ! ok {
664
+ labelsToRemove = append (labelsToRemove , old )
665
+ }
666
+ }
667
+
668
+ // go through each object group and update the labels.
669
+ if err := updateLabelsForDeployments (c , newCluster , labels , labelsToRemove ); err != nil {
670
+ return err
671
+ }
672
+
673
+ if err := updateLabelsForPVCs (c , newCluster , labels , labelsToRemove ); err != nil {
674
+ return err
675
+ }
676
+
677
+ if err := updateLabelsForConfigMaps (c , newCluster , labels , labelsToRemove ); err != nil {
678
+ return err
679
+ }
680
+
681
+ if err := updateLabelsForSecrets (c , newCluster , labels , labelsToRemove ); err != nil {
682
+ return err
683
+ }
684
+
685
+ return updateLabelsForServices (c , newCluster , labels , labelsToRemove )
686
+ }
687
+
688
+ // updateLabelsForConfigMaps updates the custom labels for ConfigMaps
689
+ func updateLabelsForConfigMaps (c * Controller , cluster * crv1.Pgcluster , labels map [string ]string , labelsToRemove []string ) error {
690
+ ctx := context .TODO ()
691
+
692
+ options := metav1.ListOptions {
693
+ LabelSelector : fields .AndSelectors (
694
+ fields .OneTermEqualSelector (config .LABEL_PG_CLUSTER , cluster .Name ),
695
+ fields .OneTermEqualSelector (config .LABEL_VENDOR , config .LABEL_CRUNCHY ),
696
+ ).String (),
697
+ }
698
+
699
+ items , err := c .Client .CoreV1 ().ConfigMaps (cluster .Namespace ).List (ctx , options )
700
+
701
+ if err != nil {
702
+ return err
703
+ }
704
+
705
+ for i := range items .Items {
706
+ item := & items .Items [i ]
707
+
708
+ for j := range labelsToRemove {
709
+ delete (item .ObjectMeta .Labels , labelsToRemove [j ])
710
+ }
711
+
712
+ for k , v := range labels {
713
+ item .ObjectMeta .Labels [k ] = v
714
+ }
715
+
716
+ if _ , err := c .Client .CoreV1 ().ConfigMaps (cluster .Namespace ).Update (ctx ,
717
+ item , metav1.UpdateOptions {}); err != nil {
718
+ return err
719
+ }
720
+ }
721
+
722
+ return nil
723
+ }
724
+
725
+ // updateLabelsForDeployments updates the custom labels for Deployments, except
726
+ // for the **templates** on the Postgres instances
727
+ func updateLabelsForDeployments (c * Controller , cluster * crv1.Pgcluster , labels map [string ]string , labelsToRemove []string ) error {
728
+ ctx := context .TODO ()
729
+
730
+ options := metav1.ListOptions {
731
+ LabelSelector : fields .AndSelectors (
732
+ fields .OneTermEqualSelector (config .LABEL_PG_CLUSTER , cluster .Name ),
733
+ fields .OneTermEqualSelector (config .LABEL_VENDOR , config .LABEL_CRUNCHY ),
734
+ ).String (),
735
+ }
736
+
737
+ items , err := c .Client .AppsV1 ().Deployments (cluster .Namespace ).List (ctx , options )
738
+
739
+ if err != nil {
740
+ return err
741
+ }
742
+
743
+ for i := range items .Items {
744
+ item := & items .Items [i ]
745
+
746
+ for j := range labelsToRemove {
747
+ delete (item .ObjectMeta .Labels , labelsToRemove [j ])
748
+
749
+ // only remove the labels on the template if this is not a Postgres
750
+ // instance
751
+ if _ , ok := item .ObjectMeta .Labels [config .LABEL_PG_DATABASE ]; ! ok {
752
+ delete (item .Spec .Template .ObjectMeta .Labels , labelsToRemove [j ])
753
+ }
754
+ }
755
+
756
+ for k , v := range labels {
757
+ item .ObjectMeta .Labels [k ] = v
758
+
759
+ // only update the labels on the template if this is not a Postgres
760
+ // instance
761
+ if _ , ok := item .ObjectMeta .Labels [config .LABEL_PG_DATABASE ]; ! ok {
762
+ item .Spec .Template .ObjectMeta .Labels [k ] = v
763
+ }
764
+ }
765
+
766
+ if _ , err := c .Client .AppsV1 ().Deployments (cluster .Namespace ).Update (ctx ,
767
+ item , metav1.UpdateOptions {}); err != nil {
768
+ return err
769
+ }
770
+ }
771
+
772
+ return nil
773
+ }
774
+
775
+ // updateLabelsForPVCs updates the custom labels for PVCs
776
+ func updateLabelsForPVCs (c * Controller , cluster * crv1.Pgcluster , labels map [string ]string , labelsToRemove []string ) error {
777
+ ctx := context .TODO ()
778
+
779
+ options := metav1.ListOptions {
780
+ LabelSelector : fields .AndSelectors (
781
+ fields .OneTermEqualSelector (config .LABEL_PG_CLUSTER , cluster .Name ),
782
+ fields .OneTermEqualSelector (config .LABEL_VENDOR , config .LABEL_CRUNCHY ),
783
+ ).String (),
784
+ }
785
+
786
+ items , err := c .Client .CoreV1 ().PersistentVolumeClaims (cluster .Namespace ).List (ctx , options )
787
+
788
+ if err != nil {
789
+ return err
790
+ }
791
+
792
+ for i := range items .Items {
793
+ item := & items .Items [i ]
794
+
795
+ for j := range labelsToRemove {
796
+ delete (item .ObjectMeta .Labels , labelsToRemove [j ])
797
+ }
798
+
799
+ for k , v := range labels {
800
+ item .ObjectMeta .Labels [k ] = v
801
+ }
802
+
803
+ if _ , err := c .Client .CoreV1 ().PersistentVolumeClaims (cluster .Namespace ).Update (ctx ,
804
+ item , metav1.UpdateOptions {}); err != nil {
805
+ return err
806
+ }
807
+ }
808
+
809
+ return nil
810
+ }
811
+
812
+ // updateLabelsForSecrets updates the custom labels for Secrets
813
+ func updateLabelsForSecrets (c * Controller , cluster * crv1.Pgcluster , labels map [string ]string , labelsToRemove []string ) error {
814
+ ctx := context .TODO ()
815
+
816
+ options := metav1.ListOptions {
817
+ LabelSelector : fields .AndSelectors (
818
+ fields .OneTermEqualSelector (config .LABEL_PG_CLUSTER , cluster .Name ),
819
+ fields .OneTermEqualSelector (config .LABEL_VENDOR , config .LABEL_CRUNCHY ),
820
+ ).String (),
821
+ }
822
+
823
+ items , err := c .Client .CoreV1 ().Secrets (cluster .Namespace ).List (ctx , options )
824
+
825
+ if err != nil {
826
+ return err
827
+ }
828
+
829
+ for i := range items .Items {
830
+ item := & items .Items [i ]
831
+
832
+ for j := range labelsToRemove {
833
+ delete (item .ObjectMeta .Labels , labelsToRemove [j ])
834
+ }
835
+
836
+ for k , v := range labels {
837
+ item .ObjectMeta .Labels [k ] = v
838
+ }
839
+
840
+ if _ , err := c .Client .CoreV1 ().Secrets (cluster .Namespace ).Update (ctx ,
841
+ item , metav1.UpdateOptions {}); err != nil {
842
+ return err
843
+ }
844
+ }
845
+
846
+ return nil
847
+ }
848
+
849
+ // updateLabelsForServices updates the custom labels for Services
850
+ func updateLabelsForServices (c * Controller , cluster * crv1.Pgcluster , labels map [string ]string , labelsToRemove []string ) error {
851
+ ctx := context .TODO ()
852
+
853
+ options := metav1.ListOptions {
854
+ LabelSelector : fields .AndSelectors (
855
+ fields .OneTermEqualSelector (config .LABEL_PG_CLUSTER , cluster .Name ),
856
+ fields .OneTermEqualSelector (config .LABEL_VENDOR , config .LABEL_CRUNCHY ),
857
+ ).String (),
858
+ }
859
+
860
+ items , err := c .Client .CoreV1 ().Services (cluster .Namespace ).List (ctx , options )
861
+
862
+ if err != nil {
863
+ return err
864
+ }
865
+
866
+ for i := range items .Items {
867
+ item := & items .Items [i ]
868
+
869
+ for j := range labelsToRemove {
870
+ delete (item .ObjectMeta .Labels , labelsToRemove [j ])
871
+ }
872
+
873
+ for k , v := range labels {
874
+ item .ObjectMeta .Labels [k ] = v
875
+ }
876
+
877
+ if _ , err := c .Client .CoreV1 ().Services (cluster .Namespace ).Update (ctx ,
878
+ item , metav1.UpdateOptions {}); err != nil {
879
+ return err
880
+ }
881
+ }
882
+
883
+ return nil
884
+ }
885
+
642
886
// updatePgBouncer updates the pgBouncer Deployment to reflect any changes that
643
887
// may be made, which include:
644
888
// - enabling a pgBouncer Deployment :)
0 commit comments