diff --git a/adapter/main.go b/adapter/main.go index cda7cbe9182..6bd9b6a91a2 100644 --- a/adapter/main.go +++ b/adapter/main.go @@ -135,7 +135,11 @@ func (a *Adapter) makeProvider(ctx context.Context, globalHTTPTimeout time.Durat broadcaster := record.NewBroadcaster() recorder := broadcaster.NewRecorder(scheme, corev1.EventSource{Component: "keda-metrics-adapter"}) - kubeClientset, _ := kubernetes.NewForConfig(ctrl.GetConfigOrDie()) + kubeClientset, err := kubernetes.NewForConfig(cfg) + if err != nil { + logger.Error(err, "Unable to create kube clientset") + return nil, nil, err + } objectNamespace, err := kedautil.GetClusterObjectNamespace() if err != nil { logger.Error(err, "Unable to get cluster object namespace") diff --git a/main.go b/main.go index a9384e4bdc1..5b8ed8089a6 100644 --- a/main.go +++ b/main.go @@ -162,7 +162,11 @@ func main() { globalHTTPTimeout := time.Duration(globalHTTPTimeoutMS) * time.Millisecond eventRecorder := mgr.GetEventRecorderFor("keda-operator") - kubeClientset, _ := kubernetes.NewForConfig(ctrl.GetConfigOrDie()) + kubeClientset, err := kubernetes.NewForConfig(cfg) + if err != nil { + setupLog.Error(err, "Unable to create kube clientset") + os.Exit(1) + } objectNamespace, err := kedautil.GetClusterObjectNamespace() if err != nil { setupLog.Error(err, "Unable to get cluster object namespace") diff --git a/pkg/scaling/resolver/scale_resolvers.go b/pkg/scaling/resolver/scale_resolvers.go index 9870e8bec05..f424f752f70 100644 --- a/pkg/scaling/resolver/scale_resolvers.go +++ b/pkg/scaling/resolver/scale_resolvers.go @@ -20,7 +20,6 @@ import ( "bytes" "context" "fmt" - "os" "strings" "github.com/go-logr/logr" @@ -43,17 +42,19 @@ const ( referenceCloser = ')' ) -var kedaNamespace, _ = util.GetClusterObjectNamespace() +var ( + kedaNamespace, _ = util.GetClusterObjectNamespace() + restrictSecretAccess = util.GetRestrictSecretAccess() +) + // isSecretAccessRestricted returns whether secret access need to be restricted in KEDA namespace func isSecretAccessRestricted(logger logr.Logger) bool { - const RestrictSecretAccessEnvVar = "KEDA_RESTRICT_SECRET_ACCESS" - restrictSecretAccess, found := os.LookupEnv(RestrictSecretAccessEnvVar) - if !found { + if restrictSecretAccess == "" { return false } if strings.ToLower(restrictSecretAccess) == "true" { - logger.V(1).Info("Secret Access is restricted to be in KEDA namespace, pls. use ClusterTriggerAuthentication instead of TriggerAuthentication", "Env Var", RestrictSecretAccessEnvVar, "Env Value", strings.ToLower(restrictSecretAccess)) + logger.V(1).Info("Secret Access is restricted to be in Cluster Object Namespace, please use ClusterTriggerAuthentication instead of TriggerAuthentication", "Cluster Object Namespace", kedaNamespace, "Env Var", util.RestrictSecretAccessEnvVar, "Env Value", strings.ToLower(restrictSecretAccess)) return true } return false diff --git a/pkg/util/env_resolver.go b/pkg/util/env_resolver.go index 40835f888c1..319af51bddc 100644 --- a/pkg/util/env_resolver.go +++ b/pkg/util/env_resolver.go @@ -22,6 +22,8 @@ import ( "time" ) +const RestrictSecretAccessEnvVar = "KEDA_RESTRICT_SECRET_ACCESS" + var clusterObjectNamespaceCache *string func ResolveOsEnvBool(envName string, defaultValue bool) (bool, error) { @@ -73,3 +75,8 @@ func GetClusterObjectNamespace() (string, error) { clusterObjectNamespaceCache = &strData return strData, nil } + +// GetRestrictSecretAccess retrieves the value of the environment variable of KEDA_RESTRICT_SECRET_ACCESS +func GetRestrictSecretAccess() string { + return os.Getenv(RestrictSecretAccessEnvVar) +} \ No newline at end of file