Compare commits

...

10 Commits

Author SHA1 Message Date
Patryk Gudalewicz e562634d9a Added curl command info to Readme 2022-11-03 14:00:32 +01:00
Patryk Gudalewicz b7ce89b3f7 Added curl command info to Readme 2022-11-03 13:59:16 +01:00
Patryk Gudalewicz 3501b8340c Adjusting readme 2022-10-28 10:39:20 +02:00
Patryk Gudalewicz b86f267408 Adjusting parameters 2022-10-28 10:25:13 +02:00
Patryk Gudalewicz ce782dc70d Adjusting parameters 2022-10-28 10:18:44 +02:00
Patryk Gudalewicz 07cbe8e4c9 Adjusting parameters 2022-10-28 10:14:47 +02:00
Patryk Gudalewicz 2b12f710a0 Adjusting env calls 2022-10-28 10:02:40 +02:00
Patryk Gudalewicz 2c45333704 Adjusting env calls 2022-10-28 09:59:01 +02:00
Patryk Gudalewicz 8194efced9 Adjusting env calls 2022-10-28 09:56:59 +02:00
Patryk Gudalewicz cd050a201f Adding Jenkinsfile 2022-10-28 09:51:58 +02:00
3 changed files with 55 additions and 25 deletions

21
Jenkinsfile vendored
View File

