Skip to content

Commit

Permalink
fix:修复java-agent注入时plugin.enable格式不对 (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Feb 1, 2024
1 parent 9beea2b commit 0b706c5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/inject/pkg/kube/inject/apply/javaagent/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 中,用于初始化相关的配置文件信息
Expand Down Expand Up @@ -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))
Expand All @@ -191,15 +193,15 @@ 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]),
}
}
}
if javaEnvIndex == -1 {
// 注入 java agent 需要用到的参数信息
container.Env = append(container.Env, corev1.EnvVar{
Name: "JAVA_TOOL_OPTIONS",
Value: ActiveJavaAgentCmd,
Value: fmt.Sprintf(ActiveJavaAgentCmd, opt.ExternalInfo[customJavaAgentVersion]),
})
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/inject/pkg/kube/inject/pod_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type PatchOptions struct {
Sic *SidecarInjectionSpec
// Workload 的名称
WorkloadName string
// ExternalInfo .
ExternalInfo map[string]string
}

type OperateContainerRequest struct {
Expand Down

0 comments on commit 0b706c5

Please sign in to comment.