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: handle injected resources without own labels, add injector tests #20

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
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
71 changes: 70 additions & 1 deletion mutation/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ func (m *mutatorImpl) MutateAppsV1DaemonSet(daemonSet *appsv1.DaemonSet) error {
return err
}

if daemonSet.ObjectMeta.Labels == nil {
daemonSet.ObjectMeta.Labels = map[string]string{}
}

daemonSet.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

if daemonSet.Spec.Template.ObjectMeta.Labels == nil {
daemonSet.Spec.Template.ObjectMeta.Labels = map[string]string{}
}

daemonSet.Spec.Template.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

return nil
Expand All @@ -93,7 +102,16 @@ func (m *mutatorImpl) MutateAppsV1Deployment(deployment *appsv1.Deployment) erro
return err
}

if deployment.ObjectMeta.Labels == nil {
deployment.ObjectMeta.Labels = map[string]string{}
}

deployment.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

if deployment.Spec.Template.ObjectMeta.Labels == nil {
deployment.Spec.Template.ObjectMeta.Labels = map[string]string{}
}

deployment.Spec.Template.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

return nil
Expand All @@ -108,7 +126,16 @@ func (m *mutatorImpl) MutateAppsV1ReplicaSet(replicaSet *appsv1.ReplicaSet) erro
return err
}

if replicaSet.ObjectMeta.Labels == nil {
replicaSet.ObjectMeta.Labels = map[string]string{}
}

replicaSet.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

if replicaSet.Spec.Template.ObjectMeta.Labels == nil {
replicaSet.Spec.Template.ObjectMeta.Labels = map[string]string{}
}

replicaSet.Spec.Template.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

return nil
Expand All @@ -123,7 +150,16 @@ func (m *mutatorImpl) MutateAppsV1StatefulSet(statefulSet *appsv1.StatefulSet) e
return err
}

if statefulSet.ObjectMeta.Labels == nil {
statefulSet.ObjectMeta.Labels = map[string]string{}
}

statefulSet.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

if statefulSet.Spec.Template.ObjectMeta.Labels == nil {
statefulSet.Spec.Template.ObjectMeta.Labels = map[string]string{}
}

statefulSet.Spec.Template.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

return nil
Expand All @@ -138,7 +174,16 @@ func (m *mutatorImpl) MutateBatchV1CronJob(batchJob *batchv1.CronJob) error {
return err
}

if batchJob.ObjectMeta.Labels == nil {
batchJob.ObjectMeta.Labels = map[string]string{}
}

batchJob.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

if batchJob.Spec.JobTemplate.Spec.Template.Labels == nil {
batchJob.Spec.JobTemplate.Spec.Template.Labels = map[string]string{}
}

batchJob.Spec.JobTemplate.Spec.Template.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

return nil
Expand All @@ -153,7 +198,16 @@ func (m *mutatorImpl) MutateBatchV1Job(job *batchv1.Job) error {
return err
}

if job.ObjectMeta.Labels == nil {
job.ObjectMeta.Labels = map[string]string{}
}

job.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

if job.Spec.Template.ObjectMeta.Labels == nil {
job.Spec.Template.ObjectMeta.Labels = map[string]string{}
}

job.Spec.Template.ObjectMeta.Labels[LumigoAutoTraceLabelKey] = m.lumigoAutotraceLabelVersion

return nil
Expand All @@ -180,6 +234,10 @@ func (m *mutatorImpl) mutatePodSpec(podSpec *corev1.PodSpec) error {
}

volumes := podSpec.Volumes
if volumes == nil {
volumes = []corev1.Volume{}
}

