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

Feature/Replace Rancher API calls by kubectl calls #2

Merged
merged 10 commits into from
Sep 9, 2024
184 changes: 65 additions & 119 deletions scripts/rancher/cluster-actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,125 +3,84 @@

#######################################
# List clusters managed by Rancher
# Arguments:
# Rancher URL
# token
# Examples:
# rancher_list_clusters rancher.random_string.geek xxxxx
#######################################
rancher_list_clusters() {
local rancherUrl=$1
local token=$2

echo "Listing clusters registered in Rancher..."
curl -s -k "$rancherUrl/v3/clusters" -H "Authorization: Bearer $token" | jq .
kubectl get clusters.provisioning.cattle.io --all-namespaces | awk 'NR>1 {print $2}'
oleg-vorobiov-suse marked this conversation as resolved.
Show resolved Hide resolved
}

#######################################
# Create downstream custom cluster in Rancher
# Globals:
# CLUSTER_ID
# Arguments:
# Rancher URL
# token
# name
# version (Kubernetes)
# Examples:
# rancher_create_customcluster rancher.random_string.geek xxxxx demo 'v1.27.16+rke2r1'
# rancher_create_customcluster demo 'v1.27.16+rke2r1'
#######################################
rancher_create_customcluster() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to be validated manually (as the CI pipeline is not possible for the moment)

Copy link
Collaborator

@devpro devpro Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I duplicated Rancher Playground track, called "Rancher Playground PR review", and started it.

Got the error:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check on this in a few

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
Its alive

Copy link
Contributor Author

@oleg-vorobiov-suse oleg-vorobiov-suse Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also updated the variables in a main script to get the latest k8s and Rancher

# defines variables
K3S_VERSION='v1.30'
CERTMANAGER_VERSION='v1.13.0'
LETSENCRYPT_EMAIL_ADDRESS='[email protected]'
RANCHER_REPOSITORY='latest'
RANCHER_VERSION='2.9.1'
RANCHER_DOMAIN="rancher.${HOSTNAME}.${_SANDBOX_ID}.instruqt.io"
RANCHER_REPLICAS='1'
ADMIN_PASSWORD='Sus3R@ncherR0x'
INGRESS_CLASSNAME='traefik'
DOWNSTREAM_CLUSTER_NAME='demo'
RKE2_K8S_VERSION='v1.27.16+rke2r1'

local rancherUrl=$1
local token=$2
local name=$3
local version=$4
local name=$1
local version=$2

echo "Creating downstream cluster in Rancher..."
CLUSTER_CONFIG=$(cat <<EOF
{
"type": "provisioning.cattle.io.cluster",
"metadata": {
"namespace": "fleet-default",
"name": "$name"
},
"spec": {
"rkeConfig": {
"chartValues": {
"rke2-calico": {}
},
"upgradeStrategy": {
"controlPlaneConcurrency": "1",
"controlPlaneDrainOptions": {
"deleteEmptyDirData": true,
"disableEviction": false,
"enabled": false,
"force": false,
"gracePeriod": -1,
"ignoreDaemonSets": true,
"skipWaitForDeleteTimeoutSeconds": 0,
"timeout": 120
},
"workerConcurrency": "1",
"workerDrainOptions": {
"deleteEmptyDirData": true,
"disableEviction": false,
"enabled": false,
"force": false,
"gracePeriod": -1,
"ignoreDaemonSets": true,
"skipWaitForDeleteTimeoutSeconds": 0,
"timeout": 120
}
},
"machineGlobalConfig": {
"cni": "calico",
"disable-kube-proxy": false,
"etcd-expose-metrics": false
},
"machineSelectorConfig": [
{
"config": {
"protect-kernel-defaults": false
}
}
],
"etcd": {
"disableSnapshots": false,
"s3": null,
"snapshotRetention": 5,
"snapshotScheduleCron": "0 */5 * * *"
},
"registries": {
"configs": {},
"mirrors": {}
},
"machinePools": []
},
"machineSelectorConfig": [
{
"config": {}
}
],
"kubernetesVersion": "$version",
"defaultPodSecurityAdmissionConfigurationTemplateName": "",
"localClusterAuthEndpoint": {
"enabled": false,
"caCerts": "",
"fqdn": ""
}
}
}
cat <<EOF | kubectl apply -f -
apiVersion: provisioning.cattle.io/v1
kind: Cluster
metadata:
name: "$name"
namespace: fleet-default
spec:
kubernetesVersion: "$version"
localClusterAuthEndpoint: {}
rkeConfig:
chartValues:
rke2-calico: {}
dataDirectories: {}
etcd:
snapshotRetention: 5
snapshotScheduleCron: 0 */5 * * *
machineGlobalConfig:
cni: calico
disable-kube-proxy: false
etcd-expose-metrics: false
machinePoolDefaults: {}
machineSelectorConfig:
- config:
protect-kernel-defaults: false
registries: {}
upgradeStrategy:
controlPlaneConcurrency: '1'
controlPlaneDrainOptions:
deleteEmptyDirData: true
disableEviction: false
enabled: false
force: false
gracePeriod: -1
ignoreDaemonSets: true
ignoreErrors: false
postDrainHooks: null
preDrainHooks: null
skipWaitForDeleteTimeoutSeconds: 0
timeout: 120
workerConcurrency: '1'
workerDrainOptions:
deleteEmptyDirData: true
disableEviction: false
enabled: false
force: false
gracePeriod: -1
ignoreDaemonSets: true
ignoreErrors: false
postDrainHooks: null
preDrainHooks: null
skipWaitForDeleteTimeoutSeconds: 0
timeout: 120
EOF
)

