Skip to content

Commit 3591a97

Browse files
committed
address feedback
leverages must delete functions during creation changes log.fatal to panic since log fatal will immediately exit, skipping all defers leverages wait for daemonset instead of wait for pods adds retry parameter to exec cmd on pod, adjusting associated calls incorporates exec cmd on pod error into lrp test
1 parent e7fa7d8 commit 3591a97

File tree

8 files changed

+92
-89
lines changed

8 files changed

+92
-89
lines changed

test/integration/lrp/lrp_fqdn_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,50 +48,57 @@ func TestLRPFQDN(t *testing.T) {
4848
command []string
4949
expectedMsgContains string
5050
expectedErrMsgContains string
51+
shouldError bool
5152
countIncreases bool
5253
}{
5354
{
5455
name: "nslookup google succeeds",
5556
command: []string{"nslookup", "www.google.com", "10.0.0.10"},
56-
expectedMsgContains: "Server:",
57+
expectedMsgContains: "answer:",
5758
countIncreases: true,
59+
shouldError: false,
5860
},
5961
{
6062
name: "wget google succeeds",
6163
command: []string{"wget", "-O", "index.html", "www.google.com", "--timeout=5"},
6264
expectedErrMsgContains: "saved",
6365
countIncreases: true,
66+
shouldError: false,
6467
},
6568
{
6669
name: "nslookup bing succeeds",
6770
command: []string{"nslookup", "www.bing.com", "10.0.0.10"},
68-
expectedMsgContains: "Server:",
71+
expectedMsgContains: "answer:",
6972
countIncreases: true,
73+
shouldError: false,
7074
},
7175
{
7276
name: "wget bing fails but dns succeeds",
7377
command: []string{"wget", "-O", "index.html", "www.bing.com", "--timeout=5"},
7478
expectedErrMsgContains: "timed out",
7579
countIncreases: true,
80+
shouldError: true,
7681
},
7782
{
7883
name: "nslookup example fails",
7984
command: []string{"nslookup", "www.example.com", "10.0.0.10"},
8085
expectedMsgContains: "REFUSED",
8186
countIncreases: false,
87+
shouldError: true,
8288
},
8389
{
8490
// won't be able to nslookup, let alone query the website
8591
name: "wget example fails",
8692
command: []string{"wget", "-O", "index.html", "www.example.com", "--timeout=5"},
8793
expectedErrMsgContains: "bad address",
8894
countIncreases: false,
95+
shouldError: true,
8996
},
9097
}
9198
for _, tt := range tests {
9299
tt := tt
93100
t.Run(tt.name, func(t *testing.T) {
94-
testLRPCase(t, ctx, *selectedPod, tt.command, tt.expectedMsgContains, tt.expectedErrMsgContains, tt.countIncreases)
101+
testLRPCase(t, ctx, *selectedPod, tt.command, tt.expectedMsgContains, tt.expectedErrMsgContains, tt.shouldError, tt.countIncreases)
95102
})
96103
}
97104
}

