Skip to content

Commit

Permalink
pkg/alertmanager: mergepatch alertmanager containers
Browse files Browse the repository at this point in the history
Instead of just appending user defined containers to the alertmanager
pods, use k8sutil.MergePatchContainers so that the default containers
can be modified.
  • Loading branch information
pgier authored and s-urbaniak committed Jun 15, 2020
1 parent b6601c8 commit 847d66c
Showing 1 changed file with 53 additions and 46 deletions.
99 changes: 53 additions & 46 deletions pkg/alertmanager/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,53 @@ func makeStatefulSetSpec(a *monitoringv1.Alertmanager, config Config) (*appsv1.S
terminationGracePeriod := int64(120)
finalLabels := config.Labels.Merge(podLabels)

// PodManagementPolicy is set to Parallel to mitigate issues in kuberentes: https://github.com/kubernetes/kubernetes/issues/60164
defaultContainers := []v1.Container{
{
Args: amArgs,
Name: "alertmanager",
Image: image,
Ports: ports,
VolumeMounts: amVolumeMounts,
LivenessProbe: livenessProbe,
ReadinessProbe: readinessProbe,
Resources: a.Spec.Resources,
Env: []v1.EnvVar{
{
// Necessary for '--cluster.listen-address' flag
Name: "POD_IP",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "status.podIP",
},
},
},
},
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
}, {
Name: "config-reloader",
Image: config.ConfigReloaderImage,
Args: []string{
fmt.Sprintf("-webhook-url=%s", localReloadURL),
fmt.Sprintf("-volume-dir=%s", alertmanagerConfDir),
},
VolumeMounts: []v1.VolumeMount{
{
Name: "config-volume",
ReadOnly: true,
MountPath: alertmanagerConfDir,
},
},
Resources: resources,
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
},
}

containers, err := k8sutil.MergePatchContainers(defaultContainers, a.Spec.Containers)
if err != nil {
return nil, errors.Wrap(err, "failed to merge containers spec")
}

// PodManagementPolicy is set to Parallel to mitigate issues in kubernetes: https://github.com/kubernetes/kubernetes/issues/60164
// This is also mentioned as one of limitations of StatefulSets: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#limitations
return &appsv1.StatefulSetSpec{
ServiceName: governingServiceName,
Expand All @@ -490,51 +536,12 @@ func makeStatefulSetSpec(a *monitoringv1.Alertmanager, config Config) (*appsv1.S
PriorityClassName: a.Spec.PriorityClassName,
TerminationGracePeriodSeconds: &terminationGracePeriod,
InitContainers: a.Spec.InitContainers,
Containers: append([]v1.Container{
{
Args: amArgs,
Name: "alertmanager",
Image: image,
Ports: ports,
VolumeMounts: amVolumeMounts,
LivenessProbe: livenessProbe,
ReadinessProbe: readinessProbe,
Resources: a.Spec.Resources,
Env: []v1.EnvVar{
{
// Necessary for '--cluster.listen-address' flag
Name: "POD_IP",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "status.podIP",
},
},
},
},
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
}, {
Name: "config-reloader",
Image: config.ConfigReloaderImage,
Args: []string{
fmt.Sprintf("-webhook-url=%s", localReloadURL),
fmt.Sprintf("-volume-dir=%s", alertmanagerConfDir),
},
VolumeMounts: []v1.VolumeMount{
{
Name: "config-volume",
ReadOnly: true,
MountPath: alertmanagerConfDir,
},
},
Resources: resources,
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
},
}, a.Spec.Containers...),
Volumes: volumes,
ServiceAccountName: a.Spec.ServiceAccountName,
SecurityContext: securityContext,
Tolerations: a.Spec.Tolerations,
Affinity: a.Spec.Affinity,
Containers: containers,
Volumes: volumes,
ServiceAccountName: a.Spec.ServiceAccountName,
SecurityContext: securityContext,
Tolerations: a.Spec.Tolerations,
Affinity: a.Spec.Affinity,
},
},
}, nil
Expand Down

0 comments on commit 847d66c

Please sign in to comment.