Skip to content

Commit

Permalink
code format improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Haoyuan Ma committed Aug 14, 2023
1 parent b224c82 commit 24b1db7
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 61 deletions.
5 changes: 5 additions & 0 deletions scripts/cloudlab/setup_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func SetupNode(sandbox string, useStargz string) error {
return &utils.ShellError{Msg: "Sandbox technique not supported!", ExitCode: 1}
}

if sandbox != "stock-only" && useStargz == "use-stargz" {
utils.FatalPrintf("Invalid options! Stargz is only supported with stock-only mode!\n")
return &utils.ShellError{Msg: "Invalid options: use-stargz", ExitCode: 1}
}

// Turn off automatic update
utils.InfoPrintf("Turn off automatic update\n")
if err := utils.TurnOffAutomaticUpgrade(); err != nil {
Expand Down
9 changes: 5 additions & 4 deletions scripts/cluster/create_multinode_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ func ExtractMasterNodeInfo() error {
return err
}
configs.Kube.ApiserverTokenHash = shellOut
masterKeyYamlTemplate := `ApiserverAdvertiseAddress: %s
ApiserverPort: %s
ApiserverToken: %s
ApiserverTokenHash: %s`
masterKeyYamlTemplate :=
"ApiserverAdvertiseAddress: %s\n" +
"ApiserverPort: %s\n" +
"ApiserverToken: %s\n" +
"ApiserverTokenHash: %s"

// Create masterKey.yaml with master node information
utils.WaitPrintf("Creating masterKey.yaml with master node information")
Expand Down
11 changes: 5 additions & 6 deletions scripts/cluster/setup_worker_kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func SetupWorkerKubelet(stockContainerd string) error {
criSock = "/etc/vhive-cri/vhive-cri.sock"
}

err := CreateWorkerKubeletService(criSock)
if err != nil {
if err := CreateWorkerKubeletService(criSock); err != nil {
return err
}

Expand All @@ -50,10 +49,10 @@ func CreateWorkerKubeletService(criSock string) error {
if !utils.CheckErrorWithMsg(err, "Failed to create kubelet service!\n") {
return err
}
bashCmd := `sudo sh -c 'cat <<EOF > /etc/systemd/system/kubelet.service.d/0-containerd.conf
[Service]
Environment="KUBELET_EXTRA_ARGS=--container-runtime=remote --runtime-request-timeout=15m --container-runtime-endpoint=unix://%s"
EOF'`
bashCmd := "sudo sh -c 'cat <<EOF > /etc/systemd/system/kubelet.service.d/0-containerd.conf\n" +
"[Service]\n" +
`Environment="KUBELET_EXTRA_ARGS=--container-runtime=remote --runtime-request-timeout=15m --container-runtime-endpoint=unix://%s"` +
"\nEOF'"
_, err = utils.ExecShellCmd(bashCmd, criSock)
if !utils.CheckErrorWithMsg(err, "Failed to create kubelet service!\n") {
return err
Expand Down
16 changes: 8 additions & 8 deletions scripts/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,28 +206,28 @@ func main() {
utils.InfoPrintf("Set up system\n")
err = setup.SetupSystem()
case "setup_gvisor_containerd":
utils.InfoPrintf("Set up gvisor_containerd")
utils.InfoPrintf("Set up gvisor_containerd\n")
err = setup.SetupGvisorContainerd()
case "setup_firecracker_containerd":
utils.InfoPrintf("Set up firecracker_containerd")
utils.InfoPrintf("Set up firecracker_containerd\n")
err = setup.SetupFirecrackerContainerd()
case "install_stock":
utils.InfoPrintf("Install stock")
utils.InfoPrintf("Install stock\n")
err = setup.InstallStock()
case "install_pmutools":
utils.InfoPrintf("Install pmutools")
utils.InfoPrintf("Install pmutools\n")
err = setup.InstallPmuTools()
case "install_go":
utils.InfoPrintf("Install go")
utils.InfoPrintf("Install go\n")
err = setup.InstallGo()
case "create_docker_image":
utils.InfoPrintf("Create docker image")
utils.InfoPrintf("Create docker image\n")
err = setup.CreateDockerImage()
case "create_devmapper":
utils.InfoPrintf("Create devmapper")
utils.InfoPrintf("Create devmapper\n")
err = setup.CreateDevmapper()
case "clean_fcctr":
utils.InfoPrintf("Clean fcctr")
utils.InfoPrintf("Clean fcctr\n")
err = setup.CleanFcctr()
default:
utils.FatalPrintf("Invalid subcommand --> %s! Available subcommands list: \n", subCmd)
Expand Down
154 changes: 116 additions & 38 deletions scripts/setup/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,31 @@ func InstallPmuTools() error {
// Original Bash Scripts: scripts/install_pmutools.sh

utils.WaitPrintf("Installing pmu-tools")
// Get Linux kernel version
kernelVersion, err := utils.GetKernelVersion()
if !utils.CheckErrorWithMsg(err, "Failed to get kernel version!\n") {
return err
}

// Install required packages (kernel version specific)
err = utils.InstallPackages("numactl linux-tools-%s", kernelVersion)
if !utils.CheckErrorWithMsg(err, "Failed to install required packages!\n") {
return err
}

// Clone the repo of pmu-tools to temporary directory
repoPath, err := utils.CloneRepoToTmpDir("master", configs.System.PmuToolsRepoUrl)
if !utils.CheckErrorWithMsg(err, "Failed to clone required repo!\n") {
return err
}

// write system config
err = utils.WriteToSysctl("kernel.perf_event_paranoid=-1")
if !utils.CheckErrorWithMsg(err, "Failed to write sysctl.conf!\n") {
return err
}

// copy and execute the tool
err = utils.CopyToDir(repoPath, "/usr/local/", true)
if !utils.CheckErrorWithMsg(err, "Failed to copy files to /usr/local!\n") {
return err
Expand All @@ -89,18 +94,8 @@ func InstallPmuTools() error {
return err
}

func InstallStock() error {
// Original Bash Scripts: scripts/install_stock.sh

// Install required packages
utils.WaitPrintf("Installing required packages for installing stock")
err := utils.InstallPackages("btrfs-progs pkg-config libseccomp-dev unzip tar libseccomp2 socat util-linux apt-transport-https curl ipvsadm apparmor apparmor-utils")
if !utils.CheckErrorWithTagAndMsg(err, "Failed to install required packages for installing stock") {
return err
}

// Install protoc
utils.WaitPrintf("Installing protoc")
// Install protoc
func InstallProtoc() error {
// Download protoc
protocFilePath, err := utils.DownloadToTmpDir(configs.System.GetProtocDownloadUrl())
if !utils.CheckErrorWithMsg(err, "Failed to download protoc!\n") {
Expand All @@ -111,9 +106,12 @@ func InstallStock() error {
if !utils.CheckErrorWithTagAndMsg(err, "Failed to extract downloaded protoc!\n") {
return err
}
// Success
return nil
}

// Install containerd
utils.WaitPrintf("Installing containerd(ver %s)", configs.System.ContainerdVersion)
// Install containerd
func InstallContainerd() error {
// Download containerd
containerdFilePath, err := utils.DownloadToTmpDir(configs.System.GetContainerdDownloadUrl())
if !utils.CheckErrorWithMsg(err, "Failed to Download containerd(ver %s)\n", configs.System.ContainerdVersion) {
Expand All @@ -124,23 +122,26 @@ func InstallStock() error {
if !utils.CheckErrorWithTagAndMsg(err, "Failed to extract containerd!\n") {
return err
}
return nil
}

// Install runc
// Download and install runc
func InstallRunc() error {
// Download runc
utils.WaitPrintf("Downloading runc(ver %s)", configs.System.RuncVersion)
runcFilePath, err := utils.DownloadToTmpDir(configs.System.GetRuncDownloadUrl())
if !utils.CheckErrorWithTagAndMsg(err, "Failed to download runc(ver %s)!\n", configs.System.RuncVersion) {
if !utils.CheckErrorWithMsg(err, "Failed to download runc(ver %s)!\n", configs.System.RuncVersion) {
return err
}
// Install runc
utils.WaitPrintf("Installing runc")
_, err = utils.ExecShellCmd("sudo install -m 755 %s /usr/local/sbin/runc", runcFilePath)
if !utils.CheckErrorWithTagAndMsg(err, "Failed to install runc!\n") {
if !utils.CheckErrorWithTagAndMsg(err, "Failed to copy runc!\n") {
return err
}
return nil
}

// Install runsc
utils.WaitPrintf("Installing runsc")
// Download and install runsc
func InstallRunsc() error {
// Download runsc
runscFilePath, err := utils.DownloadToTmpDir(configs.System.GetRunscDownloadUrl())
if !utils.CheckErrorWithMsg(err, "Failed to download runsc!\n") {
Expand All @@ -151,17 +152,13 @@ func InstallStock() error {
if !utils.CheckErrorWithTagAndMsg(err, "Failed to install runsc!\n") {
return err
}
return nil
}

// Verify containerd installation
_, err = utils.ExecShellCmd("containerd --version")
if !utils.CheckErrorWithMsg(err, "Failed to build containerd!\n") {
return err
}

// Install k8s
// Download Google Cloud public signing key and Add the Kubernetes apt repository
// Install Kubernetes components
func InstallKubernetes() error {
utils.WaitPrintf("Adding the Kubernetes apt repository")
_, err = utils.ExecShellCmd("sudo mkdir -p /etc/apt/keyrings && sudo curl -fsSLo /etc/apt/keyrings/kubernetes-archive-keyring.gpg https://dl.k8s.io/apt/doc/apt-key.gpg && echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main' | sudo tee /etc/apt/sources.list.d/kubernetes.list")
_, err := utils.ExecShellCmd("sudo mkdir -p /etc/apt/keyrings && sudo curl -fsSLo /etc/apt/keyrings/kubernetes-archive-keyring.gpg https://dl.k8s.io/apt/doc/apt-key.gpg && echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main' | sudo tee /etc/apt/sources.list.d/kubernetes.list")
if !utils.CheckErrorWithTagAndMsg(err, "Failed to add the Kubernetes apt repository!\n") {
return err
}
Expand All @@ -171,8 +168,10 @@ func InstallStock() error {
if !utils.CheckErrorWithTagAndMsg(err, "Failed to install kubeadm, kubelet, kubectl!\n") {
return err
}
return nil
}

// Install knative CLI
func InstallKnativeCLI() error {
// Clone Repo
utils.WaitPrintf("Cloning knative CLI repo")
knativeRepoPath, err := utils.CloneRepoToTmpDir(configs.Knative.KnativeCLIBranch, configs.Knative.KnativeCLIRepoUrl)
Expand All @@ -185,30 +184,109 @@ func InstallStock() error {
if !utils.CheckErrorWithTagAndMsg(err, "Failed to install knative CLI!\n") {
return err
}
return nil
}

// Enable IP forwading & br_netfilter
func ConfigSystemNetwork() error {
// Necessary for containerd as container runtime but not docker
// Set up required sysctl params, these persist across reboots.
// ******************************************************************************
// *********** correct while ugly implementation, need to be improved ***********
// ******************************************************************************
// Enable IP forwading & br_netfilter
// ******** Enable IP forwading & br_netfilter ********
utils.WaitPrintf("Enabling IP forwading & br_netfilter")
_, err = utils.ExecShellCmd("sudo modprobe br_netfilter && sudo modprobe overlay && sudo sysctl -w net.ipv4.ip_forward=1 && sudo sysctl -w net.ipv4.conf.all.forwarding=1 && sudo sysctl -w net.bridge.bridge-nf-call-iptables=1 && sudo sysctl -w net.bridge.bridge-nf-call-ip6tables=1")
_, err := utils.ExecShellCmd(
"sudo modprobe br_netfilter" +
" && sudo modprobe overlay" +
" && sudo sysctl -w net.ipv4.ip_forward=1" +
" && sudo sysctl -w net.ipv4.conf.all.forwarding=1" +
" && sudo sysctl -w net.bridge.bridge-nf-call-iptables=1" +
" && sudo sysctl -w net.bridge.bridge-nf-call-ip6tables=1")
if !utils.CheckErrorWithTagAndMsg(err, "Failed to enable IP forwading & br_netfilter!\n") {
return err
}
// Ensure Boot-Resistant
utils.WaitPrintf("Ensuring Boot-Resistant")
_, err = utils.ExecShellCmd("echo 'br_netfilter' | sudo tee /etc/modules-load.d/netfilter.conf && echo 'overlay' | sudo tee -a /etc/modules-load.d/netfilter.conf && sudo sed -i 's/# *net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf && sudo sed -i 's/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g' /etc/sysctl.conf && echo 'net.bridge.bridge-nf-call-iptables=1\nnet.bridge.bridge-nf-call-ip6tables=1\nnet.ipv4.conf.all.forwarding=1' | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf")
_, err = utils.ExecShellCmd(
"echo 'br_netfilter' | sudo tee /etc/modules-load.d/netfilter.conf && echo 'overlay'" +
" | sudo tee -a /etc/modules-load.d/netfilter.conf" +
" && sudo sed -i 's/# *net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf" +
" && sudo sed -i 's/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g' /etc/sysctl.conf" +
" && echo 'net.bridge.bridge-nf-call-iptables=1\nnet.bridge.bridge-nf-call-ip6tables=1\nnet.ipv4.conf.all.forwarding=1'" +
" | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf")
if !utils.CheckErrorWithTagAndMsg(err, "Failed to ensure Boot-Resistant!\n") {
return err
}

// `sudo sysctl --quiet --system`
_, err = utils.ExecShellCmd("sudo sysctl --quiet --system")
if !utils.CheckErrorWithMsg(err, "Failed to execute `sudo sysctl --quiet --system`!\n") {
return err
}
return nil
}

func InstallStock() error {
// Original Bash Scripts: scripts/install_stock.sh

// Install required packages
utils.WaitPrintf("Installing required packages for installing stock")
err := utils.InstallPackages("btrfs-progs pkg-config libseccomp-dev unzip tar libseccomp2 socat util-linux apt-transport-https curl ipvsadm apparmor apparmor-utils")
if !utils.CheckErrorWithTagAndMsg(err, "Failed to install required packages for installing stock") {
return err
}

// Install protoc
utils.WaitPrintf("Installing protoc")
if err := InstallProtoc(); err != nil {
utils.ErrorPrintf("Failed to install protoc!\n")
return err
}

// Install containerd
utils.WaitPrintf("Installing containerd(ver %s)", configs.System.ContainerdVersion)
if err := InstallContainerd(); err != nil {
utils.ErrorPrintf("Failed to install containerd!\n")
return err
}

// Install runc
utils.WaitPrintf("Installing runc(ver %s)", configs.System.RuncVersion)
if err := InstallRunc(); err != nil {
utils.ErrorPrintf("Failed to install runc!\n")
return err
}

// Install runsc
utils.WaitPrintf("Installing runsc")
if err := InstallRunsc(); err != nil {
utils.ErrorPrintf("Failed to install runsc!\n")
return err
}

// Verify containerd installation
_, err = utils.ExecShellCmd("containerd --version")
if !utils.CheckErrorWithMsg(err, "Failed to build containerd!\n") {
return err
}

// Install Kubernetes
utils.InfoPrintf("Installing Kubernetes\n")
if err := InstallKubernetes(); err != nil {
utils.ErrorPrintf("Failed to install Kubernetes!\n")
return err
}

// Install knative CLI
utils.InfoPrintf("Installing Knative CLI\n")
if err := InstallKnativeCLI(); err != nil {
utils.ErrorPrintf("Failed to install Knative CLI!\n")
return err
}

// Configure system network
utils.InfoPrintf("Configuring system network\n")
if err := ConfigSystemNetwork(); err != nil {
utils.ErrorPrintf("Failed to configure system network!\n")
return err
}

// Success
return nil
Expand Down
14 changes: 9 additions & 5 deletions scripts/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ import (

// Set up firecracker containerd
func SetupFirecrackerContainerd() error {
_, err := utils.ExecShellCmd("sudo mkdir -p /etc/firecracker-containerd && sudo mkdir -p /var/lib/firecracker-containerd/runtime && sudo mkdir -p /etc/containerd/")
_, err := utils.ExecShellCmd(
"sudo mkdir -p /etc/firecracker-containerd" +
" && sudo mkdir -p /var/lib/firecracker-containerd/runtime" +
" && sudo mkdir -p /etc/containerd/")
if err != nil {
return err
}
Expand Down Expand Up @@ -308,10 +311,11 @@ func SetupZipkin() error {

// Enable tracing in Knative
utils.WaitPrintf("Enabling tracing in Knative")
_, err = utils.ExecShellCmd(`kubectl patch configmap/config-tracing \
-n knative-serving \
--type merge \
-p '{"data":{"backend":"zipkin","zipkin-endpoint":"http://zipkin.istio-system.svc.cluster.local:9411/api/v2/spans","debug":"true"}}'`)
_, err = utils.ExecShellCmd(
`kubectl patch configmap/config-tracing` +
` -n knative-serving` +
` --type merge` +
` -p '{"data":{"backend":"zipkin","zipkin-endpoint":"http://zipkin.istio-system.svc.cluster.local:9411/api/v2/spans","debug":"true"}}'`)
if !utils.CheckErrorWithTagAndMsg(err, "Failed to enable tracing in Knative!\n") {
return err
}
Expand Down

0 comments on commit 24b1db7

Please sign in to comment.