Skip to content

Commit 979a0a2

Browse files
committed
feat(expand CRDs): Moved comparison into its own function and updated how condition is evaluated for []string
1 parent 3731b38 commit 979a0a2

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

vault.go

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8-
log "github.com/sirupsen/logrus"
8+
"github.com/uswitch/vault-webhook/pkg/apis/vaultwebhook.uswitch.com/v1alpha1"
99
corev1 "k8s.io/api/core/v1"
1010
"k8s.io/apimachinery/pkg/api/resource"
1111
)
@@ -108,32 +108,9 @@ func addVault(pod *corev1.Pod, namespace string, databases []database) (patch []
108108

109109
initContainer := vaultContainer
110110

111-
// TODO: This is likely to be in a function by itself
112-
// Conditionally set Lifecycle if it exists in containerSpec
113-
if vaultContainerSpec.Lifecycle.PreStop.Exec.Command != nil {
114-
vaultContainer.Lifecycle = &corev1.Lifecycle{
115-
PreStop: &corev1.LifecycleHandler{
116-
Exec: &corev1.ExecAction{
117-
Command: vaultContainerSpec.Lifecycle.PreStop.Exec.Command,
118-
},
119-
},
120-
}
121-
}
122-
123-
// Conditionally set Lifecycle if it exists in InitContainerSpec
124-
if len(initVaultContainerSpec.Lifecycle.PreStop.Exec.Command) > 0 {
125-
log.Info("I AM NOT EMPTY")
126-
log.Infof("ContainerSpec Lifecycle Command content: %+v", initVaultContainerSpec.Lifecycle.PreStop.Exec.Command)
127-
log.Infof("ContainerSpec Lifecycle Command type: %T", initVaultContainerSpec.Lifecycle.PreStop.Exec.Command)
128-
// initContainer.Lifecycle = &corev1.Lifecycle{
129-
// PreStop: &corev1.LifecycleHandler{
130-
// Exec: &corev1.ExecAction{
131-
// Command: initVaultContainerSpec.Lifecycle.PreStop.Exec.Command,
132-
// },
133-
// },
134-
// }
135-
}
136-
// TODO: End function to wrap
111+
// Configure Lifecycle Hooks if spec exists
112+
initContainer = addLifecycleHook(initContainer, initVaultContainerSpec)
113+
vaultContainer = addLifecycleHook(vaultContainer, vaultContainerSpec)
137114

138115
jobLikeOwnerReferencesKinds := map[string]bool{"Job": true, "Workflow": true}
139116
if len(pod.ObjectMeta.OwnerReferences) != 0 {
@@ -228,3 +205,17 @@ func appendVolumeMountIfMissing(slice []corev1.VolumeMount, v corev1.VolumeMount
228205
}
229206
return append(slice, v)
230207
}
208+
209+
func addLifecycleHook(container corev1.Container, containerSpec v1alpha1.Container) corev1.Container {
210+
// Conditionally set Lifecycle if it exists in containerSpec
211+
if len(containerSpec.Lifecycle.PreStop.Exec.Command) > 0 {
212+
container.Lifecycle = &corev1.Lifecycle{
213+
PreStop: &corev1.LifecycleHandler{
214+
Exec: &corev1.ExecAction{
215+
Command: containerSpec.Lifecycle.PreStop.Exec.Command,
216+
},
217+
},
218+
}
219+
}
220+
return container
221+
}

0 commit comments

Comments
 (0)