Explorar o código

set namespace for agent

Alexander Belanger %!s(int64=4) %!d(string=hai) anos
pai
achega
65d6cd9ba5
Modificáronse 1 ficheiros con 18 adicións e 11 borrados
  1. 18 11
      api/server/authz/cluster.go

+ 18 - 11
api/server/authz/cluster.go

@@ -104,6 +104,7 @@ func (d *OutOfClusterAgentGetter) GetAgent(r *http.Request, cluster *models.Clus
 
 	// if agent not found in context, get the agent from out of cluster config
 	ooc := d.GetOutOfClusterConfig(cluster)
+	ooc.DefaultNamespace = getNamespaceFromRequest(r)
 
 	agent, err := kubernetes.GetAgentOutOfClusterConfig(ooc)
 
@@ -135,17 +136,7 @@ func (d *OutOfClusterAgentGetter) GetHelmAgent(r *http.Request, cluster *models.
 		return nil, err
 	}
 
-	// look for namespace in context, otherwise go with default
-	reqScopes, _ := r.Context().Value(types.RequestScopeCtxKey).(map[types.PermissionScope]*types.RequestAction)
-	namespace := "default"
-
-	if nsPolicy, ok := reqScopes[types.NamespaceScope]; ok {
-		namespace = nsPolicy.Resource.Name
-	}
-
-	if strings.ToLower(namespace) == "all" {
-		namespace = ""
-	}
+	namespace := getNamespaceFromRequest(r)
 
 	helmAgent, err := helm.GetAgentFromK8sAgent("secret", namespace, d.config.Logger, k8sAgent)
 
@@ -172,3 +163,19 @@ func (d *OutOfClusterAgentGetter) GetDynamicClient(r *http.Request, cluster *mod
 
 	return kubernetes.GetDynamicClientOutOfClusterConfig(d.GetOutOfClusterConfig(cluster))
 }
+
+func getNamespaceFromRequest(r *http.Request) string {
+	// look for namespace in context, otherwise go with default
+	reqScopes, _ := r.Context().Value(types.RequestScopeCtxKey).(map[types.PermissionScope]*types.RequestAction)
+	namespace := "default"
+
+	if nsPolicy, ok := reqScopes[types.NamespaceScope]; ok {
+		namespace = nsPolicy.Resource.Name
+	}
+
+	if strings.ToLower(namespace) == "all" {
+		namespace = ""
+	}
+
+	return namespace
+}