Skip to content

Commit

Permalink
Make it easier to deploy with argo
Browse files Browse the repository at this point in the history
Adding a simple script to help make it easy to deploy policies with
argocd.

Signed-off-by: Gus Parvin <[email protected]>
(cherry picked from commit 17dad52)
  • Loading branch information
gparvin authored and JustinKuli committed Apr 4, 2024
1 parent c3d1682 commit 21fda6e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ namespace something else, you can run `kubectl create ns <custom ns>` instead.

From within this directory in terminal, run `cd deploy` to access the deployment directory, then run
`bash ./deploy.sh -u <url> -p <path> -n <namespace>`. (Details on all of the parameters for this
command can be viewed in its [README](deploy/README.md).)
command can be viewed in its [README](deploy/README.md).) This script assumes you have enabled
Application lifecycle management as an addon in your Open Cluster Management installation. See
[Application lifecycle management](https://open-cluster-management.io/getting-started/integration/app-lifecycle/)
for details on installing the Application addon.
**Note**: If you are using ArgoCD for gitops, a similar script [argoDeploy.sh](deploy/argoDeploy.sh) is provided that does
not require the Application Lifecycle addon.

The policies are applied to all managed clusters that are available, and have the `environment` set
to `dev`. Specifically, an available managed cluster has the `status` parameter set to `true` by the
Expand Down
7 changes: 6 additions & 1 deletion deploy/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Deploy policies to Open Cluster Management

Deploy policies to Open Cluster Management with the `deploy.sh` script.
Deploy policies to Open Cluster Management with the `deploy.sh` script. The `deploy.sh` script uses Application Lifecycle subscriptions for deployment. To use ArgoCD use the [argoDeploy.sh](argoDeploy.sh) script instead.

## Deploying policies with GitOps

Expand Down Expand Up @@ -38,6 +38,8 @@ Usage:
--dry-run Print the YAML to stdout without applying them to the cluster
```

**Note**: The `argoDeploy.sh` uses similar options to `deploy.sh` but does not provide the sync rate or the deploy app options.

For more details on the `sync` parameter values, see the git subscription chapter
[Resource reconciliation rate settings](https://github.com/stolostron/multicloud-operators-subscription/blob/main/docs/gitrepo_subscription.md#resource-reconciliation-rate-settings).

Expand Down Expand Up @@ -66,3 +68,6 @@ Usage:
-n|--namespace <namespace> Namespace on the cluster that resources are located
-a|--name <resource-name> Prefix for the Channel and Subscription resources
```

**Note**: Use the ArgoCD console interface to remove the application deployed using the `argoDeploy.sh` script.

135 changes: 135 additions & 0 deletions deploy/argoDeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/bin/bash

set -e
set -o pipefail

# Display help information
help () {
echo "Deploy policies to Open Cluster Management via OpenShift GitOps"
echo ""
echo "Prerequisites:"
echo " - oc or kubectl CLI must be pointing to the cluster to which to deploy policies"
echo " - OpenShift Gitops must be installed and functional"
echo ""
echo "Usage:"
echo " ./deploy.sh [-u <url>] [-b <branch>] [-p <path/to/dir>] [-n <namespace>]"
echo " [-a|--name <application-name>] [--dry-run]"
echo ""
echo " -h|--help Display this menu"
echo " -u|--url <url> URL to the Git repository"
echo ' (Default URL: "https://github.com/open-cluster-management-io/policy-collection.git")'
echo " -b|--branch <branch> Branch of the Git repository to point to"
echo ' (Default branch: "main")'
echo " -p|--path <path/to/dir> Path to the desired subdirectory of the Git repository"
echo " (Default path: stable)"
echo " -n|--namespace <namespace> Namespace on the cluster to deploy policies to (must exist already)"
echo ' (Default namespace: "policies")'
echo " -a|--name <resource-name> The name of the OpenShift Gitops Application"
echo ' (Default name: "demo-stable-policies")'
echo " --dry-run Print the YAML to stdout without applying them to the cluster"
echo ""
}

# Parse arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
help
exit 0
;;
-u|--url)
shift
GH_URL=${1}
shift
;;
-b|--branch)
shift
GH_BRANCH=${1}
shift
;;
-p|--path)
shift
GH_PATH=${1}
shift
;;
-n|--namespace)
shift
NAMESPACE=${1}
shift
;;
-a|--name)
shift
NAME=${1}
shift
;;
--dry-run)
shift
DRY_RUN="true"
;;
*) # default
echo "Invalid input: ${1}"
exit 1
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

if ! kubectl get deployment openshift-gitops-server -n openshift-gitops &>/dev/null; then
echo "The OpenShift Gitops server is required, but is not installed."
exit 1
fi

# Display configuration and set default values if needed
echo "Deploying policies using the following configuration:"
echo "====================================================="
echo "kubectl config: $(kubectl config get-contexts | awk '/^\052/ {print $4"/"$3}')"
echo "Cluster Namespace: ${NAMESPACE:=policies}"
echo "Application name: ${NAME:=demo-stable-policies}"
echo "Git URL: ${GH_URL:=https://github.com/open-cluster-management-io/policy-collection.git}"
echo "Git Branch: ${GH_BRANCH:=main}"
echo "Git Path: ${GH_PATH:=stable}"
echo "Dry run: ${DRY_RUN:=false}"
echo "====================================================="

while read -r -p "Would you like to proceed (y/n)? " response; do
case "$response" in
Y|y|Yes|yes ) break
;;
N|n|No|no ) exit 1
;;
esac
done

APPLICATION="apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: $NAME
namespace: openshift-gitops
spec:
destination:
namespace: $NAMESPACE
server: https://kubernetes.default.svc
project: default
source:
path: $GH_PATH
repoURL: $GH_URL
targetRevision: $GH_BRANCH
syncPolicy:
automated: {}
syncOptions:
- CreateNamespace=true"

# Deploy the resources to the cluster
echo "$APPLICATION" > manifests.yaml
echo "* Application manifests saved to 'manifests.yaml'"
if [ "$DRY_RUN" = "true" ]; then
echo "* Dry-run is enabled. Not creating resources on cluster."
echo "---"
echo "$APPLICATION"
else
printf ""
echo "$APPLICATION" | kubectl apply -f -
fi

0 comments on commit 21fda6e

Please sign in to comment.