Skip to content

Commit

Permalink
Feature/Replace Rancher API calls by kubectl calls (#2)
Browse files Browse the repository at this point in the history
* reduced the number of API calls, simplified the code

* removed automation api token creation

* reverted some changes, modified upadate serverurl function

* removed one unneccessary parameter

* awk to jsonpath

* fixed waiting for nodes function

* debuggind serverurl

* serverurl

---------

Co-authored-by: olegvorobiov <[email protected]>
Co-authored-by: Bertrand Thomas <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2024
1 parent 817a36d commit 4545d7a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 131 deletions.
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 -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
}

#######################################
# 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() {
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
}
}

0 comments on commit 4545d7a

Please sign in to comment.