Skip to content

Commit

Permalink
feat: add overwriting container spec for deployment and job
Browse files Browse the repository at this point in the history
!fixes #68
  • Loading branch information
TrayserCassa committed Dec 20, 2024
1 parent 8e485d6 commit 2645d25
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 14 deletions.
93 changes: 90 additions & 3 deletions api/v1/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1

import (
"maps"

autoscalerv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -28,8 +30,15 @@ type StoreList struct {
}

type StoreSpec struct {
Database DatabaseSpec `json:"database"`
Container ContainerSpec `json:"container"`
Database DatabaseSpec `json:"database"`

Container ContainerSpec `json:"container"`
AdminDeploymentContainer ContainerMergeSpec `json:"adminDeploymentContainer,omitempty"`
WorkerDeploymentContainer ContainerMergeSpec `json:"workerDeploymentContainer,omitempty"`
StorefrontDeploymentContainer ContainerMergeSpec `json:"storefrontDeploymentContainer,omitempty"`
SetupJobContainer ContainerMergeSpec `json:"setupJobContainer,omitempty"`
MigrationJobContainer ContainerMergeSpec `json:"migrationJobContainer,omitempty"`

Network NetworkSpec `json:"network,omitempty"`
S3Storage S3Storage `json:"s3Storage,omitempty"`
CDNURL string `json:"cdnURL"`
Expand Down Expand Up @@ -138,9 +147,9 @@ type ContainerSpec struct {
// ReadinessProbe corev1.Probe `json:"readinessProbe,omitempty"`
// LivenessProbe corev1.Probe `json:"livenessProbe,omitempty"`

NodeSelector map[string]string `json:"nodeSelector,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
Affinity corev1.Affinity `json:"affinity,omitempty"`
Expand All @@ -159,6 +168,30 @@ type ContainerSpec struct {
ExtraEnvs []corev1.EnvVar `json:"extraEnvs,omitempty"`
}

type ContainerMergeSpec struct {
// +kubebuilder:validation:MinLength=1
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Volumes []corev1.Volume `json:"volumes,omitempty"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
RestartPolicy corev1.RestartPolicy `json:"restartPolicy,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"podSecurityContext,omitempty"`
ExtraContainers []corev1.Container `json:"extraContainers,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
ProgressDeadlineSeconds int32 `json:"progressDeadlineSeconds,omitempty"`

Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
Affinity corev1.Affinity `json:"affinity,omitempty"`

Resources corev1.ResourceRequirements `json:"resources,omitempty"`
ExtraEnvs []corev1.EnvVar `json:"extraEnvs,omitempty"`
}

type SessionCacheSpec struct {
RedisSpec `json:",inline"`

Expand Down Expand Up @@ -274,3 +307,57 @@ type SecretRef struct {
func (s *Store) GetSecretName() string {
return s.Spec.SecretName
}

func (c *ContainerSpec) Merge(from ContainerMergeSpec) {
if from.Image != "" {
c.Image = from.Image
}
if from.ImagePullPolicy != "" {
c.ImagePullPolicy = from.ImagePullPolicy
}
if from.Replicas != 0 {
c.Replicas = from.Replicas
}
if from.ProgressDeadlineSeconds != 0 {
c.ProgressDeadlineSeconds = from.ProgressDeadlineSeconds
}
if from.RestartPolicy != "" {
c.RestartPolicy = from.RestartPolicy
}
if from.ExtraEnvs != nil {
c.ExtraEnvs = from.ExtraEnvs
}
if from.VolumeMounts != nil {
c.VolumeMounts = from.VolumeMounts
}
if from.ImagePullSecrets != nil {
c.ImagePullSecrets = from.ImagePullSecrets
}
if from.Volumes != nil {
c.Volumes = from.Volumes
}
if from.Resources.Requests != nil {
c.Resources.Requests = from.Resources.Requests
}
if from.Resources.Limits != nil {
c.Resources.Limits = from.Resources.Limits
}
if from.ExtraContainers != nil {
c.ExtraContainers = from.ExtraContainers
}
if from.NodeSelector != nil {
c.NodeSelector = from.NodeSelector
}
if from.TopologySpreadConstraints != nil {
c.TopologySpreadConstraints = from.TopologySpreadConstraints
}
if from.Tolerations != nil {
c.Tolerations = from.Tolerations
}
if from.Annotations != nil {
maps.Copy(c.Annotations, from.Annotations)
}
if from.Labels != nil {
maps.Copy(c.Labels, from.Labels)
}
}
125 changes: 119 additions & 6 deletions api/v1/zz_generated.deepcopy.go

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

8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ metadata:
name: manager-role
namespace: default
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
8 changes: 7 additions & 1 deletion internal/deployment/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ func GetAdminDeployment(
return search, err
}

func AdminDeployment(store *v1.Store) *appsv1.Deployment {
func AdminDeployment(st *v1.Store) *appsv1.Deployment {
store := st.DeepCopy()

appName := "shopware-admin"
labels := map[string]string{
"app": appName,
}
maps.Copy(labels, util.GetDefaultLabels(store))

// Merge Overwritten adminContainer fields into container fields
store.Spec.Container.Merge(store.Spec.AdminDeploymentContainer)
maps.Copy(labels, store.Spec.Container.Labels)

containers := append(store.Spec.Container.ExtraContainers, corev1.Container{
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Expand Down
8 changes: 7 additions & 1 deletion internal/deployment/storefront.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ func GetStorefrontDeployment(
return search, err
}

func StorefrontDeployment(store *v1.Store) *appsv1.Deployment {
func StorefrontDeployment(st *v1.Store) *appsv1.Deployment {
store := st.DeepCopy()

appName := "shopware-storefront"
labels := map[string]string{
"app": appName,
}
maps.Copy(labels, util.GetDefaultLabels(store))

// Merge Overwritten storefrontContainer fields into container fields
store.Spec.Container.Merge(store.Spec.StorefrontDeploymentContainer)
maps.Copy(labels, store.Spec.Container.Labels)

containers := append(store.Spec.Container.ExtraContainers, corev1.Container{
Name: DEPLOYMENT_STOREFRONT_CONTAINER_NAME,
LivenessProbe: &corev1.Probe{
Expand Down
8 changes: 7 additions & 1 deletion internal/deployment/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ func GetWorkerDeployment(
return search, err
}

func WorkerDeployment(store *v1.Store) *appsv1.Deployment {
func WorkerDeployment(st *v1.Store) *appsv1.Deployment {
store := st.DeepCopy()

appName := "shopware-worker"
labels := map[string]string{
"app": appName,
}
maps.Copy(labels, util.GetDefaultLabels(store))

// Merge Overwritten storefrontContainer fields into container fields
store.Spec.Container.Merge(store.Spec.WorkerDeploymentContainer)
maps.Copy(labels, store.Spec.Container.Labels)

containers := append(store.Spec.Container.ExtraContainers, corev1.Container{
Name: appName,
Image: store.Spec.Container.Image,
Expand Down
Loading

0 comments on commit 2645d25

Please sign in to comment.