Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get instance info from labels #6383

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -235,6 +240,11 @@ func extractEnvs(pod corev1.Pod) (
}
}
}

for k, v := range labelSuffixMap {
*v = pod.Labels[k]
}

return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Loading