From 735b1ce5cb0bcbcb3b9597e46eacaacccaf9f528 Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Fri, 29 Sep 2023 14:44:00 +0300 Subject: [PATCH] fix k8s provider panic for incluster config (#1982) Signed-off-by: Ivan Milchev --- providers/k8s/connection/api/connection.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/providers/k8s/connection/api/connection.go b/providers/k8s/connection/api/connection.go index f7efc86006..52557045a1 100644 --- a/providers/k8s/connection/api/connection.go +++ b/providers/k8s/connection/api/connection.go @@ -83,6 +83,21 @@ func NewConnection(id uint32, asset *inventory.Asset) (shared.Connection, error) return nil, errors.Wrap(err, "could not create kubernetes clientset") } + currentClusterName := "" + if ctx, ok := kubeConfig.Contexts[kubeConfig.CurrentContext]; ok { + currentClusterName = ctx.Cluster + } else { + // right now we use the name of the first node to identify the cluster + result, err := clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{}) + if err != nil { + return nil, err + } + + if len(result.Items) > 0 { + currentClusterName = result.Items[0].GetName() + } + } + res := Connection{ id: id, asset: asset, @@ -90,7 +105,7 @@ func NewConnection(id uint32, asset *inventory.Asset) (shared.Connection, error) config: config, clientset: clientset, namespace: asset.Connections[0].Options[shared.OPTION_NAMESPACE], - currentClusterName: kubeConfig.Contexts[kubeConfig.CurrentContext].Cluster, + currentClusterName: currentClusterName, } return &res, nil