Skip to content

Commit 1231468

Browse files
committed
add k8 boilerplate for cnp
1 parent dc603d9 commit 1231468

File tree

5 files changed

+84
-40
lines changed

5 files changed

+84
-40
lines changed

test/integration/lrp/lrp_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func setupLRP(t *testing.T, ctx context.Context) (*v1.Pod, func()) {
153153
return &selectedClientPod, cleanupFn
154154
}
155155

156-
func testLRPCase(t *testing.T, ctx context.Context, clientPod v1.Pod, clientCmd []string, expectResponse string, countShouldIncrease bool) {
156+
func testLRPCase(t *testing.T, ctx context.Context, clientPod v1.Pod, clientCmd []string, expectResponse, expectErrMsg string, countShouldIncrease bool) {
157157
config := kubernetes.MustGetRestConfig()
158158
cs := kubernetes.MustGetClientset()
159159

@@ -171,10 +171,10 @@ func testLRPCase(t *testing.T, ctx context.Context, clientPod v1.Pod, clientCmd
171171

172172
t.Log("calling command from client")
173173
// nslookup to 10.0.0.10 (coredns)
174-
val, err := kubernetes.ExecCmdOnPod(ctx, cs, clientPod.Namespace, clientPod.Name, clientContainer, clientCmd, config)
175-
require.NoError(t, err, string(val))
176-
// can connect
174+
val, errMsg, err := kubernetes.ExecCmdOnPodOnce(ctx, cs, clientPod.Namespace, clientPod.Name, clientContainer, clientCmd, config)
175+
177176
require.Contains(t, string(val), expectResponse)
177+
require.Contains(t, string(errMsg), expectErrMsg)
178178

179179
// in case there is time to propagate
180180
time.Sleep(500 * time.Millisecond)
@@ -205,7 +205,7 @@ func TestLRP(t *testing.T) {
205205

206206
testLRPCase(t, ctx, *selectedPod, []string{
207207
"nslookup", "google.com", "10.0.0.10",
208-
}, "Server:", true)
208+
}, "Server:", "", true)
209209
}
210210

211211
// TakeOne takes one item from the slice randomly; if empty, it returns the empty value for the type

test/internal/kubernetes/utils.go

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ func MustSetupLRP(ctx context.Context, clientset *cilium.Clientset, lrpPath stri
258258
}
259259
}
260260

261+
func MustSetupCNP(ctx context.Context, clientset *cilium.Clientset, cnpPath string) (ciliumv2.CiliumNetworkPolicy, func()) { // nolint
262+
cnp := mustParseCNP(cnpPath)
263+
cnps := clientset.CiliumV2().CiliumNetworkPolicies(cnp.Namespace)
264+
mustCreateCiliumNetworkPolicy(ctx, cnps, cnp)
265+
return cnp, func() {
266+
MustDeleteCiliumNetworkPolicy(ctx, cnps, cnp)
267+
}
268+
}
269+
261270
func Int32ToPtr(i int32) *int32 { return &i }
262271

263272
func WaitForPodsRunning(ctx context.Context, clientset *kubernetes.Clientset, namespace, labelselector string) error {
@@ -482,47 +491,56 @@ func writeToFile(dir, fileName, str string) error {
482491
func ExecCmdOnPod(ctx context.Context, clientset *kubernetes.Clientset, namespace, podName, containerName string, cmd []string, config *rest.Config) ([]byte, error) {
483492
var result []byte
484493
execCmdOnPod := func() error {
485-
req := clientset.CoreV1().RESTClient().Post().
486-
Resource("pods").
487-
Name(podName).
488-
Namespace(namespace).
489-
SubResource("exec").
490-
VersionedParams(&corev1.PodExecOptions{
491-
Command: cmd,
492-
Container: containerName,
493-
Stdin: false,
494-
Stdout: true,
495-
Stderr: true,
496-
TTY: false,
497-
}, scheme.ParameterCodec)
498-
499-
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
500-
if err != nil {
501-
return errors.Wrapf(err, "error in creating executor for req %s", req.URL())
502-
}
503-
504-
var stdout, stderr bytes.Buffer
505-
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
506-
Stdin: nil,
507-
Stdout: &stdout,
508-
Stderr: &stderr,
509-
Tty: false,
510-
})
511-
if err != nil {
512-
log.Printf("Error: %v had error %v from command - %v, will retry", podName, err, cmd)
513-
return errors.Wrapf(err, "error in executing command %s", cmd)
514-
}
515-
if len(stdout.Bytes()) == 0 {
516-
log.Printf("Warning: %v had 0 bytes returned from command - %v", podName, cmd)
517-
}
518-
result = stdout.Bytes()
519-
return nil
494+
output, _, err := ExecCmdOnPodOnce(ctx, clientset, namespace, podName, containerName, cmd, config)
495+
result = output
496+
return err
520497
}
521498
retrier := retry.Retrier{Attempts: ShortRetryAttempts, Delay: RetryDelay}
522499
err := retrier.Do(ctx, execCmdOnPod)
523500
return result, errors.Wrapf(err, "could not execute the cmd %s on %s", cmd, podName)
524501
}
525502

