func()

in operator/pkg/operator/injector/injector.go [95:147]


func (s *SidecarInjectField) Inject(pod *corev1.Pod) {
	log.Info(fmt.Sprintf("inject pod : %s", pod.GenerateName))

	// add initcontrainers to spec
	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 spec
	if pod.Spec.Volumes == nil {
		pod.Spec.Volumes = []corev1.Volume{}
	}
	pod.Spec.Volumes = append(pod.Spec.Volumes, s.SidecarVolume)

	// choose a specific container to inject
	targetContainers := s.findInjectContainer(pod.Spec.Containers)

	// add volumemount and env to container
	for i := range targetContainers {
		log.Info(fmt.Sprintf("inject container : %s", targetContainers[i].Name))
		if (*targetContainers[i]).VolumeMounts == nil {
			(*targetContainers[i]).VolumeMounts = []corev1.VolumeMount{}
		}

		(*targetContainers[i]).VolumeMounts = append((*targetContainers[i]).VolumeMounts, s.SidecarVolumeMount)

		if (*targetContainers[i]).Env != nil {
			(*targetContainers[i]).Env = append((*targetContainers[i]).Env, s.Env)
		} else {
			(*targetContainers[i]).Env = []corev1.EnvVar{s.Env}
		}

		// envs to be append
		var envsTBA []corev1.EnvVar
		for j, envInject := range s.Envs {
			isExists := false
			for _, envExists := range targetContainers[i].Env {
				if strings.EqualFold(envExists.Name, envInject.Name) {
					isExists = true
					break
				}
			}
			if !isExists {
				envsTBA = append(envsTBA, s.Envs[j])
			}
		}
		if len(s.Envs) > 0 {
			(*targetContainers[i]).Env = append((*targetContainers[i]).Env, envsTBA...)
		}
	}
}