From 8b072e3c3ffafdae7235855a0c92243357a95788 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Sat, 29 Jun 2024 16:01:53 +0800 Subject: [PATCH 01/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index b7c5c9f8..0fcb1b5c 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -91,9 +91,23 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co if len(oldImageInfo) > 1 { opt.ExternalInfo[customJavaAgentVersion] = oldImageInfo[1] } + // if val, ok := annonations[customJavaAgentVersion]; ok { + // add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) + // opt.ExternalInfo[customJavaAgentVersion] = val + // } + + // 获取默认的 Java Agent 版本 + defaultJavaAgentVersion := oldImageInfo[1] + // 检查客户的 Java Agent 版本是否与默认版本一致 + isCustomVersionSameAsDefault := false if val, ok := annonations[customJavaAgentVersion]; ok { add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) opt.ExternalInfo[customJavaAgentVersion] = val + if val == defaultJavaAgentVersion { + isCustomVersionSameAsDefault = true + } + } else { + isCustomVersionSameAsDefault = true } // 需要将用户的框架信息注入到 javaagent-init 中,用于初始化相关的配置文件信息 @@ -101,13 +115,13 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co if !ok { log.InjectScope().Warnf("handle polaris-javaagent-init inject for pod=[%s, %s] not found frameworkName", pod.Namespace, pod.Name) - return fmt.Errorf("pod annonations not set %s", customJavaAgentPluginFramework) + // return fmt.Errorf("pod annonations not set %s", customJavaAgentPluginFramework) } frameworkVersion, ok := annonations[customJavaAgentPluginFrameworkVersion] if !ok { log.InjectScope().Warnf("handle polaris-javaagent-init inject for pod=[%s, %s] not found frameworkVersion", pod.Namespace, pod.Name) - return fmt.Errorf("pod annonations not set %s", customJavaAgentPluginFrameworkVersion) + // return fmt.Errorf("pod annonations not set %s", customJavaAgentPluginFrameworkVersion) } pluginType := frameworkName + frameworkVersion @@ -158,16 +172,34 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 + // if properties, ok := annonations[customJavaAgentPluginConfig]; ok { + // customProperties := map[string]string{} + // if err := json.Unmarshal([]byte(properties), &customProperties); err != nil { + // return err + // } + // // 先从 configmap 中获取 java-agent 不同 plugin-type 的默认配置信息 + // for k, v := range customProperties { + // defaultProperties[k] = v + // } + // } + if properties, ok := annonations[customJavaAgentPluginConfig]; ok { customProperties := map[string]string{} if err := json.Unmarshal([]byte(properties), &customProperties); err != nil { return err } - // 先从 configmap 中获取 java-agent 不同 plugin-type 的默认配置信息 - for k, v := range customProperties { - defaultProperties[k] = v + + if !isCustomVersionSameAsDefault { + // 如果客户的 Java Agent 版本与默认版本不一致,则合并默认配置和客户配置 + for k, v := range customProperties { + defaultProperties[k] = v + } + } else { + // 如果客户的 Java Agent 版本与默认版本一致,则只使用客户配置 + defaultProperties = customProperties } } + exportAgentPluginConf := "" for key, value := range defaultProperties { exportAgentPluginConf += fmt.Sprintf("%s=%s\n", key, value) From a8e3436ba236b6dbf0e2ff8aed10baad7349a9c8 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Sun, 30 Jun 2024 11:37:34 +0800 Subject: [PATCH 02/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 51 +++++++------------ 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 0fcb1b5c..61138a10 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -91,25 +91,25 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co if len(oldImageInfo) > 1 { opt.ExternalInfo[customJavaAgentVersion] = oldImageInfo[1] } - // if val, ok := annonations[customJavaAgentVersion]; ok { - // add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) - // opt.ExternalInfo[customJavaAgentVersion] = val - // } - - // 获取默认的 Java Agent 版本 - defaultJavaAgentVersion := oldImageInfo[1] - // 检查客户的 Java Agent 版本是否与默认版本一致 - isCustomVersionSameAsDefault := false if val, ok := annonations[customJavaAgentVersion]; ok { add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) opt.ExternalInfo[customJavaAgentVersion] = val - if val == defaultJavaAgentVersion { - isCustomVersionSameAsDefault = true - } - } else { - isCustomVersionSameAsDefault = true } + // // 获取默认的 Java Agent 版本 + // defaultJavaAgentVersion := oldImageInfo[1] + // // 检查客户的 Java Agent 版本是否与默认版本一致 + // isCustomVersionSameAsDefault := false + // if val, ok := annonations[customJavaAgentVersion]; ok { + // add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) + // opt.ExternalInfo[customJavaAgentVersion] = val + // if val == defaultJavaAgentVersion { + // isCustomVersionSameAsDefault = true + // } + // } else { + // isCustomVersionSameAsDefault = true + // } + // 需要将用户的框架信息注入到 javaagent-init 中,用于初始化相关的配置文件信息 frameworkName, ok := annonations[customJavaAgentPluginFramework] if !ok { @@ -172,31 +172,14 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 - // if properties, ok := annonations[customJavaAgentPluginConfig]; ok { - // customProperties := map[string]string{} - // if err := json.Unmarshal([]byte(properties), &customProperties); err != nil { - // return err - // } - // // 先从 configmap 中获取 java-agent 不同 plugin-type 的默认配置信息 - // for k, v := range customProperties { - // defaultProperties[k] = v - // } - // } - if properties, ok := annonations[customJavaAgentPluginConfig]; ok { customProperties := map[string]string{} if err := json.Unmarshal([]byte(properties), &customProperties); err != nil { return err } - - if !isCustomVersionSameAsDefault { - // 如果客户的 Java Agent 版本与默认版本不一致,则合并默认配置和客户配置 - for k, v := range customProperties { - defaultProperties[k] = v - } - } else { - // 如果客户的 Java Agent 版本与默认版本一致,则只使用客户配置 - defaultProperties = customProperties + // 先从 configmap 中获取 java-agent 不同 plugin-type 的默认配置信息 + for k, v := range customProperties { + defaultProperties[k] = v } } From 9bead0c5ea77b3499405bb0ebb986220599b9364 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Sun, 30 Jun 2024 21:16:34 +0800 Subject: [PATCH 03/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 63 +++++++++---------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 61138a10..213cfb55 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -91,52 +91,47 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co if len(oldImageInfo) > 1 { opt.ExternalInfo[customJavaAgentVersion] = oldImageInfo[1] } - if val, ok := annonations[customJavaAgentVersion]; ok { + if val, ok := annonations[customJavaAgentVersion]; ok && val != "" { add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) opt.ExternalInfo[customJavaAgentVersion] = val } - // // 获取默认的 Java Agent 版本 - // defaultJavaAgentVersion := oldImageInfo[1] - // // 检查客户的 Java Agent 版本是否与默认版本一致 - // isCustomVersionSameAsDefault := false - // if val, ok := annonations[customJavaAgentVersion]; ok { - // add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) - // opt.ExternalInfo[customJavaAgentVersion] = val - // if val == defaultJavaAgentVersion { - // isCustomVersionSameAsDefault = true - // } - // } else { - // isCustomVersionSameAsDefault = true - // } - - // 需要将用户的框架信息注入到 javaagent-init 中,用于初始化相关的配置文件信息 - frameworkName, ok := annonations[customJavaAgentPluginFramework] - if !ok { + frameworkName, frameworkNameOk := annonations[customJavaAgentPluginFramework] + if !frameworkNameOk { log.InjectScope().Warnf("handle polaris-javaagent-init inject for pod=[%s, %s] not found frameworkName", pod.Namespace, pod.Name) - // return fmt.Errorf("pod annonations not set %s", customJavaAgentPluginFramework) } - frameworkVersion, ok := annonations[customJavaAgentPluginFrameworkVersion] - if !ok { + + frameworkVersion, frameworkVersionOk := annonations[customJavaAgentPluginFrameworkVersion] + if !frameworkVersionOk { log.InjectScope().Warnf("handle polaris-javaagent-init inject for pod=[%s, %s] not found frameworkVersion", pod.Namespace, pod.Name) - // return fmt.Errorf("pod annonations not set %s", customJavaAgentPluginFrameworkVersion) } pluginType := frameworkName + frameworkVersion - add.Env = append(add.Env, corev1.EnvVar{ - Name: "JAVA_AGENT_PLUGIN_TYPE", - Value: pluginType, - }) - add.Env = append(add.Env, corev1.EnvVar{ - Name: "JAVA_AGENT_FRAMEWORK_NAME", - Value: frameworkName, - }) - add.Env = append(add.Env, corev1.EnvVar{ - Name: "JAVA_AGENT_FRAMEWORK_VERSION", - Value: frameworkVersion, - }) + if frameworkName != "" && frameworkVersion != "" { + add.Env = append(add.Env, corev1.EnvVar{ + Name: "JAVA_AGENT_PLUGIN_TYPE", + Value: pluginType, + }) + } else { + pluginType = "spring-cloud2022" + } + + if frameworkName != "" { + add.Env = append(add.Env, corev1.EnvVar{ + Name: "JAVA_AGENT_FRAMEWORK_NAME", + Value: frameworkName, + }) + } + + if frameworkVersion != "" { + add.Env = append(add.Env, corev1.EnvVar{ + Name: "JAVA_AGENT_FRAMEWORK_VERSION", + Value: frameworkVersion, + }) + } + kubeClient := opt.KubeClient pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), "plugin-default.properties", metav1.GetOptions{}) From 2155cdc8a9652cfa7a62953f2fe0e5a3bae6bf95 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Sun, 30 Jun 2024 21:43:54 +0800 Subject: [PATCH 04/20] feat:agent Automatic version matching --- pkg/inject/pkg/kube/inject/apply/javaagent/patch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 213cfb55..88b5dae9 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -84,7 +84,7 @@ func (pb *PodPatchBuilder) PatchContainer(req *inject.OperateContainerRequest) ( func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *corev1.Pod, add *corev1.Container) error { annonations := pod.Annotations - log.InjectScope().Infof("handle polaris-javaagent-init inject for pod=[%s, %s] annonations: %#v image: %s", + log.InjectScope().Infof("handle polaris-javaagent-init inject for pod=[%s, %s] annonations: %#v image: %s 111111111111111", pod.Namespace, pod.Name, pod.Annotations, add.Image) // 判断用户是否自定义了 javaagent 的版本 oldImageInfo := strings.Split(add.Image, ":") From 3622cf538feae8969009150dcc1c8198296a4d17 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Sun, 30 Jun 2024 23:22:21 +0800 Subject: [PATCH 05/20] feat:agent Automatic version matching --- pkg/inject/pkg/kube/inject/apply/javaagent/patch.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 88b5dae9..c060ae19 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -114,22 +114,16 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co Name: "JAVA_AGENT_PLUGIN_TYPE", Value: pluginType, }) - } else { - pluginType = "spring-cloud2022" - } - - if frameworkName != "" { add.Env = append(add.Env, corev1.EnvVar{ Name: "JAVA_AGENT_FRAMEWORK_NAME", Value: frameworkName, }) - } - - if frameworkVersion != "" { add.Env = append(add.Env, corev1.EnvVar{ Name: "JAVA_AGENT_FRAMEWORK_VERSION", Value: frameworkVersion, }) + } else { + pluginType = "spring-cloud2022" } kubeClient := opt.KubeClient From 14f7f1fbfc312f18d39b30301b90bc7f857f7b01 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 01:48:32 +0800 Subject: [PATCH 06/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 73 +++++++++---------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index c060ae19..6edb487a 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -15,17 +15,10 @@ package javaagent import ( - "bufio" - "bytes" - "context" "encoding/json" "fmt" "strconv" "strings" - "text/template" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/polarismesh/polaris-controller/common/log" "github.com/polarismesh/polaris-controller/pkg/inject/pkg/kube/inject" @@ -33,6 +26,7 @@ import ( "github.com/polarismesh/polaris-controller/pkg/polarisapi" "github.com/polarismesh/polaris-controller/pkg/util" utils "github.com/polarismesh/polaris-controller/pkg/util" + corev1 "k8s.io/api/core/v1" ) // Java Agent 场景下的特殊 annonations 信息 @@ -91,6 +85,7 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co if len(oldImageInfo) > 1 { opt.ExternalInfo[customJavaAgentVersion] = oldImageInfo[1] } + if val, ok := annonations[customJavaAgentVersion]; ok && val != "" { add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) opt.ExternalInfo[customJavaAgentVersion] = val @@ -122,42 +117,44 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co Name: "JAVA_AGENT_FRAMEWORK_VERSION", Value: frameworkVersion, }) - } else { - pluginType = "spring-cloud2022" - } - - kubeClient := opt.KubeClient - pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), - "plugin-default.properties", metav1.GetOptions{}) - if err != nil { - return err } - defaultParam := map[string]string{ + // } else { + // pluginType = "spring-cloud2023" + // } + + // kubeClient := opt.KubeClient + // pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), + // "plugin-default.properties", metav1.GetOptions{}) + // if err != nil { + // return err + // } + defaultProperties := map[string]string{ "MicroserviceName": opt.Annotations[util.SidecarServiceName], "PolarisServerIP": strings.Split(polarisapi.PolarisGrpc, ":")[0], "PolarisDiscoverPort": strings.Split(polarisapi.PolarisGrpc, ":")[1], } - tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) - if err != nil { - return err - } - buf := new(bytes.Buffer) - if err := tpl.Execute(buf, defaultParam); err != nil { - return err - } - defaultProperties := map[string]string{} - scanner := bufio.NewScanner(strings.NewReader(buf.String())) - scanner.Split(bufio.ScanLines) - for scanner.Scan() { - line := scanner.Text() - // 注释不放在 defaultProperties 中 - if !strings.HasPrefix(line, "#") { - kvs := strings.Split(line, "=") - if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { - defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) - } - } - } + + // tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) + // if err != nil { + // return err + // } + // buf := new(bytes.Buffer) + // if err := tpl.Execute(buf, defaultParam); err != nil { + // return err + // } + // defaultProperties := map[string]string{} + // scanner := bufio.NewScanner(strings.NewReader(buf.String())) + // scanner.Split(bufio.ScanLines) + // for scanner.Scan() { + // line := scanner.Text() + // // 注释不放在 defaultProperties 中 + // if !strings.HasPrefix(line, "#") { + // kvs := strings.Split(line, "=") + // if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { + // defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) + // } + // } + // } // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 From 5c8ac4cd113ded16662414f5019f53e70a8c5ea7 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 02:34:36 +0800 Subject: [PATCH 07/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 6edb487a..aefb282f 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -117,10 +117,13 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co Name: "JAVA_AGENT_FRAMEWORK_VERSION", Value: frameworkVersion, }) + } else { + pluginType = "spring-cloud2023" + add.Env = append(add.Env, corev1.EnvVar{ + Name: "JAVA_AGENT_PLUGIN_TYPE", + Value: pluginType, + }) } - // } else { - // pluginType = "spring-cloud2023" - // } // kubeClient := opt.KubeClient // pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), @@ -128,12 +131,22 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co // if err != nil { // return err // } - defaultProperties := map[string]string{ + defaultParam := map[string]string{ "MicroserviceName": opt.Annotations[util.SidecarServiceName], "PolarisServerIP": strings.Split(polarisapi.PolarisGrpc, ":")[0], "PolarisDiscoverPort": strings.Split(polarisapi.PolarisGrpc, ":")[1], } + defaultProperties := make(map[string]string) + + // 格式化 MicroserviceName + microserviceName := fmt.Sprintf("spring.application.name={{ .%s }}", defaultParam["MicroserviceName"]) + defaultProperties["MicroserviceName"] = microserviceName + + // 格式化 PolarisServerIP 和 PolarisDiscoverPort + polarisAddress := fmt.Sprintf("spring.cloud.polaris.address=grpc\\://{{ .%s }}\\:{{ .%s }}", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) + defaultProperties["PolarisAddress"] = polarisAddress + // tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) // if err != nil { // return err From 7c292a1a9f960457599f051a8f6445aba68dec53 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 02:52:43 +0800 Subject: [PATCH 08/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index aefb282f..78ce006c 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -118,10 +118,18 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co Value: frameworkVersion, }) } else { - pluginType = "spring-cloud2023" + // pluginType = "spring-cloud2023" + // add.Env = append(add.Env, corev1.EnvVar{ + // Name: "JAVA_AGENT_PLUGIN_TYPE", + // Value: pluginType, + // }) add.Env = append(add.Env, corev1.EnvVar{ - Name: "JAVA_AGENT_PLUGIN_TYPE", - Value: pluginType, + Name: "JAVA_AGENT_FRAMEWORK_NAME", + Value: "", + }) + add.Env = append(add.Env, corev1.EnvVar{ + Name: "JAVA_AGENT_FRAMEWORK_VERSION", + Value: "", }) } @@ -140,11 +148,11 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co defaultProperties := make(map[string]string) // 格式化 MicroserviceName - microserviceName := fmt.Sprintf("spring.application.name={{ .%s }}", defaultParam["MicroserviceName"]) + microserviceName := fmt.Sprintf("spring.application.name=%s", defaultParam["MicroserviceName"]) defaultProperties["MicroserviceName"] = microserviceName // 格式化 PolarisServerIP 和 PolarisDiscoverPort - polarisAddress := fmt.Sprintf("spring.cloud.polaris.address=grpc\\://{{ .%s }}\\:{{ .%s }}", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) + polarisAddress := fmt.Sprintf("spring.cloud.polaris.address=grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) defaultProperties["PolarisAddress"] = polarisAddress // tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) From 67b77e517eeaca0daa1404a892b1b3a3e6025dc7 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 03:12:12 +0800 Subject: [PATCH 09/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 89 ++++++++----------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 78ce006c..5c23c4d7 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -15,10 +15,14 @@ package javaagent import ( + "bufio" + "bytes" + "context" "encoding/json" "fmt" "strconv" "strings" + "text/template" "github.com/polarismesh/polaris-controller/common/log" "github.com/polarismesh/polaris-controller/pkg/inject/pkg/kube/inject" @@ -27,6 +31,7 @@ import ( "github.com/polarismesh/polaris-controller/pkg/util" utils "github.com/polarismesh/polaris-controller/pkg/util" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // Java Agent 场景下的特殊 annonations 信息 @@ -117,65 +122,49 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co Name: "JAVA_AGENT_FRAMEWORK_VERSION", Value: frameworkVersion, }) - } else { - // pluginType = "spring-cloud2023" - // add.Env = append(add.Env, corev1.EnvVar{ - // Name: "JAVA_AGENT_PLUGIN_TYPE", - // Value: pluginType, - // }) - add.Env = append(add.Env, corev1.EnvVar{ - Name: "JAVA_AGENT_FRAMEWORK_NAME", - Value: "", - }) - add.Env = append(add.Env, corev1.EnvVar{ - Name: "JAVA_AGENT_FRAMEWORK_VERSION", - Value: "", - }) } - // kubeClient := opt.KubeClient - // pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), - // "plugin-default.properties", metav1.GetOptions{}) - // if err != nil { - // return err - // } + kubeClient := opt.KubeClient + pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), + "plugin-default.properties", metav1.GetOptions{}) + if err != nil { + return err + } defaultParam := map[string]string{ "MicroserviceName": opt.Annotations[util.SidecarServiceName], "PolarisServerIP": strings.Split(polarisapi.PolarisGrpc, ":")[0], "PolarisDiscoverPort": strings.Split(polarisapi.PolarisGrpc, ":")[1], } - defaultProperties := make(map[string]string) + // // 格式化 MicroserviceName + // microserviceName := fmt.Sprintf("spring.application.name=%s", defaultParam["MicroserviceName"]) + // defaultProperties["MicroserviceName"] = microserviceName + // + // // 格式化 PolarisServerIP 和 PolarisDiscoverPort + // polarisAddress := fmt.Sprintf("spring.cloud.polaris.address=grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) + // defaultProperties["PolarisAddress"] = polarisAddress - // 格式化 MicroserviceName - microserviceName := fmt.Sprintf("spring.application.name=%s", defaultParam["MicroserviceName"]) - defaultProperties["MicroserviceName"] = microserviceName - - // 格式化 PolarisServerIP 和 PolarisDiscoverPort - polarisAddress := fmt.Sprintf("spring.cloud.polaris.address=grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) - defaultProperties["PolarisAddress"] = polarisAddress - - // tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) - // if err != nil { - // return err - // } - // buf := new(bytes.Buffer) - // if err := tpl.Execute(buf, defaultParam); err != nil { - // return err - // } - // defaultProperties := map[string]string{} - // scanner := bufio.NewScanner(strings.NewReader(buf.String())) - // scanner.Split(bufio.ScanLines) - // for scanner.Scan() { - // line := scanner.Text() - // // 注释不放在 defaultProperties 中 - // if !strings.HasPrefix(line, "#") { - // kvs := strings.Split(line, "=") - // if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { - // defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) - // } - // } - // } + tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) + if err != nil { + return err + } + buf := new(bytes.Buffer) + if err := tpl.Execute(buf, defaultParam); err != nil { + return err + } + defaultProperties := map[string]string{} + scanner := bufio.NewScanner(strings.NewReader(buf.String())) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + line := scanner.Text() + // 注释不放在 defaultProperties 中 + if !strings.HasPrefix(line, "#") { + kvs := strings.Split(line, "=") + if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { + defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) + } + } + } // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 From 107dbda51095f8f3ff93b37d9d3068544c1d190c Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 03:29:33 +0800 Subject: [PATCH 10/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 77 +++++++++---------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 5c23c4d7..64b7d2e2 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -15,14 +15,10 @@ package javaagent import ( - "bufio" - "bytes" - "context" "encoding/json" "fmt" "strconv" "strings" - "text/template" "github.com/polarismesh/polaris-controller/common/log" "github.com/polarismesh/polaris-controller/pkg/inject/pkg/kube/inject" @@ -31,7 +27,6 @@ import ( "github.com/polarismesh/polaris-controller/pkg/util" utils "github.com/polarismesh/polaris-controller/pkg/util" corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // Java Agent 场景下的特殊 annonations 信息 @@ -124,47 +119,51 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co }) } - kubeClient := opt.KubeClient - pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), - "plugin-default.properties", metav1.GetOptions{}) - if err != nil { - return err - } + // kubeClient := opt.KubeClient + // pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), + // "plugin-default.properties", metav1.GetOptions{}) + // if err != nil { + // return err + // } defaultParam := map[string]string{ "MicroserviceName": opt.Annotations[util.SidecarServiceName], "PolarisServerIP": strings.Split(polarisapi.PolarisGrpc, ":")[0], "PolarisDiscoverPort": strings.Split(polarisapi.PolarisGrpc, ":")[1], } - // // 格式化 MicroserviceName - // microserviceName := fmt.Sprintf("spring.application.name=%s", defaultParam["MicroserviceName"]) - // defaultProperties["MicroserviceName"] = microserviceName - // - // // 格式化 PolarisServerIP 和 PolarisDiscoverPort - // polarisAddress := fmt.Sprintf("spring.cloud.polaris.address=grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) - // defaultProperties["PolarisAddress"] = polarisAddress + defaultProperties := make(map[string]string) - tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) - if err != nil { - return err - } - buf := new(bytes.Buffer) - if err := tpl.Execute(buf, defaultParam); err != nil { - return err - } - defaultProperties := map[string]string{} - scanner := bufio.NewScanner(strings.NewReader(buf.String())) - scanner.Split(bufio.ScanLines) - for scanner.Scan() { - line := scanner.Text() - // 注释不放在 defaultProperties 中 - if !strings.HasPrefix(line, "#") { - kvs := strings.Split(line, "=") - if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { - defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) - } - } - } + // 格式化 MicroserviceName + microserviceNameKey := "spring.application.name" + microserviceNameValue := fmt.Sprintf("%s", defaultParam["MicroserviceName"]) + defaultProperties[microserviceNameKey] = microserviceNameValue + + // 格式化 PolarisServerIP 和 PolarisDiscoverPort + polarisAddressKey := "spring.cloud.polaris.address" + polarisAddressValue := fmt.Sprintf("grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) + defaultProperties[polarisAddressKey] = polarisAddressValue + + // tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) + // if err != nil { + // return err + // } + // buf := new(bytes.Buffer) + // if err := tpl.Execute(buf, defaultParam); err != nil { + // return err + // } + // defaultProperties := map[string]string{} + // scanner := bufio.NewScanner(strings.NewReader(buf.String())) + // scanner.Split(bufio.ScanLines) + // for scanner.Scan() { + // line := scanner.Text() + // // 注释不放在 defaultProperties 中 + // if !strings.HasPrefix(line, "#") { + // kvs := strings.Split(line, "=") + // if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { + // defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) + // } + // } + // } // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 From 805c9293630fcbb311bbae7f90b3e0b9e199ef12 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 09:58:12 +0800 Subject: [PATCH 11/20] feat:agent Automatic version matching --- .../pkg/kube/inject/apply/javaagent/patch.go | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 64b7d2e2..dea4e5be 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -119,12 +119,6 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co }) } - // kubeClient := opt.KubeClient - // pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), - // "plugin-default.properties", metav1.GetOptions{}) - // if err != nil { - // return err - // } defaultParam := map[string]string{ "MicroserviceName": opt.Annotations[util.SidecarServiceName], "PolarisServerIP": strings.Split(polarisapi.PolarisGrpc, ":")[0], @@ -143,28 +137,6 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co polarisAddressValue := fmt.Sprintf("grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) defaultProperties[polarisAddressKey] = polarisAddressValue - // tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) - // if err != nil { - // return err - // } - // buf := new(bytes.Buffer) - // if err := tpl.Execute(buf, defaultParam); err != nil { - // return err - // } - // defaultProperties := map[string]string{} - // scanner := bufio.NewScanner(strings.NewReader(buf.String())) - // scanner.Split(bufio.ScanLines) - // for scanner.Scan() { - // line := scanner.Text() - // // 注释不放在 defaultProperties 中 - // if !strings.HasPrefix(line, "#") { - // kvs := strings.Split(line, "=") - // if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { - // defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) - // } - // } - // } - // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 if properties, ok := annonations[customJavaAgentPluginConfig]; ok { From e5d3ade0c3e548d1e4f4c390420c2970b58ad324 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 10:03:06 +0800 Subject: [PATCH 12/20] change log --- pkg/inject/pkg/kube/inject/apply/javaagent/patch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index dea4e5be..ca205cb3 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -78,7 +78,7 @@ func (pb *PodPatchBuilder) PatchContainer(req *inject.OperateContainerRequest) ( func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *corev1.Pod, add *corev1.Container) error { annonations := pod.Annotations - log.InjectScope().Infof("handle polaris-javaagent-init inject for pod=[%s, %s] annonations: %#v image: %s 111111111111111", + log.InjectScope().Infof("handle polaris-javaagent-init inject for pod=[%s, %s] annonations: %#v image: %s", pod.Namespace, pod.Name, pod.Annotations, add.Image) // 判断用户是否自定义了 javaagent 的版本 oldImageInfo := strings.Split(add.Image, ":") From f8fc470e090398a7f01453a04327505477253aae Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 10:26:47 +0800 Subject: [PATCH 13/20] Compatible with older versions --- .../pkg/kube/inject/apply/javaagent/patch.go | 70 ++++++++++++++++--- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index ca205cb3..06484dec 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -15,10 +15,14 @@ package javaagent import ( + "bufio" + "bytes" + "context" "encoding/json" "fmt" "strconv" "strings" + "text/template" "github.com/polarismesh/polaris-controller/common/log" "github.com/polarismesh/polaris-controller/pkg/inject/pkg/kube/inject" @@ -27,6 +31,7 @@ import ( "github.com/polarismesh/polaris-controller/pkg/util" utils "github.com/polarismesh/polaris-controller/pkg/util" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // Java Agent 场景下的特殊 annonations 信息 @@ -37,6 +42,25 @@ const ( customJavaAgentPluginConfig = "polarismesh.cn/javaagentConfig" ) +var validVersions = map[string]bool{ + "1.7.0-RC4": true, + "1.7.0-RC3": true, + "1.7.0-RC2": true, + "1.7.0-RC1": true, + "1.6.1": true, + "1.6.0": true, + "1.5.0": true, + "1.2.0-nacos-1.3.0": true, + "1.2.0-nacos-2.1.0": true, + "1.2.0-nacos-1.3.1": true, + "1.2.0-dubbox-2.x": true, + "v1.0.4": true, + "v1.0.3": true, + "v1.0.2": true, + "v1.0.1": true, + "v1.0.0": true, +} + const ( ActiveJavaAgentCmd = "-javaagent:/app/lib/.polaris/java_agent/polaris-java-agent-%s/polaris-agent-core-bootstrap.jar" ) @@ -85,7 +109,6 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co if len(oldImageInfo) > 1 { opt.ExternalInfo[customJavaAgentVersion] = oldImageInfo[1] } - if val, ok := annonations[customJavaAgentVersion]; ok && val != "" { add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) opt.ExternalInfo[customJavaAgentVersion] = val @@ -127,15 +150,44 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co defaultProperties := make(map[string]string) - // 格式化 MicroserviceName - microserviceNameKey := "spring.application.name" - microserviceNameValue := fmt.Sprintf("%s", defaultParam["MicroserviceName"]) - defaultProperties[microserviceNameKey] = microserviceNameValue + if validVersions[annonations[customJavaAgentVersion]] { + kubeClient := opt.KubeClient + pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), + "plugin-default.properties", metav1.GetOptions{}) + if err != nil { + return err + } + tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) + if err != nil { + return err + } + buf := new(bytes.Buffer) + if err := tpl.Execute(buf, defaultParam); err != nil { + return err + } + scanner := bufio.NewScanner(strings.NewReader(buf.String())) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + line := scanner.Text() + // 注释不放在 defaultProperties 中 + if !strings.HasPrefix(line, "#") { + kvs := strings.Split(line, "=") + if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { + defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) + } + } + } + } else { + // 格式化 MicroserviceName + microserviceNameKey := "spring.application.name" + microserviceNameValue := fmt.Sprintf("%s", defaultParam["MicroserviceName"]) + defaultProperties[microserviceNameKey] = microserviceNameValue - // 格式化 PolarisServerIP 和 PolarisDiscoverPort - polarisAddressKey := "spring.cloud.polaris.address" - polarisAddressValue := fmt.Sprintf("grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) - defaultProperties[polarisAddressKey] = polarisAddressValue + // 格式化 PolarisServerIP 和 PolarisDiscoverPort + polarisAddressKey := "spring.cloud.polaris.address" + polarisAddressValue := fmt.Sprintf("grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) + defaultProperties[polarisAddressKey] = polarisAddressValue + } // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 From cf698000fd9bcb859b90732cc107a9a1ace63a40 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 10:30:37 +0800 Subject: [PATCH 14/20] Compatible with older versions --- pkg/inject/pkg/kube/inject/apply/javaagent/patch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 06484dec..7bdfd196 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -150,7 +150,7 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co defaultProperties := make(map[string]string) - if validVersions[annonations[customJavaAgentVersion]] { + if val, ok := annonations[customJavaAgentVersion]; ok && (validVersions[val] && val != "") { kubeClient := opt.KubeClient pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), "plugin-default.properties", metav1.GetOptions{}) From 892867066a2d130202efbb1dad180c3908787b5d Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 10:32:02 +0800 Subject: [PATCH 15/20] Compatible with older versions --- pkg/inject/pkg/kube/inject/apply/javaagent/patch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 7bdfd196..4fbe0ffb 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -149,7 +149,7 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co } defaultProperties := make(map[string]string) - + // 判断是不是老版本,如果是老版本且客户填写的版本号不为空则走老的逻辑,否则走新的逻辑,只下发北极星的地址和端口信息 if val, ok := annonations[customJavaAgentVersion]; ok && (validVersions[val] && val != "") { kubeClient := opt.KubeClient pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), From fa69949b6e561a9591b5e4e4e9c75decb5991369 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 11:14:01 +0800 Subject: [PATCH 16/20] Compatible with older versions --- pkg/inject/pkg/kube/inject/apply/javaagent/patch.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 4fbe0ffb..5bd6595e 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -146,6 +146,8 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co "MicroserviceName": opt.Annotations[util.SidecarServiceName], "PolarisServerIP": strings.Split(polarisapi.PolarisGrpc, ":")[0], "PolarisDiscoverPort": strings.Split(polarisapi.PolarisGrpc, ":")[1], + "PolarisConfigIP": strings.Split(polarisapi.PolarisConfigGrpc, ":")[0], + "PolarisConfigPort": strings.Split(polarisapi.PolarisConfigGrpc, ":")[1], } defaultProperties := make(map[string]string) @@ -187,6 +189,10 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co polarisAddressKey := "spring.cloud.polaris.address" polarisAddressValue := fmt.Sprintf("grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) defaultProperties[polarisAddressKey] = polarisAddressValue + + polarisConfigAddressKey := "spring.cloud.polaris.address" + polarisConfigAddressValue := fmt.Sprintf("grpc\\://%s\\:%s", defaultParam["PolarisConfigIP"], defaultParam["PolarisConfigPort"]) + defaultProperties[polarisConfigAddressKey] = polarisConfigAddressValue } // 查看用户是否自定义了相关配置信息 From 408d5084f76a990d523842d74fa19405e558cbef Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 11:54:54 +0800 Subject: [PATCH 17/20] Compatible with older versions --- .../pkg/kube/inject/apply/javaagent/patch.go | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 5bd6595e..1f8e7038 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -150,6 +150,24 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co "PolarisConfigPort": strings.Split(polarisapi.PolarisConfigGrpc, ":")[1], } + add.Env = append(add.Env, + corev1.EnvVar{ + Name: "POLARIS_SERVER_IP", + Value: defaultParam["PolarisServerIP"], + }, + corev1.EnvVar{ + Name: "POLARIS_DISCOVER_PORT", + Value: defaultParam["PolarisDiscoverPort"], + }, + corev1.EnvVar{ + Name: "POLARIS_CONFIG_PORT", + Value: defaultParam["PolarisConfigIP"], + }, + corev1.EnvVar{ + Name: "POLARIS_CONFIG_PORT", + Value: defaultParam["PolarisConfigPort"], + }, + ) defaultProperties := make(map[string]string) // 判断是不是老版本,如果是老版本且客户填写的版本号不为空则走老的逻辑,否则走新的逻辑,只下发北极星的地址和端口信息 if val, ok := annonations[customJavaAgentVersion]; ok && (validVersions[val] && val != "") { @@ -179,22 +197,7 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co } } } - } else { - // 格式化 MicroserviceName - microserviceNameKey := "spring.application.name" - microserviceNameValue := fmt.Sprintf("%s", defaultParam["MicroserviceName"]) - defaultProperties[microserviceNameKey] = microserviceNameValue - - // 格式化 PolarisServerIP 和 PolarisDiscoverPort - polarisAddressKey := "spring.cloud.polaris.address" - polarisAddressValue := fmt.Sprintf("grpc\\://%s\\:%s", defaultParam["PolarisServerIP"], defaultParam["PolarisDiscoverPort"]) - defaultProperties[polarisAddressKey] = polarisAddressValue - - polarisConfigAddressKey := "spring.cloud.polaris.address" - polarisConfigAddressValue := fmt.Sprintf("grpc\\://%s\\:%s", defaultParam["PolarisConfigIP"], defaultParam["PolarisConfigPort"]) - defaultProperties[polarisConfigAddressKey] = polarisConfigAddressValue } - // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 if properties, ok := annonations[customJavaAgentPluginConfig]; ok { From 3ae4ad7b8d888036b7fe1eb298df73f60df54c1a Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 12:16:27 +0800 Subject: [PATCH 18/20] change --- .../pkg/kube/inject/apply/javaagent/patch.go | 79 +++++++++---------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 1f8e7038..4391bd50 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -42,23 +42,13 @@ const ( customJavaAgentPluginConfig = "polarismesh.cn/javaagentConfig" ) -var validVersions = map[string]bool{ - "1.7.0-RC4": true, - "1.7.0-RC3": true, - "1.7.0-RC2": true, - "1.7.0-RC1": true, - "1.6.1": true, - "1.6.0": true, - "1.5.0": true, - "1.2.0-nacos-1.3.0": true, - "1.2.0-nacos-2.1.0": true, - "1.2.0-nacos-1.3.1": true, - "1.2.0-dubbox-2.x": true, - "v1.0.4": true, - "v1.0.3": true, - "v1.0.2": true, - "v1.0.1": true, - "v1.0.0": true, +var oldAgentVersions = map[string]struct{}{ + "1.7.0-RC4": {}, + "1.7.0-RC3": {}, + "1.7.0-RC2": {}, + "1.7.0-RC1": {}, + "1.6.1": {}, + "1.6.0": {}, } const ( @@ -155,6 +145,10 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co Name: "POLARIS_SERVER_IP", Value: defaultParam["PolarisServerIP"], }, + corev1.EnvVar{ + Name: "POLARIS_DISCOVER_IP", + Value: defaultParam["PolarisServerIP"], + }, corev1.EnvVar{ Name: "POLARIS_DISCOVER_PORT", Value: defaultParam["PolarisDiscoverPort"], @@ -170,33 +164,36 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co ) defaultProperties := make(map[string]string) // 判断是不是老版本,如果是老版本且客户填写的版本号不为空则走老的逻辑,否则走新的逻辑,只下发北极星的地址和端口信息 - if val, ok := annonations[customJavaAgentVersion]; ok && (validVersions[val] && val != "") { - kubeClient := opt.KubeClient - pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), - "plugin-default.properties", metav1.GetOptions{}) - if err != nil { - return err - } - tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) - if err != nil { - return err - } - buf := new(bytes.Buffer) - if err := tpl.Execute(buf, defaultParam); err != nil { - return err - } - scanner := bufio.NewScanner(strings.NewReader(buf.String())) - scanner.Split(bufio.ScanLines) - for scanner.Scan() { - line := scanner.Text() - // 注释不放在 defaultProperties 中 - if !strings.HasPrefix(line, "#") { - kvs := strings.Split(line, "=") - if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { - defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) + if val, ok := annonations[customJavaAgentVersion]; ok && val != "" { + if _, valid := oldAgentVersions[val]; valid { + kubeClient := opt.KubeClient + pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), + "plugin-default.properties", metav1.GetOptions{}) + if err != nil { + return err + } + tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) + if err != nil { + return err + } + buf := new(bytes.Buffer) + if err := tpl.Execute(buf, defaultParam); err != nil { + return err + } + scanner := bufio.NewScanner(strings.NewReader(buf.String())) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + line := scanner.Text() + // 注释不放在 defaultProperties 中 + if !strings.HasPrefix(line, "#") { + kvs := strings.Split(line, "=") + if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { + defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) + } } } } + } // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 From 334b717ac6457bb4d809dc0c64c6b601a73bb338 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 13:54:31 +0800 Subject: [PATCH 19/20] Compatible with older versions --- pkg/inject/pkg/kube/inject/apply/javaagent/patch.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 4391bd50..6956a1a0 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -96,8 +96,12 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co pod.Namespace, pod.Name, pod.Annotations, add.Image) // 判断用户是否自定义了 javaagent 的版本 oldImageInfo := strings.Split(add.Image, ":") + var imageHasBeenChanged bool if len(oldImageInfo) > 1 { opt.ExternalInfo[customJavaAgentVersion] = oldImageInfo[1] + if oldImageInfo[1] != "latest" { + imageHasBeenChanged = true + } } if val, ok := annonations[customJavaAgentVersion]; ok && val != "" { add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) @@ -165,7 +169,7 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co defaultProperties := make(map[string]string) // 判断是不是老版本,如果是老版本且客户填写的版本号不为空则走老的逻辑,否则走新的逻辑,只下发北极星的地址和端口信息 if val, ok := annonations[customJavaAgentVersion]; ok && val != "" { - if _, valid := oldAgentVersions[val]; valid { + if _, valid := oldAgentVersions[val]; valid || imageHasBeenChanged { kubeClient := opt.KubeClient pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), "plugin-default.properties", metav1.GetOptions{}) @@ -193,7 +197,6 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co } } } - } // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 From 5b768f9c174b0a205fdf7516530440ae4cef7a74 Mon Sep 17 00:00:00 2001 From: shuhanliu Date: Mon, 1 Jul 2024 14:19:07 +0800 Subject: [PATCH 20/20] Compatible with older versions --- .../pkg/kube/inject/apply/javaagent/patch.go | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index 6956a1a0..ac86079e 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -96,12 +96,8 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co pod.Namespace, pod.Name, pod.Annotations, add.Image) // 判断用户是否自定义了 javaagent 的版本 oldImageInfo := strings.Split(add.Image, ":") - var imageHasBeenChanged bool if len(oldImageInfo) > 1 { opt.ExternalInfo[customJavaAgentVersion] = oldImageInfo[1] - if oldImageInfo[1] != "latest" { - imageHasBeenChanged = true - } } if val, ok := annonations[customJavaAgentVersion]; ok && val != "" { add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) @@ -168,36 +164,36 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co ) defaultProperties := make(map[string]string) // 判断是不是老版本,如果是老版本且客户填写的版本号不为空则走老的逻辑,否则走新的逻辑,只下发北极星的地址和端口信息 - if val, ok := annonations[customJavaAgentVersion]; ok && val != "" { - if _, valid := oldAgentVersions[val]; valid || imageHasBeenChanged { - kubeClient := opt.KubeClient - pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), - "plugin-default.properties", metav1.GetOptions{}) - if err != nil { - return err - } - tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) - if err != nil { - return err - } - buf := new(bytes.Buffer) - if err := tpl.Execute(buf, defaultParam); err != nil { - return err - } - scanner := bufio.NewScanner(strings.NewReader(buf.String())) - scanner.Split(bufio.ScanLines) - for scanner.Scan() { - line := scanner.Text() - // 注释不放在 defaultProperties 中 - if !strings.HasPrefix(line, "#") { - kvs := strings.Split(line, "=") - if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { - defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) - } + newImageInfo := strings.Split(add.Image, ":") + if _, valid := oldAgentVersions[newImageInfo[1]]; valid { + kubeClient := opt.KubeClient + pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(), + "plugin-default.properties", metav1.GetOptions{}) + if err != nil { + return err + } + tpl, err := template.New(pluginType).Parse(pluginCm.Data[nameOfPluginDefault(pluginType)]) + if err != nil { + return err + } + buf := new(bytes.Buffer) + if err := tpl.Execute(buf, defaultParam); err != nil { + return err + } + scanner := bufio.NewScanner(strings.NewReader(buf.String())) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + line := scanner.Text() + // 注释不放在 defaultProperties 中 + if !strings.HasPrefix(line, "#") { + kvs := strings.Split(line, "=") + if len(kvs) == 2 && kvs[0] != "" && kvs[1] != "" { + defaultProperties[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1]) } } } } + // 查看用户是否自定义了相关配置信息 // 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中 if properties, ok := annonations[customJavaAgentPluginConfig]; ok {