Skip to content

Commit

Permalink
install/kubernetes: Remove sh and mount dependency from init cont…
Browse files Browse the repository at this point in the history
…ainer

The mount-cgroup init container runs a mount command on the underlying
host using `nsenter`. However, certain distros like Talos don't have `sh`
or `mount` utilities available. Hence, move the logic to check and mount
cgroup2 fs to a statically linked Go program binary.

Fixes: fa8bea4 ("cilium-daemonset: Fix ineffective socket-lb caused by incorrect cgroup2 fs mount")

Signed-off-by: Aditi Ghag <aditi@cilium.io>
  • Loading branch information
aditighag committed Jul 8, 2021
1 parent eecca2e commit a76bbde
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ debug: all

include Makefile.defs

SUBDIRS_CILIUM_CONTAINER := proxylib envoy bpf cilium daemon cilium-health bugtool
SUBDIRS_CILIUM_CONTAINER := proxylib envoy bpf cilium daemon cilium-health bugtool tools/mount
SUBDIRS := $(SUBDIRS_CILIUM_CONTAINER) operator plugins tools hubble-relay

SUBDIRS_CILIUM_CONTAINER += plugins/cilium-cni
Expand Down
15 changes: 10 additions & 5 deletions install/kubernetes/cilium/templates/cilium-agent-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,24 @@ spec:
env:
- name: CGROUP_ROOT
value: {{ .Values.cgroup.hostRoot }}
- name: BIN_PATH
value: {{ .Values.cni.binPath }}
command:
- nsenter
- --cgroup=/hostproc/1/ns/cgroup
- --mount=/hostproc/1/ns/mnt
- --
- sh
- -c
- 'mount | grep "$CGROUP_ROOT type cgroup2" || { echo "Mounting cgroup filesystem..."; mount -t cgroup2 none $CGROUP_ROOT; }'
# The statically linked Go program binary is invoked to avoid any
# dependency on utilities like sh and mount that can be missing on certain
# distros installed on the underlying host. Copy the binary to the
# same directory where we install cilium cni plugin so that exec permissions
# are available.
- 'cp /usr/bin/cilium-mount /hostbin/cilium-mount && nsenter --cgroup=/hostproc/1/ns/cgroup --mount=/hostproc/1/ns/mnt "${BIN_PATH}/cilium-mount" $CGROUP_ROOT; rm /hostbin/cilium-mount'
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}{{ if .Values.image.useDigest }}@{{ .Values.image.digest }}{{ end }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
volumeMounts:
- mountPath: /hostproc
name: hostproc
- mountPath: /hostbin
name: cni-path
securityContext:
privileged: true
{{- end }}
Expand Down
4 changes: 2 additions & 2 deletions tools/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright 2017-2019 Authors of Cilium
# Copyright 2017-2021 Authors of Cilium
# SPDX-License-Identifier: Apache-2.0

include ../Makefile.defs

SUBDIRS := alignchecker maptool
SUBDIRS := alignchecker maptool mount

.PHONY: all $(SUBDIRS) clean install

Expand Down
1 change: 1 addition & 0 deletions tools/mount/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cilium-mount
27 changes: 27 additions & 0 deletions tools/mount/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2021 Authors of Cilium
# SPDX-License-Identifier: Apache-2.0

include ../../Makefile.defs

TARGET := cilium-mount

.PHONY: all $(TARGET) $(SUBDIRS) clean install

all: $(TARGET)

$(TARGET):
@$(ECHO_GO)
$(QUIET)$(GO_BUILD) -o $@

clean:
@$(ECHO_CLEAN)
-$(QUIET)rm -f $(TARGET)
$(QUIET)$(GO_CLEAN)

install:
$(QUIET)$(INSTALL) -m 0755 -d $(DESTDIR)$(BINDIR)
$(QUIET)$(INSTALL) -m 0755 $(TARGET) $(DESTDIR)$(BINDIR)

install-binary: install

install-bash-completion:
35 changes: 35 additions & 0 deletions tools/mount/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

"github.com/cilium/cilium/pkg/cgroups"
)

func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "usage: %s <cgroup-mount-point> \n\n", os.Args[0])
os.Exit(1)
}

cgroupMountPoint := os.Args[1]
// This program is executed by an init container so we purposely don't
// exit with any error codes. In case of errors, the function will log warnings,
// but we don't block cilium agent pod from running.
cgroups.CheckOrMountCgrpFS(cgroupMountPoint)
}

0 comments on commit a76bbde

Please sign in to comment.