8000 Refactor how kubernetes.go reads the final lines of stdout by AaronH88 · Pull Request #1285 · ansible/receptor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Refactor how kubernetes.go reads the final lines of stdout #1285

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

Merged
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion pkg/workceptor/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,25 @@ func (kw *KubeUnit) KubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout

return
}

if err == io.EOF {
if line != "" {
_, err = stdout.Write([]byte(line + "\n"))
if err != nil {
*stdoutErr = fmt.Errorf("writing final line to stdout: %s", err)
kw.GetWorkceptor().nc.GetLogger().Error("Error writing final line to stdout: %s", err)

return
}
}
kw.GetWorkceptor().nc.GetLogger().Info("Detected EOF for pod %s/%s.",
podNamespace,
podName,
)

return
}

kw.GetWorkceptor().nc.GetLogger().Info(
"Detected Error: %s for pod %s/%s. Will retry %d more times.",
err,
Expand Down Expand Up @@ -439,7 +458,6 @@ func (kw *KubeUnit) KubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout
remainingRetries = retries // each time we read successfully, reset this counter
successfulWrite = true
}

logStream.Close()
}
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/workceptor/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,51 @@ func TestKubeLoggingWithReconnect(t *testing.T) {
mockKubeAPI.EXPECT().NewSPDYExecutor(gomock.Any(), gomock.Any(), gomock.Any()).Return(&exec, nil).AnyTimes()
},
},
{
name: "Kube error 503",
expectedCalls: func() {
mockBaseWorkUnit.EXPECT().UpdateBasicStatus(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
config := rest.Config{}
mockKubeAPI.EXPECT().InClusterConfig().Return(&config, nil)
mockBaseWorkUnit.EXPECT().GetWorkceptor().Return(w).AnyTimes()
clientset := kubernetes.Clientset{}
mockKubeAPI.EXPECT().NewForConfig(gomock.Any()).Return(&clientset, nil)
lock := &sync.RWMutex{}
mockBaseWorkUnit.EXPECT().GetStatusLock().Return(lock).AnyTimes()
mockBaseWorkUnit.EXPECT().MonitorLocalStatus().AnyTimes()
mockBaseWorkUnit.EXPECT().UnitDir().Return("TestDir2").AnyTimes()
kubeExtraData := workceptor.KubeExtraData{}
status := workceptor.StatusFileData{ExtraData: &kubeExtraData}
mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&status).AnyTimes()
mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(status).AnyTimes()
mockBaseWorkUnit.EXPECT().GetContext().Return(ctx).AnyTimes()
pod := corev1.Pod{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "Test_Name"}, Spec: corev1.PodSpec{}, Status: corev1.PodStatus{}}
mockKubeAPI.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&pod, nil).AnyTimes()
mockBaseWorkUnit.EXPECT().UpdateFullStatus(gomock.Any()).AnyTimes()
field := hasTerm{}
mockKubeAPI.EXPECT().OneTermEqualSelector(gomock.Any(), gomock.Any()).Return(&field).AnyTimes()
ev := watch.Event{Object: &pod}
mockKubeAPI.EXPECT().UntilWithSync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&ev, nil).AnyTimes()
mockKubeAPI.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&pod, nil).AnyTimes()
req := fakerest.RESTClient{
Client: fakerest.CreateHTTPClient(func(request *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: http.StatusServiceUnavailable, // 503
Body: io.NopCloser(strings.NewReader("kube error")),
}

return resp, nil
}),
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
}
mockKubeAPI.EXPECT().GetLogs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(req.Request()).AnyTimes()
logger := logger.NewReceptorLogger("")
mockNetceptor.EXPECT().GetLogger().Return(logger).AnyTimes()
mockKubeAPI.EXPECT().SubResource(gomock.Any(), gomock.Any(), gomock.Any()).Return(req.Request()).AnyTimes()
exec := ex{}
mockKubeAPI.EXPECT().NewSPDYExecutor(gomock.Any(), gomock.Any(), gomock.Any()).Return(&exec, nil).AnyTimes()
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
0