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

fix:fix POLARIS_CONFIG_IP is the same as POLARIS_CONFIG_PORT BUG #191

Merged
merged 5 commits into from
Jul 9, 2024
Merged
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
98 changes: 68 additions & 30 deletions pkg/inject/pkg/kube/inject/apply/javaagent/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
)

var oldAgentVersions = map[string]struct{}{
"1.7.0-RC5": {},
"1.7.0-RC4": {},
"1.7.0-RC3": {},
"1.7.0-RC2": {},
Expand All @@ -52,7 +53,8 @@ var oldAgentVersions = map[string]struct{}{
}

const (
ActiveJavaAgentCmd = "-javaagent:/app/lib/.polaris/java_agent/polaris-java-agent-%s/polaris-agent-core-bootstrap.jar"
ActiveJavaAgentCmd = "-javaagent:/app/lib/.polaris/java_agent/polaris-java-agent/polaris-agent-core-bootstrap.jar"
OldActiveJavaAgentCmd = "-javaagent:/app/lib/.polaris/java_agent/polaris-java-agent-%s/polaris-agent-core-bootstrap.jar"
)

func init() {
Expand Down Expand Up @@ -154,7 +156,7 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co
Value: defaultParam["PolarisDiscoverPort"],
},
corev1.EnvVar{
Name: "POLARIS_CONFIG_PORT",
Name: "POLARIS_CONFIG_IP",
Value: defaultParam["PolarisConfigIP"],
},
corev1.EnvVar{
Expand Down Expand Up @@ -192,45 +194,78 @@ 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
// 查看用户是否自定义了相关配置信息
// 需要根据用户的自定义参数信息,将 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
}
}
// 先从 configmap 中获取 java-agent 不同 plugin-type 的默认配置信息
for k, v := range customProperties {
defaultProperties[k] = v

exportAgentPluginConf := ""
for key, value := range defaultProperties {
exportAgentPluginConf += fmt.Sprintf("%s=%s\n", key, value)
}
}

exportAgentPluginConf := ""
for key, value := range defaultProperties {
exportAgentPluginConf += fmt.Sprintf("%s=%s\n", key, value)
add.Env = append(add.Env, corev1.EnvVar{
Name: "JAVA_AGENT_PLUGIN_CONF",
Value: exportAgentPluginConf,
})
}

add.Env = append(add.Env, corev1.EnvVar{
Name: "JAVA_AGENT_PLUGIN_CONF",
Value: exportAgentPluginConf,
})
return nil
}

func nameOfPluginDefault(v string) string {
return v + "-default-properties"
}

func updateJavaEnvVar(envVar corev1.EnvVar, cmd string, version string) corev1.EnvVar {
return corev1.EnvVar{
Name: "JAVA_TOOL_OPTIONS",
Value: envVar.Value + " " + cmd + version,
}
}

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))

annonations := pod.Annotations
if val, ok := annonations[customJavaAgentVersion]; ok && val != "" {
opt.ExternalInfo[customJavaAgentVersion] = val
} else {
annonations[customJavaAgentVersion] = "latest"
}

defaultProperties := make(map[string]string)
var javaToolOptionsValue string

for index, container := range target {
envs := container.Env
javaEnvIndex := -1
if _, valid := oldAgentVersions[annonations[customJavaAgentVersion]]; !valid {
if properties, ok := annonations[customJavaAgentPluginConfig]; ok {
customProperties := map[string]string{}
if properties != "" {
json.Unmarshal([]byte(properties), &customProperties)
}
// 先从 configmap 中获取 java-agent 不同 plugin-type 的默认配置信息
for k, v := range customProperties {
defaultProperties[k] = v
}
}
}

for key, value := range defaultProperties {
javaToolOptionsValue += fmt.Sprintf(" -D%s=%s", key, value)
}

if len(envs) != 0 {
for i := range envs {
if envs[i].Name == "JAVA_TOOL_OPTIONS" {
Expand All @@ -239,19 +274,22 @@ func (pb *PodPatchBuilder) updateContainer(opt *inject.PatchOptions, sidecarMode
}
}
if javaEnvIndex != -1 {
oldVal := envs[javaEnvIndex].Value
envs[javaEnvIndex] = corev1.EnvVar{
Name: "JAVA_TOOL_OPTIONS",
Value: oldVal + " " + fmt.Sprintf(ActiveJavaAgentCmd, opt.ExternalInfo[customJavaAgentVersion]),
if _, valid := oldAgentVersions[annonations[customJavaAgentVersion]]; !valid {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

270~279和285~295的代码优化下,重复的逻辑有嗲多

envs[javaEnvIndex] = updateJavaEnvVar(envs[javaEnvIndex], ActiveJavaAgentCmd, javaToolOptionsValue)
} else {
envs[javaEnvIndex] = updateJavaEnvVar(envs[javaEnvIndex], fmt.Sprintf(OldActiveJavaAgentCmd, opt.ExternalInfo[customJavaAgentVersion]), "")
}
}
}
if javaEnvIndex == -1 {
// 注入 java agent 需要用到的参数信息
container.Env = append(container.Env, corev1.EnvVar{
Name: "JAVA_TOOL_OPTIONS",
Value: fmt.Sprintf(ActiveJavaAgentCmd, opt.ExternalInfo[customJavaAgentVersion]),
})
var newEnvVar corev1.EnvVar
if _, valid := oldAgentVersions[annonations[customJavaAgentVersion]]; !valid {
newEnvVar = updateJavaEnvVar(corev1.EnvVar{}, ActiveJavaAgentCmd, javaToolOptionsValue)
} else {
newEnvVar = updateJavaEnvVar(corev1.EnvVar{}, fmt.Sprintf(OldActiveJavaAgentCmd, opt.ExternalInfo[customJavaAgentVersion]), "")
}
container.Env = append(container.Env, newEnvVar)
}

// container 需要新挂载磁盘
Expand Down
Loading