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

fix: ensure the server pod is deleted even if it is not running #32

Merged
merged 1 commit into from
Mar 17, 2024
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
11 changes: 8 additions & 3 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@ func (o *Options) Run(ctx context.Context, args []string) error {
}

klog.InfoS("Creating krelay-server", "namespace", o.serverNamespace)
svrPodName, err := ensureServerPod(ctx, cs, o.serverImage, o.serverNamespace)
svrPodName, err := createServerPod(ctx, cs, o.serverImage, o.serverNamespace)
if err != nil {
return fmt.Errorf("ensure krelay-server: %w", err)
return fmt.Errorf("create krelay-server pod: %w", err)
}
defer removeServerPod(cs, o.serverNamespace, svrPodName, time.Minute)

err = ensureServerPodIsRunning(ctx, cs, o.serverNamespace, svrPodName)
if err != nil {
return fmt.Errorf("ensure krelay-server is running: %w", err)
}
defer removeServerPod(cs, svrPodName, o.serverNamespace, time.Minute)
klog.InfoS("krelay-server is running", "pod", svrPodName, "namespace", o.serverNamespace)

transport, upgrader, err := spdy.RoundTripperFor(restCfg)
Expand Down
19 changes: 11 additions & 8 deletions cmd/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func toPtr[T any](v T) *T {
return &v
}

func ensureServerPod(ctx context.Context, cs kubernetes.Interface, svrImg, namespace string) (string, error) {
func createServerPod(ctx context.Context, cs kubernetes.Interface, svrImg, namespace string) (string, error) {
pod, err := cs.CoreV1().Pods(namespace).Create(ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Expand All @@ -52,7 +52,7 @@ func ensureServerPod(ctx context.Context, cs kubernetes.Interface, svrImg, names
{
MaxSkew: 1,
TopologyKey: "kubernetes.io/hostname",
WhenUnsatisfiable: "ScheduleAnyway",
WhenUnsatisfiable: corev1.ScheduleAnyway,
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": constants.ServerName,
Expand All @@ -63,17 +63,20 @@ func ensureServerPod(ctx context.Context, cs kubernetes.Interface, svrImg, names
},
}, metav1.CreateOptions{})
if err != nil {
return "", fmt.Errorf("create krelay-server pod: %w", err)
return "", err
}
return pod.Name, nil
}

func ensureServerPodIsRunning(ctx context.Context, cs kubernetes.Interface, namespace, podName string) error {
timeoutCtx, cancel := context.WithTimeout(ctx, time.Minute*5)
defer cancel()

w, err := cs.CoreV1().Pods(namespace).Watch(timeoutCtx, metav1.ListOptions{
FieldSelector: fmt.Sprintf("metadata.name=%s", pod.Name),
FieldSelector: fmt.Sprintf("metadata.name=%s", podName),
})
if err != nil {
return "", fmt.Errorf("watch krelay-server pod: %w", err)
return fmt.Errorf("watch krelay-server pod: %w", err)
}
defer w.Stop()

Expand All @@ -99,13 +102,13 @@ loop:
}
}
if !running {
return "", fmt.Errorf("krelay-server pod is not running")
return fmt.Errorf("krelay-server pod is not running")
}

return pod.Name, nil
return nil
}

func removeServerPod(cs kubernetes.Interface, podName, namespace string, timeout time.Duration) {
func removeServerPod(cs kubernetes.Interface, namespace, podName string, timeout time.Duration) {
klog.InfoS("Removing krelay-server pod", "pod", podName)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down