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 _watch_resource_iterator(label, targetFolder, url, method, payload, current, folderAnnotation, resource): v1 = client.CoreV1Api() namespace = os.getenv("NAMESPACE", current) if namespace == "ALL": stream = watch.Watch().stream( getattr(v1, _list_for_all_namespaces[resource])) else: stream = watch.Watch().stream(getattr(v1, _list_namespaced[resource]), namespace=namespace) for event in stream: destFolder = targetFolder metadata = event['object'].metadata if metadata.labels is None: continue print(f'Working on {resource} {metadata.namespace}/{metadata.name}') if label in event['object'].metadata.labels.keys(): print(f"{resource} with label found") if event['object'].metadata.annotations is not None: if folderAnnotation in event[ 'object'].metadata.annotations.keys(): destFolder = event['object'].metadata.annotations[ folderAnnotation] print('Found a folder override annotation, ' f'placing the {resource} in: {destFolder}') dataMap = event['object'].data if dataMap is None: print(f"{resource} does not have data.") continue eventType = event['type'] for data_key in dataMap.keys(): print(f"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) writeTextToFile(destFolder, filename, filedata) if url is not None: request(url, method, payload) else: filename = data_key[:-4] if data_key.endswith( ".url") else data_key removeFile(destFolder, filename) if url is not None: request(url, method, payload)
def _watch_resource_iterator(label, targetFolder, url, method, payload, current, folderAnnotation, resource, logger): v1 = client.CoreV1Api() namespace = os.getenv("NAMESPACE", current) if namespace == "ALL": stream = watch.Watch().stream(getattr(v1, _list_for_all_namespaces[resource])) else: stream = watch.Watch().stream(getattr(v1, _list_namespaced[resource]), namespace=namespace) for event in stream: destFolder = targetFolder metadata = event['object'].metadata if metadata.labels is None: continue logger.info("Working on %s %s/%s" % (resource, metadata.namespace, metadata.name)) if label in event['object'].metadata.labels.keys(): logger.info("%s with label found" % resource) if event['object'].metadata.annotations is not None: if folderAnnotation in event['object'].metadata.annotations.keys(): destFolder = event['object'].metadata.annotations[folderAnnotation] logger.info("Found a folder override annotation, placing the %s in: %s" % (resource, destFolder)) dataMap = event['object'].data if dataMap is None: logger.info("%s does not have data." % resource) continue eventType = event['type'] for data_key in dataMap.keys(): logger.info("File in %s %s %s " % (resource, data_key, eventType)) if (eventType == "ADDED") or (eventType == "MODIFIED"): filename, filedata = _get_file_data_and_name(data_key, dataMap[data_key], resource, logger) writeTextToFile(destFolder, filename, filedata) if url is not None: request(url, method, logger, payload) else: filename = data_key[:-4] if data_key.endswith(".url") else data_key removeFile(destFolder, filename, logger) if url is not None: request(url, method, logger, payload)