Skip to content

Commit 6d43b35

Browse files
committed
Add JOYLIVE_CONTROL_PLANE_URL environment variable and refactor config package
1 parent 5c5e4d7 commit 6d43b35

File tree

11 files changed

+153
-55
lines changed

11 files changed

+153
-55
lines changed

deploy/all-cr.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ spec:
741741
value: joylive
742742
- name: JOYLIVE_MATCH_ENV_LABELS
743743
value: x-live, x-service
744+
- name: JOYLIVE_CONTROL_PLANE_URL
745+
value:
744746
name: joylive-injector
745747
image: ghcr.m.daocloud.io/jd-opensource/joylive-injector:v1.2.2
746748
imagePullPolicy: Always

deploy/joylive-injector/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ spec:
3434
value: {{ .Values.namespace }}
3535
- name: JOYLIVE_MATCH_ENV_LABELS
3636
value: {{ .Values.matchEnvLabels }}
37+
- name: JOYLIVE_CONTROL_PLANE_URL
38+
value: {{ .Values.controlPlaneUrl }}
3739
name: joylive-injector
3840
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
3941
imagePullPolicy: {{ .Values.image.pullPolicy }}

deploy/joylive-injector/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ containerArgs:
2525
- --key=/etc/kubernetes/ssl/dac-key.pem
2626

2727
matchEnvLabels: x-live, x-service
28+
controlPlaneUrl:
2829

2930
imagePullSecrets: [ ]
3031
nameOverride: ""
24 Bytes
Binary file not shown.

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/jd-opensource/joylive-injector/pkg/admission"
1818
_ "github.com/jd-opensource/joylive-injector/pkg/mutation"
19+
_ "github.com/jd-opensource/joylive-injector/pkg/watcher"
1920

2021
"github.com/jd-opensource/joylive-injector/pkg/config"
2122

pkg/config/config.go

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,34 @@ import (
55

66
v1 "github.com/jd-opensource/joylive-injector/client-go/apis/injector/v1"
77
"github.com/jd-opensource/joylive-injector/pkg/log"
8-
"github.com/jd-opensource/joylive-injector/pkg/resource"
9-
"go.uber.org/zap"
108
)
119

1210
const (
13-
AgentConfig = "config.yaml"
14-
InjectorConfigName = "injector.yaml"
15-
LogConfig = "logback.xml"
16-
BootConfig = "bootstrap.properties"
17-
ConfigMountPath = "/joylive/config"
18-
EmptyDirMountPath = "/joylive"
19-
InitEmptyDirMountPath = "/agent"
20-
InitContainerCmd = "/bin/sh"
21-
InitContainerArgs = "-c, cp -r /joylive/* /agent && chmod -R 777 /agent"
22-
ConfigMapEnvName = "JOYLIVE_CONFIGMAP_NAME"
23-
NamespaceEnvName = "JOYLIVE_NAMESPACE"
24-
MatchLabelsEnvName = "JOYLIVE_MATCH_ENV_LABELS"
25-
DefaultNamespace = "joylive"
26-
AgentVersionLabel = "x-live-version"
11+
AgentConfig = "config.yaml"
12+
InjectorConfigName = "injector.yaml"
13+
LogConfig = "logback.xml"
14+
BootConfig = "bootstrap.properties"
15+
ConfigMountPath = "/joylive/config"
16+
EmptyDirMountPath = "/joylive"
17+
InitEmptyDirMountPath = "/agent"
18+
InitContainerCmd = "/bin/sh"
19+
InitContainerArgs = "-c, cp -r /joylive/* /agent && chmod -R 777 /agent"
20+
ConfigMapEnvName = "JOYLIVE_CONFIGMAP_NAME"
21+
NamespaceEnvName = "JOYLIVE_NAMESPACE"
22+
MatchLabelsEnvName = "JOYLIVE_MATCH_ENV_LABELS"
23+
ControlPlaneUrlEnvName = "JOYLIVE_CONTROL_PLANE_URL"
24+
DefaultNamespace = "joylive"
25+
AgentVersionLabel = "x-live-version"
26+
ServiceSpaceLabel = "jmsf.jd.com/space"
27+
ApplicationLabel = "jmsf.jd.com/application"
2728
)
2829

2930
var (
30-
Cert string
31-
Key string
32-
Addr string
33-
MatchLabels string
31+
Cert string
32+
Key string
33+
Addr string
34+
MatchLabels string
35+
ControlPlaneUrl string
3436
)
3537

