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

feat: support agent auto injection #188

Merged
merged 20 commits into from
Jul 1, 2024
Merged
Changes from 11 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
90 changes: 36 additions & 54 deletions pkg/inject/pkg/kube/inject/apply/javaagent/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,18 @@
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"
"github.com/polarismesh/polaris-controller/pkg/inject/pkg/kube/inject/apply/base"
"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 信息
Expand Down Expand Up @@ -84,77 +78,64 @@ 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",
chuntaojun marked this conversation as resolved.
Show resolved Hide resolved
pod.Namespace, pod.Name, pod.Annotations, add.Image)
// 判断用户是否自定义了 javaagent 的版本
oldImageInfo := strings.Split(add.Image, ":")
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
}

// 需要将用户的框架信息注入到 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,
})
kubeClient := opt.KubeClient
pluginCm, err := kubeClient.CoreV1().ConfigMaps(util.RootNamespace).Get(context.Background(),
"plugin-default.properties", metav1.GetOptions{})
if err != nil {
return err
if 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,
})
}

defaultParam := 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])
}
}
}

defaultProperties := make(map[string]string)
chuntaojun marked this conversation as resolved.
Show resolved Hide resolved

// 格式化 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

// 查看用户是否自定义了相关配置信息
// 需要根据用户的自定义参数信息,将 agent 的特定 application.properties 文件注入到 javaagent-init 中
Expand All @@ -168,6 +149,7 @@ func (pb *PodPatchBuilder) handleJavaAgentInit(opt *inject.PatchOptions, pod *co
defaultProperties[k] = v
}
}

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