## Cluster Gitops - Workspaces-Projects-Gitops
### The purpose of this tutorial is to show the user how to:
* Create a workspace
* Attach a cluster to the workspace
* Create a project in the created workspace
* Create a gitops source in the created project
* Deploy the above through a Gitlab pipeline
## Workspace Creation
Pre-requisites
* A DKP Enterprise cluster
* A DKP Essentials cluster that is not attached to a kommander cluster
* The kubeconfig for the new cluster
This document shows two methods to accomplish these tasks. The first is through a command line. The second is through a Gitlab pipeline. As of version 2.2 of DKP, the only step that can't be accomplished through using DKP command is project creation.
#### Method 1 - Command line
A workspace is created using the dkp command. The command to create a workspace looks like:
Declare variables
```
export WORKSPACE=workspacename
export NAMESPACE=workspace namespace
export CLUSTER_NAME=clustername #Descriptive name of a cluster to attach to the new workspace.
export KUBECONFIG=config #DKP Enterprise kube config
export NEWKUBECONFIG=newconfig # Cluster to attach kube config
export PROJECT_DESCRIPTION=
export PROJECT_NAME=
export PROJECT_NAMESPACE=
export PROJECT_CLUSTER_1=
```
Create workspace:
```
$ kubectl --kubeconfig ${KUBECONFIG} apply -f- << EOF
apiVersion: workspaces.kommander.mesosphere.io/v1alpha1
kind: Workspace
metadata:
name: ${WORKSPACE}
spec:
namespaceName: ${NAMESPACE}
EOF
```
Attach new cluster
```
kubectl --kubeconfig ${KUBECONFIG} apply -f- << EOF
apiVersion: v1
data:
kubeconfig: $(cat ${NEWKUBECONFIG} | base64)
kind: Secret
metadata:
name: ${CLUSTER_NAME}-kubeconfig
namespace: ${NAMESPACE}
type: Opaque
---
apiVersion: kommander.mesosphere.io/v1beta1
kind: KommanderCluster
metadata:
name: ${CLUSTER_NAME}
namespace: ${NAMESPACE}
spec:
kubeconfigRef:
name: ${CLUSTER_NAME}-kubeconfig
EOF
```
To see what is created in the cluster when you create a workspace, execute:
```
kubectl get workspaces --kubeconfig ${KUBECONFIG}
NAME DISPLAY NAME WORKSPACE NAMESPACE AGE
ciscotoday ciscotoday 7h54m
default-workspace Default Workspace kommander-default-workspace 25h
firewall firewall 18h
ips ips 17h
kommander-workspace Management Cluster Workspace kommander 25h
```
Here you see that there are 5 workspaces including the two that are built in to Kommander. From here you can describe the workspaces to see more information with:
```
kubectl describe workspaces
````
#### Project creation
Create a yaml file like this example:
```
kubectl --kubeconfig ${KUBECONFIG} apply -f- << EOF
apiVersion: workspaces.kommander.mesosphere.io/v1alpha1
kind: Project
metadata:
annotations:
kommander.mesosphere.io/description: ${PROJECT_DESCRIPTION}
kommander.mesosphere.io/display-name: ${PROJECT_NAME}
labels:
project-owner: qa
name: ${PROJECT_DESCRIPTION}
namespace: ${NAMESPACE}
spec:
namespaceName: ${PROJECT_NAMESPACE}
placement:
clusters:
- name: ${PROJECT_CLUSTER_1}
workspaceRef:
name: ${WORKSPACE}
EOF
```
Applying the below yaml will create a project
"kubectl apply -f {file}.yaml"
that looks like this:

```
apiVersion: dispatch.d2iq.io/v1alpha2
kind: GitopsRepository
metadata:
name: gitopsid
namespace: projectnamespace1
spec:
cloneUrl: https://github.com/kisahm/dkp-demo-sockshop
template:
ref:
branch: master
```
You now have a workspace with a cluster attached and a project with gitops.
Lets look at the cluster resources.
Workspaces
```kubectl get workspaces -A```
```NAME DISPLAY NAME WORKSPACE NAMESPACE AGE
ciscotoday ciscotoday 11h
default-workspace Default Workspace kommander-default-workspace 29h
firewall firewall 21h
ips ips 20h
```
Projects
kubectl get projects -A
```
NAMESPACE NAME DISPLAY NAME PROJECT NAMESPACE AGE
ips cisco909 cisco909 cisco909 18h
kommander cisco9 cisco909 cisco9 18h
kommander dkp dkp dkp 153m
kommander projectnamespace1 projectname1 projectnamespace1 122m
```
```kubectl get gitopsrepositories -A```
```NAMESPACE NAME AGE
cisco9 socks89 18h
cisco909 socks89 18h
dkp dkp 144m
projectnamespace1 gitopsid 116m
```
For each of the cluster resources listed you can describe those for more information. For more information on the status of releases and gitops see this [Link](https://docs.d2iq.com/dkp/kommander/2.2/projects/project-deployments/continuous-delivery/)
#### Method 2 - Gitlab Pipeline
In Gitlab create a pipeline in the pipeline editor. See this [doc](https://github.com/) for detailed instructions to setup a gitlab instance and pipeline.

Pipeline code
```
deploy:
image: registry.dev.dkp2demo.com/bjacobs/brucerepo/dkpcontainer:v1.2
script:
- cd /home
- echo $KUBECONFIGCONTENT | base64 -d > config
- export WORKSPACE=workspacename
- export NAMESPACE=workspacenamespace
- export CLUSTER_NAME=clustername
- export KUBECONFIG=config
- export NEWKUBECONFIG=newconfig
- ./dkp create workspace ${WORKSPACE} --namespace ${NAMESPACE} --kubeconfig ${KUBECONFIG}
- ./dkp attach cluster -n ${CLUSTER_NAME} --attached-kubeconfig ${NEWKUBECONFIG} -w ${WORKSPACE}
- kubectl apply -f https://gitlab.dev.dkp2demo.com/bjacobs/brucerepo/-/raw/main/project.yaml?inline=false
```
Note: It is possible to apply everything via yaml instead of using dkp commands. This will be updated in the next version of this document.