forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
genpolicy: add utility script for containerd pull #213
Draft
Redent0r
wants to merge
3
commits into
msft-main
Choose a base branch
from
saulparedes/add_util_setup_script
base: msft-main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−4
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,127 @@ | ||
#!/bin/bash | ||
|
||
# Copyright (c) 2023 Microsoft Corporation | ||
Redent0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# This script aims to adapt the system for the genpolicy tool to be able to use containerd pull function properly. | ||
# This is needed for managed identity based authentication to private registries using an identity token. | ||
|
||
# This script needs 'sudo' access. It will: | ||
# - install containerd if not installed already. Genpolicy tool uses containerd to pull image when using -d option | ||
# - ensure cri plugin is NOT disabled in containerd config. This is needed for policy tool to pull image | ||
# - restart containerd or start it if not running already. | ||
# - print containerd socket file location. Use this when running genpolicy tool. Eg genpolicy -d=$SOCKET_FILE_LOCATION -y foo.yaml | ||
# - fix containerd socket file permissions if needed. This is so genpolicy tool is able to access containerd socket file without 'sudo' | ||
# - adapt docker config.json if needed. This is done so 'az acr login' command saves identity token to the docker config | ||
|
||
set -e -x | ||
|
||
# Function to check if a command exists | ||
command_exists() { | ||
command -v "$1" &> /dev/null | ||
Redent0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
# Utility function for error messages and exit | ||
error_exit() { | ||
echo "$1" 1>&2 | ||
exit 1 | ||
} | ||
|
||
# Function to ensure a command is installed | ||
ensure_command_installed() { | ||
if ! command_exists "$1"; then | ||
echo "$1 could not be found, installing..." | ||
sudo apt-get install -y "$1" | ||
Redent0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ! command_exists "$1"; then | ||
error_exit "Failed to install $1" | ||
fi | ||
else | ||
echo "$1 is already installed" | ||
fi | ||
} | ||
|
||
sudo apt-get update | ||
|
||
ensure_command_installed containerd | ||
|
||
# Modify containerd config if needed | ||
CONTAINERD_CONFIG_FILE="/etc/containerd/config.toml" | ||
|
||
if [ ! -f "$CONTAINERD_CONFIG_FILE" ]; then | ||
error_exit "Containerd config file not found: $CONTAINERD_CONFIG_FILE. Please update CONTAINERD_CONFIG_FILE in this script to point to the correct containerd config file." | ||
fi | ||
|
||
if grep -qE 'disabled_plugins.*\["cri"\]' "$CONTAINERD_CONFIG_FILE" || grep -qE "disabled_plugins.*\['cri'\]" "$CONTAINERD_CONFIG_FILE"; then | ||
echo "Modifying containerd config to enable cri plugin..." | ||
sudo sed -i -E "s/disabled_plugins.*(\['cri'\]|\[\"cri\"\])/disabled_plugins = []/g" "$CONTAINERD_CONFIG_FILE" | ||
else | ||
echo "CRI plugin is already enabled in containerd config" | ||
fi | ||
|
||
# Restart containerd using systemctl | ||
echo "Restarting containerd service..." | ||
if systemctl is-active --quiet containerd; then | ||
sudo systemctl restart containerd | ||
else | ||
sudo systemctl start containerd | ||
fi | ||
|
||
# Print containerd status | ||
echo "Containerd status:" | ||
sudo systemctl status containerd --no-pager | ||
|
||
# Print containerd socket file location found in config | ||
SOCKET_FILE_LOCATION=$(awk '/\[grpc\]/ {found=1} found && /address *= */ {print $3; exit}' "$CONTAINERD_CONFIG_FILE" | tr -d '"') | ||
|
||
if [ -z "$SOCKET_FILE_LOCATION" ]; then | ||
error_exit "Socket file location not found in config" | ||
fi | ||
echo "Containerd socket file location: $SOCKET_FILE_LOCATION" | ||
|
||
# Wait for the socket file to be created | ||
echo "Waiting for containerd socket file to be created..." | ||
for i in {1..10}; do | ||
if [ -e "$SOCKET_FILE_LOCATION" ]; then | ||
echo "Containerd socket file found: $SOCKET_FILE_LOCATION" | ||
break | ||
fi | ||
echo "Attempt $i: Socket file not found, waiting..." | ||
sleep 1 | ||
done | ||
|
||
if [ ! -e "$SOCKET_FILE_LOCATION" ]; then | ||
error_exit "Socket file not found after waiting: $SOCKET_FILE_LOCATION" | ||
fi | ||
|
||
# Fix containerd socket file permissions | ||
echo "Ensuring containerd socket file permissions are set correctly..." | ||
sudo chmod a+rw "$SOCKET_FILE_LOCATION" | ||
|
||
if ! command_exists docker; then | ||
echo "$1 could not be found, installing..." | ||
sudo apt-get install -y docker.io | ||
if ! command_exists docker; then | ||
error_exit "Failed to install docker.io" | ||
fi | ||
else | ||
echo "docker is already installed" | ||
fi | ||
|
||
ensure_command_installed jq | ||
|
||
# Adapt Docker config if needed | ||
DOCKER_CONFIG_FILE="$HOME/.docker/config.json" | ||
echo "Checking Docker config file: $DOCKER_CONFIG_FILE" | ||
|
||
if [ -f "$DOCKER_CONFIG_FILE" ]; then | ||
if grep -q '"credstore":' "$DOCKER_CONFIG_FILE"; then | ||
echo "Modifying Docker config to remove 'credstore' key..." | ||
jq 'del(.credstore)' "$DOCKER_CONFIG_FILE" > "$DOCKER_CONFIG_FILE.tmp" && mv "$DOCKER_CONFIG_FILE.tmp" "$DOCKER_CONFIG_FILE" | ||
else | ||
echo "'credstore' key not found in Docker config" | ||
fi | ||
else | ||
error_exit "Docker config file not found. Please update DOCKER_CONFIG_FILE in this script to point to the correct Docker config file." | ||
fi | ||
|
||
echo "Script execution completed. Please use $SOCKET_FILE_LOCATION as socker file location." | ||
Redent0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
echo "Eg. genpolicy -d=$SOCKET_FILE_LOCATION -y foo.yaml" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these changes are too intrusive to push average users to use this script.
For average users, I would prefer a list of steps in an MD file - similar to the steps from https://github.com/kata-containers/kata-containers/blob/main/docs/how-to/containerd-kata.md. Also, I would like to update such doc Upstream instead of pushing just MSFT users in this direction.
But let's talk with other folks in our team too - maybe they have different opinions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it's too much to require them to use this. This might exist for users that don't feel like doing all these steps, but they are also welcome to do them on their own. I'll add better instructions to the MD, and open a PR upstream with all squashed changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree with: