Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation to change init container order #91

Merged
merged 5 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions agent-inject/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Agent struct {
// in a pod request.
Inject bool

// InitFirst controls whether an init container is first to run.
InitFirst bool

// LimitsCPU is the upper CPU limit the sidecar container is allowed to consume.
LimitsCPU string

Expand Down Expand Up @@ -206,6 +209,11 @@ func New(pod *corev1.Pod, patches []*jsonpatch.JsonPatchOperation) (*Agent, erro
return agent, err
}

agent.InitFirst, err = agent.initFirst()
if err != nil {
return agent, err
}

agent.PrePopulate, err = agent.prePopulate()
if err != nil {
return agent, err
Expand Down Expand Up @@ -309,10 +317,27 @@ func (a *Agent) Patch() ([]byte, error) {
if err != nil {
return patches, err
}
a.Patches = append(a.Patches, addContainers(
a.Pod.Spec.InitContainers,
[]corev1.Container{container},
"/spec/initContainers")...)

// Init Containers run sequentially in Kubernetes and sometimes the order in
// which they run matters. This reorders the init containers to put the agent first.
if a.InitFirst {

// Remove all init containers from the document so we can readd them after the agent.
a.Patches = append(a.Patches, removeContainers("/spec/initContainers")...)

containers := []corev1.Container{container}
containers = append(containers, a.Pod.Spec.InitContainers...)

a.Patches = append(a.Patches, addContainers(
[]corev1.Container{},
containers,
"/spec/initContainers")...)
} else {
a.Patches = append(a.Patches, addContainers(
a.Pod.Spec.InitContainers,
[]corev1.Container{container},
"/spec/initContainers")...)
}
}

// Sidecar Container
Expand Down
13 changes: 13 additions & 0 deletions agent-inject/agent/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ const (
// originated from.
AnnotationAgentRequestNamespace = "vault.hashicorp.com/agent-request-namespace"

// AnnotationAgentInitFirst makes the initialization container the first container
// to run when a pod starts. Default is last.
AnnotationAgentInitFirst = "vault.hashicorp.com/agent-init-first"

// AnnotationAgentPrePopulate controls whether an init container is included
// to pre-populate the shared memory volume with secrets prior to the application
// starting.
Expand Down Expand Up @@ -290,6 +294,15 @@ func (a *Agent) inject() (bool, error) {
return strconv.ParseBool(raw)
}

func (a *Agent) initFirst() (bool, error) {
raw, ok := a.Annotations[AnnotationAgentInitFirst]
if !ok {
return false, nil
}

return strconv.ParseBool(raw)
}

func (a *Agent) prePopulate() (bool, error) {
raw, ok := a.Annotations[AnnotationAgentPrePopulate]
if !ok {
Expand Down
9 changes: 9 additions & 0 deletions agent-inject/agent/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func addVolumeMounts(target, mounts []corev1.VolumeMount, base string) []*jsonpa
return result
}

func removeContainers(path string) []*jsonpatch.JsonPatchOperation {
var result []*jsonpatch.JsonPatchOperation

return append(result, &jsonpatch.JsonPatchOperation{
Operation: "remove",
Path: path,
})
}

func addContainers(target, containers []corev1.Container, base string) []*jsonpatch.JsonPatchOperation {
var result []*jsonpatch.JsonPatchOperation
first := len(target) == 0
Expand Down
45 changes: 45 additions & 0 deletions agent-inject/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,51 @@ func TestHandlerHandle(t *testing.T) {
},
},

{
"init first ",
Handler{VaultAddress: "https://vault:8200", VaultAuthPath: "kubernetes", ImageVault: "vault", Log: hclog.Default().Named("handler")},
v1beta1.AdmissionRequest{
Namespace: "test",
Object: encodeRaw(t, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
agent.AnnotationAgentInject: "true",
agent.AnnotationVaultRole: "demo",
agent.AnnotationAgentInitFirst: "true",
},
},
Spec: basicSpec,
}),
},
"",
[]jsonpatch.JsonPatchOperation{
{
Operation: "add",
Path: "/spec/volumes",
},
{
Operation: "add",
Path: "/spec/containers/0/volumeMounts/-",
},
{
Operation: "remove",
Path: "/spec/initContainers",
},
{
Operation: "add",
Path: "/spec/initContainers",
},
{
Operation: "add",
Path: "/spec/containers/-",
},
{
Operation: "add",
Path: "/metadata/annotations/" + agent.EscapeJSONPointer(agent.AnnotationAgentStatus),
},
},
},

{
"configmap pod injection",
Handler{VaultAddress: "https://vault:8200", VaultAuthPath: "kubernetes", ImageVault: "vault", Log: hclog.Default().Named("handler")},
Expand Down