Repository: kubernetes-sigs/kwok
Pull request #367 (2023-03-08 13:12:01)

Fix hostNetwork
<!--  Thanks for sending a pull request!  Here are some tips for you:

1. If this is your first time, please read our contributor guidelines: https://git.k8s.io/community/contributors/guide/first-contribution.md#your-first-contribution and developer guide https://git.k8s.io/community/contributors/devel/development.md#development-guide
2. Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request. For reference on required PR/issue labels, read here:
https://git.k8s.io/community/contributors/devel/sig-release/release.md#issuepr-kind-label
3. Ensure you have added or ran the appropriate tests for your PR: https://git.k8s.io/community/contributors/devel/sig-testing/testing.md
4. If you want *faster* PR reviews, read how: https://git.k8s.io/community/contributors/guide/pull-requests.md#best-practices-for-faster-reviews
5. If the PR is unfinished, see how to mark it: https://git.k8s.io/community/contributors/guide/pull-requests.md#marking-unfinished-pull-requests
-->

#### What type of PR is this?

<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind feature

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->

/kind bug

#### What this PR does / why we need it:

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

#### Special notes for your reviewer:

#### Does this PR introduce a user-facing change?
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".

For more information on release notes see: https://git.k8s.io/community/contributors/guide/release-notes.md
-->
```release-note

```

#### Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

<!--
This section can be blank if this pull request does not require a release note.

When adding links which point to resources within git repositories, like
KEPs or supporting documentation, please reference a specific commit and avoid
linking directly to the master branch. This ensures that links reference a
specific point in time, rather than a document that may change over time.

See here for guidance on getting permanent links to files: https://help.github.com/en/articles/getting-permanent-links-to-files

Please use the following format for linking documentation:
- [KEP]: <link>
- [Usage]: <link>
- [Other doc]: <link>
-->
```docs

```



Patch:
diff --git a/pkg/kwok/controllers/node_controller.go b/pkg/kwok/controllers/node_controller.go
index 3d0ce11b..b2f073d7 100644
--- a/pkg/kwok/controllers/node_controller.go
+++ b/pkg/kwok/controllers/node_controller.go
@@ -552,14 +552,14 @@ func (c *NodeController) Get(nodeName string) (*NodeInfo, bool) {
 	return nil, has
 }
 
