Skip to content
Open
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
Binary file added .DS_Store
Binary file not shown.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
# delegate-quickstart-api
# Deploy Docker Delegate and Setup Local K8s Infrastructure In Under 5 Minutes
## Prerquisites

---
- Tools Used
- [**k3d**](https://k3d.io/v5.4.6/) - K3d is wrapper to k3s on docker,we used k3d to create a single node Kubernetes cluster locally
- [**docker**](https://docs.docker.com/install/) to be able to use k3d
- [**kubectl**](https://k3d.io/v5.4.6/) to interact with the Kubernetes cluster
* You need to create a Harness API Key, using the Harness SaaS platform, the detailed steps for the same are mentioned [here](https://docs.harness.io/article/bn72tvbj6r-harness-rest-api-reference#step_1_create_a_harness_api_key_and_pat), further details on managing the API keys and the PAT can be found in our [documentation](https://docs.harness.io/article/tdoad7xrh9-add-and-manage-api-keys#harness_api_key).
* Next, start with [creating an organisation](https://docs.harness.io/article/36fw2u92i4-create-an-organization#step_1_create_a_harness_org), followed by [creating a project](https://docs.harness.io/article/36fw2u92i4-create-an-organization#step_3_create_a_project) and then start with creating a pipeline under any module of your choice, and move to the YAML view to find the "ORGIDENTIFIER" and "PROJECTIDENTIFIER".
![Alt Text](readme-images/YAML-view.png)
* For "ACCOUNTIDENTIFIER", is same as the Account ID, which could be found in the URL or under the account settings.
![Alt Text](readme-images/account-id.png)
:tada: Now get started with the script to get your delegate installed.

## Steps to get the script running

---
* Export the below variables in your environment 

- For Windows users:

```bash
set APIKEY=‘XXXXXX’
set ACCOUNTIDENTIFIER=‘XXXXXX’
set ORGIDENTIFIER=‘default’
set PROJECTIDENTIFIER=‘TestProject’
set TOKENIDENTIFIER=‘mytoken1’
set KUBAENETESDELEGATENAME=‘k8delgate’
set DOCKERDELEGATENAME=‘dockerdelegate’
```
- For Linux and MacOS users:

```bash
export APIKEY='XXXXXX'
export ACCOUNTIDENTIFIER='XXXXXX'
export ORGIDENTIFIER='default'
export PROJECTIDENTIFIER='TestProject'
export TOKENIDENTIFIER='mytoken1'
export KUBAENETESDELEGATENAME='k8delgate'
export DOCKERDELEGATENAME='dockerdelegate'
```

* Finally run the below  command

```bash
curl -s -H "Accept:application/vnd.github.v3.raw" https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh>installk3d.sh && curl -s -H "Accept:application/vnd.github.v3.raw" https://raw.githubusercontent.com/ronakforgit/delegate-quickstart-api/delegate-script/automatedelegate.sh | bash
```
69 changes: 69 additions & 0 deletions automatedelegate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
#
##########################################################
# Get Delegate docker-compose file and deploy it locally #
##########################################################
#
#
print_color(){
GREEN=$(tput setaf 2)
NC=$(tput sgr0)
echo -e " "
echo -e "*${GREEN} ${1} ${NC} "

}
# check if docker and kubectl are installed.

if ! [ -x "$(command -v kubectl)" ]; then
print_color 'Error: kubectl is not installed.' >&2
exit 1
fi

if ! [ -x "$(command -v docker)" ]; then
print_color 'Error: docker is not installed.' >&2
exit 1
fi

#install k3ds
print_color " Installing k3ds.. "
bash installk3d.sh
print_color " Installing k3ds -- completed .. "

#create single node cluster
print_color "Creating single node cluster.. "
k3d cluster create mylocalcluster
print_color " Creating single node cluster -- completed "

#create delegate token
print_color " Creating delegate token and delegate.yml. "
curl -X POST "https://app.harness.io/gateway/ng/api/delegate-token-ng?accountIdentifier=${ACCOUNTIDENTIFIER}&orgIdentifier=${ORGIDENTIFIER}&projectIdentifier=${PROJECTIDENTIFIER}&tokenName=${TOKENIDENTIFIER}" -H "x-api-key: ${APIKEY}" -H "Content-Length: 0" --silent --show-error

#download kubernetes delegate mainifest
curl -X POST "https://app.harness.io/gateway/ng/api/download-delegates/docker?accountIdentifier=${ACCOUNTIDENTIFIER}&orgIdentifier=${ORGIDENTIFIER}&projectIdentifier=${PROJECTIDENTIFIER}" -H "Content-Type: application/json" -H "x-api-key: ${APIKEY}" -o "docker-compose.yml" --silent --show-error --data-binary @- <<DATA
{
"name": "${KUBAENETESDELEGATENAME}",
"size": "LAPTOP",
"tags": [
"string"
],
"tokenName": "${TOKENIDENTIFIER}",
"clusterPermissionType": "CLUSTER_ADMIN"
}
DATA
print_color "--"
print_color "creating delegate token and delegate.yml -- completed "

# apply delegate in cluster
print_color " apply delegate Docker compose "
docker-compose -f docker-compose.yml up -d
print_color " delegate applied successfully -- completed"

print_color "Creating Service Account with cluster admin role in k3d cluster"
kubectl create serviceaccount myserviceaccount
kubectl create clusterrolebinding comman-admin-sa-crb --clusterrole=cluster-admin --serviceaccount=default:myserviceaccount
print_color "Use Below Service Account Token while setting up kubernets connector "
kubectl create token myserviceaccount
print_color "Use below url as master node url for kuberntes connector setup"
export KPORT=$(kubectl cluster-info | head -n 1 | sed 's/.*://')
export KIP=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | head -1 | awk '{ print $2 }')
echo "https://${KIP}:${KPORT}"
Binary file added readme-images/.DS_Store
Binary file not shown.
Binary file added readme-images/YAML-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme-images/account-id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.