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

chore: add options for resource requests for vault-agent #275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 26 additions & 6 deletions pkg/webhook/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ type VaultConfig struct {
AgentOnce bool
AgentShareProcess bool
AgentShareProcessDefault string
AgentCPU resource.Quantity
AgentMemory resource.Quantity
AgentCPULimit resource.Quantity
AgentMemoryLimit resource.Quantity
AgentCPURequest resource.Quantity
AgentMemoryRequest resource.Quantity
AgentImage string
AgentImagePullPolicy corev1.PullPolicy
AgentEnvVariables string
Expand Down Expand Up @@ -318,16 +320,34 @@ func parseVaultConfig(obj metav1.Object, ar *model.AdmissionReview) VaultConfig
vaultConfig.AgentOnce = false
}

// This is done to preserve backwards compatibility with vault-agent-cpu
if val, err := resource.ParseQuantity(annotations["vault.security.banzaicloud.io/vault-agent-cpu"]); err == nil {
vaultConfig.AgentCPU = val
vaultConfig.AgentCPULimit = val
} else if val, err := resource.ParseQuantity(annotations["vault.security.banzaicloud.io/vault-agent-cpu-limit"]); err == nil {
vaultConfig.AgentCPULimit = val
} else {
vaultConfig.AgentCPU = resource.MustParse("100m")
vaultConfig.AgentCPULimit = resource.MustParse("100m")
}

// This is done to preserve backwards compatibility with vault-agent-memory
if val, err := resource.ParseQuantity(annotations["vault.security.banzaicloud.io/vault-agent-memory"]); err == nil {
vaultConfig.AgentMemory = val
vaultConfig.AgentMemoryLimit = val
} else if val, err := resource.ParseQuantity(annotations["vault.security.banzaicloud.io/vault-agent-memory-limit"]); err == nil {
vaultConfig.AgentMemoryLimit = val
} else {
vaultConfig.AgentMemory = resource.MustParse("128Mi")
vaultConfig.AgentMemoryLimit = resource.MustParse("128Mi")
}

if val, err := resource.ParseQuantity(annotations["vault.security.banzaicloud.io/vault-agent-cpu-request"]); err == nil {
vaultConfig.AgentCPURequest = val
} else {
vaultConfig.AgentCPURequest = resource.MustParse("100m")
}

if val, err := resource.ParseQuantity(annotations["vault.security.banzaicloud.io/vault-agent-memory-request"]); err == nil {
vaultConfig.AgentMemoryRequest = val
} else {
vaultConfig.AgentMemoryRequest = resource.MustParse("128Mi")
}

if val, ok := annotations["vault.security.banzaicloud.io/vault-agent-share-process-namespace"]; ok {
Expand Down
8 changes: 6 additions & 2 deletions pkg/webhook/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,12 @@ func getAgentContainers(originalContainers []corev1.Container, podSecurityContex
VolumeMounts: containerVolMounts,
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: vaultConfig.AgentCPU,
corev1.ResourceMemory: vaultConfig.AgentMemory,
corev1.ResourceCPU: vaultConfig.AgentCPULimit,
corev1.ResourceMemory: vaultConfig.AgentMemoryLimit,
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: vaultConfig.AgentCPURequest,
corev1.ResourceMemory: vaultConfig.AgentMemoryRequest,
},
},
})
Expand Down
14 changes: 10 additions & 4 deletions pkg/webhook/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,8 +999,10 @@ func Test_mutatingWebhook_mutatePod(t *testing.T) {
ConfigfilePath: "/vault/secrets",
Addr: "test",
SkipVerify: false,
AgentCPU: resource.MustParse("50m"),
AgentMemory: resource.MustParse("128Mi"),
AgentCPURequest: resource.MustParse("200m"),
AgentMemoryRequest: resource.MustParse("256Mi"),
AgentCPULimit: resource.MustParse("500m"),
AgentMemoryLimit: resource.MustParse("384Mi"),
AgentImage: "hashicorp/vault:latest",
AgentImagePullPolicy: "IfNotPresent",
ServiceAccountTokenVolumeName: "/var/run/secrets/kubernetes.io/serviceaccount",
Expand All @@ -1021,8 +1023,12 @@ func Test_mutatingWebhook_mutatePod(t *testing.T) {
Args: []string{"agent", "-config", "/vault/config/config.hcl"},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("384Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("200m"),
corev1.ResourceMemory: resource.MustParse("256Mi"),
},
},
Env: []corev1.EnvVar{
Expand Down
Loading