From a8f0fdd824a82b17bef49cf3850cf98df99c83a7 Mon Sep 17 00:00:00 2001 From: lbbniu Date: Wed, 29 Jun 2022 22:26:13 +0800 Subject: [PATCH] Make crane's namespace configurable --- deploy/craned/deployment.yaml | 4 ++++ pkg/controller/analytics/analytics_controller.go | 8 ++++---- pkg/known/const.go | 4 ---- pkg/known/vars.go | 13 +++++++++++++ 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 pkg/known/vars.go diff --git a/deploy/craned/deployment.yaml b/deploy/craned/deployment.yaml index 856178f5b..dc5abdab3 100644 --- a/deploy/craned/deployment.yaml +++ b/deploy/craned/deployment.yaml @@ -40,6 +40,10 @@ spec: env: - name: TZ value: Asia/Shanghai + - name: CRANE_SYSTEM_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace command: - /craned - --prometheus-address=PROMETHEUS_ADDRESS diff --git a/pkg/controller/analytics/analytics_controller.go b/pkg/controller/analytics/analytics_controller.go index 491614dc6..605346de7 100644 --- a/pkg/controller/analytics/analytics_controller.go +++ b/pkg/controller/analytics/analytics_controller.go @@ -13,7 +13,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + unstructuredv1 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -329,7 +329,7 @@ func (c *Controller) getIdentities(ctx context.Context, analytics *analysisv1alp } gvr := gv.WithResource(resName) - var unstructureds []unstructured.Unstructured + var unstructureds []unstructuredv1.Unstructured if rs.Name != "" { unstructured, err := c.dynamicClient.Resource(gvr).Namespace(analytics.Namespace).Get(ctx, rs.Name, metav1.GetOptions{}) @@ -338,7 +338,7 @@ func (c *Controller) getIdentities(ctx context.Context, analytics *analysisv1alp } unstructureds = append(unstructureds, *unstructured) } else { - var unstructuredList *unstructured.UnstructuredList + var unstructuredList *unstructuredv1.UnstructuredList var err error if analytics.Namespace == known.CraneSystemNamespace { @@ -352,7 +352,7 @@ func (c *Controller) getIdentities(ctx context.Context, analytics *analysisv1alp for _, item := range unstructuredList.Items { // todo: rename rs.LabelSelector to rs.matchLabelSelector ? - m, ok, err := unstructured.NestedStringMap(item.Object, "spec", "selector", "matchLabels") + m, ok, err := unstructuredv1.NestedStringMap(item.Object, "spec", "selector", "matchLabels") if !ok || err != nil { return nil, fmt.Errorf("%s not supported", gvr.String()) } diff --git a/pkg/known/const.go b/pkg/known/const.go index ec449f5fc..34325391e 100644 --- a/pkg/known/const.go +++ b/pkg/known/const.go @@ -1,9 +1,5 @@ package known -const ( - CraneSystemNamespace = "crane-system" -) - const ( MetricNamePodCpuUsage = "crane_pod_cpu_usage" ) diff --git a/pkg/known/vars.go b/pkg/known/vars.go new file mode 100644 index 000000000..8cc470dfc --- /dev/null +++ b/pkg/known/vars.go @@ -0,0 +1,13 @@ +package known + +import "os" + +var ( + CraneSystemNamespace = "crane-system" +) + +func init() { + if namespace, ok := os.LookupEnv("CRANE_SYSTEM_NAMESPACE"); ok { + CraneSystemNamespace = namespace + } +}