Skip to content

Commit a7c3fa5

Browse files
committed
Integrate logger into env initialization, add convenience logging when running containerized
1 parent 25ef8f4 commit a7c3fa5

3 files changed

Lines changed: 61 additions & 13 deletions

File tree

‎cmd/env.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/spf13/cobra"
87
"github.com/stackrox/roxie/internal/env"
8+
"github.com/stackrox/roxie/internal/logger"
99
)
1010

1111
func newEnvCmd() *cobra.Command {
@@ -21,10 +21,10 @@ func newEnvCmd() *cobra.Command {
2121
}
2222

2323
func runEnv(cmd *cobra.Command, args []string) {
24-
fmt.Println("Roxie Environment Information:")
25-
fmt.Println("==============================")
26-
fmt.Printf("Kube config: %s\n", os.Getenv("KUBECONFIG"))
27-
fmt.Printf("Running in Container: %v\n", env.RunningInContainer)
28-
fmt.Printf("Current Context: %s\n", env.GetCurrentContext())
29-
fmt.Printf("Cluster Type: %s\n", env.GetCurrentClusterType().String())
24+
log := logger.New()
25+
env.Initialize(log)
26+
log.Infof("Kube config: %s", os.Getenv("KUBECONFIG"))
27+
log.Infof("Running in Container: %v", env.RunningInContainer)
28+
log.Infof("Current Context: %s", env.GetCurrentContext())
29+
log.Infof("Cluster Type: %s", env.GetCurrentClusterType().String())
3030
}

‎internal/deployer/deployer.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ func (d *Deployer) deleteSecuredClusterResources(ctx context.Context, wait bool)
308308
}
309309

310310
func New(log *logger.Logger, overrideFile string, overrideSetExpressions []string) (*Deployer, error) {
311+
env.Initialize(log)
312+
311313
// Check required tools first
312314
if err := checkRequiredTools(); err != nil {
313315
return nil, err

‎internal/env/env.go‎

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package env
22

33
import (
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
7982
func GetCurrentClusterType() ClusterType {
80-
ensureInitialized()
8183
return currentClusterType
8284
}
8385

8486
// GetCurrentContext returns the current kubectl context, initializing if needed
8587
func 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
119125
func 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
228274
func fetchAPIResources() []string {
229275
cmd := exec.Command("kubectl", "api-resources", "-o", "name")

0 commit comments

Comments
 (0)