pkg/util/cluster.go (33 lines of code) (raw):
package util
import (
"fmt"
"os"
"strconv"
)
const (
watchNamespaceEnvVar = "WATCH_NAMESPACE"
debugModeEnvVar = "DEBUG_MODE"
inClusterNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)
// GetWatchNamespace returns the namespace the operator should be watching for changes.
func GetWatchNamespace() (string, error) {
ns, found := os.LookupEnv(watchNamespaceEnvVar)
if !found {
return "", fmt.Errorf("failed to read watch namespace env variable: %s must be set", watchNamespaceEnvVar)
}
return ns, nil
}
// GetDebugMode returns the debug mode value.
func GetDebugMode() (bool, error) {
mode, found := os.LookupEnv(debugModeEnvVar)
if !found {
return false, nil
}
b, err := strconv.ParseBool(mode)
if err != nil {
return false, fmt.Errorf("failed to parse string to bool: %w", err)
}
return b, nil
}
// RunningInCluster checks whether the operator is running in cluster.
func RunningInCluster() bool {
_, err := os.Stat(inClusterNamespacePath)
return !os.IsNotExist(err)
}