Compare commits

..

No commits in common. "e562634d9aa6f39101f62eea89dd81f23f23fe04" and "b3cc35a755b59517e8afcd3cbd9015c9b31b0c81" have entirely different histories.

3 changed files with 25 additions and 55 deletions

21
Jenkinsfile vendored
View File

@ -6,9 +6,8 @@ pipeline {
agent {label 'libraryBuild'} agent {label 'libraryBuild'}
parameters { parameters {
string(name: 'CLUSTER', defaultValue: '', description: 'Enter the cluster name') string(name: 'CLUSTER', defaultValue: '', description: 'Enter the cluster name')
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') string(name: 'ENVIRONMENT', defaultValue: '', description: 'Enter single environment name. Possible values are: euprod, eupreprod, napreprod, naprod, cnprod. cnpreprod')
booleanParam(name: 'REMOVE', defaultValue: false, description: 'Check if dashboard for provided cluster should be removed instead of created')
} }
@ -46,27 +45,19 @@ pipeline {
stage('Execute Dashboard script') { stage('Execute Dashboard script') {
steps { steps {
script { script {
def SCRIPT_PARAMETER = '' if(CLUSTER.isEmpty()) {
if(CLUSTER.isEmpty() || ENVIRONMENT.isEmpty()) {
currentBuild.result = 'ABORTED' currentBuild.result = 'ABORTED'
error('Aborting due to missing CLUSTER or ENVIRONMENT parameters') error('Aborting due to missing CLUSTER parameter')
return return
} }
else if(REMOVE.toBoolean()) { else if(ENVIRONMENT.isEmpty()) {
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' currentBuild.result = 'ABORTED'
error('Aborting due to missing OWNER parameter') error('Aborting due to missing ENVIRONMENT parameter')
return return
} }
else { else {
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -C " + CLUSTER.toString() SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -C " + CLUSTER.toString()
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -E " + ENVIRONMENT.toString() SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -E " + ENVIRONMENT.toString()
SCRIPT_PARAMETER = SCRIPT_PARAMETER + " -O " + OWNER.toString()
sh "python createDash.py ${SCRIPT_PARAMETER}" 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("-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('--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('-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() args = parser.parse_args()
def make_request(url, DTAPIToken,verify, method, jsondata): def make_request(url, DTAPIToken,verify, method, jsondata):
headers = { headers = {
@ -42,7 +42,7 @@ def make_request(url, DTAPIToken,verify, method, jsondata):
return response return response
def get_all_dashboards_withname(DTAPIToken, DTENV,name): 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) r = make_request(DTAPIURL,DTAPIToken,True,"get",None)
print(r) print(r)
entityResponse = r.json() entityResponse = r.json()
@ -57,7 +57,7 @@ def get_all_dashboards_withname(DTAPIToken, DTENV,name):
def remove_dashboards(DTAPIToken, DTENV, dashboards): def remove_dashboards(DTAPIToken, DTENV, dashboards):
for dashboard in dashboards: for dashboard in dashboards:
print("Removing dashboard from Dynatrace: "+dashboard["name"]) 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)) print(make_request(DTAPIURL,DTAPIToken,True,"delete",None))
def create_or_update_dashboard(DTAPIToken, DTENV, dashboards, templatename, dashname): 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) existingdashboard = next((dashboard for dashboard in dashboards if dashboard["name"] == dashname), None)
if existingdashboard: if existingdashboard:
print("Found dashboard, Name: "+ existingdashboard["name"]) 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) r = make_request(DTAPIURL,DTAPIToken,True,"get",None)
entityResponse = r.json() entityResponse = r.json()
entityResponse["tiles"] = tilesjson entityResponse["tiles"] = tilesjson
@ -80,14 +80,14 @@ def create_or_update_dashboard(DTAPIToken, DTENV, dashboards, templatename, dash
newdashboard = { newdashboard = {
"dashboardMetadata":{ "dashboardMetadata":{
"name": dashname, "name": dashname,
"owner": args.owner, "owner": config("DASHBOARD_OWNER"),
"tags": ["Kubernetes"], "tags": ["Kubernetes"],
#"preset": 'true', #"preset": 'true',
"shared":'true' "shared":'true'
}, },
"tiles":[] "tiles":[]
} }
DTAPIURL = DTENV + "/api/config/v1/dashboards" DTAPIURL = DTENV + "api/config/v1/dashboards"
newdashboard["tiles"] = tilesjson newdashboard["tiles"] = tilesjson
print("Creating dashboard: "+newdashboard["dashboardMetadata"]["name"]) print("Creating dashboard: "+newdashboard["dashboardMetadata"]["name"])
print(make_request(DTAPIURL,DTAPIToken,True,"post",json.dumps(newdashboard))) print(make_request(DTAPIURL,DTAPIToken,True,"post",json.dumps(newdashboard)))
@ -106,14 +106,21 @@ def main(slo_path):
print("Gather data, hold on a minute") print("Gather data, hold on a minute")
DTTOKEN = config(token.get('env-token-name')) DTTOKEN = config(token.get('env-token-name'))
DTURL = url.get('env-url') DTURL = url.get('env-url')
if args.environment == item: existingdashboards = get_all_dashboards_withname(DTTOKEN, DTURL,FULL_DASHBOARD_NAME)
existingdashboards = get_all_dashboards_withname(DTTOKEN, DTURL,FULL_DASHBOARD_NAME) if not args.environment:
if not args.remove: if not args.remove:
print("Uploading dashboard to Dynatrace ("+item+")...") print("Uploading dashboard to Dynatrace...")
create_or_update_dashboard(DTTOKEN, DTURL, existingdashboards, "kubernetes_tiles_template.json", FULL_DASHBOARD_NAME) create_or_update_dashboard(DTTOKEN, DTURL, existingdashboards, "kubernetes_tiles_template.json", FULL_DASHBOARD_NAME)
break
else: else:
remove_dashboards(DTTOKEN, DTURL, existingdashboards) 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: else:
print("ERROR: No cluster specified") print("ERROR: No cluster specified")

View File

@ -5,31 +5,6 @@ Dashboard is generated for specific cluster provided as parameter for the script
Dashboard naming convention is: "[PROVIDED CLUSTER NAME] - Kubernetes cluster overview" 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 # Prerequisites
## Python packages ## Python packages
@ -50,7 +25,7 @@ To provide authentication for API calls, create ".env" file in the script direct
# Usage # Usage
usage: createDash.py [-h] -C CLUSTER [--remove] [-E ENVIRONMENT] [-O OWNER] usage: createDash.py [-h] -C CLUSTER [--remove] [-E ENVIRONMENT]
Generate and deploy the Kubernetes Overview Dashboard as Code. Generate and deploy the Kubernetes Overview Dashboard as Code.
@ -60,10 +35,7 @@ To provide authentication for API calls, create ".env" file in the script direct
Name of the Kubernetes cluster (default: None) Name of the Kubernetes cluster (default: None)
--remove Remove dashboard for given cluster. If not specified dashboard will be created or updated (default: False) --remove Remove dashboard for given cluster. If not specified dashboard will be created or updated (default: False)
-E ENVIRONMENT, --environment ENVIRONMENT -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 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)
(default: None)
-O OWNER, --owner OWNER
Email address of dashboard owner (default: None)
# Files # Files
## createDash.py ## createDash.py