Skip to content

Commit

Permalink
fix k8s provider panic for incluster config (#1982)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Sep 29, 2023
1 parent 31bf660 commit 735b1ce
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion providers/k8s/connection/api/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,29 @@ 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,
d: d,
config: config,
clientset: clientset,
namespace: asset.Connections[0].Options[shared.OPTION_NAMESPACE],
currentClusterName: kubeConfig.Contexts[kubeConfig.CurrentContext].Cluster,
currentClusterName: currentClusterName,
}

return &res, nil
Expand Down

0 comments on commit 735b1ce

Please sign in to comment.