-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #957 from fasaxc/v3.19-node-init
[v3.19] Add calico-node -init command to mount bpffs.
- Loading branch information
Showing
3 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) 2021 Tigera, Inc. All rights reserved. | ||
// | ||
// 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 nodeinit contains the calico-node -init command, which is intended to be run from | ||
// an init container to do privileged pre-flight initialisation. At present, it mounts | ||
// the BPF filesystem so it is only useful for BPF mode. | ||
package nodeinit | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/projectcalico/node/pkg/startup" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const bpfFSMountPoint = "/sys/fs/bpf" | ||
|
||
func Run() { | ||
// Check $CALICO_STARTUP_LOGLEVEL to capture early log statements | ||
startup.ConfigureLogging() | ||
|
||
err := ensureBPFFilesystem() | ||
if err != nil { | ||
logrus.WithError(err).Error("Failed to mount BPF filesystem.") | ||
os.Exit(2) // Using 2 just to distinguish from the usage error case. | ||
} | ||
} | ||
|
||
func ensureBPFFilesystem() error { | ||
// Check if the BPF filesystem is mounted at the expected location. | ||
logrus.Info("Checking if BPF filesystem is mounted.") | ||
mounts, err := os.Open("/proc/mounts") | ||
if err != nil { | ||
return fmt.Errorf("failed to open /proc/mounts: %w", err) | ||
} | ||
scanner := bufio.NewScanner(mounts) | ||
for scanner.Scan() { | ||
parts := strings.Split(scanner.Text(), " ") | ||
mountPoint := parts[1] | ||
fs := parts[2] | ||
|
||
if mountPoint == bpfFSMountPoint && fs == "bpf" { | ||
logrus.Info("BPF filesystem is mounted.") | ||
return nil | ||
} | ||
} | ||
if scanner.Err() != nil { | ||
return fmt.Errorf("failed to read /proc/mounts: %w", scanner.Err()) | ||
} | ||
|
||
// If we get here, the BPF filesystem is not mounted. Try to mount it. | ||
logrus.Info("BPF filesystem is not mounted. Trying to mount it...") | ||
err = syscall.Mount(bpfFSMountPoint, bpfFSMountPoint, "bpf", 0, "") | ||
if err != nil { | ||
return fmt.Errorf("failed to mount BPF filesystem: %w", err) | ||
} | ||
logrus.Info("Mounted BPF filesystem.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) 2021 Tigera, Inc. All rights reserved. | ||
// | ||
// 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 nodeinit | ||
|
||
func Run() { | ||
// Unused on Windows. | ||
} |