#!/usr/bin/env pythonimportopenshift# https://github.com/openshift/openshift-restclient-python/blob/master/openshift/docs/OapiApi.mdfromopenshiftimportclient,configfrompprintimportpprintfromkubernetes.client.restimportApiExceptionfromdatetimeimportdatetime,timedeltafromdateutil.parserimportparseimportdateutil.tz#config.load_kube_config()config.load_incluster_config()oapi=client.OapiApi()# for i in dir(oapi):# print iproject_list=oapi.list_project()forprojectinproject_list.items:requester="unknown"if'openshift.io/requester'inproject.metadata.annotations.keys():requester=project.metadata.annotations['openshift.io/requester']if'bohne.io/auto-scale-down'inproject.metadata.annotations.keys():d=datetime.now(dateutil.tz.tzutc())-timedelta(days=int(project.metadata.annotations['bohne.io/auto-scale-down']))#print('Name: {}, Create: {}'.format(project.metadata.name, project.metadata.creation_timestamp))ifproject.metadata.creation_timestamp<d:try:api_response=oapi.list_namespaced_deployment_config(project.metadata.name)fordcinapi_response.items:ifint(dc.status.replicas)>0:print("Scale down: dc/{} from {} to 0 (namespace: {}, requester: {} )".format(dc.metadata.name,dc.status.replicas,project.metadata.name,requester))try:oapi.patch_namespaced_deployment_config(dc.metadata.name,project.metadata.name,openshift.client.V1DeploymentConfig(spec=openshift.client.V1DeploymentConfigSpec(replicas=0)))exceptApiExceptionase:print("Exception when calling OapiApi->patch_namespaced_deployment_config_scale: %s\n"%e)exceptApiExceptionase:print("Exception when calling OapiApi->list_deployment_config_for_all_namespaces: %s\n"%e)