Skip to content

Commit

Permalink
Remove namespace from linkerd2-viz chart (#6638)
Browse files Browse the repository at this point in the history
* Remove namespace for linkerd2-viz chart

Second part of #6584, followup of #6635 (and based off of alpeb/no-ns-helm-core)

Stop rendering `namespace.yaml` in the `linkerd-viz` chart, same as we did in #6635.

The additional change here is the addition of the `namespace-metadata.yaml` template (and its RBAC), _not_ rendered in CLI installs, which is a Helm `post-install` hook, consisting on a Job that executes a script adding the required annotations and labels to the viz namespace using a PATCH request against kube-api. The script first checks if the namespace doesn't already have an annotations/labels entries, in which case it has to add extra ops in that patch.

The Job uses a multiarch `curlimages/curl` image, which is built by curl.haxx.se. I couldn't find a simple multiarch "kubectl" image that would have allowed us issuing simple kubectl commands. The `curlimages/curl` image is based on Alpine; it won't have as much as CVE-related reports noise as a Debian image, but it'd be better to have something slimmer still. Eventually we could build our own scratch-based binary if deemed worthy.
  • Loading branch information
alpeb authored Aug 13, 2021
1 parent 898e722 commit 57a83eb
Show file tree
Hide file tree
Showing 29 changed files with 379 additions and 130 deletions.
3 changes: 2 additions & 1 deletion bin/test-cleanup
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ echo "cleaning up linkerd resources [${k8s_context}]"
# Helm cleanup. Just the entries in `helm ls` as the resources should have already been cleaned up by the code above.
releases=$("$bindir/helm" ls -A -q)
if [[ "${releases[*]}" =~ 'l5d-viz' ]]; then
"$bindir/helm" --kube-context="$k8s_context" delete l5d-viz
"$bindir/helm" --kube-context="$k8s_context" --namespace linkerd-viz delete l5d-viz
kubectl delete ns linkerd-viz
fi
if [[ "${releases[*]}" =~ 'helm-test' ]]; then
"$bindir/helm" --kube-context="$k8s_context" --namespace linkerd delete helm-test
Expand Down
2 changes: 2 additions & 0 deletions test/integration/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ func helmOverridesEdge(root *tls.CA) ([]string, []string) {
"--set", "identity.issuer.crtExpiry=" + root.Cred.Crt.Certificate.NotAfter.Format(time.RFC3339),
}
vizArgs := []string{
"--namespace", TestHelper.GetVizNamespace(),
"--create-namespace",
"--set", "linkerdVersion=" + TestHelper.GetVersion(),
}
return coreArgs, vizArgs
Expand Down
2 changes: 0 additions & 2 deletions viz/charts/linkerd-viz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ Kubernetes: `>=1.16.0-0`
| grafanaUrl | string | `""` | url of external grafana instance with reverse proxy configured. |
| identityTrustDomain | string | clusterDomain | Trust domain used for identity |
| imagePullSecrets | list | `[]` | For Private docker registries, authentication is needed. Registry secrets are applied to the respective service accounts |
| installNamespace | bool | `true` | Set to false when installing in a custom namespace. |
| jaegerUrl | string | `""` | url of external jaeger instance Set this to `jaeger.linkerd-jaeger.svc.<clusterDomain>` if you plan to use jaeger extension |
| linkerdNamespace | string | `"linkerd"` | Namespace of the Linkerd core control-plane install |
| linkerdVersion | string | `"linkerdVersionValue"` | control plane version. See Proxy section for proxy version |
Expand All @@ -133,7 +132,6 @@ Kubernetes: `>=1.16.0-0`
| metricsAPI.resources.memory.limit | string | `nil` | Maximum amount of memory that metrics-api container can use |
| metricsAPI.resources.memory.request | string | `nil` | Amount of memory that the metrics-api container requests |
| metricsAPI.tolerations | string | `nil` | Tolerations section, See the [K8S documentation](https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/) for more information |
| namespace | string | `"linkerd-viz"` | Namespace in which the Linkerd Viz extension has to be installed |
| nodeSelector | object | `{"beta.kubernetes.io/os":"linux"}` | Default nodeSelector section, See the [K8S documentation](https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector) for more information |
| prometheus.alertRelabelConfigs | string | `nil` | Alert relabeling is applied to alerts before they are sent to the Alertmanager. |
| prometheus.alertmanagers | string | `nil` | Alertmanager instances the Prometheus server sends alerts to configured via the static_configs parameter. |
Expand Down
4 changes: 2 additions & 2 deletions viz/charts/linkerd-viz/templates/grafana-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ kind: ServiceAccount
apiVersion: v1
metadata:
name: grafana
namespace: {{.Values.namespace}}
{{ include "partials.namespace" . }}
labels:
linkerd.io/extension: viz
component: grafana
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
{{- include "partials.image-pull-secrets" .Values.imagePullSecrets }}
{{ end -}}
22 changes: 13 additions & 9 deletions viz/charts/linkerd-viz/templates/grafana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ kind: ConfigMap
apiVersion: v1
metadata:
name: grafana-config
namespace: {{.Values.namespace}}
{{ include "partials.namespace" . }}
labels:
linkerd.io/extension: viz
component: grafana
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
annotations:
{{ include "partials.annotations.created-by" . }}
data:
Expand Down Expand Up @@ -40,7 +40,7 @@ data:
{{- if .Values.prometheusUrl }}
url: {{.Values.prometheusUrl}}
{{- else }}
url: http://prometheus.{{.Values.namespace}}.svc.{{.Values.clusterDomain}}:9090
url: http://prometheus.{{.Release.Namespace}}.svc.{{.Values.clusterDomain}}:9090
{{- end }}
isDefault: true
jsonData:
Expand All @@ -65,13 +65,14 @@ kind: Service
apiVersion: v1
metadata:
name: grafana
namespace: {{.Values.namespace}}
{{ include "partials.namespace" . }}
labels:
linkerd.io/extension: viz
component: grafana
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
annotations:
{{ include "partials.annotations.created-by" . }}
linkerd.io/inject: enabled
spec:
type: ClusterIP
selector:
Expand All @@ -87,22 +88,24 @@ kind: Deployment
metadata:
annotations:
{{ include "partials.annotations.created-by" . }}
linkerd.io/inject: enabled
config.linkerd.io/proxy-await: "enabled"
labels:
linkerd.io/extension: viz
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: Linkerd
app.kubernetes.io/version: {{default .Values.linkerdVersion .Values.cliVersion}}
component: grafana
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
name: grafana
namespace: {{.Values.namespace}}
{{ include "partials.namespace" . }}
spec:
replicas: 1
selector:
matchLabels:
linkerd.io/extension: viz
component: grafana
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
template:
metadata:
annotations:
Expand All @@ -111,10 +114,11 @@ spec:
{{- include "partials.proxy.config.annotations" .resources | nindent 8 }}
{{- end }}
{{- with .Values.podAnnotations }}{{ toYaml . | trim | nindent 8 }}{{- end }}
linkerd.io/inject: enabled
labels:
linkerd.io/extension: viz
component: grafana
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
{{- with .Values.podLabels }}{{ toYaml . | trim | nindent 8 }}{{- end }}
spec:
{{- if .Values.grafana.tolerations -}}
Expand Down
10 changes: 5 additions & 5 deletions viz/charts/linkerd-viz/templates/metrics-api-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: linkerd-{{.Values.namespace}}-metrics-api
name: linkerd-{{.Release.Namespace}}-metrics-api
labels:
linkerd.io/extension: viz
component: metrics-api
Expand All @@ -29,24 +29,24 @@ rules:
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: linkerd-{{.Values.namespace}}-metrics-api
name: linkerd-{{.Release.Namespace}}-metrics-api
labels:
linkerd.io/extension: viz
component: metrics-api
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: linkerd-{{.Values.namespace}}-metrics-api
name: linkerd-{{.Release.Namespace}}-metrics-api
subjects:
- kind: ServiceAccount
name: metrics-api
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
---
kind: ServiceAccount
apiVersion: v1
metadata:
name: metrics-api
namespace: {{.Values.namespace}}
{{ include "partials.namespace" . }}
labels:
linkerd.io/extension: viz
component: metrics-api
Expand Down
10 changes: 7 additions & 3 deletions viz/charts/linkerd-viz/templates/metrics-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ kind: Service
apiVersion: v1
metadata:
name: metrics-api
namespace: {{.Values.namespace}}
{{ include "partials.namespace" . }}
labels:
linkerd.io/extension: viz
component: metrics-api
annotations:
{{ include "partials.annotations.created-by" . }}
linkerd.io/inject: enabled
spec:
type: ClusterIP
selector:
Expand All @@ -27,14 +28,16 @@ kind: Deployment
metadata:
annotations:
{{ include "partials.annotations.created-by" . }}
linkerd.io/inject: enabled
config.linkerd.io/proxy-await: "enabled"
labels:
linkerd.io/extension: viz
app.kubernetes.io/name: metrics-api
app.kubernetes.io/part-of: Linkerd
app.kubernetes.io/version: {{default .Values.linkerdVersion .Values.cliVersion}}
component: metrics-api
name: metrics-api
namespace: {{.Values.namespace}}
{{ include "partials.namespace" . }}
spec:
replicas: {{.Values.metricsAPI.replicas}}
selector:
Expand All @@ -52,6 +55,7 @@ spec:
{{- include "partials.proxy.config.annotations" .resources | nindent 8 }}
{{- end }}
{{- with .Values.podAnnotations }}{{ toYaml . | trim | nindent 8 }}{{- end }}
linkerd.io/inject: enabled
labels:
linkerd.io/extension: viz
component: metrics-api
Expand All @@ -73,7 +77,7 @@ spec:
{{- if .Values.prometheusUrl }}
- -prometheus-url={{.Values.prometheusUrl}}
{{- else if .Values.prometheus.enabled }}
- -prometheus-url=http://prometheus.{{.Values.namespace}}.svc.{{.Values.clusterDomain}}:9090
- -prometheus-url=http://prometheus.{{.Release.Namespace}}.svc.{{.Values.clusterDomain}}:9090
{{- else }}
{{ fail "Please enable `linkerd-prometheus` or provide `prometheusUrl` for the viz extension to function properly"}}
{{- end }}
Expand Down
43 changes: 43 additions & 0 deletions viz/charts/linkerd-viz/templates/namespace-metadata-rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
kind: ServiceAccount
apiVersion: v1
metadata:
annotations:
{{ include "partials.annotations.created-by" . }}
"helm.sh/hook": post-install
"helm.sh/hook-weight": "0"
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
name: namespace-metadata
{{- include "partials.image-pull-secrets" .Values.imagePullSecrets }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
annotations:
{{ include "partials.annotations.created-by" . }}
"helm.sh/hook": post-install
"helm.sh/hook-weight": "0"
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
name: namespace-metadata
rules:
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["get", "patch"]
resourceNames: ["{{.Release.Namespace}}"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
annotations:
{{ include "partials.annotations.created-by" . }}
"helm.sh/hook": post-install
"helm.sh/hook-weight": "0"
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
name: namespace-metadata
roleRef:
kind: Role
name: namespace-metadata
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: namespace-metadata
namespace: {{.Release.Namespace}}
53 changes: 53 additions & 0 deletions viz/charts/linkerd-viz/templates/namespace-metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
apiVersion: batch/v1
kind: Job
metadata:
annotations:
{{ include "partials.annotations.created-by" . }}
"helm.sh/hook": post-install
"helm.sh/hook-weight": "0"
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
labels:
app.kubernetes.io/name: namespace-metadata
app.kubernetes.io/part-of: Linkerd
app.kubernetes.io/version: {{default .Values.linkerdVersion .Values.cliVersion}}
name: namespace-metadata
spec:
template:
metadata:
annotations:
{{ include "partials.annotations.created-by" . }}
labels:
app.kubernetes.io/name: namespace-metadata
app.kubernetes.io/part-of: Linkerd
app.kubernetes.io/version: {{default .Values.linkerdVersion .Values.cliVersion}}
spec:
restartPolicy: Never
serviceAccountName: namespace-metadata
containers:
- name: namespace-metadata
image: curlimages/curl:7.78.0
imagePullPolicy: {{.Values.defaultImagePullPolicy}}
command: ["/bin/sh"]
args:
- -c
- |
ops=''
token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
ns=$(curl -kfv -H "Authorization: Bearer $token" \
"https://kubernetes.default.svc/api/v1/namespaces/{{.Release.Namespace}}")
if echo "$ns" | grep -vq 'labels'; then
ops="$ops{\"op\": \"add\",\"path\": \"/metadata/labels\",\"value\": {}},"
fi
if echo "$ns" | grep -vq 'annotations'; then
ops="$ops{\"op\": \"add\", \"path\": \"/metadata/annotations\", \"value\": {}},"
fi
{{- if .Values.prometheusUrl }}
ops="$ops{\"op\": \"add\", \"path\": \"/metadata/annotations/viz.linkerd.io~1external-prometheus\", \"value\": \"{{.Values.prometheusUrl}}\"},"
{{- end }}
ops="$ops{\"op\": \"add\", \"path\": \"/metadata/labels/linkerd.io~1extension\", \"value\": \"viz\"}"
curl -kfv -XPATCH -H "Content-Type: application/json-patch+json" -H "Authorization: Bearer $token" \
-d "[$ops]" \
"https://kubernetes.default.svc/api/v1/namespaces/{{.Release.Namespace}}?fieldManager=kubectl-label"
6 changes: 2 additions & 4 deletions viz/charts/linkerd-viz/templates/namespace.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
{{- if (.Values.installNamespace) -}}
{{- if eq .Release.Service "CLI" -}}
---
###
### Linkerd Viz Extension Namespace
###
kind: Namespace
apiVersion: v1
metadata:
name: {{.Values.namespace}}
name: {{.Release.Namespace}}
labels:
linkerd.io/extension: viz
annotations:
{{- if .Values.prometheusUrl }}
viz.linkerd.io/external-prometheus: {{.Values.prometheusUrl}}
{{- end }}
linkerd.io/inject: enabled
config.linkerd.io/proxy-await: "enabled"
{{ end -}}
12 changes: 6 additions & 6 deletions viz/charts/linkerd-viz/templates/prometheus-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: linkerd-{{.Values.namespace}}-prometheus
name: linkerd-{{.Release.Namespace}}-prometheus
labels:
linkerd.io/extension: viz
component: prometheus
Expand All @@ -18,27 +18,27 @@ rules:
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: linkerd-{{.Values.namespace}}-prometheus
name: linkerd-{{.Release.Namespace}}-prometheus
labels:
linkerd.io/extension: viz
component: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: linkerd-{{.Values.namespace}}-prometheus
name: linkerd-{{.Release.Namespace}}-prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
---
kind: ServiceAccount
apiVersion: v1
metadata:
name: prometheus
namespace: {{.Values.namespace}}
{{ include "partials.namespace" . }}
labels:
linkerd.io/extension: viz
component: prometheus
namespace: {{.Values.namespace}}
namespace: {{.Release.Namespace}}
{{- include "partials.image-pull-secrets" .Values.imagePullSecrets }}
{{ end -}}
Loading

0 comments on commit 57a83eb

Please sign in to comment.