func CleanSidecar(instanceName string, deployment *appsv1.Deployment) {
	delete(deployment.Labels, Label)
	for c := 0; c < len(deployment.Spec.Template.Spec.Containers); c++ {
		if deployment.Spec.Template.Spec.Containers[c].Name == "jaeger-agent" {
			deployment.Spec.Template.Spec.Containers = append(deployment.Spec.Template.Spec.Containers[:c], deployment.Spec.Template.Spec.Containers[c+1:]...)
			break
		}
	}
	if viper.GetString("platform") == v1.FlagPlatformOpenShift {
		names := map[string]bool{
			ca.TrustedCANameFromString(instanceName): true,
			ca.ServiceCANameFromString(instanceName): true,
		}
		for v := 0; v < len(deployment.Spec.Template.Spec.Volumes); v++ {
			if _, ok := names[deployment.Spec.Template.Spec.Volumes[v].Name]; ok {
				deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes[:v], deployment.Spec.Template.Spec.Volumes[v+1:]...)
				v--
			}
		}
	}
}
CleanSidecar of  deployments  associated with the jaeger instance.
package inject
import (
	"fmt"
	"reflect"
	"sort"
	"strings"
	log "github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	appsv1 "k8s.io/api/apps/v1"
	corev1 "k8s.io/api/core/v1"
	v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
	"github.com/jaegertracing/jaeger-operator/pkg/config/ca"
	"github.com/jaegertracing/jaeger-operator/pkg/deployment"
	"github.com/jaegertracing/jaeger-operator/pkg/service"
	"github.com/jaegertracing/jaeger-operator/pkg/util"
)
var (
	Annotation = "sidecar.jaegertracing.io/inject"
	Label = "sidecar.jaegertracing.io/injected"
	AnnotationLegacy = "inject-jaeger-agent"
	PrometheusDefaultAnnotations = map[string]string{
		"prometheus.io/scrape": "true",
		"prometheus.io/port":   "14271",
	}
)
const (
	envVarTags        = "JAEGER_TAGS"
	envVarServiceName = "JAEGER_SERVICE_NAME"
	envVarPropagation = "JAEGER_PROPAGATION"
	envVarPodName     = "POD_NAME"
	envVarHostIP      = "HOST_IP"
)
func Sidecar(jaeger *v1.Jaeger, dep *appsv1.Deployment) *appsv1.Deployment {
	deployment.NewAgent(jaeger)  
	logFields := jaeger.Logger().WithField("deployment", dep.Name)
	if jaeger == nil {
		logFields.Trace("no Jaeger instance found, skipping sidecar injection")
		return dep
	}
	if val, ok := dep.Labels[Label]; ok && val != jaeger.Name {
		logFields.Trace("deployment is assigned to a different Jaeger instance, skipping sidecar injection")
		return dep
	}
	decorate(dep)
	hasAgent, agentContainerIndex := HasJaegerAgent(dep)
	logFields.Debug("injecting sidecar")
	if hasAgent {  
		dep.Spec.Template.Spec.Containers[agentContainerIndex] = container(jaeger, dep, agentContainerIndex)
	} else {
		dep.Spec.Template.Spec.Containers = append(dep.Spec.Template.Spec.Containers, container(jaeger, dep, -1))
	}
	jaegerName := util.Truncate(jaeger.Name, 63)
	if dep.Labels == nil {
		dep.Labels = map[string]string{Label: jaegerName}
	} else {
		dep.Labels[Label] = jaegerName
	}
	return dep
}
func desired(dep *appsv1.Deployment, ns *corev1.Namespace) bool {
	logger := log.WithFields(log.Fields{
		"namespace":  dep.Namespace,
		"deployment": dep.Name,
	})
	depAnnotationValue, depExist := dep.Annotations[Annotation]
	nsAnnotationValue, nsExist := ns.Annotations[Annotation]
	if depExist && !strings.EqualFold(depAnnotationValue, "false") {
		logger.Debug("annotation present on deployment")
		return true
	}
	if nsExist && !strings.EqualFold(nsAnnotationValue, "false") {
		logger.Debug("annotation present on namespace")
		return true
	}
	return false
}
func Needed(dep *appsv1.Deployment, ns *corev1.Namespace) bool {
	if !desired(dep, ns) {
		return false
	}
	if dep.Labels["app"] == "jaeger" && dep.Labels["app.kubernetes.io/component"] != "query" {
		return false
	}
	hasAgent, _ := HasJaegerAgent(dep)
	if hasAgent {
		_, hasLabel := dep.Labels[Label]
		return hasLabel
	}
	return true
}
func Select(target *appsv1.Deployment, ns *corev1.Namespace, availableJaegerPods *v1.JaegerList) *v1.Jaeger {
	jaegerNameDep := target.Annotations[Annotation]
	jaegerNameNs := ns.Annotations[Annotation]
	if jaegerNameDep != "" && !strings.EqualFold(jaegerNameDep, "true") {
		if jaeger := getJaeger(jaegerNameDep, availableJaegerPods); jaeger != nil {
			return jaeger
		}
		return nil
	}
	if jaeger := getJaeger(jaegerNameNs, availableJaegerPods); jaeger != nil {
		return jaeger
	}
	if strings.EqualFold(jaegerNameDep, "true") || strings.EqualFold(jaegerNameNs, "true") {
		if len(availableJaegerPods.Items) == 1 {
			jaeger := &availableJaegerPods.Items[0]
			return jaeger
		}
		instancesInNamespace := getJaegerFromNamespace(target.Namespace, availableJaegerPods)
		if len(instancesInNamespace) == 1 {
			jaeger := instancesInNamespace[0]
			return jaeger
		}
	}
	return nil
}
func getJaegerFromNamespace(namespace string, jaegers *v1.JaegerList) []*v1.Jaeger {
	var instances []*v1.Jaeger
	for _, p := range jaegers.Items {
		if p.Namespace == namespace {
			instances = append(instances, &p)
		}
	}
	return instances
}
func getJaeger(name string, jaegers *v1.JaegerList) *v1.Jaeger {
	for _, p := range jaegers.Items {
		if p.Name == name {
			return &p
		}
	}
	return nil
}
func container(jaeger *v1.Jaeger, dep *appsv1.Deployment, agentIdx int) corev1.Container {
	args := append(jaeger.Spec.Agent.Options.ToArgs())
	if len(util.FindItem("--reporter.grpc.host-port=", args)) == 0 {
		args = append(args, fmt.Sprintf("--reporter.grpc.host-port=dns:///%s.%s.svc:14250", service.GetNameForHeadlessCollectorService(jaeger), jaeger.Namespace))
	}
	if viper.GetString("platform") == v1.FlagPlatformOpenShift {
		if len(util.FindItem("--reporter.grpc.tls.enabled=", args)) == 0 {
			args = append(args, "--reporter.grpc.tls.enabled=true")
			args = append(args, fmt.Sprintf("--reporter.grpc.tls.ca=%s", ca.ServiceCAPath))
		}
	}
	zkCompactTrft := util.GetPort("--processor.zipkin-compact.server-host-port=", args, 5775)
	configRest := util.GetPort("--http-server.host-port=", args, 5778)
	jgCompactTrft := util.GetPort("--processor.jaeger-compact.server-host-port=", args, 6831)
	jgBinaryTrft := util.GetPort("--processor.jaeger-binary.server-host-port=", args, 6832)
	adminPort := util.GetAdminPort(args, 14271)
	if len(util.FindItem("--agent.tags=", args)) == 0 {
		defaultAgentTagsMap := make(map[string]string)
		defaultAgentTagsMap["cluster"] = "undefined"  
		defaultAgentTagsMap["deployment.name"] = dep.Name
		defaultAgentTagsMap["pod.namespace"] = dep.Namespace
		defaultAgentTagsMap["pod.name"] = fmt.Sprintf("${%s:}", envVarPodName)
		defaultAgentTagsMap["host.ip"] = fmt.Sprintf("${%s:}", envVarHostIP)
		defaultContainerName := getContainerName(dep.Spec.Template.Spec.Containers, agentIdx)
		if defaultContainerName != "" {
			defaultAgentTagsMap["container.name"] = defaultContainerName
		}
		if agentIdx > -1 {
			existingAgentTags := parseAgentTags(dep.Spec.Template.Spec.Containers[agentIdx].Args)
			for key, value := range defaultAgentTagsMap {
				existingAgentTags[key] = value
			}
			args = append(args, fmt.Sprintf(`--agent.tags=%s`, joinTags(existingAgentTags)))
		} else {
			args = append(args, fmt.Sprintf(`--agent.tags=%s`, joinTags(defaultAgentTagsMap)))
		}
	}
	commonSpec := util.Merge([]v1.JaegerCommonSpec{jaeger.Spec.Agent.JaegerCommonSpec, jaeger.Spec.JaegerCommonSpec})
	volumesAndMountsSpec := jaeger.Spec.Agent.JaegerCommonSpec
	ca.Update(jaeger, &volumesAndMountsSpec)
	ca.AddServiceCA(jaeger, &volumesAndMountsSpec)
	sort.Strings(args)
	dep.Spec.Template.Spec.ImagePullSecrets = util.RemoveDuplicatedImagePullSecrets(append(dep.Spec.Template.Spec.ImagePullSecrets, jaeger.Spec.Agent.ImagePullSecrets...))
	dep.Spec.Template.Spec.Volumes = util.RemoveDuplicatedVolumes(append(dep.Spec.Template.Spec.Volumes, volumesAndMountsSpec.Volumes...))
	return corev1.Container{
		Image: util.ImageName(jaeger.Spec.Agent.Image, "jaeger-agent-image"),
		Name:  "jaeger-agent",
		Args:  args,
		Env: []corev1.EnvVar{
			{
				Name: envVarPodName,
				ValueFrom: &corev1.EnvVarSource{
					FieldRef: &corev1.ObjectFieldSelector{
						FieldPath: "metadata.name",
					},
				},
			},
			{
				Name: envVarHostIP,
				ValueFrom: &corev1.EnvVarSource{
					FieldRef: &corev1.ObjectFieldSelector{
						FieldPath: "status.hostIP",
					},
				},
			},
		},
		Ports: []corev1.ContainerPort{
			{
				ContainerPort: zkCompactTrft,
				Name:          "zk-compact-trft",
				Protocol:      corev1.ProtocolUDP,
			},
			{
				ContainerPort: configRest,
				Name:          "config-rest",
			},
			{
				ContainerPort: jgCompactTrft,
				Name:          "jg-compact-trft",
				Protocol:      corev1.ProtocolUDP,
			},
			{
				ContainerPort: jgBinaryTrft,
				Name:          "jg-binary-trft",
				Protocol:      corev1.ProtocolUDP,
			},
			{
				ContainerPort: adminPort,
				Name:          "admin-http",
			},
		},
		Resources:       commonSpec.Resources,
		SecurityContext: jaeger.Spec.Agent.SidecarSecurityContext,
		VolumeMounts:    volumesAndMountsSpec.VolumeMounts,
	}
}
func decorate(dep *appsv1.Deployment) {
	app, found := dep.Spec.Template.Labels["app.kubernetes.io/instance"]
	if !found {
		app, found = dep.Spec.Template.Labels["app.kubernetes.io/name"]
	}
	if !found {
		app, found = dep.Spec.Template.Labels["app"]
	}
	if found {
		if len(dep.Namespace) > 0 {
			app += "." + dep.Namespace
		} else {
			app += ".default"
		}
		for i := 0; i < len(dep.Spec.Template.Spec.Containers); i++ {
			if !hasEnv(envVarServiceName, dep.Spec.Template.Spec.Containers[i].Env) {
				dep.Spec.Template.Spec.Containers[i].Env = append(dep.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{
					Name:  envVarServiceName,
					Value: app,
				})
			}
			if !hasEnv(envVarPropagation, dep.Spec.Template.Spec.Containers[i].Env) {
				dep.Spec.Template.Spec.Containers[i].Env = append(dep.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{
					Name:  envVarPropagation,
					Value: "jaeger,b3,w3c",
				})
			}
		}
	}
	for key, value := range PrometheusDefaultAnnotations {
		_, ok := dep.Annotations[key]
		if !ok {
			dep.Annotations[key] = value
		}
	}
}
func hasEnv(name string, vars []corev1.EnvVar) bool {
	for i := 0; i < len(vars); i++ {
		if vars[i].Name == name {
			return true
		}
	}
	return false
}