test/integration/lrp/lrp_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func setupLRP(t *testing.T, ctx context.Context) (*corev1.Pod, func()) {
105105
cleanUpFns = append(cleanUpFns, cleanupService)
106106
nodeLocalDNSDS, cleanupNodeLocalDNS := kubernetes.MustSetupDaemonset(ctx, cs, tempNodeLocalDNSDaemonsetPath)
107107
cleanUpFns = append(cleanUpFns, cleanupNodeLocalDNS)
108-
err = kubernetes.WaitForPodsRunning(ctx, cs, nodeLocalDNSDS.Namespace, nodeLocalDNSLabelSelector)
108+
kubernetes.WaitForPodDaemonset(ctx, cs, nodeLocalDNSDS.Namespace, nodeLocalDNSDS.Name, nodeLocalDNSLabelSelector)
109109
require.NoError(t, err)
110110
// select a local dns pod after they start running
111111
pods, err := kubernetes.GetPodsByNode(ctx, cs, nodeLocalDNSDS.Namespace, nodeLocalDNSLabelSelector, selectedNode)
@@ -119,7 +119,7 @@ func setupLRP(t *testing.T, ctx context.Context) (*corev1.Pod, func()) {
119119
// create client pods
120120
clientDS, cleanupClient := kubernetes.MustSetupDaemonset(ctx, cs, clientPath)
121121
cleanUpFns = append(cleanUpFns, cleanupClient)
122-
err = kubernetes.WaitForPodsRunning(ctx, cs, clientDS.Namespace, clientLabelSelector)
122+
kubernetes.WaitForPodDaemonset(ctx, cs, clientDS.Namespace, clientDS.Name, clientLabelSelector)
123123
require.NoError(t, err)
124124
// select a client pod after they start running
125125
clientPods, err := kubernetes.GetPodsByNode(ctx, cs, clientDS.Namespace, clientLabelSelector, selectedNode)
@@ -153,7 +153,9 @@ func setupLRP(t *testing.T, ctx context.Context) (*corev1.Pod, func()) {
153153
return &selectedClientPod, cleanupFn
154154
}
155155

156-
func testLRPCase(t *testing.T, ctx context.Context, clientPod corev1.Pod, clientCmd []string, expectResponse, expectErrMsg string, countShouldIncrease bool) {
156+
func testLRPCase(t *testing.T, ctx context.Context, clientPod corev1.Pod, clientCmd []string, expectResponse, expectErrMsg string,
157+
shouldError, countShouldIncrease bool) {
158+
157159
config := kubernetes.MustGetRestConfig()
158160
cs := kubernetes.MustGetClientset()
159161

@@ -171,10 +173,15 @@ func testLRPCase(t *testing.T, ctx context.Context, clientPod corev1.Pod, client
171173

172174
t.Log("calling command from client")
173175

174-
val, errMsg, err := kubernetes.ExecCmdOnPodOnce(ctx, cs, clientPod.Namespace, clientPod.Name, clientContainer, clientCmd, config)
176+
val, errMsg, err := kubernetes.ExecCmdOnPod(ctx, cs, clientPod.Namespace, clientPod.Name, clientContainer, clientCmd, config, false)
175177

176178
require.Contains(t, string(val), expectResponse)
177179
require.Contains(t, string(errMsg), expectErrMsg)
180+
if shouldError {
181+
require.Error(t, err)
182+
} else {
183+
require.NoError(t, err)
184+
}
178185

179186
// in case there is time to propagate
180187
time.Sleep(500 * time.Millisecond)
@@ -205,7 +212,7 @@ func TestLRP(t *testing.T) {
205212

206213
testLRPCase(t, ctx, *selectedPod, []string{
207214
"nslookup", "google.com", "10.0.0.10",
208-
}, "Server:", "", true)
215+
}, "Server:", "", false, true)
209216
}
210217

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

test/internal/datapath/datapath_win.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var ipv6PrefixPolicy = []string{"powershell", "-c", "curl.exe", "-6", "-v", "www
1919

2020
func podTest(ctx context.Context, clientset *kubernetes.Clientset, srcPod *apiv1.Pod, cmd []string, rc *restclient.Config, passFunc func(string) error) error {
2121
logrus.Infof("podTest() - %v %v", srcPod.Name, cmd)
22-
output, err := acnk8s.ExecCmdOnPod(ctx, clientset, srcPod.Namespace, srcPod.Name, "", cmd, rc)
22+
output, _, err := acnk8s.ExecCmdOnPod(ctx, clientset, srcPod.Namespace, srcPod.Name, "", cmd, rc, true)
2323
if err != nil {
2424
return errors.Wrapf(err, "failed to execute command on pod: %v", srcPod.Name)
2525
}

test/internal/kubernetes/utils.go

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -233,37 +233,37 @@ func MustSetupDeployment(ctx context.Context, clientset *kubernetes.Clientset, d
233233

234234
func MustSetupServiceAccount(ctx context.Context, clientset *kubernetes.Clientset, serviceAccountPath string) (corev1.ServiceAccount, func()) { // nolint
235235
sa := mustParseServiceAccount(serviceAccountPath)
236-
sas := clientset.CoreV1().ServiceAccounts(sa.Namespace)
237-
mustCreateServiceAccount(ctx, sas, sa)
236+
saClient := clientset.CoreV1().ServiceAccounts(sa.Namespace)
237+
mustCreateServiceAccount(ctx, saClient, sa)
238238
return sa, func() {
239-
MustDeleteServiceAccount(ctx, sas, sa)
239+
MustDeleteServiceAccount(ctx, saClient, sa)
240240
}
241241
}
242242

243243
func MustSetupService(ctx context.Context, clientset *kubernetes.Clientset, servicePath string) (corev1.Service, func()) { // nolint
244244
svc := mustParseService(servicePath)
245-
svcs := clientset.CoreV1().Services(svc.Namespace)
246-
mustCreateService(ctx, svcs, svc)
245+
svcClient := clientset.CoreV1().Services(svc.Namespace)
246+
mustCreateService(ctx, svcClient, svc)
247247
return svc, func() {
248-
MustDeleteService(ctx, svcs, svc)
248+
MustDeleteService(ctx, svcClient, svc)
249249
}
250250
}
251251

252252
func MustSetupLRP(ctx context.Context, clientset *cilium.Clientset, lrpPath string) (ciliumv2.CiliumLocalRedirectPolicy, func()) { // nolint
253253
lrp := mustParseLRP(lrpPath)
254-
lrps := clientset.CiliumV2().CiliumLocalRedirectPolicies(lrp.Namespace)
255-
mustCreateCiliumLocalRedirectPolicy(ctx, lrps, lrp)
254+
lrpClient := clientset.CiliumV2().CiliumLocalRedirectPolicies(lrp.Namespace)
255+
mustCreateCiliumLocalRedirectPolicy(ctx, lrpClient, lrp)
256256
return lrp, func() {
257-
MustDeleteCiliumLocalRedirectPolicy(ctx, lrps, lrp)
257+
MustDeleteCiliumLocalRedirectPolicy(ctx, lrpClient, lrp)
258258
}
259259
}
260260

261261
func MustSetupCNP(ctx context.Context, clientset *cilium.Clientset, cnpPath string) (ciliumv2.CiliumNetworkPolicy, func()) { // nolint
262262
cnp := mustParseCNP(cnpPath)
263-
cnps := clientset.CiliumV2().CiliumNetworkPolicies(cnp.Namespace)
264-
mustCreateCiliumNetworkPolicy(ctx, cnps, cnp)
263+
cnpClient := clientset.CiliumV2().CiliumNetworkPolicies(cnp.Namespace)
264+
mustCreateCiliumNetworkPolicy(ctx, cnpClient, cnp)
265265
return cnp, func() {
266-
MustDeleteCiliumNetworkPolicy(ctx, cnps, cnp)
266+
MustDeleteCiliumNetworkPolicy(ctx, cnpClient, cnp)
267267
}
268268
}
269269

@@ -488,59 +488,60 @@ func writeToFile(dir, fileName, str string) error {
488488
return errors.Wrap(err, "failed to write string")
489489
}
490490

491-
func ExecCmdOnPod(ctx context.Context, clientset *kubernetes.Clientset, namespace, podName, containerName string, cmd []string, config *rest.Config) ([]byte, error) {
491+
// ExecCmdOnPod runs the specified command on a particular pod and retries the command on failure if doRetry is set to true
492+
// The function returns the standard output, standard error, and error (if any) in that order
493+
func ExecCmdOnPod(ctx context.Context, clientset *kubernetes.Clientset, namespace, podName, containerName string, cmd []string, config *rest.Config, doRetry bool) ([]byte, []byte, error) { // nolint
492494
var result []byte
495+
var errResult []byte
493496
execCmdOnPod := func() error {
494-
output, _, err := ExecCmdOnPodOnce(ctx, clientset, namespace, podName, containerName, cmd, config)
495-
result = output
496-
return err
497-
}
498-
retrier := retry.Retrier{Attempts: ShortRetryAttempts, Delay: RetryDelay}
499-
err := retrier.Do(ctx, execCmdOnPod)
500-
return result, errors.Wrapf(err, "could not execute the cmd %s on %s", cmd, podName)
501-
}
502-
503-
// ExecCmdOnPodOnce runs a command on the specified pod and returns its standard output, standard error output, and error in that order
504-
// The command does not retry when the command fails (ex: due to timeout), unlike ExecCmdOnPod
505-
func ExecCmdOnPodOnce(ctx context.Context, clientset *kubernetes.Clientset, namespace, podName, containerName string, cmd []string, config *rest.Config) ([]byte, []byte, error) { // nolint
506-
req := clientset.CoreV1().RESTClient().Post().
507-
Resource("pods").
508-
Name(podName).
509-
Namespace(namespace).
510-
SubResource("exec").
511-
VersionedParams(&corev1.PodExecOptions{
512-
Command: cmd,
513-
Container: containerName,
514-
Stdin: false,
515-
Stdout: true,
516-
Stderr: true,
517-
TTY: false,
518-
}, scheme.ParameterCodec)
519-
520-
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
521-
if err != nil {
522-
return nil, nil, errors.Wrapf(err, "error in creating executor for req %s", req.URL())
523-
}
497+
req := clientset.CoreV1().RESTClient().Post().
498+
Resource("pods").
499+
Name(podName).
500+
Namespace(namespace).
501+
SubResource("exec").
502+
VersionedParams(&corev1.PodExecOptions{
503+
Command: cmd,
504+
Container: containerName,
505+
Stdin: false,
506+
Stdout: true,
507+
Stderr: true,
508+
TTY: false,
509+
}, scheme.ParameterCodec)
510+
511+
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
512+
if err != nil {
513+
return errors.Wrapf(err, "error in creating executor for req %s", req.URL())
514+
}
524515

525-
var stdout, stderr bytes.Buffer
526-
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
527-
Stdin: nil,
528-
Stdout: &stdout,
529-
Stderr: &stderr,
530-
Tty: false,
531-
})
516+
var stdout, stderr bytes.Buffer
517+
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
518+
Stdin: nil,
519+
Stdout: &stdout,
520+
Stderr: &stderr,
521+
Tty: false,
522+
})
532523

533-
result := stdout.Bytes()
534-
errResult := stderr.Bytes()
524+
result = stdout.Bytes()
525+
errResult = stderr.Bytes()
535526

536-
if err != nil {
537-
log.Printf("Error: %v had error %v from command - %v", podName, err, cmd)
538-
return result, errResult, errors.Wrapf(err, "error in executing command %s", cmd)
527+
if err != nil {
528+
log.Printf("Error: %v had error %v from command - %v", podName, err, cmd)
529+
return errors.Wrapf(err, "error in executing command %s", cmd)
530+
}
531+
if len(stdout.Bytes()) == 0 {
532+
log.Printf("Warning: %v had 0 bytes returned from command - %v", podName, cmd)
533+
}
534+
return nil
539535
}
540-
if len(stdout.Bytes()) == 0 {
541-
log.Printf("Warning: %v had 0 bytes returned from command - %v", podName, cmd)
536+
537+
var err error
538+
if doRetry {
539+
retrier := retry.Retrier{Attempts: ShortRetryAttempts, Delay: RetryDelay}
540+
err = retrier.Do(ctx, execCmdOnPod)
541+
} else {
542+
err = execCmdOnPod()
542543
}
543-
return result, errResult, nil
544+
return result, errResult, errors.Wrapf(err, "could not execute the cmd %s on %s", cmd, podName)
544545
}
545546

546547
func NamespaceExists(ctx context.Context, clientset *kubernetes.Clientset, namespace string) (bool, error) {
@@ -655,7 +656,7 @@ func RestartKubeProxyService(ctx context.Context, clientset *kubernetes.Clientse
655656
}
656657
privilegedPod := pod.Items[0]
657658
// exec into the pod and restart kubeproxy
658-
_, err = ExecCmdOnPod(ctx, clientset, privilegedNamespace, privilegedPod.Name, "", restartKubeProxyCmd, config)
659+
_, _, err = ExecCmdOnPod(ctx, clientset, privilegedNamespace, privilegedPod.Name, "", restartKubeProxyCmd, config, true)
659660
if err != nil {
660661
return errors.Wrapf(err, "failed to exec into privileged pod %s on node %s", privilegedPod.Name, node.Name)
661662
}

test/internal/kubernetes/utils_create.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -166,38 +166,26 @@ func mustCreateConfigMap(ctx context.Context, cmi typedcorev1.ConfigMapInterface
166166
}
167167

168168
func mustCreateService(ctx context.Context, svci typedcorev1.ServiceInterface, svc corev1.Service) {
169-
if err := svci.Delete(ctx, svc.Name, metav1.DeleteOptions{}); err != nil {
170-
if !apierrors.IsNotFound(err) {
171-
log.Fatal(errors.Wrap(err, "failed to delete service"))
172-
}
173-
}
169+
MustDeleteService(ctx, svci, svc)
174170
log.Printf("Creating Service %v", svc.Name)
175171
if _, err := svci.Create(ctx, &svc, metav1.CreateOptions{}); err != nil {
176-
log.Fatal(errors.Wrap(err, "failed to create service"))
172+
panic(errors.Wrap(err, "failed to create service"))
177173
}
178174
}
179175

180176
func mustCreateCiliumLocalRedirectPolicy(ctx context.Context, lrpClient typedciliumv2.CiliumLocalRedirectPolicyInterface, clrp ciliumv2.CiliumLocalRedirectPolicy) {
181-
if err := lrpClient.Delete(ctx, clrp.Name, metav1.DeleteOptions{}); err != nil {
182-
if !apierrors.IsNotFound(err) {
183-
log.Fatal(errors.Wrap(err, "failed to delete cilium local redirect policy"))
184-
}
185-
}
177+
MustDeleteCiliumLocalRedirectPolicy(ctx, lrpClient, clrp)
186178
log.Printf("Creating CiliumLocalRedirectPolicy %v", clrp.Name)
187179
if _, err := lrpClient.Create(ctx, &clrp, metav1.CreateOptions{}); err != nil {
188-
log.Fatal(errors.Wrap(err, "failed to create cilium local redirect policy"))
180+
panic(errors.Wrap(err, "failed to create cilium local redirect policy"))
189181
}
190182
}
191183

192184
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-
}
185+
MustDeleteCiliumNetworkPolicy(ctx, cnpClient, cnp)
198186
log.Printf("Creating CiliumNetworkPolicy %v", cnp.Name)
199187
if _, err := cnpClient.Create(ctx, &cnp, metav1.CreateOptions{}); err != nil {
200-
log.Fatal(errors.Wrap(err, "failed to create cilium network policy"))
188+
panic(errors.Wrap(err, "failed to create cilium network policy"))
201189
}
202190
}
203191

test/validate/linux_validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (v *Validator) validateRestartNetwork(ctx context.Context) error {
326326
}
327327
privilegedPod := pod.Items[0]
328328
// exec into the pod to get the state file
329-
_, err = acnk8s.ExecCmdOnPod(ctx, v.clientset, privilegedNamespace, privilegedPod.Name, "", restartNetworkCmd, v.config)
329+
_, _, err = acnk8s.ExecCmdOnPod(ctx, v.clientset, privilegedNamespace, privilegedPod.Name, "", restartNetworkCmd, v.config, true)
330330
if err != nil {
331331
return errors.Wrapf(err, "failed to exec into privileged pod %s on node %s", privilegedPod.Name, node.Name)
332332
}

test/validate/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (v *Validator) validateIPs(ctx context.Context, stateFileIps stateFileIpsFu
142142
podName := pod.Items[0].Name
143143
// exec into the pod to get the state file
144144
log.Printf("Executing command %s on pod %s, container %s", cmd, podName, containerName)
145-
result, err := acnk8s.ExecCmdOnPod(ctx, v.clientset, namespace, podName, containerName, cmd, v.config)
145+
result, _, err := acnk8s.ExecCmdOnPod(ctx, v.clientset, namespace, podName, containerName, cmd, v.config, true)
146146
if err != nil {
147147
return errors.Wrapf(err, "failed to exec into privileged pod - %s", podName)
148148
}

test/validate/windows_validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func validateHNSNetworkState(ctx context.Context, nodes *corev1.NodeList, client
269269
}
270270
podName := pod.Items[0].Name
271271
// exec into the pod to get the state file
272-
result, err := acnk8s.ExecCmdOnPod(ctx, clientset, privilegedNamespace, podName, "", hnsNetworkCmd, restConfig)
272+
result, _, err := acnk8s.ExecCmdOnPod(ctx, clientset, privilegedNamespace, podName, "", hnsNetworkCmd, restConfig, true)
273273
if err != nil {
274274
return errors.Wrap(err, "failed to exec into privileged pod")
275275
}

0 commit comments

Comments
 (0)