Skip to content

Commit

Permalink
Check for pod command Arg in pod CIDR discovery
Browse files Browse the repository at this point in the history
...in addition to the command itself

Fixes #3180

Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
  • Loading branch information
tpantelis committed Sep 12, 2024
1 parent ab6aed4 commit 6236639
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/discovery/network/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ var _ = Describe("Generic Network", func() {
})
})

When("There is a kube-controller pod with the right parameter passed as an Arg", func() {
BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
fakePodWithArg("kube-controller-manager", []string{"kube-controller-manager"}, "--cluster-cidr="+testPodCIDR),
)
Expect(clusterNet).NotTo(BeNil())
})

It("Should return the ClusterNetwork structure with pod CIDR", func() {
Expect(clusterNet.PodCIDRs).To(Equal([]string{testPodCIDR}))
})
})

When("There is a kube-proxy pod but no kube-controller", func() {
BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
Expand Down
7 changes: 7 additions & 0 deletions pkg/discovery/network/network_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func fakePod(component string, command []string, env []v1.EnvVar) *v1.Pod {
return fakePodWithName(component, component, command, env)
}

func fakePodWithArg(component string, command []string, arg string, env ...v1.EnvVar) *v1.Pod {
pod := fakePodWithName(component, component, command, env)
pod.Spec.Containers[0].Args = []string{arg}

return pod
}

func fakePodWithName(name, component string, command []string, env []v1.EnvVar) *v1.Pod {
return fakePodWithNamespace("default", name, component, command, env)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/discovery/network/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package network

import (
"context"
"slices"
"strings"

"github.com/pkg/errors"
Expand All @@ -39,7 +40,12 @@ func FindPodCommandParameter(ctx context.Context, client controllerClient.Client
for _, arg := range pod.Spec.Containers[i].Command {
if strings.HasPrefix(arg, parameter) {
return strings.SplitN(arg, "=", 2)[1], nil
} else if index := slices.IndexFunc(pod.Spec.Containers[i].Args, func(s string) bool {
return strings.HasPrefix(s, parameter)
}); index >= 0 {
return strings.SplitN(pod.Spec.Containers[i].Args[index], "=", 2)[1], nil
}

// Handling the case where the command is in the form of /bin/sh -c exec ....
if strings.Contains(arg, " ") {
for _, subArg := range strings.Split(arg, " ") {
Expand Down

0 comments on commit 6236639

Please sign in to comment.