Skip to content

Commit

Permalink
Add custom sidecar containers pod extension
Browse files Browse the repository at this point in the history
This new pod extension allows for additional containers to be added
to the primary Mattermost app pod.
  • Loading branch information
gabrieljackson committed Jan 9, 2024
1 parent 829dfe8 commit 5a4fa0a
Show file tree
Hide file tree
Showing 8 changed files with 3,335 additions and 504 deletions.
8 changes: 6 additions & 2 deletions apis/mattermost/v1beta1/mattermost_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,15 @@ type UpdateJob struct {

// PodExtensions specify customized extensions for a pod.
type PodExtensions struct {
// Additional InitContainers injected to pods.
// Additional InitContainers injected into pods.
// The setting does not override InitContainers defined by the Operator.
InitContainers []v1.Container `json:"initContainers,omitempty"`

// Additional Container Ports injected to pod's main container.
// Additional sidecar containers injected into pods.
// The setting does not override Sidecar containers defined by the Operator.
SidecarContainers []v1.Container `json:"sidecarContainers,omitempty"`

// Additional Container Ports injected into pod's main container.
// The setting does not override ContainerPorts defined by the Operator.
ContainerPorts []v1.ContainerPort `json:"containerPorts,omitempty"`
}
Expand Down
7 changes: 7 additions & 0 deletions apis/mattermost/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,276 changes: 1,273 additions & 3 deletions config/crd/bases/installation.mattermost.com_mattermosts.yaml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions controllers/mattermost/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ func (r *MattermostReconciler) checkMattermostDeployment(
mattermost.GetImageName(),
)

reqLogger.Info("SC: %+v", mattermost.Spec.PodExtensions.SidecarContainers)
reqLogger.Info("CONTAINERS: %+v", desired.Spec.Template.Spec.Containers)

patchedObj, applied, err := mattermost.Spec.ResourcePatch.ApplyToDeployment(desired)
if err != nil {
reqLogger.Error(err, "Failed to patch deployment")
Expand Down
2,445 changes: 1,971 additions & 474 deletions docs/mattermost-operator/mattermost-operator.yaml

Large diffs are not rendered by default.

55 changes: 31 additions & 24 deletions pkg/mattermost/mattermost_v1beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,28 @@ func GenerateDeploymentV1Beta(mattermost *mmv1beta.Mattermost, db DatabaseConfig
revisionHistoryLimit = mattermost.Spec.DeploymentTemplate.RevisionHistoryLimit
}

containers := []corev1.Container{
{
Name: mattermostv1alpha1.MattermostAppContainerName,
Image: containerImage,
ImagePullPolicy: mattermost.Spec.ImagePullPolicy,
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
Command: []string{"mattermost"},
Env: envVars,
Ports: containerPorts,
ReadinessProbe: readiness,
LivenessProbe: liveness,
VolumeMounts: volumeMounts,
Resources: mattermost.Spec.Scheduling.Resources,
SecurityContext: containerSecurityContext,
},
}

// Final container extensions
if mattermost.Spec.PodExtensions.SidecarContainers != nil {
containers = append(containers, mattermost.Spec.PodExtensions.SidecarContainers...)
}

return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: deploymentName,
Expand Down Expand Up @@ -394,30 +416,15 @@ func GenerateDeploymentV1Beta(mattermost *mmv1beta.Mattermost, db DatabaseConfig
Spec: corev1.PodSpec{
ServiceAccountName: serviceAccountName,
InitContainers: initContainers,
Containers: []corev1.Container{
{
Name: mattermostv1alpha1.MattermostAppContainerName,
Image: containerImage,
ImagePullPolicy: mattermost.Spec.ImagePullPolicy,
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
Command: []string{"mattermost"},
Env: envVars,
Ports: containerPorts,
ReadinessProbe: readiness,
LivenessProbe: liveness,
VolumeMounts: volumeMounts,
Resources: mattermost.Spec.Scheduling.Resources,
SecurityContext: containerSecurityContext,
},
},
ImagePullSecrets: mattermost.Spec.ImagePullSecrets,
Volumes: volumes,
DNSConfig: mattermost.Spec.DNSConfig,
DNSPolicy: mattermost.Spec.DNSPolicy,
Affinity: mattermost.Spec.Scheduling.Affinity,
NodeSelector: mattermost.Spec.Scheduling.NodeSelector,
Tolerations: mattermost.Spec.Scheduling.Tolerations,
SecurityContext: podSecurityContext,
Containers: containers,
ImagePullSecrets: mattermost.Spec.ImagePullSecrets,
Volumes: volumes,
DNSConfig: mattermost.Spec.DNSConfig,
DNSPolicy: mattermost.Spec.DNSPolicy,
Affinity: mattermost.Spec.Scheduling.Affinity,
NodeSelector: mattermost.Spec.Scheduling.NodeSelector,
Tolerations: mattermost.Spec.Scheduling.Tolerations,
SecurityContext: podSecurityContext,
},
},
},
Expand Down
45 changes: 44 additions & 1 deletion pkg/mattermost/mattermost_v1beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,6 @@ func TestGenerateDeployment_V1Beta(t *testing.T) {
description string
mmSpec mmv1beta.MattermostSpec
dbConfig DatabaseConfig
fileStoreConfig FileStoreConfig
expectedInitContainers []corev1.Container
}{
{
Expand Down Expand Up @@ -887,6 +886,50 @@ func TestGenerateDeployment_V1Beta(t *testing.T) {
}
})

t.Run("custom sidecar container pod extension", func(t *testing.T) {
dbConfig := &ExternalDBConfig{dbType: database.PostgreSQLDatabase, hasDBCheckURL: false}
customSideBarContainers := []corev1.Container{
{Image: "my-log-exporter-image", Name: "log-exporter"},
{Image: "my-audit-image", Name: "audit"},
}

for _, testCase := range []struct {
description string
mmSpec mmv1beta.MattermostSpec
expectedSidecarContainers []corev1.Container
}{
{
description: "no custom sidebar containers",
mmSpec: mmv1beta.MattermostSpec{
PodExtensions: mmv1beta.PodExtensions{
SidecarContainers: nil,
},
},
expectedSidecarContainers: nil,
},
{
description: "custom sidebar containers",
mmSpec: mmv1beta.MattermostSpec{
PodExtensions: mmv1beta.PodExtensions{
SidecarContainers: customSideBarContainers,
},
},
expectedSidecarContainers: customSideBarContainers,
},
} {
t.Run(testCase.description, func(t *testing.T) {
mattermost := &mmv1beta.Mattermost{
Spec: testCase.mmSpec,
}
deployment := GenerateDeploymentV1Beta(mattermost, dbConfig, &ExternalFileStore{}, "", "", "", "image")
require.Equal(t, len(testCase.expectedSidecarContainers), len(deployment.Spec.Template.Spec.Containers)-1)
if testCase.mmSpec.PodExtensions.SidecarContainers != nil {
assert.Equal(t, testCase.expectedSidecarContainers, deployment.Spec.Template.Spec.Containers[1:])
}
})
}
})

t.Run("should set SiteURL env if ingress host provided", func(t *testing.T) {
mattermost := &mmv1beta.Mattermost{
Spec: mmv1beta.MattermostSpec{},
Expand Down
Empty file modified vendor/k8s.io/code-generator/generate-groups.sh
100644 → 100755
Empty file.

0 comments on commit 5a4fa0a

Please sign in to comment.