Skip to content

Commit

Permalink
Avoid PodScheduled overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Oct 23, 2024
1 parent 2f5d45b commit 3ce8589
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/utils/lifecycle/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
Expand Down Expand Up @@ -109,7 +110,21 @@ func computePatch(renderer gotpl.Renderer, resource any, patch internalversion.S
return nil, "", err
}
return patchData, types.StrategicMergePatchType, nil
case internalversion.StagePatchTypeMergePatch, "":
case internalversion.StagePatchTypeMergePatch:
patchData, err := computeMergePatch(renderer, resource, patch.Root, patch.Template)
if err != nil {
return nil, "", err
}
return patchData, types.MergePatchType, nil
case "":
switch resource.(type) {
case *corev1.Node, *corev1.Pod:
patchData, err := computeMergePatch(renderer, resource, patch.Root, patch.Template)
if err != nil {
return nil, "", err
}
return patchData, types.StrategicMergePatchType, nil
}
patchData, err := computeMergePatch(renderer, resource, patch.Root, patch.Template)
if err != nil {
return nil, "", err
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/helper/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func CreatePod(pod *corev1.Pod) features.Func {
t.Fatal(err)
}

checkPodScheduled := pod.Spec.NodeName == ""

t.Log("creating pod", log.KObj(pod))
err = client.Create(ctx, pod)
if err != nil {
Expand All @@ -143,6 +145,15 @@ func CreatePod(pod *corev1.Pod) features.Func {
t.Fatal("pod node name is empty", log.KObj(pod))
}

if checkPodScheduled {
_, ok := slices.Find(pod.Status.Conditions, func(cond corev1.PodCondition) bool {
return cond.Type == corev1.PodScheduled && cond.Status == corev1.ConditionTrue
})
if !ok {
t.Fatal("pod is scheduled, but there are no PodScheduled conditions", log.KObj(pod))
}
}

if pod.Status.PodIP != "" {
if pod.Spec.HostNetwork {
if pod.Status.PodIP != pod.Status.HostIP {
Expand Down
34 changes: 34 additions & 0 deletions test/e2e/issue_1243.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"sigs.k8s.io/e2e-framework/pkg/features"
"sigs.k8s.io/kwok/test/e2e/helper"
)

func Issue1243() *features.FeatureBuilder {
node := helper.NewNodeBuilder("node0").
Build()
pod0 := helper.NewPodBuilder("pod0").
Build()
return features.New("Pod can be created and deleted").
Setup(helper.CreateNode(node)).
Teardown(helper.DeleteNode(node)).
Assess("create pod", helper.CreatePod(pod0)).
Assess("delete pod", helper.DeletePod(pod0))
}
6 changes: 6 additions & 0 deletions test/e2e/kwokctl/binary/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,9 @@ func TestResourceUsage(t *testing.T) {
Feature()
testEnv.Test(t, f0)
}

func TestIssue1243(t *testing.T) {
f0 := e2e.Issue1243().
Feature()
testEnv.Test(t, f0)
}

0 comments on commit 3ce8589

Please sign in to comment.