免费A级毛片无码专区网站-成人国产精品视频一区二区-啊 日出水了 用力乖乖在线-国产黑色丝袜在线观看下-天天操美女夜夜操美女-日韩网站在线观看中文字幕-AV高清hd片XXX国产-亚洲av中文字字幕乱码综合-搬开女人下面使劲插视频

Skywalking Swck Agent注入實(shí)現(xiàn)分析( 四 )

  1. 第四個(gè)執(zhí)行的是OverlayAgent
// OverlayAgent overlays the agent by getting the pod's annotations// If the agent overlay option is not set, go directly to the next step// If set the wrong value in the annotation , inject the error info and returnfunc (oa *OverlayAgent) execute(ipd *InjectProcessData) admission.Response {log.Info("=============== OverlayAgent ================")if !ipd.injectFileds.OverlayAgent(*ipd.annotation, ipd.annotationOverlay, &ipd.pod.ObjectMeta.Annotations) {ipd.log.Info("overlay agent config error!please look the error annotation!")return PatchReq(ipd.pod, ipd.req)}return oa.next.execute(ipd)}// OverlayAgent overlays agentfunc (s *SidecarInjectField) OverlayAgent(a Annotations, ao *AnnotationOverlay, annotation *map[string]string) bool {// jvmAgentConfigStr inits.JvmAgentConfigStr = ""http://遍歷pod的注解,如果注解的名稱(chēng)存在于全量注解中,則將Pod注解及值保存到AnnotationOverlay map對(duì)象中anno := GetAnnotationsByPrefix(a, agentAnnotationPrefix)for k, v := range *annotation {if strings.HasPrefix(k, agentAnnotationPrefix) {for _, an := range anno.Annotations {if strings.EqualFold(k, an.Name) {if !s.AgentOverlayandGetValue(ao, annotation, an) {return false}}}// 將pod注解去掉agent前綴,追加到JvmAgentConfigStr字段中configName := strings.TrimPrefix(k, agentAnnotationPrefix)config := strings.Join([]string{configName, v}, "=")// add to jvmAgentConfigStrif s.JvmAgentConfigStr != "" {s.JvmAgentConfigStr = strings.Join([]string{s.JvmAgentConfigStr, config}, ",")} else {s.JvmAgentConfigStr = config}}}return true}
  1. 第五個(gè)執(zhí)行的是OverlayPlugins,與OverlayAgent邏輯類(lèi)似 。
  2. 第六個(gè)執(zhí)行的是GetConfigmap,其作用是檢查如果pod配置了agent configmap,則檢查configmap配置的值是否正確.
func (s *SidecarInjectField) ValidateConfigmap(ctx context.Context, kubeclient client.Client, namespace string,annotation *map[string]string) bool {if len(s.ConfigmapVolume.Name) == 0 || len(s.ConfigmapVolume.ConfigMap.Name) == 0 {return true}configmap := &corev1.ConfigMap{}configmapName := s.ConfigmapVolume.VolumeSource.ConfigMap.LocalObjectReference.Name// check whether the configmap is existederr := kubeclient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: configmapName}, configmap)if err != nil && !errors.IsNotFound(err) {log.Error(err, "Get Configmap failed", "configmapName", configmapName, "namespace", namespace)return false}// if configmap exist , validate itif !errors.IsNotFound(err) {ok, errinfo := ValidateConfigmap(configmap)if ok {log.Info("the configmap validate true", "configmapName", configmapName)return true}log.Error(errinfo, "the configmap validate false", "configmapName", configmapName)}return true}
  1. 最后一步是PodInject,顧名思義,其作用是進(jìn)行Pod注入
// PodInject will inject all fields to the podfunc (pi *PodInject) execute(ipd *InjectProcessData) admission.Response {log.Info("=============== PodInject ================")ipd.injectFileds.Inject(ipd.pod)// Pod注入完成后,添加sidecar.skywalking.apache.org/succeed=true注解ipd.injectFileds.injectSucceedAnnotation(&ipd.pod.Annotations)log.Info("inject successfully!")// 序列化Pod,返回給k8sreturn PatchReq(ipd.pod, ipd.req)}// Inject will do real injectionfunc (s *SidecarInjectField) Inject(pod *corev1.Pod) {log.Info(fmt.Sprintf("inject pod : %s", pod.GenerateName))// 將之前執(zhí)行得到的InitContainer與pod配置的InitContainer合并在一起,也就是說(shuō)pod initcontainer可以有多個(gè)if pod.Spec.InitContainers != nil {pod.Spec.InitContainers = append(pod.Spec.InitContainers, s.Initcontainer)} else {pod.Spec.InitContainers = []corev1.Container{s.Initcontainer}}// add volume to specif pod.Spec.Volumes == nil {pod.Spec.Volumes = []corev1.Volume{}}pod.Spec.Volumes = append(pod.Spec.Volumes, s.SidecarVolume)if len(s.ConfigmapVolume.Name) > 0 && len(s.ConfigmapVolume.ConfigMap.Name) > 0 {pod.Spec.Volumes = append(pod.Spec.Volumes, s.ConfigmapVolume)}//選擇要注入的目標(biāo)容器targetContainers := s.findInjectContainer(pod.Spec.Containers)//循環(huán)目標(biāo)容器進(jìn)行注入for i := range targetContainers {log.Info(fmt.Sprintf("inject container : %s", targetContainers[i].Name))if (*targetContainers[i]).VolumeMounts == nil {(*targetContainers[i]).VolumeMounts = []corev1.VolumeMount{}}// 注入voLume與configmap(*targetContainers[i]).VolumeMounts = append((*targetContainers[i]).VolumeMounts, s.SidecarVolumeMount)if len(s.ConfigmapVolumeMount.Name) > 0 && len(s.ConfigmapVolumeMount.MountPath) > 0 {(*targetContainers[i]).VolumeMounts = append((*targetContainers[i]).VolumeMounts, s.ConfigmapVolumeMount)}//java agent參數(shù),其值為上面的JvmAgentConfigStrif (*targetContainers[i]).Env != nil {(*targetContainers[i]).Env = append((*targetContainers[i]).Env, s.Env)} else {(*targetContainers[i]).Env = []corev1.EnvVar{s.Env}}//注入環(huán)境變量,如果container本身存在,則忽略var envsTBA []corev1.EnvVarfor j, envInject := range s.Envs {isExists := falsefor _, envExists := range targetContainers[i].Env {if strings.EqualFold(envExists.Name, envInject.Name) {isExists = truebreak}}if !isExists {envsTBA = append(envsTBA, s.Envs[j])}}if len(s.Envs) > 0 {(*targetContainers[i]).Env = append((*targetContainers[i]).Env, envsTBA...)}}}

經(jīng)驗(yàn)總結(jié)擴(kuò)展閱讀