def _watch_resource_iterator(label, labelValue, targetFolder, url, method, payload, currentNamespace, folderAnnotation, resource, uniqueFilenames): v1 = client.CoreV1Api() namespace = os.getenv("NAMESPACE", currentNamespace) # Filter resources based on label and value or just label labelSelector=f"{label}={labelValue}" if labelValue else label if namespace == "ALL": stream = watch.Watch().stream(getattr(v1, _list_for_all_namespaces[resource]), label_selector=labelSelector) else: stream = watch.Watch().stream(getattr(v1, _list_namespaced[resource]), namespace=namespace, label_selector=labelSelector) # Process events for event in stream: metadata = event["object"].metadata print(f"{timestamp()} Working on {resource} {metadata.namespace}/{metadata.name}") # Get the destination folder destFolder = _get_destination_folder(metadata, targetFolder, folderAnnotation) # Check if it's an empty ConfigMap or Secret dataMap = event["object"].data if dataMap is None: print(f"{timestamp()} {resource} does not have data.") continue eventType = event["type"] # Each key on the data is a file for data_key in dataMap.keys(): print(f"{timestamp()} File in {resource} {data_key} {eventType}") if (eventType == "ADDED") or (eventType == "MODIFIED"): filename, filedata = _get_file_data_and_name(data_key, dataMap[data_key], resource) if uniqueFilenames: filename = uniqueFilename(filename = filename, namespace = metadata.namespace, resource = resource, resource_name = metadata.name) writeTextToFile(destFolder, filename, filedata) else: # Get filename from event filename = data_key[:-4] if data_key.endswith(".url") else data_key if uniqueFilenames: filename = uniqueFilename(filename = filename, namespace = metadata.namespace, resource = resource, resource_name = metadata.name) removeFile(destFolder, filename) if url: request(url, method, payload)
def listResources(label, labelValue, targetFolder, url, method, payload, currentNamespace, folderAnnotation, resource, uniqueFilenames): v1 = client.CoreV1Api() namespace = os.getenv("NAMESPACE", currentNamespace) # Filter resources based on label and value or just label labelSelector = f"{label}={labelValue}" if labelValue else label if namespace == "ALL": ret = getattr( v1, _list_for_all_namespaces[resource])(label_selector=labelSelector) else: ret = getattr(v1, _list_namespaced[resource])(namespace=namespace, label_selector=labelSelector) # For all the found resources for sec in ret.items: metadata = sec.metadata print( f"{timestamp()} Working on {resource}: {metadata.namespace}/{metadata.name}" ) # Get the destination folder destFolder = _get_destination_folder(metadata, targetFolder, folderAnnotation) # Check if it's an empty ConfigMap or Secret dataMap = sec.data if dataMap is None: print(f"{timestamp()} No data field in {resource}") continue # Each key on the data is a file for data_key in dataMap.keys(): filename, filedata = _get_file_data_and_name( data_key, dataMap[data_key], resource) if uniqueFilenames: filename = uniqueFilename(filename=filename, namespace=metadata.namespace, resource=resource, resource_name=metadata.name) writeTextToFile(destFolder, filename, filedata) if url: request(url, method, payload)