Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func runDeploy(cmd *cobra.Command, args []string) error {
}

// On infra OpenShift we already get image pull secrets for Quay automatically.
if env.CurrentClusterType != env.InfraOpenShift4 {
if env.GetCurrentClusterType() != env.InfraOpenShift4 {
if os.Getenv("REGISTRY_USERNAME") == "" || os.Getenv("REGISTRY_PASSWORD") == "" {
return errors.New("containerized mode requires REGISTRY_USERNAME and REGISTRY_PASSWORD environment variables")
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func runDeploy(cmd *cobra.Command, args []string) error {
// Resolve "auto" resources based on cluster type
resolvedResources := resources
if resources == "auto" {
resolvedResources = resolveAutoResources(env.CurrentClusterType, log)
resolvedResources = resolveAutoResources(env.GetCurrentClusterType(), log)
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
Expand Down
4 changes: 2 additions & 2 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func runEnv(cmd *cobra.Command, args []string) {
fmt.Println("Roxie Environment Information:")
fmt.Println("==============================")
fmt.Printf("Running in Container: %v\n", env.RunningInContainer)
fmt.Printf("Current Context: %s\n", env.CurrentContext)
fmt.Printf("Cluster Type: %s\n", env.CurrentClusterType.String())
fmt.Printf("Current Context: %s\n", env.GetCurrentContext())
fmt.Printf("Cluster Type: %s\n", env.GetCurrentClusterType().String())
}
15 changes: 13 additions & 2 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"

"github.com/fatih/color"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,8 +55,13 @@ func runLogsOperator(cmd *cobra.Command, args []string) error {
kubectlArgs = append(kubectlArgs, "-f")
}

// Create kubectl command
kubectlCmd := exec.Command("kubectl", kubectlArgs...)
// Create context with timeout for initial connection
// Use a longer timeout (30s) to allow for cluster connection
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// Create kubectl command with context
kubectlCmd := exec.CommandContext(ctx, "kubectl", kubectlArgs...)

// Get stdout pipe for streaming
stdout, err := kubectlCmd.StdoutPipe()
Expand All @@ -75,6 +82,10 @@ func runLogsOperator(cmd *cobra.Command, args []string) error {

// Wait for command to complete
if err := kubectlCmd.Wait(); err != nil {
// Check if it was a timeout
if ctx.Err() == context.DeadlineExceeded {
return fmt.Errorf("timeout connecting to cluster - ensure your kubeconfig is correct and the cluster is reachable")
}
// Don't return error if kubectl was interrupted (e.g., Ctrl+C)
if _, ok := err.(*exec.ExitError); ok {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/deployer/deploy_via_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (d *Deployer) prepareNamespace(ctx context.Context, namespace string) error
return err
}

if env.CurrentClusterType != env.InfraOpenShift4 {
if env.GetCurrentClusterType() != env.InfraOpenShift4 {
if err := d.ensurePullSecretExists(ctx, namespace); err != nil {
return fmt.Errorf("ensuring image pull secret exists: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ func (d *Deployer) PrintCentralDeploymentSummary() {

// Deployment details
log.Info(cyan.Sprint("│") + createRow("Component", component))
log.Info(cyan.Sprint("│") + createRow("Cluster Type", env.CurrentClusterType.String()))
log.Info(cyan.Sprint("│") + createRow("Cluster Type", env.GetCurrentClusterType().String()))
log.Info(cyan.Sprint("│") + createRow("Main Tag", mainImageTag))
log.Info(cyan.Sprint("│") + createRow("Kubernetes Context", kubeContext))
log.Info(cyan.Sprint("│") + createRow("Deployment Method", map[bool]string{true: "Helm", false: "Operator"}[helm]))
Expand Down Expand Up @@ -821,7 +821,7 @@ func (d *Deployer) PrintSecuredClusterDeploymentSummary() {

// Deployment details
log.Info(cyan.Sprint("│") + createRow("Component", component))
log.Info(cyan.Sprint("│") + createRow("Cluster Type", env.CurrentClusterType.String()))
log.Info(cyan.Sprint("│") + createRow("Cluster Type", env.GetCurrentClusterType().String()))
log.Info(cyan.Sprint("│") + createRow("Main Tag", mainImageTag))
log.Info(cyan.Sprint("│") + createRow("Kubernetes Context", kubeContext))
log.Info(cyan.Sprint("│") + createRow("Deployment Method", map[bool]string{true: "Helm", false: "Operator"}[helm]))
Expand Down
43 changes: 33 additions & 10 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,47 @@ const (
)

var (
// CurrentClusterType holds the detected cluster type for the current kubectl context
// This is automatically populated during package initialization
CurrentClusterType ClusterType
// currentClusterType holds the detected cluster type for the current kubectl context
// This is lazily populated on first access via GetCurrentClusterType()
currentClusterType ClusterType

// CurrentContext holds the name of the current kubectl context
// This is automatically populated during package initialization
CurrentContext string
// currentContext holds the name of the current kubectl context
// This is lazily populated on first access via GetCurrentContext()
currentContext string

// initialized tracks whether we've performed the lazy initialization
initialized bool
)

func init() {
RunningInContainer = containerutil.IsRunningInContainer()
if RunningInContainer {
os.Setenv("KUBECONFIG", "/kubeconfig")
}
kubeConfig := fetchKubeConfig()
CurrentContext = kubeConfig.CurrentContext
apiResources := fetchAPIResources()
CurrentClusterType = detectClusterType(kubeConfig, apiResources)
}

// ensureInitialized performs lazy initialization of cluster information
// This avoids contacting the cluster on package import
func ensureInitialized() {
if !initialized {
kubeConfig := fetchKubeConfig()
currentContext = kubeConfig.CurrentContext
apiResources := fetchAPIResources()
currentClusterType = detectClusterType(kubeConfig, apiResources)
initialized = true
}
}

// GetCurrentClusterType returns the current cluster type, initializing if needed
func GetCurrentClusterType() ClusterType {
ensureInitialized()
return currentClusterType
}

// GetCurrentContext returns the current kubectl context, initializing if needed
func GetCurrentContext() string {
ensureInitialized()
return currentContext
}

// String returns the string representation of a ClusterType
Expand Down