3638
// injection_deploy config
@@ -47,30 +49,10 @@ var (
4749
)
4850

4951
func init() {
50-
// Start the ConfigMap listener and initialize the content
51-
cmWatcher := NewConfigMapWatcher(resource.GetResource().ClientSet)
52-
err := cmWatcher.Start()
53-
if err != nil {
54-
log.Fatal("start cmWatcher error", zap.Error(err))
55-
}
56-
err = cmWatcher.InitConfigMap(GetNamespace())
57-
if err != nil {
58-
log.Fatal("init cm error", zap.Error(err))
59-
}
60-
61-
// Start the AgentVersion listener and initialize the content
62-
avWatcher := NewAgentVersionWatcher(resource.GetResource().RestConfig)
63-
err = avWatcher.Start()
64-
if err != nil {
65-
log.Fatal("start avWatcher error", zap.Error(err))
66-
}
67-
err = avWatcher.InitAgentVersion(GetNamespace())
68-
if err != nil {
69-
log.Fatal("init agentVersion error", zap.Error(err))
70-
}
71-
7252
// Initialize the default matchLabels from environment variables
7353
MatchLabels = os.Getenv(MatchLabelsEnvName)
54+
// Initialize the default control plane url from environment variables
55+
ControlPlaneUrl = os.Getenv(ControlPlaneUrlEnvName)
7456
}
7557

7658
func GetNamespace() string {

pkg/mutation/mutation_deploy.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func injectionDeploy(request *admissionv1.AdmissionRequest) (*admissionv1.Admiss
6868
}, nil
6969
}
7070
log.Infof("[mutation] /injection-deploy: create config map for this deployment: %s, namespace: %s", deploy.Name, deploy.Namespace)
71-
err = createConfigMap(&deploy)
71+
err = createOrUpdateConfigMap(&deploy)
7272
if err != nil {
7373
errMsg := fmt.Sprintf("[mutation] /injection-deploy: failed to create configmap: %v", err)
7474
log.Error(errMsg)
@@ -80,6 +80,32 @@ func injectionDeploy(request *admissionv1.AdmissionRequest) (*admissionv1.Admiss
8080
},
8181
}, nil
8282
}
83+
// Add environment variables to the deployment
84+
if len(config.ControlPlaneUrl) > 0 {
85+
// Get the application environment variables
86+
labels := deploy.GetLabels()
87+
serviceSpace, application := labels[config.ServiceSpaceLabel], labels[config.ApplicationLabel]
88+
if len(serviceSpace) > 0 && len(application) > 0 {
89+
envs, err := resource.GetApplicationEnvironments(labels[config.ServiceSpaceLabel], labels[config.ApplicationLabel])
90+
if err != nil {
91+
errMsg := fmt.Sprintf("[mutation] /injection-deploy: failed to get application environments: %v", err)
92+
log.Error(errMsg)
93+
return &admissionv1.AdmissionResponse{
94+
Allowed: false,
95+
Result: &metav1.Status{
96+
Code: http.StatusInternalServerError,
97+
Message: errMsg,
98+
},
99+
}, nil
100+
}
101+
for k, v := range envs {
102+
deploy.Spec.Template.Spec.Containers[0].Env = append(deploy.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{Name: k, Value: v})
103+
}
104+
} else {
105+
log.Warnf("[mutation] /injection-deploy: the deployment %s/%s does not have the %s or %s label",
106+
deploy.Name, deploy.Namespace, config.ServiceSpaceLabel, config.ApplicationLabel)
107+
}
108+
}
83109
return &admissionv1.AdmissionResponse{
84110
UID: request.UID,
85111
Allowed: true,
@@ -92,7 +118,7 @@ func injectionDeploy(request *admissionv1.AdmissionRequest) (*admissionv1.Admiss
92118
}
93119
}
94120

