@@ -2,13 +2,16 @@ package env
22
33import (
44 "encoding/json"
5+ "errors"
56 "fmt"
67 "net/url"
78 "os"
89 "os/exec"
10+ "path/filepath"
911 "strings"
1012
1113 "github.com/stackrox/roxie/internal/containerutil"
14+ "github.com/stackrox/roxie/internal/logger"
1215 "golang.org/x/term"
1316)
1417
@@ -65,9 +68,9 @@ func isRunningInteractively() bool {
6568
6669// ensureInitialized performs lazy initialization of cluster information
6770// This avoids contacting the cluster on package import
68- func ensureInitialized () {
71+ func ensureInitialized (log * logger. Logger ) {
6972 if ! initialized {
70- kubeConfig := fetchKubeConfig ()
73+ kubeConfig := fetchKubeConfig (log )
7174 currentContext = kubeConfig .CurrentContext
7275 apiResources := fetchAPIResources ()
7376 currentClusterType = detectClusterType (kubeConfig , apiResources )
@@ -77,13 +80,11 @@ func ensureInitialized() {
7780
7881// GetCurrentClusterType returns the current cluster type, initializing if needed
7982func GetCurrentClusterType () ClusterType {
80- ensureInitialized ()
8183 return currentClusterType
8284}
8385
8486// GetCurrentContext returns the current kubectl context, initializing if needed
8587func GetCurrentContext () string {
86- ensureInitialized ()
8788 return currentContext
8889}
8990
@@ -113,11 +114,16 @@ type KubeCluster struct {
113114 Server string
114115}
115116
117+ // Initialize performs environment initialization and sets the global variables.
118+ func Initialize (log * logger.Logger ) {
119+ ensureInitialized (log )
120+ }
121+
116122// DetectClusterType identifies the cluster type for the current kubectl context
117123// This is a convenience wrapper that fetches the kubeconfig and API resources,
118124// then delegates to detectClusterType for the actual detection logic
119125func DetectClusterType () ClusterType {
120- kubeConfig := fetchKubeConfig ()
126+ kubeConfig := fetchKubeConfig (nil )
121127 apiResources := fetchAPIResources ()
122128 return detectClusterType (kubeConfig , apiResources )
123129}
@@ -180,7 +186,9 @@ func isOpenShift4(apiResources []string) bool {
180186}
181187
182188// fetchKubeConfig retrieves the current kubectl configuration
183- func fetchKubeConfig () KubeConfig {
189+ func fetchKubeConfig (log * logger.Logger ) KubeConfig {
190+ kubeconfigChecks (log )
191+
184192 // Get current context
185193 cmd := exec .Command ("kubectl" , "config" , "current-context" )
186194 output , err := cmd .Output ()
@@ -224,6 +232,44 @@ func fetchKubeConfig() KubeConfig {
224232 }
225233}
226234
235+ func kubeconfigChecks (log * logger.Logger ) error {
236+ kubeConfigPath , err := getKubeConfigPath ()
237+ if err != nil {
238+ return fmt .Errorf ("getting kubeconfig path: %w" , err )
239+ }
240+ log .Infof ("Using kubeconfig %s" , kubeConfigPath )
241+ if _ , err := os .Stat (kubeConfigPath ); err != nil {
242+ log .Warningf ("Kubeconfig %s cannot be found." , kubeConfigPath )
243+ if RunningInContainer {
244+ log .Warningf ("Make sure that your kubeconfig is mounted into the container, as in: -v $KUBECONFIG:/kubeconfig:U" )
245+ }
246+ return fmt .Errorf ("failed to stat kubeconfig %s: %w" , kubeConfigPath , err )
247+ }
248+
249+ file , err := os .Open (kubeConfigPath )
250+ if err != nil {
251+ log .Warningf ("Kubeconfig %s cannot be opened for reading." , kubeConfigPath )
252+ if RunningInContainer {
253+ log .Warningf ("Make sure that your kubeconfig is mounted with the 'U' option, as in: -v $KUBECONFIG:/kubeconfig:U" )
254+ }
255+ return fmt .Errorf ("failed to open kubeconfig %s: %w" , kubeConfigPath , err )
256+ }
257+ _ = file .Close ()
258+ return nil
259+ }
260+
261+ func getKubeConfigPath () (string , error ) {
262+ kubeConfigPath := os .Getenv ("KUBECONFIG" )
263+ if kubeConfigPath == "" {
264+ home := os .Getenv ("HOME" )
265+ if home == "" {
266+ return "" , errors .New ("HOME environment variable is not set" )
267+ }
268+ kubeConfigPath = filepath .Join (home , ".kube" , "config" )
269+ }
270+ return kubeConfigPath , nil
271+ }
272+
227273// fetchAPIResources retrieves the list of API resources from the cluster
228274func fetchAPIResources () []string {
229275 cmd := exec .Command ("kubectl" , "api-resources" , "-o" , "name" )
0 commit comments