lumigoInjectorVolumeIndex := slices.IndexFunc(podSpec.Volumes, func(c corev1.Volume) bool { return c.Name == "lumigo-injector" })
if lumigoInjectorVolumeIndex < 0 {
volumes = append(volumes, *lumigoInjectorVolume)
Expand Down Expand Up @@ -207,6 +265,10 @@ func (m *mutatorImpl) mutatePodSpec(podSpec *corev1.PodSpec) error {
}

initContainers := podSpec.InitContainers
if initContainers == nil {
initContainers = []corev1.Container{}
}

lumigoInjectorContainerIndex := slices.IndexFunc(initContainers, func(c corev1.Container) bool { return c.Name == "lumigo-injector" })
if lumigoInjectorContainerIndex < 0 {
initContainers = append(initContainers, *lumigoInjectorContainer)
Expand All @@ -215,7 +277,7 @@ func (m *mutatorImpl) mutatePodSpec(podSpec *corev1.PodSpec) error {
}
podSpec.InitContainers = initContainers

patchedContainers := make([]corev1.Container, 0)
patchedContainers := []corev1.Container{}
for _, container := range podSpec.Containers {
lumigoInjectorVolumeMount := &corev1.VolumeMount{
Name: "lumigo-injector",
Expand All @@ -224,6 +286,10 @@ func (m *mutatorImpl) mutatePodSpec(podSpec *corev1.PodSpec) error {
}

volumeMounts := container.VolumeMounts
if volumeMounts == nil {
volumeMounts = []corev1.VolumeMount{}
}

lumigoInjectorVolumeMountIndex := slices.IndexFunc(volumeMounts, func(c corev1.VolumeMount) bool { return c.MountPath == lumigoInjectorVolumeMountPoint })
if lumigoInjectorVolumeMountIndex < 0 {
volumeMounts = append(volumeMounts, *lumigoInjectorVolumeMount)
Expand All @@ -233,6 +299,9 @@ func (m *mutatorImpl) mutatePodSpec(podSpec *corev1.PodSpec) error {
container.VolumeMounts = volumeMounts

envVars := container.Env
if envVars == nil {
envVars = []corev1.EnvVar{}
}

ldPreloadEnvVar := &corev1.EnvVar{
Name: "LD_PRELOAD",
Expand Down
10 changes: 4 additions & 6 deletions webhooks/injector/injector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (h *LumigoInjectorWebhookHandler) Handle(ctx context.Context, request admis
return admission.Allowed(fmt.Sprintf("Tracing injection is disabled in the '%s' namespace; resource will not be mutated", namespace))
}

if err := validateMutationShouldOccur(&lumigo); err != nil {
if err := h.validateMutationShouldOccur(&lumigo); err != nil {
// If we find a reason why the mutation should not occur, pass that as reason why the admission is allowed without modifications
return admission.Allowed(err.Error())
}
Expand Down Expand Up @@ -294,19 +294,17 @@ func newResourceAdatper(gvk metav1.GroupVersionKind, raw []byte) (resourceAdapte

}

func validateMutationShouldOccur(lumigo *operatorv1alpha1.Lumigo) error {
func (h *LumigoInjectorWebhookHandler) validateMutationShouldOccur(lumigo *operatorv1alpha1.Lumigo) error {
namespace := lumigo.ObjectMeta.Namespace

status := &lumigo.Status

// Check if the Lumigo status is active (so the injection _could_ be performed)
activeCondition := conditions.GetLumigoConditionByType(status, operatorv1alpha1.LumigoConditionTypeActive)
if activeCondition == nil || activeCondition.Status != corev1.ConditionTrue {
if activeCondition := conditions.GetLumigoConditionByType(status, operatorv1alpha1.LumigoConditionTypeActive); activeCondition == nil || activeCondition.Status != corev1.ConditionTrue {
return fmt.Errorf("the Lumigo object in the '%s' namespace is not active; resource will not be mutated", namespace)
}

errorCondition := conditions.GetLumigoConditionByType(status, operatorv1alpha1.LumigoConditionTypeError)
if errorCondition != nil && errorCondition.Status == corev1.ConditionTrue {
if errorCondition := conditions.GetLumigoConditionByType(status, operatorv1alpha1.LumigoConditionTypeError); errorCondition != nil && errorCondition.Status == corev1.ConditionTrue {
return fmt.Errorf("the Lumigo object in the '%s' namespace is in an erroneous status; resource will not be mutated", namespace)
}

Expand Down
Loading