-func (c *NodeController) funcNodeIP(args ...string) string {
+func (c *NodeController) funcNodeIP() string {
 	return c.nodeIP
 }
 
-func (c *NodeController) funcNodeName(args ...string) string {
+func (c *NodeController) funcNodeName() string {
 	return c.nodeName
 }
 
-func (c *NodeController) funcNodePort(args ...string) int {
+func (c *NodeController) funcNodePort() int {
 	return c.nodePort
 }
diff --git a/pkg/kwok/controllers/pod_controller.go b/pkg/kwok/controllers/pod_controller.go
index 66124927..d0d35e5f 100644
--- a/pkg/kwok/controllers/pod_controller.go
+++ b/pkg/kwok/controllers/pod_controller.go
@@ -116,8 +116,10 @@ func NewPodController(conf PodControllerConfig) (*PodController, error) {
 		recorder:                              conf.Recorder,
 	}
 	funcMap := template.FuncMap{
-		"NodeIP": c.funcNodeIP,
-		"PodIP":  c.funcPodIP,
+		"NodeIP":     c.funcNodeIP,
+		"PodIP":      c.funcPodIP,
+		"NodeIPWith": c.funcNodeIPWith,
+		"PodIPWith":  c.funcPodIPWith,
 	}
 	for k, v := range conf.FuncMap {
 		funcMap[k] = v
@@ -509,12 +511,6 @@ func (c *PodController) configurePod(pod *corev1.Pod, template string) ([]byte,
 				}
 			}
 		}
-	} else if pod.Status.PodIP == "" {
-		ips, err := cni.Setup(context.Background(), string(pod.UID), pod.Name, pod.Namespace)
-		if err != nil {
-			return nil, err
-		}
-		pod.Status.PodIP = ips[0]
 	}
 
 	patch, err := c.computePatch(pod, template)
@@ -564,11 +560,11 @@ func (c *PodController) computePatch(pod *corev1.Pod, tpl string) ([]byte, error
 	return patch, nil
 }
 
-func (c *PodController) funcNodeIP(args ...string) string {
-	if len(args) == 0 {
-		return c.nodeIP
-	}
-	nodeName := args[0]
+func (c *PodController) funcNodeIP() string {
+	return c.nodeIP
+}
+
+func (c *PodController) funcNodeIPWith(nodeName string) string {
 	nodeInfo, has := c.nodeGetFunc(nodeName)
 	if has && len(nodeInfo.HostIPs) > 0 {
 		hostIP := nodeInfo.HostIPs[0]
@@ -579,16 +575,28 @@ func (c *PodController) funcNodeIP(args ...string) string {
 	return c.nodeIP
 }
 
-func (c *PodController) funcPodIP(args ...string) string {
-	if len(args) == 0 {
-		podCIDR := c.defaultCIDR
-		pool, err := c.ipPool(podCIDR)
-		if err == nil {
-			return pool.Get()
+func (c *PodController) funcPodIP() string {
+	podCIDR := c.defaultCIDR
+	pool, err := c.ipPool(podCIDR)
+	if err == nil {
+		return pool.Get()
+	}
+	return c.nodeIP
+}
+
+func (c *PodController) funcPodIPWith(nodeName string, hostNetwork bool, uid, name, namespace string) (string, error) {
+	if hostNetwork {
+		return c.funcNodeIPWith(nodeName), nil
+	}
+
+	if c.enableCNI {
+		ips, err := cni.Setup(context.Background(), uid, name, namespace)
+		if err != nil {
+			return "", err
 		}
-		return c.nodeIP
+		return ips[0], nil
 	}
-	nodeName := args[0]
+
 	podCIDR := c.defaultCIDR
 	nodeInfo, has := c.nodeGetFunc(nodeName)
 	if has && len(nodeInfo.PodCIDRs) > 0 {
@@ -597,7 +605,7 @@ func (c *PodController) funcPodIP(args ...string) string {
 
 	pool, err := c.ipPool(podCIDR)
 	if err == nil {
-		return pool.Get()
+		return pool.Get(), nil
 	}
-	return c.nodeIP
+	return c.nodeIP, nil
 }
diff --git a/pkg/kwok/controllers/renderer.go b/pkg/kwok/controllers/renderer.go
index c8248115..74086521 100644
--- a/pkg/kwok/controllers/renderer.go
+++ b/pkg/kwok/controllers/renderer.go
@@ -21,26 +21,28 @@ import (
 	"encoding/json"
 	"fmt"
 	"strings"
+	"sync"
 	"text/template"
 
 	"sigs.k8s.io/yaml"
 
 	"sigs.k8s.io/kwok/pkg/utils/maps"
-	"sigs.k8s.io/kwok/pkg/utils/pools"
 )
 
 type renderer struct {
 	cache      maps.SyncMap[string, *template.Template]
-	bufferPool *pools.Pool[*bytes.Buffer]
+	bufferPool sync.Pool
 	funcMap    template.FuncMap
 }
 
 func newRenderer(funcMap template.FuncMap) *renderer {
 	return &renderer{
 		funcMap: funcMap,
-		bufferPool: pools.NewPool(func() *bytes.Buffer {
-			return bytes.NewBuffer(make([]byte, 4*1024))
-		}),
+		bufferPool: sync.Pool{
+			New: func() interface{} {
+				return bytes.NewBuffer(make([]byte, 4*1024))
+			},
+		},
 	}
 }
 
@@ -56,7 +58,7 @@ func (r *renderer) renderToJSON(text string, original interface{}) ([]byte, erro
 		}
 		r.cache.Store(text, temp)
 	}
-	buf := r.bufferPool.Get()
+	buf := r.bufferPool.Get().(*bytes.Buffer)
 	defer r.bufferPool.Put(buf)
 
 	buf.Reset()
diff --git a/pkg/kwok/server/debugging_port_forword.go b/pkg/kwok/server/debugging_port_forword.go
index 872ae5eb..9841bef6 100644
--- a/pkg/kwok/server/debugging_port_forword.go
+++ b/pkg/kwok/server/debugging_port_forword.go
@@ -23,6 +23,7 @@ import (
 	"io"
 	"net"
 	"net/http"
+	"sync"
 
 	"github.com/emicklei/go-restful/v3"
 	"k8s.io/apimachinery/pkg/types"
@@ -61,18 +62,25 @@ func (s *Server) PortForward(ctx context.Context, podName, podNamespace string,
 		}()
 
 		// TODO: remove this when upgrade to go 1.21 upgrade takes place
-		buf1 := s.bufPool.Get()
-		buf2 := s.bufPool.Get()
+		buf1 := bufPool.Get().(*[]byte)
+		buf2 := bufPool.Get().(*[]byte)
 		defer func() {
-			s.bufPool.Put(buf1)
-			s.bufPool.Put(buf2)
+			bufPool.Put(buf1)
+			bufPool.Put(buf2)
 		}()
-		return tunnel(ctx, stream, dial, buf1, buf2)
+		return tunnel(ctx, stream, dial, *buf1, *buf2)
 	}
 
 	return errors.New("no target or command")
 }
 
+var bufPool = sync.Pool{
+	New: func() any {
+		buf := make([]byte, 32*1024)
+		return &buf
+	},
+}
+
 // getPortForward handles a new restful port forward request. It determines the
 // pod name and uid and then calls ServePortForward.
 func (s *Server) getPortForward(req *restful.Request, resp *restful.Response) {
diff --git a/pkg/kwok/server/server.go b/pkg/kwok/server/server.go
index 4fb378e3..860754de 100644
--- a/pkg/kwok/server/server.go
+++ b/pkg/kwok/server/server.go
@@ -30,7 +30,6 @@ import (
 
 	"sigs.k8s.io/kwok/pkg/apis/internalversion"
 	"sigs.k8s.io/kwok/pkg/log"
-	"sigs.k8s.io/kwok/pkg/utils/pools"
 )
 
 const (
@@ -44,7 +43,6 @@ type Server struct {
 	idleTimeout           time.Duration
 	streamCreationTimeout time.Duration
 	config                Config
-	bufPool               *pools.Pool[[]byte]
 }
 
 // Config holds configurations needed by the server handlers.
@@ -61,9 +59,6 @@ func NewServer(config Config) *Server {
 		idleTimeout:           1 * time.Hour,
 		streamCreationTimeout: remotecommandconsts.DefaultStreamCreationTimeout,
 		config:                config,
-		bufPool: pools.NewPool(func() []byte {
-			return make([]byte, 32*1024)
-		}),
 	}
 }
 
diff --git a/pkg/utils/pools/doc.go b/pkg/utils/pools/doc.go
deleted file mode 100644
index 27322f5f..00000000
--- a/pkg/utils/pools/doc.go
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
-Copyright 2023 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 pools provides a generic pool implementation.
-package pools
diff --git a/pkg/utils/pools/pools.go b/pkg/utils/pools/pools.go
deleted file mode 100644
index 8a5d1c45..00000000
--- a/pkg/utils/pools/pools.go
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
-Copyright 2023 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 pools
-
-import (
-	"sync"
-)
-
-// Pool is a generic pool implementation.
-type Pool[T any] struct {
-	pool sync.Pool
-}
-
-// NewPool creates a new pool.
-func NewPool[T any](new func() T) *Pool[T] {
-	return &Pool[T]{
-		pool: sync.Pool{
-			New: func() any {
-				return new()
-			},
-		},
-	}
-}
-
-// Get gets an item from the pool.
-func (p *Pool[T]) Get() T {
-	return p.pool.Get().(T)
-}
-
-// Put puts an item back to the pool.
-func (p *Pool[T]) Put(v T) {
-	p.pool.Put(v)
-}
diff --git a/stages/pod-fast.yaml b/stages/pod-fast.yaml
index a03c19e9..457b809c 100644
--- a/stages/pod-fast.yaml
+++ b/stages/pod-fast.yaml
@@ -15,7 +15,7 @@ spec:
   next:
     statusTemplate: |
       {{ $now := Now }}
-      {{ $nodeName := .spec.nodeName }}
+
       conditions:
         - lastTransitionTime: {{ $now }}
           status: "True"
@@ -57,24 +57,8 @@ spec:
               startedAt: {{ $now }}
         {{ end }}
 
-      {{ with .status }}
-      hostIP: {{ with .hostIP -}} 
-        {{- . -}}
-      {{- else -}}
-        {{- NodeIP $nodeName -}}
-      {{- end }}
-
-      podIP: {{ with .podIP -}}
-        {{- . -}}
-      {{- else -}}
-        {{- if .spec.hostNetwork -}}
-          {{- NodeIP $nodeName -}}
-        {{- else -}}
-          {{- PodIP $nodeName -}}
-        {{- end -}}
-      {{- end }}
-      {{ end }}
-
+      hostIP: {{ NodeIPWith .spec.nodeName }}
+      podIP: {{ PodIPWith .spec.nodeName ( or .spec.hostNetwork false ) ( or .metadata.uid "" ) ( or .metadata.name "" ) ( or .metadata.namespace "" ) }}
       phase: Running
       startTime: {{ $now }}
 ---
diff --git a/stages/pod-general.yaml b/stages/pod-general.yaml
index a060103d..4dfb88ea 100644
--- a/stages/pod-general.yaml
+++ b/stages/pod-general.yaml
@@ -26,7 +26,7 @@ spec:
         - value: 'kwok.x-k8s.io/fake'
     statusTemplate: |
       {{ $now := Now }}
-      {{ $nodeName := .spec.nodeName }}
+
       conditions:
       {{ if .spec.initContainers }}
         - lastProbeTime: null
@@ -95,23 +95,9 @@ spec:
               reason: ContainerCreating
         {{ end }}
       {{ end }}
-      {{ with .status }}
-      hostIP: {{ with .hostIP -}} 
-        {{- . -}}
-      {{- else -}}
-        {{- NodeIP $nodeName -}}
-      {{- end }}
-      podIP: {{ with .podIP -}}
-        {{- . -}}
-      {{- else -}}
-        {{- if .spec.hostNetwork -}}
-          {{- NodeIP $nodeName -}}
-        {{- else -}}
-          {{- PodIP $nodeName -}}
-        {{- end -}}
-      {{- end }}
-      {{- end }}
 
+      hostIP: {{ NodeIPWith .spec.nodeName }}
+      podIP: {{ PodIPWith .spec.nodeName ( or .spec.hostNetwork false ) ( or .metadata.uid "" ) ( or .metadata.name "" ) ( or .metadata.namespace "" ) }}
       phase: Pending
 ---
 kind: Stage


Test patch:
diff --git a/pkg/kwok/controllers/pod_controller_test.go b/pkg/kwok/controllers/pod_controller_test.go
index b79b8e95..fb068492 100644
--- a/pkg/kwok/controllers/pod_controller_test.go
+++ b/pkg/kwok/controllers/pod_controller_test.go
@@ -35,11 +35,11 @@ import (
 )
 
 const (
-	defaultNodeIP  = "10.0.0.0"
-	defaultPodCIDR = "10.0.0.1/24"
+	defaultNodeIP  = "10.0.0.1"
+	defaultPodCIDR = "10.100.0.1/24"
 
-	secondNodeIP  = "172.0.0.0"
-	secondPodCIDR = "172.0.0.1/24"
+	secondNodeIP  = "172.0.0.1"
+	secondPodCIDR = "172.100.0.1/24"
 )
 
 func TestPodController(t *testing.T) {
@@ -273,28 +273,6 @@ func TestPodController(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	pod := list.Items[0]
-	now := metav1.Now()
-	pod.DeletionTimestamp = &now
-	_, err = clientset.CoreV1().Pods("default").Update(ctx, &pod, metav1.UpdateOptions{})
-	if err != nil {
-		t.Fatal(fmt.Errorf("delete pod error: %w", err))
-	}
-
-	err = wait.PollUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) {
-		list, err = clientset.CoreV1().Pods("default").List(ctx, metav1.ListOptions{})
-		if err != nil {
-			return false, fmt.Errorf("list pods error: %w", err)
-		}
-		if len(list.Items) != 4 {
-			return false, fmt.Errorf("want 4 pods, got %d", len(list.Items))
-		}
-		return true, nil
-	})
-	if err != nil {
-		t.Fatal(err)
-	}
-
 	for index, pod := range list.Items {
 		if nodeInfo, ok := nodeGetFunc(pod.Spec.NodeName); ok {
 			if pod.Status.Phase != corev1.PodRunning {
@@ -302,16 +280,16 @@ func TestPodController(t *testing.T) {
 			}
 			if pods.needLockPod(&list.Items[index]) {
 				if pod.Status.HostIP != nodeInfo.HostIPs[0] {
-					t.Fatal(fmt.Errorf("want pod %s to have hostIP=%s, got %s (nodeName=%s)", pod.Name, nodeInfo.HostIPs[0], pod.Status.HostIP, pod.Spec.NodeName))
+					t.Fatal(fmt.Errorf("want pod %s hostIP=%s, got %s", pod.Name, nodeInfo.HostIPs[0], pod.Status.HostIP))
 				}
 				if pod.Spec.HostNetwork {
 					if pod.Status.PodIP != nodeInfo.HostIPs[0] {
-						t.Fatal(fmt.Errorf("want pod %s to have podIP=%s, got %s (HostNetwork)", pod.Name, nodeInfo.HostIPs[0], pod.Status.PodIP))
+						t.Fatal(fmt.Errorf("want pod %s podIP=%s, got %s", pod.Name, nodeInfo.HostIPs[0], pod.Status.PodIP))
 					}
 				} else {
 					cidr, _ := parseCIDR(nodeInfo.PodCIDRs[0])
 					if !cidr.Contains(net.ParseIP(pod.Status.PodIP)) {
-						t.Fatal(fmt.Errorf("want pod %s to not have podIP=%s (non-HostNetwork)", pod.Name, pod.Status.PodIP))
+						t.Fatal(fmt.Errorf("want pod %s podIP=%s in %s, got not", pod.Name, pod.Status.PodIP, nodeInfo.PodCIDRs[0]))
 					}
 				}
 			}
@@ -319,4 +297,26 @@ func TestPodController(t *testing.T) {
 			t.Fatal(fmt.Errorf("want pod %s phase is not running, got %s", pod.Name, pod.Status.Phase))
 		}
 	}
+
+	pod := list.Items[0]
+	now := metav1.Now()
+	pod.DeletionTimestamp = &now
+	_, err = clientset.CoreV1().Pods("default").Update(ctx, &pod, metav1.UpdateOptions{})
+	if err != nil {
+		t.Fatal(fmt.Errorf("delete pod error: %w", err))
+	}
+
+	err = wait.PollUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) {
+		list, err = clientset.CoreV1().Pods("default").List(ctx, metav1.ListOptions{})
+		if err != nil {
+			return false, fmt.Errorf("list pods error: %w", err)
+		}
+		if len(list.Items) != 4 {
+			return false, fmt.Errorf("want 4 pods, got %d", len(list.Items))
+		}
+		return true, nil
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
 }
diff --git a/pkg/utils/pools/pools_test.go b/pkg/utils/pools/pools_test.go
deleted file mode 100644
index 4fa2f4c4..00000000
--- a/pkg/utils/pools/pools_test.go
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
-Copyright 2023 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 pools
-
-import (
-	"testing"
-)
-
-func TestPool(t *testing.T) {
-	index := 0
-	pool := NewPool(func() int {
-		index++
-		return index
-	})
-
-	pool.Put(3)
-	if pool.Get() != 3 {
-		t.Errorf("expected 3, got %d", pool.Get())
-	}
-
-	if pool.Get() != 1 {
-		t.Errorf("expected 1, got %d", pool.Get())
-	}
-	if pool.Get() != 2 {
-		t.Errorf("expected 2, got %d", pool.Get())
-	}
-
-	pool.Put(1)
-	if pool.Get() != 1 {
-		t.Errorf("expected 1, got %d", pool.Get())
-	}
-
-	pool.Put(2)
-	if pool.Get() != 2 {
-		t.Errorf("expected 2, got %d", pool.Get())
-	}
-
-	if pool.Get() != 3 {
-		t.Errorf("expected 3, got %d", pool.Get())
-	}
-}