diff --git a/pkg/discovery/network/generic_test.go b/pkg/discovery/network/generic_test.go index 769cfc762..1e38ccc18 100644 --- a/pkg/discovery/network/generic_test.go +++ b/pkg/discovery/network/generic_test.go @@ -136,6 +136,22 @@ var _ = Describe("Generic Network", func() { }) }) + When("There is a kube-controller pod with the right parameter passed as an Arg", func() { + var clusterNet *network.ClusterNetwork + + 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() { var clusterNet *network.ClusterNetwork diff --git a/pkg/discovery/network/network_suite_test.go b/pkg/discovery/network/network_suite_test.go index 3ce7ef7f8..d02594590 100644 --- a/pkg/discovery/network/network_suite_test.go +++ b/pkg/discovery/network/network_suite_test.go @@ -36,6 +36,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) } diff --git a/pkg/discovery/network/pods.go b/pkg/discovery/network/pods.go index dd82e6aaa..1a25b2981 100644 --- a/pkg/discovery/network/pods.go +++ b/pkg/discovery/network/pods.go @@ -20,6 +20,7 @@ package network import ( "context" + "slices" "strings" "github.com/pkg/errors" @@ -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, " ") {