def pod_log_event_callback(event): try: append_result_output("{}\n".format(event), OUT_FILE) log_dict = globals()['log_dict'] for log in log_dict: if log in event: log_dict[log] = 1 if all(ele == 1 for ele in list(log_dict.values())): return True return False except Exception as e: sys.exit("Error occured while processing pod log event: " + str(e))
def crd_event_callback(event): try: append_result_output("{}\n".format(event), OUT_FILE) status_list = globals()['status_list'] crd_status = event['raw_object'].get('status') if not crd_status: return False for status_fields in status_list: if not crd_status.get(status_fields): return False return True except Exception as e: sys.exit("Error occured while processing crd event: " + str(e))
def pod_event_callback(event): try: append_result_output("{}\n".format(event), OUT_FILE) pod_dict = globals()['pod_dict'] pod_status = event['raw_object'].get('status') pod_name = event['object'].metadata.name if pod_status.get('containerStatuses'): for container in pod_status.get('containerStatuses'): if container.get('restartCount') > 0: sys.exit( "The pod {} was restarted. Please see the pod logs for more info." .format(container.get('name'))) if not container.get('state').get('running'): pod_dict[pod_name] = 0 return False else: pod_dict[pod_name] = 1 if all(ele == 1 for ele in list(pod_dict.values())): return True return False except Exception as e: sys.exit("Error occured while processing the pod event: " + str(e))
authority_uri = 'https://login.microsoftonline.com/' + tenant_id token = fetch_aad_token(client_id, client_secret, authority_uri, 'https://management.core.windows.net/') access_token = token.get('accessToken') # Fetch helm chart path release_train = os.getenv('RELEASE_TRAIN') if os.getenv( 'RELEASE_TRAIN') else 'stable' helm_registry_path = os.getenv('HELM_REGISTRY_PATH') if os.getenv( 'HELM_REGISTRY_PATH') else get_helm_registry(access_token, location, release_train) print("Successfully fetched helm chart path.") # Pulling helm charts result = pull_helm_chart(helm_registry_path) append_result_output("{}\n".format(result), OUT_FILE) print("Successfully pulled helm charts.") # Exporting helm charts helm_export_destination = os.getenv('HELM_EXPORT_DESTINATION') if os.getenv( 'HELM_EXPORT_DESTINATION') else "." result = export_helm_chart(helm_registry_path, helm_export_destination) append_result_output("{}\n".format(result), OUT_FILE) print("Successfully exported helm charts.") # Adding a helm repository if os.getenv('HELM_REPO_NAME') and os.getenv('HELM_REPO_URL'): helm_repo_name = os.getenv('HELM_REPO_NAME') helm_repo_url = os.getenv('HELM_REPO_URL') result = add_helm_repo(helm_repo_name, helm_repo_url) append_result_output("{}\n".format(result), OUT_FILE)
sys.exit('ERROR: variable CRD_PLURAL is required.') crd_name = os.getenv('CRD_NAME') if not crd_name: sys.exit('ERROR: variable CRD_NAME is required.') # Setting a list of status fields that will be monitored for presence in the CRD instance events global status_list status_list = [] if os.getenv( 'CRD_STATUS_FIELDS' ): # This environment variable should be provided as comma separated status fields that we want to monitor for the CRD instance crd_status_fields_list = os.getenv('CRD_STATUS_FIELDS').split(',') for status_fields in crd_status_fields_list: status_list.append(status_fields.strip()) append_result_output("Status List: {}\n".format(status_list), OUT_FILE) print("Generated the status fields list.") # The callback function to check if the crd event received has been updated with the status fields def crd_event_callback(event): try: append_result_output("{}\n".format(event), OUT_FILE) status_list = globals()['status_list'] crd_status = event['raw_object'].get('status') if not crd_status: return False for status_fields in status_list: if not crd_status.get(status_fields): return False return True
# Fetch aad token credentials from spn authority_uri = 'https://login.microsoftonline.com/' + tenant_id credential = fetch_aad_token_credentials( client_id, client_secret, authority_uri, 'https://management.core.windows.net/') print("Successfully fetched credentials object.") # Check provisioning state of the connected cluster resource cc_client = get_connected_cluster_client(credential, subscription_id) timeout_seconds = int(os.getenv('TIMEOUT')) if os.getenv('TIMEOUT') else 300 timeout = time.time() + timeout_seconds while True: provisioning_state = get_connected_cluster(cc_client, resource_group, cluster_name).provisioning_state append_result_output("Provisioning State: {}\n".format(provisioning_state), OUT_FILE) if provisioning_state == 'Succeeded': break if (provisioning_state == 'Failed' or provisioning_state == 'Cancelled'): sys.exit( "ERROR: The connected cluster creation finished with terminal provisioning state {}. " .format(provisioning_state)) if time.time() > timeout: sys.exit( "ERROR: Timeout. The connected cluster is in non terminal provisioning state." ) time.sleep(10) print("The connected cluster resource was created successfully.") # Exit with success EXIT_CODE = 0
'OPERATOR_PARAMS') else '' enable_helm_operator = True if os.getenv('ENABLE_HELM_OPERATOR') else False helm_operator_version = os.getenv('HELM_OPERATOR_VERSION') if os.getenv( 'HELM_OPERATOR_VERSION') else '0.6.0' helm_operator_params = os.getenv('HELM_OPERATOR_PARAMS') if os.getenv( 'HELM_OPERATOR_PARAMS') else '' # Fetch aad token credentials from spn authority_uri = 'https://login.microsoftonline.com/' + tenant_id credential = fetch_aad_token_credentials( client_id, client_secret, authority_uri, 'https://management.core.windows.net/') print("Successfully fetched credentials object.") # Create the source control configuration kc_client = get_source_control_configuration_client(credential, subscription_id) put_kc_response = create_kubernetes_configuration( kc_client, resource_group, repository_url, cluster_rp, cluster_type, cluster_name, configuration_name, operator_scope, operator_namespace, operator_instance_name, operator_type, operator_params, enable_helm_operator, helm_operator_version, helm_operator_params) append_result_output("{}\n".format(put_kc_response), OUT_FILE) print("Successfully created the kubernetes configuration resource.") # Exit with success EXIT_CODE = 0 atexit.unregister(save_results) atexit.register(save_results, RESULTS_TAR_FILENAME, RESULTS_DIR, RESULT_FILE, DONE_FILE, EXIT_CODE, test_cases)
# Check environment variables pod_namespace = os.getenv('POD_NAMESPACE') if not pod_namespace: sys.exit('ERROR: variable POD_NAMESPACE is required.') # Setting a dictionary of pods that will be monitored in the provided namespace global pod_dict pod_dict = {} if os.getenv( 'POD_LIST' ): # This environment variable should be provided as comma separated pod names that we want to monitor in the given namespace pod_list = os.getenv('POD_LIST').split(',') for pod in pod_list: pod_dict[pod.strip()] = 0 append_result_output("Pod dict: {}\n".format(pod_dict), OUT_FILE) print("Generated the metadata fields dictionary.") # The callback function to check if the pod is in running state def pod_event_callback(event): try: append_result_output("{}\n".format(event), OUT_FILE) pod_dict = globals()['pod_dict'] pod_status = event['raw_object'].get('status') pod_name = event['object'].metadata.name if pod_status.get('containerStatuses'): for container in pod_status.get('containerStatuses'): if container.get('restartCount') > 0: sys.exit( "The pod {} was restarted. Please see the pod logs for more info."
'https://management.core.windows.net/') print("Successfully fetched credentials object.") # Setting a dictionary of cluster metadata fields that will be monitored for presence in the connected cluster resource metadata_dict = { 'kubernetes_version': 0, 'total_node_count': 0, 'agent_version': 0 } if os.getenv( 'METADATA_FIELDS' ): # This environment variable should be provided as comma separated metadata fields that we want to monitor for the connected cluster metadata_fields_list = os.getenv('METADATA_FIELDS').split(',') for metadata_fields in metadata_fields_list: metadata_dict[metadata_fields.strip()] = 0 append_result_output( "Metadata Fields: {}\n".format(list(metadata_dict.keys())), OUT_FILE) print("Generated the metadata fields dictionary.") # Check metadata properties of the connected cluster resource cc_client = get_connected_cluster_client(credential, subscription_id) timeout_seconds = int(os.getenv('TIMEOUT')) if os.getenv('TIMEOUT') else 300 timeout = time.time() + timeout_seconds while True: cc_object = get_connected_cluster(cc_client, resource_group, cluster_name) for metadata_field in metadata_dict.keys(): try: metadata_field_value = getattr(cc_object, metadata_field) except Exception as e: sys.exit( "Error occured while fetching connected cluster attribute: " + str(e))
pod_name = os.getenv('POD_NAME') if not pod_name: sys.exit('ERROR: variable POD_NAME is required.') container_name = os.getenv('CONTAINER_NAME') if not container_name: sys.exit('ERROR: variable CONTAINER_NAME is required.') # Setting a dictionary of logs that will be monitored for presence inside the given pod global log_dict log_dict = {} if os.getenv('LOG_LIST'): # This environment variable should be provided as comma separated logs that we want to find in the pod logs log_list = os.getenv('LOG_LIST').split(',') for log in log_list: log_dict[log.strip()] = 0 append_result_output("Logs Dict: {}\n".format(log_dict), OUT_FILE) print("Generated the pod log dictionary.") # The callback function to examine the pod log def pod_log_event_callback(event): try: append_result_output("{}\n".format(event), OUT_FILE) log_dict = globals()['log_dict'] for log in log_dict: if log in event: log_dict[log] = 1 if all(ele == 1 for ele in list(log_dict.values())): return True return False except Exception as e: sys.exit("Error occured while processing pod log event: " + str(e))