Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move pod label update to goroutine with retries #4835

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
appProtectVersion = getAppProtectVersionInfo()
}

updateSelfWithVersionInfo(kubeClient, version, nginxVersion.String(), appProtectVersion)
go updateSelfWithVersionInfo(kubeClient, version, nginxVersion.String(), appProtectVersion, 10, time.Second*5)

Check warning on line 82 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L82

Added line #L82 was not covered by tests

templateExecutor, templateExecutorV2 := createTemplateExecutors()

Expand Down Expand Up @@ -789,34 +789,47 @@
return cfgParams
}

func updateSelfWithVersionInfo(kubeClient *kubernetes.Clientset, version string, nginxVersion string, appProtectVersion string) {
pod, err := kubeClient.CoreV1().Pods(os.Getenv("POD_NAMESPACE")).Get(context.TODO(), os.Getenv("POD_NAME"), meta_v1.GetOptions{})
if err != nil {
glog.Errorf("Error getting pod: %v", err)
return
}

// Copy pod and update the labels.
newPod := pod.DeepCopy()
labels := newPod.ObjectMeta.Labels
if labels == nil {
labels = make(map[string]string)
}
func updateSelfWithVersionInfo(kubeClient *kubernetes.Clientset, version, nginxVersion, appProtectVersion string, maxRetries int, waitTime time.Duration) {

Check warning on line 792 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L792

Added line #L792 was not covered by tests
nginxVer := strings.TrimSuffix(strings.Split(nginxVersion, "/")[1], "\n")
replacer := strings.NewReplacer(" ", "-", "(", "", ")", "")
nginxVer = replacer.Replace(nginxVer)
labels[nginxVersionLabel] = nginxVer
if appProtectVersion != "" {
labels[appProtectVersionLabel] = appProtectVersion
}
labels[versionLabel] = strings.TrimPrefix(version, "v")
newPod.ObjectMeta.Labels = labels
podUpdated := false

Check warning on line 796 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L796

Added line #L796 was not covered by tests

_, err = kubeClient.CoreV1().Pods(newPod.ObjectMeta.Namespace).Update(context.TODO(), newPod, meta_v1.UpdateOptions{})
if err != nil {
glog.Errorf("Error updating pod with labels: %v", err)
return
for i := 0; (i < maxRetries || maxRetries == 0) && !podUpdated; i++ {
if i > 0 {
time.Sleep(waitTime)
}
pod, err := kubeClient.CoreV1().Pods(os.Getenv("POD_NAMESPACE")).Get(context.TODO(), os.Getenv("POD_NAME"), meta_v1.GetOptions{})
if err != nil {
glog.Errorf("Error getting pod on attempt %d of %d: %v", i+1, maxRetries, err)
continue

Check warning on line 805 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L798-L805

Added lines #L798 - L805 were not covered by tests
}

// Copy pod and update the labels.
newPod := pod.DeepCopy()
labels := newPod.ObjectMeta.Labels
if labels == nil {
labels = make(map[string]string)
}

Check warning on line 813 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L809-L813

Added lines #L809 - L813 were not covered by tests

labels[nginxVersionLabel] = nginxVer
labels[versionLabel] = strings.TrimPrefix(version, "v")
if appProtectVersion != "" {
labels[appProtectVersionLabel] = appProtectVersion
}
newPod.ObjectMeta.Labels = labels

_, err = kubeClient.CoreV1().Pods(newPod.ObjectMeta.Namespace).Update(context.TODO(), newPod, meta_v1.UpdateOptions{})
if err != nil {
glog.Errorf("Error updating pod with labels on attempt %d of %d: %v", i+1, maxRetries, err)
continue

Check warning on line 825 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L815-L825

Added lines #L815 - L825 were not covered by tests
}

glog.Infof("Pod label updated: %s", pod.ObjectMeta.Name)
podUpdated = true

Check warning on line 829 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L828-L829

Added lines #L828 - L829 were not covered by tests
}

glog.Infof("Pod label updated: %s", pod.ObjectMeta.Name)
if !podUpdated {
glog.Errorf("Failed to update pod labels after %d attempts", maxRetries)
}

Check warning on line 834 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L832-L834

Added lines #L832 - L834 were not covered by tests
}
Loading