CLUSTER_CREATION_RESPONSE=$(curl -s -k -H "Authorization: Bearer $token" \
-H 'Content-Type: application/json' \
-X POST \
-d "$CLUSTER_CONFIG" \
"$rancherUrl/v1/provisioning.cattle.io.clusters")
echo "DEBUG CLUSTER_CREATION_RESPONSE=${CLUSTER_CREATION_RESPONSE}"

sleep 10

rancher_get_clusterid $rancherUrl $token $name
rancher_get_clusterid $name
echo "DEBUG CLUSTER_ID=${CLUSTER_ID}"
}

Expand All @@ -130,42 +89,29 @@ EOF
# Globals:
# CLUSTER_ID
# Arguments:
# Rancher URL
# token
# name
# Examples:
# rancher_get_clusterid rancher.random_string.geek xxxxx demo
# rancher_get_clusterid demo
#######################################
rancher_get_clusterid() {
local rancherUrl=$1
local token=$2
local name=$3
local name=$1

CLUSTER_ID=$(curl -s ${rancherUrl}/v3/clusters?name=${name} \
-H 'content-type: application/json' \
-H "Authorization: Bearer ${token}" \
| jq -r .data[0].id)
CLUSTER_ID=$(kubectl get cluster.provisioning.cattle.io -n fleet-default -o=jsonpath="{range .items[?(@.metadata.name==\"${name}\")]}{.status.clusterName}{end}")
}

#######################################
# Get cluster registration command line from Rancher
# Globals:
# REGISTRATION_COMMAND
# Arguments:
# Rancher URL
# token
# cluster ID
# Examples:
# rancher_get_clusterregistrationcommand rancher.random_string.geek xxxxx 42
# rancher_get_clusterregistrationcommand 42
#######################################
rancher_get_clusterregistrationcommand() {
local rancherUrl=$1
local token=$2
local id=$3

CLUSTER_REGISTRATION_RESPONSE=$(curl -s -k -H "Authorization: Bearer $token" "${rancherUrl}/v3/clusters/$id/clusterRegistrationTokens")
echo "DEBUG CLUSTER_REGISTRATION_RESPONSE=${CLUSTER_REGISTRATION_RESPONSE}"
local id=$1

REGISTRATION_COMMAND=$(echo $CLUSTER_REGISTRATION_RESPONSE | jq -r '.data[0].nodeCommand')
REGISTRATION_COMMAND=$(kubectl get clusterregistrationtoken.management.cattle.io -n $id -o=jsonpath='{.items[*].status.nodeCommand}'
)
echo "DEBUG REGISTRATION_COMMAND=${REGISTRATION_COMMAND}"
}
3 changes: 2 additions & 1 deletion scripts/rancher/manager-lifecycle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rancher_install_withcertmanagerclusterissuer() {
local hostname=$4
local clusterissuer=$5


echo "Installing Rancher..."
helm repo add rancher-${repository} https://releases.rancher.com/server-charts/${repository}
helm repo update
Expand Down Expand Up @@ -54,7 +55,7 @@ rancher_first_login() {
rancher_login_withpassword $rancherUrl 'admin' $BOOTSTRAP_PASSWORD
echo "DEBUG LOGIN_TOKEN=${LOGIN_TOKEN}"
rancher_update_password $rancherUrl $LOGIN_TOKEN $BOOTSTRAP_PASSWORD $newPassword
rancher_update_serverurl $rancherUrl $LOGIN_TOKEN
rancher_update_serverurl $rancherUrl
}

#######################################
Expand Down
14 changes: 4 additions & 10 deletions scripts/rancher/manager-settings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
# Set Rancher Server URL setting
# Arguments:
# Rancher URL
# Token
# Examples:
# rancher_update_serverurl https://rancher.random_string.geek xxxxx
# rancher_update_serverurl https://rancher.random_string.geek
#######################################
rancher_update_serverurl() {
local rancherUrl=$1
local token=$2

echo "Sets Rancher URL in settings..."
curl -s -k -H "Authorization: Bearer $token" \
-H 'Content-Type: application/json' \
-X PUT \
-d '{
"value": "'"$rancherUrl"'"
}' \
"$rancherUrl/v3/settings/server-url"
kubectl patch settings.management.cattle.io server-url --type='merge' --patch '{ ─╯
"value": "'$rancherUrl'"
}'
}
2 changes: 1 addition & 1 deletion scripts/rancher/user-actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ rancher_create_apikey() {
echo "DEBUG API_KEY_RESPONSE=${API_KEY_RESPONSE}"
API_TOKEN=$(echo $API_KEY_RESPONSE | jq -r .token)
sleep 5
}
}