From 0b706c58582578a2a3a374086918edfd82e61b77 Mon Sep 17 00:00:00 2001 From: liaochuntao Date: Thu, 1 Feb 2024 13:06:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8Djava-agent=E6=B3=A8?= =?UTF-8?q?=E5=85=A5=E6=97=B6plugin.enable=E6=A0=BC=E5=BC=8F=E4=B8=8D?= =?UTF-8?q?=E5=AF=B9=20(#170)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/kube/inject/apply/javaagent/patch.go | 14 ++++++++------ pkg/inject/pkg/kube/inject/pod_patch.go | 2 ++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go index c62eb7a3..bd898951 100644 --- a/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go +++ b/pkg/inject/pkg/kube/inject/apply/javaagent/patch.go @@ -44,7 +44,7 @@ const ( ) const ( - ActiveJavaAgentCmd = "-javaagent:/app/lib/.polaris/java_agent/polaris-agent-core-bootstrap.jar" + ActiveJavaAgentCmd = "-javaagent:/app/lib/.polaris/java_agent/polaris-java-agent-%s/polaris-agent-core-bootstrap.jar" ) func init() { @@ -77,7 +77,7 @@ func (pb *PodPatchBuilder) PatchContainer(req *inject.OperateContainerRequest) ( log.InjectScope().Infof("finish deal polaris-javaagent-init inject for pod=[%s, %s] added: %#v", pod.Namespace, pod.Name, added) return pb.PodPatchBuilder.PatchContainer(req) case inject.PatchType_Update: - return pb.updateContainer(req.Option.SidecarMode, req.Option.Pod, req.Option.Pod.Spec.Containers, req.BasePath), nil + return pb.updateContainer(req.Option, req.Option.SidecarMode, req.Option.Pod, req.Option.Pod.Spec.Containers, req.BasePath), nil } return nil, nil } @@ -87,9 +87,11 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co log.InjectScope().Infof("handle polaris-javaagent-init inject for pod=[%s, %s] annonations: %#v", pod.Namespace, pod.Name, pod.Annotations) // 判断用户是否自定义了 javaagent 的版本 + oldImageInfo := strings.Split(add.Image, ":") + opt.ExternalInfo[customJavaAgentVersion] = oldImageInfo[1] if val, ok := annonations[customJavaAgentVersion]; ok { - oldImageInfo := strings.Split(add.Image, ":") add.Image = fmt.Sprintf("%s:%s", oldImageInfo[0], val) + opt.ExternalInfo[customJavaAgentVersion] = val } // 需要将用户的框架信息注入到 javaagent-init 中,用于初始化相关的配置文件信息 @@ -172,7 +174,7 @@ func nameOfPluginDefault(v string) string { return v + "-default-properties" } -func (pb *PodPatchBuilder) updateContainer(sidecarMode utils.SidecarMode, pod *corev1.Pod, +func (pb *PodPatchBuilder) updateContainer(opt *inject.PatchOptions, sidecarMode utils.SidecarMode, pod *corev1.Pod, target []corev1.Container, basePath string) []inject.Rfc6902PatchOperation { patchs := make([]inject.Rfc6902PatchOperation, 0, len(target)) @@ -191,7 +193,7 @@ func (pb *PodPatchBuilder) updateContainer(sidecarMode utils.SidecarMode, pod *c oldVal := envs[javaEnvIndex].Value envs[javaEnvIndex] = corev1.EnvVar{ Name: "JAVA_TOOL_OPTIONS", - Value: oldVal + " " + ActiveJavaAgentCmd, + Value: oldVal + " " + fmt.Sprintf(ActiveJavaAgentCmd, opt.ExternalInfo[customJavaAgentVersion]), } } } @@ -199,7 +201,7 @@ func (pb *PodPatchBuilder) updateContainer(sidecarMode utils.SidecarMode, pod *c // 注入 java agent 需要用到的参数信息 container.Env = append(container.Env, corev1.EnvVar{ Name: "JAVA_TOOL_OPTIONS", - Value: ActiveJavaAgentCmd, + Value: fmt.Sprintf(ActiveJavaAgentCmd, opt.ExternalInfo[customJavaAgentVersion]), }) } diff --git a/pkg/inject/pkg/kube/inject/pod_patch.go b/pkg/inject/pkg/kube/inject/pod_patch.go index 4ee78111..49d51a9d 100644 --- a/pkg/inject/pkg/kube/inject/pod_patch.go +++ b/pkg/inject/pkg/kube/inject/pod_patch.go @@ -56,6 +56,8 @@ type PatchOptions struct { Sic *SidecarInjectionSpec // Workload 的名称 WorkloadName string + // ExternalInfo . + ExternalInfo map[string]string } type OperateContainerRequest struct {