Skip to content

Commit 86edf36

Browse files
committed
fix(expand CRDs): Improved checks by using struct methods
1 parent 08cc8b6 commit 86edf36

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

pkg/apis/vaultwebhook.uswitch.com/v1alpha1/types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,27 @@ type DatabaseCredentialBindingList struct {
3737
type Container struct {
3838
Lifecycle corev1.Lifecycle `json:"lifecycle,omitempty"`
3939
}
40+
41+
/*
42+
Check if Container.Lifecycle.PreStop is valid.
43+
This is to avoid mishandling incomplete inputs like the below:
44+
45+
https://pkg.go.dev/k8s.io/api/core/v1#LifecycleHandler
46+
{
47+
48+
"Lifecycle": {
49+
"PostStart": null,
50+
"PreStop": {
51+
"Exec": null, # <----- Missing Command!!
52+
"HTTPGet": null,
53+
"TCPSocket": null
54+
}
55+
}
56+
57+
}
58+
*/
59+
func (c Container) HasValidPreStop() bool {
60+
return c.Lifecycle.PreStop != nil &&
61+
c.Lifecycle.PreStop.Exec != nil &&
62+
len(c.Lifecycle.PreStop.Exec.Command) > 0
63+
}

vault.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,15 @@ func appendVolumeMountIfMissing(slice []corev1.VolumeMount, v corev1.VolumeMount
210210
// Conditionally set Lifecycle if it exists in containerSpec
211211
func addLifecycleHook(container corev1.Container, containerSpec v1alpha1.Container) corev1.Container {
212212

213+
// Check DatabaseCredentialBindingSpec.Container.Lifecycle is not empty
213214
emptyLifecycle := corev1.Lifecycle{}
214215
if containerSpec.Lifecycle != emptyLifecycle {
215-
container.Lifecycle = &containerSpec.Lifecycle
216+
217+
// Check for a complete PreStop hook
218+
if containerSpec.HasValidPreStop() {
219+
container.Lifecycle = &containerSpec.Lifecycle
220+
}
221+
216222
}
217223
// TODO: Fix support for initcontainer's Lifecycle hooks ( Go dep to be updated )
218224
// Init containers must have RestartPolicy=Always to be able to support Lifecycle hooks

0 commit comments

Comments
 (0)