@@ -79,7 +79,7 @@ func main() {
79
79
appProtectVersion = getAppProtectVersionInfo ()
80
80
}
81
81
82
- updateSelfWithVersionInfo (kubeClient , version , nginxVersion .String (), appProtectVersion )
82
+ go updateSelfWithVersionInfo (kubeClient , version , nginxVersion .String (), appProtectVersion , 10 , time . Second * 5 )
83
83
84
84
templateExecutor , templateExecutorV2 := createTemplateExecutors ()
85
85
@@ -789,34 +789,47 @@ func processConfigMaps(kubeClient *kubernetes.Clientset, cfgParams *configs.Conf
789
789
return cfgParams
790
790
}
791
791
792
- func updateSelfWithVersionInfo (kubeClient * kubernetes.Clientset , version string , nginxVersion string , appProtectVersion string ) {
793
- pod , err := kubeClient .CoreV1 ().Pods (os .Getenv ("POD_NAMESPACE" )).Get (context .TODO (), os .Getenv ("POD_NAME" ), meta_v1.GetOptions {})
794
- if err != nil {
795
- glog .Errorf ("Error getting pod: %v" , err )
796
- return
797
- }
798
-
799
- // Copy pod and update the labels.
800
- newPod := pod .DeepCopy ()
801
- labels := newPod .ObjectMeta .Labels
802
- if labels == nil {
803
- labels = make (map [string ]string )
804
- }
792
+ func updateSelfWithVersionInfo (kubeClient * kubernetes.Clientset , version , nginxVersion , appProtectVersion string , maxRetries int , waitTime time.Duration ) {
805
793
nginxVer := strings .TrimSuffix (strings .Split (nginxVersion , "/" )[1 ], "\n " )
806
794
replacer := strings .NewReplacer (" " , "-" , "(" , "" , ")" , "" )
807
795
nginxVer = replacer .Replace (nginxVer )
808
- labels [nginxVersionLabel ] = nginxVer
809
- if appProtectVersion != "" {
810
- labels [appProtectVersionLabel ] = appProtectVersion
811
- }
812
- labels [versionLabel ] = strings .TrimPrefix (version , "v" )
813
- newPod .ObjectMeta .Labels = labels
796
+ podUpdated := false
814
797
815
- _ , err = kubeClient .CoreV1 ().Pods (newPod .ObjectMeta .Namespace ).Update (context .TODO (), newPod , meta_v1.UpdateOptions {})
816
- if err != nil {
817
- glog .Errorf ("Error updating pod with labels: %v" , err )
818
- return
798
+ for i := 0 ; (i < maxRetries || maxRetries == 0 ) && ! podUpdated ; i ++ {
799
+ if i > 0 {
800
+ time .Sleep (waitTime )
801
+ }
802
+ pod , err := kubeClient .CoreV1 ().Pods (os .Getenv ("POD_NAMESPACE" )).Get (context .TODO (), os .Getenv ("POD_NAME" ), meta_v1.GetOptions {})
803
+ if err != nil {
804
+ glog .Errorf ("Error getting pod on attempt %d of %d: %v" , i + 1 , maxRetries , err )
805
+ continue
806
+ }
807
+
808
+ // Copy pod and update the labels.
809
+ newPod := pod .DeepCopy ()
810
+ labels := newPod .ObjectMeta .Labels
811
+ if labels == nil {
812
+ labels = make (map [string ]string )
813
+ }
814
+
815
+ labels [nginxVersionLabel ] = nginxVer
816
+ labels [versionLabel ] = strings .TrimPrefix (version , "v" )
817
+ if appProtectVersion != "" {
818
+ labels [appProtectVersionLabel ] = appProtectVersion
819
+ }
820
+ newPod .ObjectMeta .Labels = labels
821
+
822
+ _ , err = kubeClient .CoreV1 ().Pods (newPod .ObjectMeta .Namespace ).Update (context .TODO (), newPod , meta_v1.UpdateOptions {})
823
+ if err != nil {
824
+ glog .Errorf ("Error updating pod with labels on attempt %d of %d: %v" , i + 1 , maxRetries , err )
825
+ continue
826
+ }
827
+
828
+ glog .Infof ("Pod label updated: %s" , pod .ObjectMeta .Name )
829
+ podUpdated = true
819
830
}
820
831
821
- glog .Infof ("Pod label updated: %s" , pod .ObjectMeta .Name )
832
+ if ! podUpdated {
833
+ glog .Errorf ("Failed to update pod labels after %d attempts" , maxRetries )
834
+ }
822
835
}
0 commit comments