Skip to content

Commit 41b8d88

Browse files
authored
Check specifically if running in *roxie* Container, not just somehow containerized (#86)
1 parent 37a4662 commit 41b8d88

9 files changed

Lines changed: 41 additions & 44 deletions

File tree

‎Dockerfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ USER roxie
207207

208208
# Set environment variables
209209
ENV HOME=/ \
210+
RUNNING_IN_ROXIE_CONTAINER=true \
210211
KUBECONFIG=/kubeconfig \
211212
PATH=/usr/local/bin:$PATH
212213

‎cmd/deploy.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func runDeploy(cmd *cobra.Command, args []string) error {
9999

100100
portForwardEnabledFinal := portForwarding || exposure == "none"
101101

102-
if env.RunningInContainer {
102+
if env.RunningInRoxieContainer {
103103
// For running containerized we have specific requirements.
104104
if portForwardEnabledFinal {
105105
return errors.New("containerized mode does not support port-forwarding")
@@ -117,8 +117,6 @@ func runDeploy(cmd *cobra.Command, args []string) error {
117117
return fmt.Errorf("containerized mode requires /kubeconfig file: %w", err)
118118
}
119119
}
120-
log.Dim("Using KUBECONFIG=/kubeconfig.")
121-
os.Setenv("KUBECONFIG", "/kubeconfig")
122120
}
123121

124122
if konflux {

‎cmd/env.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func runEnv(cmd *cobra.Command, args []string) error {
2929

3030
fmt.Println("Roxie Environment Information:")
3131
fmt.Println("==============================")
32-
fmt.Printf("Kube config: %s\n", os.Getenv("KUBECONFIG"))
33-
fmt.Printf("Running in Container: %v\n", env.RunningInContainer)
34-
fmt.Printf("Current Context: %s\n", env.GetCurrentContext())
35-
fmt.Printf("Cluster Type: %s\n", env.GetCurrentClusterType().String())
32+
fmt.Printf("Kube config: %s\n", os.Getenv("KUBECONFIG"))
33+
fmt.Printf("Running in roxie container: %v\n", env.RunningInRoxieContainer)
34+
fmt.Printf("Current Context: %s\n", env.GetCurrentContext())
35+
fmt.Printf("Cluster Type: %s\n", env.GetCurrentClusterType().String())
3636

3737
return nil
3838
}

‎cmd/subshell.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func printBanner(endpoint, exposure string, haproxyAvailable, haproxyStarted boo
182182
cyan.Println("[roxie] Central UI: http://localhost:8080 (username: admin, password: see $ROX_ADMIN_PASSWORD)")
183183
} else if exposure != "none" && exposure != "" {
184184
cyan.Printf("[roxie] Central UI: https://%s", endpoint)
185-
} else if !env.RunningInContainer {
185+
} else if !env.RunningInRoxieContainer {
186186
cyan.Println("[roxie] Note: Installing haproxy enables automatic HTTP access to Central at http://localhost:8080")
187187
}
188188

‎go.mod‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ go 1.25.0
44

55
require (
66
github.com/fatih/color v1.16.0
7-
github.com/moby/sys/mountinfo v0.7.2
87
github.com/spf13/cobra v1.8.0
98
golang.org/x/term v0.38.0
109
gopkg.in/yaml.v3 v3.0.1
1110
k8s.io/apimachinery v0.35.3
1211
)
1312

1413
require (
14+
github.com/davecgh/go-spew v1.1.1 // indirect
1515
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
1616
github.com/go-logr/logr v1.4.3 // indirect
1717
github.com/inconshreveable/mousetrap v1.1.0 // indirect
@@ -20,7 +20,9 @@ require (
2020
github.com/mattn/go-isatty v0.0.20 // indirect
2121
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2222
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
23+
github.com/pmezard/go-difflib v1.0.0 // indirect
2324
github.com/spf13/pflag v1.0.9 // indirect
25+
github.com/stretchr/testify v1.11.1 // indirect
2426
github.com/x448/float16 v0.8.4 // indirect
2527
go.yaml.in/yaml/v2 v2.4.3 // indirect
2628
golang.org/x/net v0.48.0 // indirect

‎go.sum‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
2020
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
2121
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
2222
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
23-
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
24-
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
2523
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
2624
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
2725
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

‎internal/containerutil/detect.go‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package containerutil
22

33
import (
4-
"github.com/moby/sys/mountinfo"
4+
"fmt"
5+
"os"
6+
"strconv"
57
)
68

7-
// IsRunningInContainer detects if the current process is running inside
8-
// a Docker, Podman, or Kubernetes container by checking if the root
9-
// filesystem is an overlay filesystem (standard for containers).
10-
func IsRunningInContainer() bool {
11-
// Get root mount info using efficient filter
12-
rootMounts, err := mountinfo.GetMounts(mountinfo.SingleEntryFilter("/"))
13-
if err != nil || len(rootMounts) == 0 {
9+
// IsRunningInRoxieContainer checks if we are running inside the released roxie container.
10+
// This knowledge allows us to adjust behavior accordingly, allowing for some UX improvements.
11+
func IsRunningInRoxieContainer() bool {
12+
strVal, exists := os.LookupEnv("RUNNING_IN_ROXIE_CONTAINER")
13+
if !exists || strVal == "" {
1414
return false
1515
}
16-
17-
// Containers use overlay filesystem for root
18-
return rootMounts[0].FSType == "overlay"
16+
val, err := strconv.ParseBool(strVal)
17+
if err != nil {
18+
fmt.Fprintf(os.Stderr, "Warning: Invalid value for RUNNING_IN_ROXIE_CONTAINER: %s. Expected a boolean value (true/false). Defaulting to false.\n", strVal)
19+
return false
20+
}
21+
return val
1922
}

‎internal/containerutil/detect_test.go‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@ package containerutil
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

7-
func TestIsRunningInContainer(t *testing.T) {
8-
// This test simply verifies the function doesn't panic
9-
// The actual result depends on whether the test is running
10-
// inside a container or not
11-
result := IsRunningInContainer()
9+
func TestIsRunningInRoxieContainer(t *testing.T) {
10+
t.Setenv("RUNNING_IN_ROXIE_CONTAINER", "true")
11+
assert.True(t, IsRunningInRoxieContainer(), "Expected to detect running in roxie container when environment variable is set")
1212

13-
// Log the result for informational purposes
14-
t.Logf("IsRunningInContainer() = %v", result)
13+
t.Setenv("RUNNING_IN_ROXIE_CONTAINER", "")
14+
assert.False(t, IsRunningInRoxieContainer(), "Expected to not detect running in roxie container when environment variable is unset")
1515

16-
// We can't assert a specific value since it depends on the environment
17-
// But we can verify it returns a boolean without errors
18-
if result {
19-
t.Log("Detected running inside a container")
20-
} else {
21-
t.Log("Detected running on host (not in container)")
22-
}
16+
t.Setenv("RUNNING_IN_ROXIE_CONTAINER", "garbage")
17+
assert.False(t, IsRunningInRoxieContainer(), "Expected to not detect running in roxie container when environment variable has invalid value")
2318
}

‎internal/env/env.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
)
1919

2020
var (
21-
RunningInContainer bool
22-
RunningInteractively bool
23-
initializationMutex sync.Mutex
21+
RunningInRoxieContainer bool
22+
RunningInteractively bool
23+
initializationMutex sync.Mutex
2424
)
2525

2626
// ClusterType represents different types of Kubernetes clusters
@@ -51,8 +51,8 @@ var (
5151
)
5252

5353
func init() {
54-
RunningInContainer = containerutil.IsRunningInContainer()
55-
if RunningInContainer {
54+
RunningInRoxieContainer = containerutil.IsRunningInRoxieContainer()
55+
if RunningInRoxieContainer {
5656
os.Setenv("KUBECONFIG", "/kubeconfig")
5757
}
5858
RunningInteractively = isRunningInteractively()
@@ -143,7 +143,7 @@ func Initialize(log *logger.Logger) error {
143143
if log == nil {
144144
log = logger.New()
145145
}
146-
if RunningInContainer {
146+
if RunningInRoxieContainer {
147147
log.Dim("Running containerized.")
148148
}
149149

@@ -284,11 +284,11 @@ func kubeconfigChecks(log *logger.Logger) error {
284284
if err != nil {
285285
log.Warningf("Kubeconfig %s cannot be opened for reading.", kubeConfigPath)
286286
if errors.Is(err, os.ErrNotExist) {
287-
if RunningInContainer {
287+
if RunningInRoxieContainer {
288288
log.Warningf("Make sure that your kubeconfig is mounted into the container, as in: -v $KUBECONFIG:/kubeconfig:U")
289289
}
290290
} else {
291-
if RunningInContainer {
291+
if RunningInRoxieContainer {
292292
log.Warningf("Make sure that your kubeconfig is mounted with the 'U' option, as in: -v $KUBECONFIG:/kubeconfig:U")
293293
}
294294
}

0 commit comments

Comments
 (0)