Skip to content

Commit

Permalink
sysdump: Autodetect the cilium-operator namespace
Browse files Browse the repository at this point in the history
Commit e8cfcc7 ("sysdump: auto detect namespace from list of
defaults.") implemented autodetection for the cilium-agent namespace but
not for the cilium-operator namespace. It however cleared both
--cilium-operator-namespace and --cilium-namespace's default values,
thus causing collection of the cilium-operator deployment to fail all
the time.

This commit fixes it by implementing namespace detection for
cilium-operator.

Fixes: e8cfcc7 ("sysdump: auto detect namespace from list of defaults.")
Signed-off-by: Paul Chaignon <[email protected]>
  • Loading branch information
pchaigno committed Dec 12, 2022
1 parent 87df40c commit 36b7aa8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions sysdump/sysdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ func NewCollector(k KubernetesClient, o Options, startTime time.Time, cliVersion
c.Options.CiliumNamespace = ns
}

if c.Options.CiliumOperatorNamespace == "" {
ns, err := detectCiliumOperatorNamespace(k)
if err != nil {
return nil, err
}
c.log("Detected Cilium operator in namespace %q", ns)
c.Options.CiliumOperatorNamespace = ns
}

// Grab the Kubernetes nodes for the target cluster.
c.logTask("Collecting Kubernetes nodes")
c.allNodes, err = c.Client.ListNodes(context.Background(), metav1.ListOptions{})
Expand Down Expand Up @@ -1563,3 +1572,26 @@ func detectCiliumNamespace(k KubernetesClient) (string, error) {
}
return "", fmt.Errorf("failed to detect Cilium namespace, could not find Cilium installation in namespaces: %v", DefaultCiliumNamespaces)
}

func detectCiliumOperatorNamespace(k KubernetesClient) (string, error) {
for _, ns := range DefaultCiliumNamespaces {
ctx := context.Background()
ns, err := k.GetNamespace(ctx, ns, metav1.GetOptions{})
if errors.IsNotFound(err) {
continue
}
if err != nil {
return "", fmt.Errorf("failed to detect Cilium operator namespace: %w", err)
}

_, err = k.GetDeployment(ctx, ns.Name, ciliumOperatorDeploymentName, metav1.GetOptions{})
if errors.IsNotFound(err) {
continue
}
if err != nil {
return "", fmt.Errorf("failed to check for Cilium operator Deployment: %w", err)
}
return ns.Name, nil
}
return "", fmt.Errorf("failed to detect Cilium operator namespace, could not find Cilium installation in namespaces: %v", DefaultCiliumNamespaces)
}
2 changes: 1 addition & 1 deletion sysdump/sysdump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (c *fakeClient) GetDaemonSet(ctx context.Context, namespace, name string, o
}

func (c *fakeClient) GetDeployment(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*appsv1.Deployment, error) {
panic("implement me")
return nil, nil
}

func (c *fakeClient) GetLogs(ctx context.Context, namespace, name, container string, sinceTime time.Time, limitBytes int64, previous bool) (string, error) {
Expand Down

0 comments on commit 36b7aa8

Please sign in to comment.