Skip to content

Commit

Permalink
feat(expand CRDs): Add Lifecycle hook in container spec
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoMori8 committed Feb 7, 2025
1 parent cc4c859 commit 2aff79a
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func addVault(pod *corev1.Pod, namespace string, databases []database) (patch []
initContainers := []corev1.Container{}
for _, databaseInfo := range databases {

//* These are fields from the CRD!
vaultContainerSpec := databaseInfo.vaultContainer
initVaultContainerSpec := databaseInfo.initVaultContainer

database := databaseInfo.database
role := databaseInfo.role
serviceAccount := pod.Spec.ServiceAccountName
Expand Down Expand Up @@ -53,7 +55,7 @@ func addVault(pod *corev1.Pod, namespace string, databases []database) (patch []

vaultContainer := corev1.Container{
Image: sidecarImage,
ImagePullPolicy: "Always", // TODO: Change to IfNotPresent? https://kubernetes.io/docs/concepts/containers/images/#image-pull-policy
ImagePullPolicy: "Always",
Resources: corev1.ResourceRequirements{
Requests: requests,
Limits: limits,
Expand Down Expand Up @@ -103,9 +105,32 @@ func addVault(pod *corev1.Pod, namespace string, databases []database) (patch []
},
}

// TODO: remember not to have preStop hook in the init container.
initContainer := vaultContainer

// TODO: This is likely to be in a function by itself
// Conditionally set Lifecycle if it exists in containerSpec
if vaultContainerSpec.Lifecycle.PreStop.Exec.Command != nil {
vaultContainer.Lifecycle = &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: vaultContainerSpec.Lifecycle.PreStop.Exec.Command,
},
},
}
}

// Conditionally set Lifecycle if it exists in InitContainerSpec
if initVaultContainerSpec.Lifecycle.PreStop.Exec.Command != nil {
initContainer.Lifecycle = &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: initVaultContainerSpec.Lifecycle.PreStop.Exec.Command,
},
},
}
}
// TODO: End function to wrap

jobLikeOwnerReferencesKinds := map[string]bool{"Job": true, "Workflow": true}
if len(pod.ObjectMeta.OwnerReferences) != 0 {
ownerKind := pod.ObjectMeta.OwnerReferences[0].Kind
Expand Down

0 comments on commit 2aff79a

Please sign in to comment.