Skip to content

Commit

Permalink
fix(sync): log a warning for empty pods (#9599)
Browse files Browse the repository at this point in the history
* fix(sync): add validation for namespaces and handle empty pods

* fixes

* fixes
  • Loading branch information
idsulik authored Dec 16, 2024
1 parent ff741e3 commit 83b5b45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions pkg/skaffold/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,16 @@ func Perform(ctx context.Context, image string, files syncMap, cmdFn func(contex
pods, err := client.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{
FieldSelector: fmt.Sprintf("status.phase=%s", v1.PodRunning),
})

if err != nil {
return fmt.Errorf("getting pods for namespace %q: %w", ns, err)
}

if len(pods.Items) == 0 {
log.Entry(ctx).Warnf("no running pods found in namespace %q", ns)
continue
}

for _, p := range pods.Items {
for _, c := range p.Spec.Containers {
if c.Image != image {
Expand Down
22 changes: 17 additions & 5 deletions pkg/skaffold/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ func fakeCmd(ctx context.Context, _ v1.Pod, _ v1.Container, files syncMap) *exec
return exec.CommandContext(ctx, "copy", args...)
}

var pod = &v1.Pod{
var runningPod = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "podname",
},
Expand Down Expand Up @@ -936,15 +936,15 @@ func TestPerform(t *testing.T) {
description: "no error",
image: "gcr.io/k8s-skaffold:123",
files: syncMap{"test.go": {"/test.go"}},
pod: pod,
pod: runningPod,
cmdFn: fakeCmd,
expected: []string{"copy test.go /test.go"},
},
{
description: "cmd error",
image: "gcr.io/k8s-skaffold:123",
files: syncMap{"test.go": {"/test.go"}},
pod: pod,
pod: runningPod,
cmdFn: fakeCmd,
cmdErr: fmt.Errorf(""),
shouldErr: true,
Expand All @@ -953,16 +953,24 @@ func TestPerform(t *testing.T) {
description: "client error",
image: "gcr.io/k8s-skaffold:123",
files: syncMap{"test.go": {"/test.go"}},
pod: pod,
pod: runningPod,
cmdFn: fakeCmd,
clientErr: fmt.Errorf(""),
shouldErr: true,
},
{
description: "pod not running",
image: "gcr.io/k8s-skaffold:123",
files: syncMap{"test.go": {"/test.go"}},
pod: nil,
cmdFn: fakeCmd,
shouldErr: true,
},
{
description: "no copy",
image: "gcr.io/different-pod:123",
files: syncMap{"test.go": {"/test.go"}},
pod: pod,
pod: runningPod,
cmdFn: fakeCmd,
shouldErr: true,
},
Expand All @@ -973,6 +981,10 @@ func TestPerform(t *testing.T) {

t.Override(&util.DefaultExecCommand, cmdRecord)
t.Override(&client.Client, func(string) (kubernetes.Interface, error) {
if test.pod == nil {
return fake.NewSimpleClientset(), nil
}

return fake.NewSimpleClientset(test.pod), test.clientErr
})

Expand Down

0 comments on commit 83b5b45

Please sign in to comment.