Skip to content

Commit

Permalink
kuberesource: allow mutation of nested pod specs
Browse files Browse the repository at this point in the history
  • Loading branch information
malt3 committed Apr 25, 2024
1 parent 0512b78 commit 066e795
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/kuberesource/mutators.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"

applyappsv1 "k8s.io/client-go/applyconfigurations/apps/v1"
applybatchv1 "k8s.io/client-go/applyconfigurations/batch/v1"
applycorev1 "k8s.io/client-go/applyconfigurations/core/v1"
)

Expand Down Expand Up @@ -93,3 +94,27 @@ func AddServiceMesh(
)
return deployment, nil
}

// MapPodSpec applies a function to a PodSpec in a Kubernetes resource.
func MapPodSpec(resource any, f func(spec *applycorev1.PodSpecApplyConfiguration) *applycorev1.PodSpecApplyConfiguration) any {
if resource == nil {
return nil
}
switch r := resource.(type) {
case *applyappsv1.DaemonSetApplyConfiguration:
r.Spec.Template.Spec = f(r.Spec.Template.Spec)
case *applyappsv1.DeploymentApplyConfiguration:
r.Spec.Template.Spec = f(r.Spec.Template.Spec)
case *applybatchv1.JobApplyConfiguration:
r.Spec.Template.Spec = f(r.Spec.Template.Spec)
case *applycorev1.PodApplyConfiguration:
r.Spec = f(r.Spec)
case *applyappsv1.ReplicaSetApplyConfiguration:
r.Spec.Template.Spec = f(r.Spec.Template.Spec)
case *applycorev1.ReplicationControllerApplyConfiguration:
r.Spec.Template.Spec = f(r.Spec.Template.Spec)
case *applyappsv1.StatefulSetApplyConfiguration:
r.Spec.Template.Spec = f(r.Spec.Template.Spec)
}
return resource
}

0 comments on commit 066e795

Please sign in to comment.