95-
func createConfigMap(deploy *appsv1.Deployment) error {
121+
func createOrUpdateConfigMap(deploy *appsv1.Deployment) error {
96122
configMapData := config.DefaultInjectorConfigMap
97123
if version, ok := deploy.Spec.Template.Labels[config.AgentVersionLabel]; ok {
98124
if agentVersion, ok := config.InjectorAgentVersion[version]; ok {

pkg/resource/control.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package resource
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/jd-opensource/joylive-injector/pkg/config"
7+
"io"
8+
"net/http"
9+
)
10+
11+
type Response struct {
12+
Error error `json:"error,omitempty"`
13+
Data interface{} `json:"data,omitempty"`
14+
}
15+
16+
type ApplicationEnvResponse struct {
17+
Response
18+
Data map[string]string `json:"data"`
19+
}
20+
21+
func GetApplicationEnvironments(namespace, application string) (map[string]string, error) {
22+
url := fmt.Sprintf(
23+
"%s/v1/getApplicationEnvironment?application=%s&namespace=%s",
24+
config.ControlPlaneUrl, application, namespace,
25+
)
26+
resp, err := http.Get(url)
27+
if err != nil {
28+
return nil, err
29+
}
30+
defer resp.Body.Close()
31+
32+
if resp.StatusCode != http.StatusOK {
33+
return nil, fmt.Errorf("request failed, status: %s", resp.Status)
34+
}
35+
36+
body, err := io.ReadAll(resp.Body)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
var response ApplicationEnvResponse
42+
if err := json.Unmarshal(body, &response); err != nil {
43+
return nil, err
44+
}
45+
46+
if response.Error != nil {
47+
return nil, response.Error
48+
}
49+
return response.Data, nil
50+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package watcher
22

33
import (
44
"context"
@@ -7,6 +7,7 @@ import (
77
clientset "github.com/jd-opensource/joylive-injector/client-go/clientset/versioned"
88
"github.com/jd-opensource/joylive-injector/client-go/informers/externalversions"
99
listerv1 "github.com/jd-opensource/joylive-injector/client-go/listers/injector/v1"
10+
"github.com/jd-opensource/joylive-injector/pkg/config"
1011
"github.com/jd-opensource/joylive-injector/pkg/log"
1112
"go.uber.org/zap"
1213
"gopkg.in/yaml.v3"
@@ -120,7 +121,7 @@ func (w *AgentVersionWatcher) cacheAgentVersion(agentVersion *v1.AgentVersion) e
120121
avSpec := string(avSpecBytes)
121122
log.Info("Received AgentVersion update event, start updating local configuration.", zap.String("agentVersion", agentVersion.Name),
122123
zap.String("agentVersionSpec", avSpec))
123-
InjectorAgentVersion[agentVersion.Spec.Version] = agentVersion.Spec
124+
config.InjectorAgentVersion[agentVersion.Spec.Version] = agentVersion.Spec
124125
return nil
125126
}
126127

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package config
1+
package watcher
22

33
import (
44
"context"
55
"fmt"
6+
"github.com/jd-opensource/joylive-injector/pkg/config"
67
"github.com/jd-opensource/joylive-injector/pkg/log"
78
"go.uber.org/zap"
89
"gopkg.in/yaml.v3"
@@ -30,7 +31,7 @@ func NewConfigMapWatcher(kubeClient kubernetes.Interface) *ConfigMapWatcher {
3031
factory := informers.NewSharedInformerFactoryWithOptions(
3132
kubeClient,
3233
time.Second*10,
33-
informers.WithNamespace(GetNamespace()),
34+
informers.WithNamespace(config.GetNamespace()),
3435
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
3536
options.LabelSelector = labels.SelectorFromSet(map[string]string{
3637
"app": "joylive-injector",
@@ -123,18 +124,18 @@ func (w *ConfigMapWatcher) cacheConfigMap(configMap *v1.ConfigMap) error {
123124
cmDataString := string(cmDataBytes)
124125
log.Info("Received ConfigMap update event, start updating local configuration.", zap.String("cm", configMap.Name),
125126
zap.String("data", cmDataString))
126-
if configMap.Name == os.Getenv(ConfigMapEnvName) {
127-
DefaultInjectorConfigMap = configMap.Data
128-
if data, ok := configMap.Data[InjectorConfigName]; ok {
129-
c, err := GetAgentInjectConfig(data)
127+
if configMap.Name == os.Getenv(config.ConfigMapEnvName) {
128+
config.DefaultInjectorConfigMap = configMap.Data
129+
if data, ok := configMap.Data[config.InjectorConfigName]; ok {
130+
c, err := config.GetAgentInjectConfig(data)
130131
if err != nil {
131132
return err
132133
}
133-
DefaultInjectorConfig = c
134-
delete(DefaultInjectorConfigMap, InjectorConfigName)
134+
config.DefaultInjectorConfig = c
135+
delete(config.DefaultInjectorConfigMap, config.InjectorConfigName)
135136
}
136137
} else {
137-
InjectorConfigMaps[configMap.Name] = configMap.Data
138+
config.InjectorConfigMaps[configMap.Name] = configMap.Data
138139
}
139140
return nil
140141
}

0 commit comments

Comments
 (0)