Skip to content

Commit 58ca8cb

Browse files
authored
Merge pull request #44 from gruntwork-io/yori-improvements
Improvements to k8s-service
2 parents 3b6ebe7 + 2b22b08 commit 58ca8cb

File tree

5 files changed

+146
-6
lines changed

5 files changed

+146
-6
lines changed

charts/k8s-service/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,27 @@ The `/*` rule which routes to port 3000 will always be used even when accessing
486486
evaluated first when routing requests.
487487

488488

489+
## How do I deploy a worker service?
490+
491+
Worker services typically do not have a RPC or web server interface to access it. Instead, worker services act on their
492+
own and typically reach out to get the data they need. These services should be deployed without any ports exposed.
493+
However, by default `k8s-service` will deploy an internally exposed service with port 80 open.
494+
495+
To disable the default port, you can use the following `values.yaml` inputs:
496+
497+
```
498+
containerPorts:
499+
http:
500+
disabled: true
501+
502+
service:
503+
enabled: false
504+
```
505+
506+
This will override the default settings such that only the `Deployment` resource is created, with no ports exposed on
507+
the container.
508+
509+
489510
## How do I check the status of the rollout?
490511

491512
This Helm Chart packages your application into a `Deployment` controller. The `Deployment` controller will be
@@ -1013,7 +1034,7 @@ spec:
10131034
spec:
10141035
containers:
10151036
... The first entry relates to the application ...
1016-
- name: datadog
1037+
- name: datadog
10171038
image: datadog/agent:latest
10181039
env:
10191040
- name: DD_API_KEY

charts/k8s-service/templates/deployment.yaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@ Similarly, we need to decide whether or not there are environment variables to a
1313
We need this because certain sections are omitted if there are no volumes or environment variables to add.
1414
*/ -}}
1515
{{/* Go Templates do not support variable updating, so we simulate it using dictionaries */}}
16-
{{- $hasInjectionTypes := dict "hasVolume" false "hasEnvVars" false "hasIRSA" false -}}
16+
{{- $hasInjectionTypes := dict "hasVolume" false "hasEnvVars" false "hasIRSA" false "exposePorts" false -}}
1717
{{- if .Values.envVars -}}
1818
{{- $_ := set $hasInjectionTypes "hasEnvVars" true -}}
1919
{{- end -}}
20+
{{- $allContainerPorts := values .Values.containerPorts -}}
21+
{{- range $allContainerPorts -}}
22+
{{/* We are exposing ports if there is at least one key in containerPorts that is not disabled (disabled = false or
23+
omitted)
24+
*/}}
25+
{{- if or (not (hasKey . "disabled")) (not .disabled) -}}
26+
{{- $_ := set $hasInjectionTypes "exposePorts" true -}}
27+
{{- end -}}
28+
{{- end -}}
2029
{{- if gt (len .Values.aws.irsa.role_arn) 0 -}}
2130
{{- $_ := set $hasInjectionTypes "hasEnvVars" true -}}
2231
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
@@ -78,6 +87,13 @@ spec:
7887
{{ toYaml . | indent 8 }}
7988
{{- end }}
8089
spec:
90+
{{- if gt (len .Values.serviceAccount.name) 0 }}
91+
serviceAccountName: "{{ .Values.serviceAccount.name }}"
92+
{{- end }}
93+
{{- if hasKey .Values.serviceAccount "automountServiceAccountToken" }}
94+
automountServiceAccountToken : {{ .Values.serviceAccount.automountServiceAccountToken }}
95+
{{- end }}
96+
8197
containers:
8298
- name: {{ .Values.applicationName }}
8399
{{- $repo := required "containerImage.repository is required" .Values.containerImage.repository }}
@@ -89,7 +105,7 @@ spec:
89105
{{ toYaml .Values.containerCommand | indent 12 }}
90106
{{- end }}
91107