503+
func ExecCmdOnPodOnce(ctx context.Context, clientset *kubernetes.Clientset, namespace, podName, containerName string, cmd []string, config *rest.Config) ([]byte, []byte, error) {
504+
req := clientset.CoreV1().RESTClient().Post().
505+
Resource("pods").
506+
Name(podName).
507+
Namespace(namespace).
508+
SubResource("exec").
509+
VersionedParams(&corev1.PodExecOptions{
510+
Command: cmd,
511+
Container: containerName,
512+
Stdin: false,
513+
Stdout: true,
514+
Stderr: true,
515+
TTY: false,
516+
}, scheme.ParameterCodec)
517+
518+
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
519+
if err != nil {
520+
return nil, nil, errors.Wrapf(err, "error in creating executor for req %s", req.URL())
521+
}
522+
523+
var stdout, stderr bytes.Buffer
524+
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
525+
Stdin: nil,
526+
Stdout: &stdout,
527+
Stderr: &stderr,
528+
Tty: false,
529+
})
530+
531+
result := stdout.Bytes()
532+
errResult := stderr.Bytes()
533+
534+
if err != nil {
535+
log.Printf("Error: %v had error %v from command - %v", podName, err, cmd)
536+
return result, errResult, errors.Wrapf(err, "error in executing command %s", cmd)
537+
}
538+
if len(stdout.Bytes()) == 0 {
539+
log.Printf("Warning: %v had 0 bytes returned from command - %v", podName, cmd)
540+
}
541+
return result, errResult, nil
542+
}
543+
526544
func NamespaceExists(ctx context.Context, clientset *kubernetes.Clientset, namespace string) (bool, error) {
527545
_, err := clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
528546
if err != nil {

test/internal/kubernetes/utils_create.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ func mustCreateCiliumLocalRedirectPolicy(ctx context.Context, lrpClient typedcil
189189
}
190190
}
191191

192+
func mustCreateCiliumNetworkPolicy(ctx context.Context, cnpClient typedciliumv2.CiliumNetworkPolicyInterface, cnp ciliumv2.CiliumNetworkPolicy) {
193+
if err := cnpClient.Delete(ctx, cnp.Name, metav1.DeleteOptions{}); err != nil {
194+
if !apierrors.IsNotFound(err) {
195+
log.Fatal(errors.Wrap(err, "failed to delete cilium network policy"))
196+
}
197+
}
198+
log.Printf("Creating CiliumNetworkPolicy %v", cnp.Name)
199+
if _, err := cnpClient.Create(ctx, &cnp, metav1.CreateOptions{}); err != nil {
200+
log.Fatal(errors.Wrap(err, "failed to create cilium network policy"))
201+
}
202+
}
203+
192204
func MustScaleDeployment(ctx context.Context,
193205
deploymentsClient typedappsv1.DeploymentInterface,
194206
deployment appsv1.Deployment,

test/internal/kubernetes/utils_delete.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,11 @@ func MustDeleteCiliumLocalRedirectPolicy(ctx context.Context, lrpClient typedcil
8181
}
8282
}
8383
}
84+
85+
func MustDeleteCiliumNetworkPolicy(ctx context.Context, cnpClient typedciliumv2.CiliumNetworkPolicyInterface, cnp ciliumv2.CiliumNetworkPolicy) {
86+
if err := cnpClient.Delete(ctx, cnp.Name, metav1.DeleteOptions{}); err != nil {
87+
if !apierrors.IsNotFound(err) {
88+
panic(errors.Wrap(err, "failed to delete cilium network policy"))
89+
}
90+
}
91+
}

test/internal/kubernetes/utils_parse.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ func mustParseLRP(path string) ciliumv2.CiliumLocalRedirectPolicy {
6666
mustParseResource(path, &lrp)
6767
return lrp
6868
}
69+
70+
func mustParseCNP(path string) ciliumv2.CiliumNetworkPolicy {
71+
var cnp ciliumv2.CiliumNetworkPolicy
72+
mustParseResource(path, &cnp)
73+
return cnp
74+
}

0 commit comments

Comments
 (0)