forked from antrea-io/antrea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update checking validation NetworkPolicy
Signed-off-by: Wenqi Qiu <[email protected]>
- Loading branch information
Showing
8 changed files
with
285 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2024 Antrea 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 client_pod | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/google/uuid" | ||
"strings" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/util/retry" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func CreateClientPod(ctx context.Context, kClient kubernetes.Interface, probes []string, containerName string) (*corev1.Pod, error) { | ||
var err error | ||
var newPod *corev1.Pod | ||
namespace := ClientPodsNamespace | ||
podName := ScaleTestClientPodNamePrefix + uuid.New().String()[:6] | ||
err = retry.RetryOnConflict(retry.DefaultRetry, func() error { | ||
newPod.Namespace = namespace | ||
newPod.Name = podName | ||
newPod.Spec = corev1.PodSpec{ | ||
Affinity: &RealNodeAffinity, | ||
Tolerations: []corev1.Toleration{MasterToleration}, | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: ScaleClientContainerName, | ||
Image: "busybox", | ||
Command: []string{"nc", "-lk", "-p", "80"}, | ||
ImagePullPolicy: corev1.PullIfNotPresent, | ||
}, | ||
}, | ||
} | ||
var containers []corev1.Container | ||
for _, probe := range probes { | ||
l := strings.Split(probe, ":") | ||
server, port := l[0], l[1] | ||
if server == "" { | ||
server = "$NODE_IP" | ||
} | ||
|
||
containers = append(containers, corev1.Container{ | ||
Name: containerName, | ||
Image: "busybox", | ||
// read up rest </proc/uptime; t1="${up%.*}${up#*.}" | ||
Command: []string{"/bin/sh", "-c", fmt.Sprintf("server=%s; output_file=\"ping_log.txt\"; if [ ! -e \"$output_file\" ]; then touch \"$output_file\"; fi; last_status=\"unknown\"; last_change_time=$(adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf(\"%%06d\", $2) }' && printf \"\\n\"); while true; do status=$(nc -vz -w 1 \"$server\" %s > /dev/null && echo \"up\" || echo \"down\"); current_time=$(adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf(\"%%06d\", $2) }' && printf \"\\n\"); time_diff=$((current_time - last_change_time)); if [ \"$status\" != \"$last_status\" ]; then echo \"$(adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf(\"%%06d\", $2) }' && printf \"\\n\") Status changed from $last_status to $status after ${time_diff} nanoseconds\"; echo \"$(adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf(\"%%06d\", $2) }' && printf \"\\n\") Status changed from $last_status to $status after ${time_diff} nanoseconds\" >> \"$output_file\"; last_change_time=$current_time; last_status=$status; fi; sleep 0.1; done\n", server, port)}, | ||
ImagePullPolicy: corev1.PullIfNotPresent, | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "NODE_IP", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
FieldRef: &corev1.ObjectFieldSelector{ | ||
FieldPath: "status.hostIP", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
newPod.Spec.Containers = append(newPod.Spec.Containers, containers...) | ||
|
||
_, err = kClient.CoreV1().Pods(namespace).Create(ctx, newPod, metav1.CreateOptions{}) | ||
return err | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = wait.PollImmediate(time.Second, 30, func() (bool, error) { | ||
pod, err := kClient.CoreV1().Pods(namespace).Get(ctx, newPod.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
return false, err | ||
} | ||
return pod.Status.Phase == corev1.PodRunning, nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
klog.InfoS("Create Client Pod successfully!") | ||
return newPod, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.