@ -6,8 +6,9 @@ pipeline {
agent {label 'libraryBuild'}
parameters {
string(name: 'CLUSTER', defaultValue: '', description: 'Enter the cluster name')
string(name: 'ENVIRONMENT', defaultValue: '', description: 'Enter single environment name. Possible values are: euprod, eupreprod, napreprod, naprod, cnprod. cnpreprod')
choice(name: 'ENVIRONMENT', choices: ['euprod', 'eupreprod', 'napreprod', 'naprod', 'cnprod', 'cnpreprod'], description: 'Choose Dynatrace environment')
string(name: 'OWNER', defaultValue: '', description: 'Enter e-mail address of dashboard owner. Optional if REMOVE parameter is set to true')
booleanParam(name: 'REMOVE', defaultValue: false, description: 'Check if dashboard for provided cluster should be removed instead of created')
}
@ -45,19 +46,27 @@ pipeline {
stage('Execute Dashboard script') {
steps {
script {
if(CLUSTER.isEmpty()) {
def SCRIPT_PARAMETER = ''
if(CLUSTER.isEmpty() || ENVIRONMENT.isEmpty()) {
currentBuild.result = 'ABORTED'
error('Aborting due to missing CLUSTER parameter')
error('Aborting due to missing CLUSTER or ENVIRONMENT parameters')
return
}
else if(ENVIRONMENT.isEmpty()) {
else if(REMOVE.toBoolean()) {
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -C " + CLUSTER.toString()
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -E " + ENVIRONMENT.toString()
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " --remove "
sh "python createDash.py ${SCRIPT_PARAMETER}"
}
else if(OWNER.isEmpty()) {
currentBuild.result = 'ABORTED'
error('Aborting due to missing ENVIRONMENT parameter')
error('Aborting due to missing OWNER parameter')
return
}
else {
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -C " + CLUSTER.toString()
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -E " + ENVIRONMENT.toString()
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -O " + OWNER.toString()
sh "python createDash.py ${SCRIPT_PARAMETER}"
}
}

View File

@ -13,7 +13,7 @@ parser = argparse.ArgumentParser(description="Generate and deploy the Kubernetes
parser.add_argument("-C", "--cluster", type=str,required=True, help="Name of the Kubernetes cluster")
parser.add_argument('--remove', default=False, action='store_true', help="Remove dashboard for given cluster. If not specified dashboard will be created or updated")
parser.add_argument('-E', '--environment', type=str, help="Name of the environment (the same as in environment.yaml file. Used to upload dashboard to specific Dynatrace environment. If not specified all environments in file will be used")
parser.add_argument('-O', '--owner', type=str, help="Email address of dashboard owner")
args = parser.parse_args()
def make_request(url, DTAPIToken,verify, method, jsondata):
headers = {
@ -42,7 +42,7 @@ def make_request(url, DTAPIToken,verify, method, jsondata):
return response
def get_all_dashboards_withname(DTAPIToken, DTENV,name):
DTAPIURL= DTENV + "api/config/v1/dashboards"
DTAPIURL= DTENV + "/api/config/v1/dashboards"
r = make_request(DTAPIURL,DTAPIToken,True,"get",None)
print(r)
entityResponse = r.json()
@ -57,7 +57,7 @@ def get_all_dashboards_withname(DTAPIToken, DTENV,name):
def remove_dashboards(DTAPIToken, DTENV, dashboards):
for dashboard in dashboards:
print("Removing dashboard from Dynatrace: "+dashboard["name"])
DTAPIURL = DTENV + "api/config/v1/dashboards/" + dashboard["id"]
DTAPIURL = DTENV + "/api/config/v1/dashboards/" + dashboard["id"]
print(make_request(DTAPIURL,DTAPIToken,True,"delete",None))
def create_or_update_dashboard(DTAPIToken, DTENV, dashboards, templatename, dashname):
@ -70,7 +70,7 @@ def create_or_update_dashboard(DTAPIToken, DTENV, dashboards, templatename, dash
existingdashboard = next((dashboard for dashboard in dashboards if dashboard["name"] == dashname), None)
if existingdashboard:
print("Found dashboard, Name: "+ existingdashboard["name"])
DTAPIURL = DTENV + "api/config/v1/dashboards/" + existingdashboard["id"]
DTAPIURL = DTENV + "/api/config/v1/dashboards/" + existingdashboard["id"]
r = make_request(DTAPIURL,DTAPIToken,True,"get",None)
entityResponse = r.json()
entityResponse["tiles"] = tilesjson
@ -80,14 +80,14 @@ def create_or_update_dashboard(DTAPIToken, DTENV, dashboards, templatename, dash
newdashboard = {
"dashboardMetadata":{
"name": dashname,
"owner": config("DASHBOARD_OWNER"),
"owner": args.owner,
"tags": ["Kubernetes"],
#"preset": 'true',
"shared":'true'
},
"tiles":[]
}
DTAPIURL = DTENV + "api/config/v1/dashboards"
DTAPIURL = DTENV + "/api/config/v1/dashboards"
newdashboard["tiles"] = tilesjson
print("Creating dashboard: "+newdashboard["dashboardMetadata"]["name"])
print(make_request(DTAPIURL,DTAPIToken,True,"post",json.dumps(newdashboard)))
@ -106,21 +106,14 @@ def main(slo_path):
print("Gather data, hold on a minute")
DTTOKEN = config(token.get('env-token-name'))
DTURL = url.get('env-url')
existingdashboards = get_all_dashboards_withname(DTTOKEN, DTURL,FULL_DASHBOARD_NAME)
if not args.environment:
if args.environment == item:
existingdashboards = get_all_dashboards_withname(DTTOKEN, DTURL,FULL_DASHBOARD_NAME)
if not args.remove:
print("Uploading dashboard to Dynatrace...")
print("Uploading dashboard to Dynatrace ("+item+")...")
create_or_update_dashboard(DTTOKEN, DTURL, existingdashboards, "kubernetes_tiles_template.json", FULL_DASHBOARD_NAME)
break
else:
remove_dashboards(DTTOKEN, DTURL, existingdashboards)
else:
if args.environment == item:
if not args.remove:
print("Uploading dashboard to Dynatrace ("+item+")...")
create_or_update_dashboard(DTTOKEN, DTURL, existingdashboards, "kubernetes_tiles_template.json", FULL_DASHBOARD_NAME)
break
else:
remove_dashboards(DTTOKEN, DTURL, existingdashboards)
else:
print("ERROR: No cluster specified")

View File

@ -5,6 +5,31 @@ Dashboard is generated for specific cluster provided as parameter for the script
Dashboard naming convention is: "[PROVIDED CLUSTER NAME] - Kubernetes cluster overview"
# Running with Jenkins
To run this script with Jenkins go to Jenkins project:
https://jaws.bmwgroup.net/opapm/job/CoCo_Kubernetes_Dashboard_as_code/job/master/
then use option "Build with parameters".
Jenkins provides following parameters:
CLUSTER - value is provided as "-C CLUSTER, --cluster CLUSTER" parameter for script
ENVIRONMENT - value is provided as "-E ENVIRONMENT, --environment ENVIRONMENT" parameter for script
OWNER - value is provided as "-O OWNER, --owner OWNER" parameter for script
REMOVE - value is provided as "--remove" parameter for script
## Using webhook
To run job with webhook include above parameters as URL params:
Create or update:
curl -X POST "https://jaws.bmwgroup.net/opapm/job/CoCo_Kubernetes_Dashboard_as_code/job/master/buildWithParameters?CLUSTER=[CLUSTER]&ENVIRONMENT=[ENVIRONMENT]&OWNER=[YOUR_EMAIL_ADDRESS]" --user USERNAME:APITOKEN
Delete:
curl -X POST "https://jaws.bmwgroup.net/opapm/job/CoCo_Kubernetes_Dashboard_as_code/job/master/buildWithParameters?CLUSTER=[CLUSTER]&ENVIRONMENT=[ENVIRONMENT]&&REMOVE=true" --user USERNAME:APITOKEN
# Prerequisites
## Python packages
@ -25,7 +50,7 @@ To provide authentication for API calls, create ".env" file in the script direct
# Usage
usage: createDash.py [-h] -C CLUSTER [--remove] [-E ENVIRONMENT]
usage: createDash.py [-h] -C CLUSTER [--remove] [-E ENVIRONMENT] [-O OWNER]
Generate and deploy the Kubernetes Overview Dashboard as Code.
@ -35,7 +60,10 @@ To provide authentication for API calls, create ".env" file in the script direct
Name of the Kubernetes cluster (default: None)
--remove Remove dashboard for given cluster. If not specified dashboard will be created or updated (default: False)
-E ENVIRONMENT, --environment ENVIRONMENT
Name of the environment (the same as in environment.yaml file. Used to upload dashboard to specific Dynatrace environment. If not specified all environments in file will be used (default: None)
Name of the environment (the same as in environment.yaml file. Used to upload dashboard to specific Dynatrace environment. If not specified all environments in file will be used
(default: None)
-O OWNER, --owner OWNER
Email address of dashboard owner (default: None)
# Files
## createDash.py