|
5 | 5 | "fmt" |
6 | 6 | "strings" |
7 | 7 |
|
8 | | - log "github.com/sirupsen/logrus" |
| 8 | + "github.com/uswitch/vault-webhook/pkg/apis/vaultwebhook.uswitch.com/v1alpha1" |
9 | 9 | corev1 "k8s.io/api/core/v1" |
10 | 10 | "k8s.io/apimachinery/pkg/api/resource" |
11 | 11 | ) |
@@ -108,32 +108,9 @@ func addVault(pod *corev1.Pod, namespace string, databases []database) (patch [] |
108 | 108 |
|
109 | 109 | initContainer := vaultContainer |
110 | 110 |
|
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) |
137 | 114 |
|
138 | 115 | jobLikeOwnerReferencesKinds := map[string]bool{"Job": true, "Workflow": true} |
139 | 116 | if len(pod.ObjectMeta.OwnerReferences) != 0 { |
@@ -228,3 +205,17 @@ func appendVolumeMountIfMissing(slice []corev1.VolumeMount, v corev1.VolumeMount |
228 | 205 | } |
229 | 206 | return append(slice, v) |
230 | 207 | } |
| 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