diff --git a/internal/tools/orchestrator/scheduler/executor/plugins/k8s/instanceinfosync/pod.go b/internal/tools/orchestrator/scheduler/executor/plugins/k8s/instanceinfosync/pod.go index d701df9f636..c3e072b8e01 100644 --- a/internal/tools/orchestrator/scheduler/executor/plugins/k8s/instanceinfosync/pod.go +++ b/internal/tools/orchestrator/scheduler/executor/plugins/k8s/instanceinfosync/pod.go @@ -206,26 +206,31 @@ func extractEnvs(pod corev1.Pod) ( edgeApplicationName, edgeSites string) { envsuffixmap := map[string]*string{ - "ADDON_ID": &addonID, "PIPELINE_ID": &pipelineID, - "DICE_CLUSTER_NAME": &cluster, - "DICE_ORG_NAME": &orgName, - "DICE_ORG_ID": &orgID, - "DICE_PROJECT_NAME": &projectName, - "DICE_PROJECT_ID": &projectID, - "DICE_APPLICATION_NAME": &applicationName, "DICE_EDGE_APPLICATION_NAME": &edgeApplicationName, "DICE_EDGE_SITE": &edgeSites, - "DICE_APPLICATION_ID": &applicationID, "DICE_RUNTIME_NAME": &runtimeName, - "DICE_RUNTIME_ID": &runtimeID, - "DICE_SERVICE_NAME": &serviceName, - "DICE_WORKSPACE": &workspace, - "DICE_ADDON_NAME": &addonName, "DICE_CPU_ORIGIN": &cpuOriginStr, "DICE_MEM_ORIGIN": &memOriginStr, "DICE_COMPONENT": &diceComponent, } + + // TODO use const value in `labels` package for the key (wait new labels merge) + labelSuffixMap := map[string]*string{ + "addon.erda.cloud/id": &addonID, + "core.erda.cloud/cluster-name": &cluster, + "core.erda.cloud/org-name": &orgName, + "core.erda.cloud/org-id": &orgID, + "core.erda.cloud/project-name": &projectName, + "core.erda.cloud/project-id": &projectID, + "core.erda.cloud/app-name": &applicationName, + "core.erda.cloud/app-id": &applicationID, + "core.erda.cloud/runtime-id": &runtimeID, + "core.erda.cloud/service-name": &serviceName, + "core.erda.cloud/workspace": &workspace, + "addon.erda.cloud/type": &addonName, + } + for _, container := range pod.Spec.Containers { for _, env := range container.Env { for k, v := range envsuffixmap { @@ -235,6 +240,11 @@ func extractEnvs(pod corev1.Pod) ( } } } + + for k, v := range labelSuffixMap { + *v = pod.Labels[k] + } + return } diff --git a/internal/tools/orchestrator/scheduler/executor/plugins/k8s/instanceinfosync/pod_test.go b/internal/tools/orchestrator/scheduler/executor/plugins/k8s/instanceinfosync/pod_test.go new file mode 100644 index 00000000000..001aceb6bc3 --- /dev/null +++ b/internal/tools/orchestrator/scheduler/executor/plugins/k8s/instanceinfosync/pod_test.go @@ -0,0 +1,47 @@ +// Copyright (c) 2021 Terminus, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package instanceinfosync + +import ( + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "testing" +) + +func Test_extractEnvs(t *testing.T) { + pod := v1.Pod{ + Spec: v1.PodSpec{ + Containers: []v1.Container{}, + }, + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + "addon.erda.cloud/id": "10001", + "core.erda.cloud/cluster-name": "erda-jicheng", + "core.erda.cloud/org-name": "development-erda", + "core.erda.cloud/org-id": "888", + "core.erda.cloud/project-name": "Master", + "core.erda.cloud/project-id": "8888", + "core.erda.cloud/app-name": "go-demo", + "core.erda.cloud/app-id": "88888", + "core.erda.cloud/runtime-id": "66666", + "core.erda.cloud/service-name": "go-demo-web", + "core.erda.cloud/workspace": "prod", + "addon.erda.cloud/type": "mysql", + }, + }, + } + + extractEnvs(pod) +}