92-
{{- if .Values.containerPorts }}
108+
{{- if index $hasInjectionTypes "exposePorts" }}
93109
ports:
94110
{{- /*
95111
NOTE: we check for a disabled flag here so that users of the helm

charts/k8s-service/values.yaml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ containerCommand: null
5858
# - protocol (string) (required) : The network protocol (e.g TCP or UDP) that is exposed.
5959
# - disabled (bool) : Whether or not this port is disabled. This defaults to false if unset. Provided as a
6060
# convenience to override the default ports on the commandline. For example, to
61-
# disable the default port, you can pass `--set containerPorts.https.disabled=true`.
61+
# disable the default port, you can pass `--set containerPorts.http.disabled=true`.
6262
#
6363
# The default config exposes TCP port 80 and binds the name `http` to it.
6464
containerPorts:
@@ -103,12 +103,12 @@ readinessProbe: {}
103103
# securityContext is a map that specified the privillege and access control settings for a Pod of Container. Security Context
104104
# can be specified when the application requires additional access control permissions. More details on securityContext and supported
105105
# settings can be found at https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
106-
#
106+
#
107107
# EXAMPLE:
108108
# 1) To run a container in privilleged mode
109109
# securityContext:
110110
# privilleged: true
111-
#
111+
#
112112
# 2) To run a container as a specific user
113113
# securityContext:
114114
# runAsUser: 2000
@@ -234,6 +234,8 @@ ingress:
234234
# application container. The keys will be mapped to environment variable keys, with the values mapping to the
235235
# environment variable values.
236236
#
237+
# NOTE: If you wish to set environment variables using Secrets, see the `secrets` setting in this file.
238+
#
237239
# The following example configures two environment variables, DB_HOST and DB_PORT:
238240
#
239241
# EXAMPLE:
@@ -359,6 +361,19 @@ tolerations: []
359361
# list is a string that corresponds to the Secret name.
360362
imagePullSecrets: []
361363

364+
# serviceAccount is a map that configures the ServiceAccount information for the Pod.
365+
# The expected keys of serviceAccount are:
366+
# - name (string) : The name of the ServiceAccount in the Namespace where the Pod is deployed
367+
# that should be used. By default this is the default ServiceAccount of the
368+
# Namespace.
369+
# - automountServiceAccountToken (bool) : Whether or not to automatically mount the ServiceAccount token as a volume
370+
# into the Pod. Note that this can be used to override the equivalent config
371+
# on the SerrviceAccount.
372+
#
373+
# The default config uses empty string to indicate that the default service account should be used.
374+
serviceAccount:
375+
name: ""
376+
362377
#----------------------------------------------------------------------------------------------------------------------
363378
# AWS SPECIFIC VALUES
364379
# These input values relate to AWS specific features, such as those relating to EKS and the AWS ALB Ingress Controller.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// +build all tpl
2+
3+
// NOTE: We use build flags to differentiate between template tests and integration tests so that you can conveniently
4+
// run just the template tests. See the test README for more information.
5+
6+
package test
7+
8+
import (
9+
"strings"
10+
"testing"
11+
12+
"github.com/gruntwork-io/terratest/modules/random"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestK8SServiceServiceAccountInjection(t *testing.T) {
18+
t.Parallel()
19+
randomSAName := strings.ToLower(random.UniqueId())
20+
deployment := renderK8SServiceDeploymentWithSetValues(
21+
t,
22+
map[string]string{
23+
"serviceAccount.name": randomSAName,
24+
},
25+
)
26+
renderedServiceAccountName := deployment.Spec.Template.Spec.ServiceAccountName
27+
assert.Equal(t, renderedServiceAccountName, randomSAName)
28+
}
29+
30+
func TestK8SServiceServiceAccountNoNameIsEmpty(t *testing.T) {
31+
t.Parallel()
32+
deployment := renderK8SServiceDeploymentWithSetValues(
33+
t,
34+
map[string]string{},
35+
)
36+
renderedServiceAccountName := deployment.Spec.Template.Spec.ServiceAccountName
37+
assert.Equal(t, renderedServiceAccountName, "")
38+
}
39+
40+
func TestK8SServiceServiceAccountAutomountTokenTrueInjection(t *testing.T) {
41+
t.Parallel()
42+
deployment := renderK8SServiceDeploymentWithSetValues(
43+
t,
44+
map[string]string{
45+
"serviceAccount.automountServiceAccountToken": "true",
46+
},
47+
)
48+
renderedServiceAccountTokenAutomountSetting := deployment.Spec.Template.Spec.AutomountServiceAccountToken
49+
require.NotNil(t, renderedServiceAccountTokenAutomountSetting)
50+
assert.True(t, *renderedServiceAccountTokenAutomountSetting)
51+
}
52+
53+
func TestK8SServiceServiceAccountAutomountTokenFalseInjection(t *testing.T) {
54+
t.Parallel()
55+
deployment := renderK8SServiceDeploymentWithSetValues(
56+
t,
57+
map[string]string{
58+
"serviceAccount.automountServiceAccountToken": "false",
59+
},
60+
)
61+
renderedServiceAccountTokenAutomountSetting := deployment.Spec.Template.Spec.AutomountServiceAccountToken
62+
require.NotNil(t, renderedServiceAccountTokenAutomountSetting)
63+
assert.False(t, *renderedServiceAccountTokenAutomountSetting)
64+
}
65+
66+
func TestK8SServiceServiceAccountOmitAutomountToken(t *testing.T) {
67+
t.Parallel()
68+
deployment := renderK8SServiceDeploymentWithSetValues(
69+
t,
70+
map[string]string{},
71+
)
72+
renderedServiceAccountTokenAutomountSetting := deployment.Spec.Template.Spec.AutomountServiceAccountToken
73+
assert.Nil(t, renderedServiceAccountTokenAutomountSetting)
74+
}

test/k8s_service_template_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,17 @@ func TestK8SServiceSideCarContainersRendersCorrectly(t *testing.T) {
689689
sideCarContainer := renderedContainers[1]
690690
assert.Equal(t, sideCarContainer.Image, "datadog/agent:latest")
691691
}
692+
693+
func TestK8SServiceDisableDefaultPort(t *testing.T) {
694+
t.Parallel()
695+
deployment := renderK8SServiceDeploymentWithSetValues(
696+
t,
697+
map[string]string{
698+
"containerPorts.http.disabled": "true",
699+
},
700+
)
701+
renderedContainers := deployment.Spec.Template.Spec.Containers
702+
require.Equal(t, len(renderedContainers), 1)
703+
mainContainer := renderedContainers[0]
704+
assert.Equal(t, len(mainContainer.Ports), 0)
705+
}

0 commit comments

Comments
 (0)