Skip to content

Commit

Permalink
Add pod2pod test
Browse files Browse the repository at this point in the history
Similar to ipv6 test, but with ipv4 and kuerouter only

Signed-off-by: Natanael Copa <[email protected]>
  • Loading branch information
ncopa committed Apr 23, 2024
1 parent 49132b8 commit 7ace5ca
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions inttest/Makefile.variables
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ smoketests := \
check-noderole-no-taints \
check-noderole-single \
check-openebs\
check-pod2pod \
check-psp \
check-reset \
check-singlenode \
Expand Down
136 changes: 136 additions & 0 deletions inttest/pod2pod/pod2pod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2021 k0s 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 pod2pod

import (
"fmt"
"net"
"os"
"strings"

"github.com/stretchr/testify/suite"

"testing"

"time"

"github.com/k0sproject/k0s/inttest/common"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"

"context"

"k8s.io/apimachinery/pkg/util/wait"
)

type Pod2PodSuite struct {
common.BootlooseSuite

client *k8s.Clientset
}

func (s *Pod2PodSuite) TestPod2Pod() {

k0sConfig := k0sConfigWithKuberouter

s.PutFile(s.ControllerNode(0), "/tmp/k0s.yaml", k0sConfig)
controllerArgs := []string{"--config=/tmp/k0s.yaml"}
if os.Getenv("K0S_ENABLE_DYNAMIC_CONFIG") == "true" {
s.T().Log("Enabling dynamic config for controller")
controllerArgs = append(controllerArgs, "--enable-dynamic-config")
}
s.Require().NoError(s.InitController(0, controllerArgs...))
s.Require().NoError(s.RunWorkers())
client, err := s.KubeClient(s.ControllerNode(0))
s.Require().NoError(err)
err = s.WaitForNodeReady(s.WorkerNode(0), client)
s.Require().NoError(err)

err = s.WaitForNodeReady(s.WorkerNode(1), client)
s.Require().NoError(err)

kc, err := s.KubeClient("controller0", "")
s.Require().NoError(err)
restConfig, err := s.GetKubeConfig("controller0", "")
s.Require().NoError(err)

createdTargetPod, err := kc.CoreV1().Pods("default").Create(s.Context(), &corev1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "nginx-worker0"},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Name: "nginx-worker0", Image: "docker.io/library/nginx:1.23.1-alpine"}},
NodeSelector: map[string]string{
"kubernetes.io/hostname": "worker0",
},
},
}, metav1.CreateOptions{})
s.Require().NoError(err)
s.Require().NoError(common.WaitForPod(s.Context(), kc, "nginx-worker0", "default"), "nginx-worker0 pod did not start")

targetPod, err := kc.CoreV1().Pods(createdTargetPod.Namespace).Get(s.Context(), createdTargetPod.Name, metav1.GetOptions{})
s.Require().NoError(err)

sourcePod, err := kc.CoreV1().Pods("default").Create(s.Context(), &corev1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "nginx-worker1"},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Name: "alpine", Image: "docker.io/library/nginx:1.23.1-alpine"}},
NodeSelector: map[string]string{
"kubernetes.io/hostname": "worker1",
},
},
}, metav1.CreateOptions{})
s.Require().NoError(err)
s.NoError(common.WaitForPod(s.Context(), kc, "nginx-worker1", "default"), "nginx-worker1 pod did not start")

// test ipv4 address
err = wait.PollImmediateWithContext(s.Context(), 100*time.Millisecond, time.Minute, func(ctx context.Context) (done bool, err error) {
s.Require().Equal(len(targetPod.Status.PodIPs), 1)
podIP := targetPod.Status.PodIPs[0].IP
targetIP := net.ParseIP(podIP)
s.Require().NotNil(targetIP)
out, err := common.PodExecCmdOutput(kc, restConfig, sourcePod.Name, sourcePod.Namespace, fmt.Sprintf("/usr/bin/wget -qO- %s", targetIP))
s.T().Log(out, err)
if err != nil {
s.T().Log("error calling ipv4 address: ", err)
return false, nil
}
s.T().Log("server response", out)
return strings.Contains(out, "Welcome to nginx"), nil
})
s.Require().NoError(err)
s.client = client
}

func TestPod2PodSuite(t *testing.T) {
s := Pod2PodSuite{
common.BootlooseSuite{
ControllerCount: 1,
WorkerCount: 2,
},
nil,
}

suite.Run(t, &s)
}

const k0sConfigWithKuberouter = `
spec:
network:
provider: kuberouter
`

0 comments on commit 7ace5ca

Please sign in to comment.