Skip to content

Commit 9eb8df6

Browse files
Better kubernetes configuration detection based on k8s libraries (max-rocket-internet#30)
* Instead of doing our own user home directory detection for .kube/config, use the k8s.io/cmdclient built-in methods which support KUBECONFIG env variable, $HOME/.kube/config, and KUBERNETES_SERVICE env variables automatically. Also print out a sanitized representation of the config at startup in case of misconfiguration. * no need to pre-initialize variables and require an extra import * remove superfluous environment variables in helm chart and make the container securityContext configurable via variables instead of hard-coded --------- Co-authored-by: Max Williams <[email protected]>
1 parent a21ab2a commit 9eb8df6

File tree

4 files changed

+27
-44
lines changed

4 files changed

+27
-44
lines changed

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ COPY . .
66
RUN go mod vendor
77
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o k8s-event-logger &&\
88
if ldd 'k8s-event-logger'; then exit 1; fi; # Ensure binary is statically-linked
9-
RUN echo "k8s-event-logger:x:10001:10001::/:/bin/false" > /etc_passwd_to_copy
109

1110
FROM --platform=${TARGETPLATFORM} scratch
12-
COPY --from=builder /etc_passwd_to_copy /go/src/github.com/max-rocket-internet/k8s-event-logger/k8s-event-logger /
13-
ENV USER=k8s-event-logger
11+
COPY --from=builder /go/src/github.com/max-rocket-internet/k8s-event-logger/k8s-event-logger /
1412
USER 10001
1513
ENTRYPOINT ["/k8s-event-logger"]

chart/templates/deployment.yaml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,7 @@ spec:
3333
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
3434
imagePullPolicy: {{ .Values.image.pullPolicy }}
3535
securityContext:
36-
readOnlyRootFilesystem: true
37-
runAsNonRoot: true
38-
runAsUser: 10001
39-
runAsGroup: 10001
40-
allowPrivilegeEscalation: false
41-
capabilities:
42-
drop:
43-
- ALL
44-
seccompProfile:
45-
type: RuntimeDefault
36+
{{- toYaml .Values.securityContext | nindent 12 }}
4637
env:
4738
{{- range $key, $value := .Values.env }}
4839
- name: {{ $key }}

chart/values.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ resources:
1111
cpu: 100m
1212
memory: 128Mi
1313

14-
env:
15-
KUBERNETES_API_URL: https://172.20.0.1:443
16-
CA_FILE: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
17-
14+
env: {}
1815
imagePullSecrets: []
1916
nameOverride: ""
2017
fullnameOverride: ""
@@ -23,3 +20,14 @@ tolerations: []
2320
affinity: {}
2421
podLabels: {}
2522
podAnnotations: {}
23+
securityContext:
24+
readOnlyRootFilesystem: true
25+
runAsNonRoot: true
26+
runAsUser: 10001
27+
runAsGroup: 10001
28+
allowPrivilegeEscalation: false
29+
capabilities:
30+
drop:
31+
- ALL
32+
seccompProfile:
33+
type: RuntimeDefault

main.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ package main
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"log"
76
"os"
8-
"os/user"
97

108
corev1 "k8s.io/api/core/v1"
119
"k8s.io/apimachinery/pkg/fields"
1210
"k8s.io/client-go/kubernetes"
13-
"k8s.io/client-go/rest"
1411
"k8s.io/client-go/tools/cache"
1512
"k8s.io/client-go/tools/clientcmd"
1613
)
@@ -19,35 +16,24 @@ func main() {
1916
loggerApplication := log.New(os.Stderr, "", log.LstdFlags)
2017
loggerEvent := log.New(os.Stdout, "", 0)
2118

22-
usr, err := user.Current()
23-
if err != nil {
24-
loggerApplication.Panicln(err.Error())
25-
}
19+
// Using First sample from https://pkg.go.dev/k8s.io/client-go/tools/clientcmd to automatically deal with environment variables and default file paths
20+
21+
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
22+
// if you want to change the loading rules (which files in which order), you can do so here
2623

27-
var config *rest.Config
24+
configOverrides := &clientcmd.ConfigOverrides{}
25+
// if you want to change override values or bind them to flags, there are methods to help you
2826

29-
if k8s_port := os.Getenv("KUBERNETES_PORT"); k8s_port == "" {
30-
loggerApplication.Println("Using local kubeconfig")
31-
var kubeconfig string
32-
home := usr.HomeDir
33-
if home != "" {
34-
kubeconfig = fmt.Sprintf("%s/.kube/config", home)
35-
} else {
36-
loggerApplication.Panicln("home directory unknown")
37-
}
27+
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
3828

39-
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
40-
if err != nil {
41-
loggerApplication.Panicln(err.Error())
42-
}
43-
} else {
44-
loggerApplication.Println("Using in-cluster authentication")
45-
config, err = rest.InClusterConfig()
46-
if err != nil {
47-
loggerApplication.Panicln(err.Error())
48-
}
29+
config, err := kubeConfig.ClientConfig()
30+
if err != nil {
31+
loggerApplication.Panicln(err.Error())
4932
}
5033

34+
// Note that this *should* automatically sanitize sensitive fields
35+
loggerApplication.Println("Using configuration:", config.String())
36+
5137
clientset, err := kubernetes.NewForConfig(config)
5238
if err != nil {
5339
loggerApplication.Panicln(err.Error())

0 commit comments

Comments
 (0)