Skip to content
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

Install system agent #8

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bootstrap/internal/cloudinit/controlplane_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ runcmd:
- 'systemctl start rke2-server.service'
- 'mkdir /run/cluster-api'
- '{{ .SentinelFileCommand }}'
- 'sudo sh /opt/system-agent-install.sh'
{{- template "commands" .PostRKE2Commands }}
{{ .AdditionalCloudInit -}}
`
Expand Down
15 changes: 15 additions & 0 deletions pkg/rke2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"

Check failure on line 27 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s blank -s dot -s default -s prefix(sigs.k8s.io/cluster-api) -s prefix(github.com/rancher-sandbox/cluster-api-provider-rke2) --custom-order (gci)
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

Check failure on line 31 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/rancher-sandbox/cluster-api-provider-rke2 (goimports)
bootstrapv1 "github.com/rancher-sandbox/cluster-api-provider-rke2/bootstrap/api/v1beta1"
controlplanev1 "github.com/rancher-sandbox/cluster-api-provider-rke2/controlplane/api/v1beta1"
"github.com/rancher-sandbox/cluster-api-provider-rke2/pkg/consts"
bsutil "github.com/rancher-sandbox/cluster-api-provider-rke2/pkg/util"
"k8s.io/client-go/tools/clientcmd"

Check failure on line 36 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s blank -s dot -s default -s prefix(sigs.k8s.io/cluster-api) -s prefix(github.com/rancher-sandbox/cluster-api-provider-rke2) --custom-order (gci)
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

Expand Down Expand Up @@ -371,23 +371,27 @@
Kind: "Setting",
Version: "v3",
})
if err := opts.Client.Get(context.Background(), client.ObjectKey{

Check failure on line 374 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)

Check failure on line 374 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
Name: "server-url",
}, serverUrlSetting); err != nil {
return nil, nil, fmt.Errorf("failed to get server url setting: %w", err)
}
serverUrl, ok := serverUrlSetting.Object["value"].(string)

Check failure on line 379 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

assignments should only be cuddled with other assignments (wsl)
if !ok {

Check failure on line 380 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)
return nil, nil, fmt.Errorf("server url setting is missing value")
}

if serverUrl == "" {
return nil, nil, fmt.Errorf("server url is empty")
}

caSetting := &unstructured.Unstructured{}
caSetting.SetGroupVersionKind(schema.GroupVersionKind{
Group: "management.cattle.io",
Kind: "Setting",
Version: "v3",
})
if err := opts.Client.Get(context.Background(), client.ObjectKey{

Check failure on line 394 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
Name: "cacerts",
}, caSetting); err != nil {
return nil, nil, fmt.Errorf("failed to get ca setting: %w", err)
Expand Down Expand Up @@ -418,7 +422,7 @@
},
CurrentContext: "agent",
})

Check failure on line 425 in pkg/rke2/config.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate kubeconfig: %w", err)
}
Expand All @@ -440,12 +444,14 @@

files = append(files, bootstrapv1.File{
Path: "/etc/rancher/agent/connect-info-config.json",
Owner: consts.DefaultFileOwner,
Permissions: "0600",
Content: string(connectInfoConfigJson),
})

files = append(files, bootstrapv1.File{
Path: "/etc/rancher/agent/config.yaml",
Owner: consts.DefaultFileOwner,
Permissions: "0600",
Content: `workDirectory: /var/lib/rancher/agent/work
localPlanDirectory: /var/lib/rancher/agent/plans
Expand All @@ -454,6 +460,15 @@
preserveWorkDirectory: true`,
})

serverUrlBash := fmt.Sprintf("CATTLE_SERVER=%s\n", serverUrl)

files = append(files, bootstrapv1.File{
Path: "/opt/system-agent-install.sh",
Owner: consts.DefaultFileOwner,
Permissions: "0600",
Content: fmt.Sprintf("%s%s", serverUrlBash, installsh),
})

return rke2ServerConfig, files, nil
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/rke2/installsh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package rke2

var installsh = `
#!/bin/sh

set -x
CURL_LOG="-v"

echo "Downloading cert"
CACERT=$(mktemp)
curl --connect-timeout 60 --max-time 60 --write-out "%{http_code}\n" --insecure ${CURL_LOG} -fL "${CATTLE_SERVER}/cacerts" -o ${CACERT}

echo "Download system agent binary"
CURL_CAFLAG="--cacert ${CACERT}"
CATTLE_AGENT_BIN_PREFIX="/usr/local"
mkdir -p ${CATTLE_AGENT_BIN_PREFIX}/bin
curl --connect-timeout 60 --max-time 300 --write-out "%{http_code}\n" ${CURL_CAFLAG} -v -fL "${CATTLE_SERVER}/assets/rancher-system-agent-amd64" -o "${CATTLE_AGENT_BIN_PREFIX}/bin/rancher-system-agent"

Check failure on line 17 in pkg/rke2/installsh.go

View workflow job for this annotation

GitHub Actions / lint

line is 201 characters (lll)
chmod +x "${CATTLE_AGENT_BIN_PREFIX}/bin/rancher-system-agent"

echo "systemd: Creating service file"
cat <<-EOF >"/etc/systemd/system/rancher-system-agent.service"
[Unit]
Description=Rancher System Agent
Documentation=https://www.rancher.com
Wants=network-online.target
After=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
EnvironmentFile=-/etc/default/rancher-system-agent
EnvironmentFile=-/etc/sysconfig/rancher-system-agent
EnvironmentFile=-/etc/systemd/system/rancher-system-agent.env
Type=simple
Restart=always
RestartSec=5s
Environment=CATTLE_LOGLEVEL=debug
Environment=CATTLE_AGENT_CONFIG=/etc/rancher/agent/config.yaml
ExecStart=${CATTLE_AGENT_BIN_PREFIX}/bin/rancher-system-agent sentinel
EOF

FILE_SA_ENV="/etc/systemd/system/rancher-system-agent.env"
echo "Creating environment file ${FILE_SA_ENV}"
install -m 0600 /dev/null "${FILE_SA_ENV}"
for i in "HTTP_PROXY" "HTTPS_PROXY" "NO_PROXY"; do
eval v=\"\$$i\"
if [ -z "${v}" ]; then
env | grep -E -i "^${i}" | tee -a ${FILE_SA_ENV} >/dev/null
else
echo "$i=$v" | tee -a ${FILE_SA_ENV} >/dev/null
fi
done

systemctl daemon-reload >/dev/null
echo "Enabling rancher-system-agent.service"
systemctl enable rancher-system-agent
echo "Starting/restarting rancher-system-agent.service"
systemctl restart rancher-system-agent
rm -f ${CATTLE_AGENT_VAR_DIR}/interlock/restart-pending
`
Loading