| 1 |
# Bacula(R) - The Network Backup Solution |
| 2 |
# |
| 3 |
# Copyright (C) 2000-2022 Kern Sibbald |
| 4 |
# |
| 5 |
# The original author of Bacula is Kern Sibbald, with contributions |
| 6 |
# from many others, a complete list can be found in the file AUTHORS. |
| 7 |
# |
| 8 |
# You may use this file and others of this release according to the |
| 9 |
# license defined in the LICENSE file, which includes the Affero General |
| 10 |
# Public License, v3.0 ("AGPLv3") and some additional permissions and |
| 11 |
# terms pursuant to its AGPLv3 Section 7. |
| 12 |
# |
| 13 |
# This notice must be preserved when any source code is |
| 14 |
# conveyed and/or propagated. |
| 15 |
# |
| 16 |
# Bacula(R) is a registered trademark of Kern Sibbald. |
| 17 |
|
| 18 |
import logging |
| 19 |
import re |
| 20 |
|
| 21 |
from baculak8s.entities.file_info import FileInfo |
| 22 |
from baculak8s.io.default_io import DefaultIO |
| 23 |
from baculak8s.io.packet_definitions import ESTIMATION_START_PACKET |
| 24 |
from baculak8s.jobs.job_pod_bacula import (PVCDATA_GET_ERROR, JobPodBacula) |
| 25 |
from baculak8s.plugins.k8sbackend.baculaannotations import BaculaAnnotationsClass, BaculaBackupMode |
| 26 |
from baculak8s.util.respbody import parse_json_descr |
| 27 |
|
| 28 |
PATTERN_NOT_FOUND = "No matches found for pattern {}" |
| 29 |
NO_PV_FOUND = "No required Persistent Volumes found at the cluster" |
| 30 |
NO_SC_FOUND = "No required Storage Classes found at the cluster" |
| 31 |
NO_NS_FOUND = "No required Namespaces found at the cluster" |
| 32 |
PV_LIST_ERROR = "Cannot list PV objects. Err={}" |
| 33 |
PODS_LIST_ERROR = "Cannot list Pods objects. Err={}" |
| 34 |
SC_LIST_ERROR = "Cannot list StorageClass objects. Err={}" |
| 35 |
NS_LIST_ERROR = "Cannot list Namespace objects. Err={}" |
| 36 |
RES_LIST_ERROR = "Cannot list resource objects. Err={}" |
| 37 |
# PVCDATA_LIST_ERROR = "Cannot list PVC Data objects. Err={}" |
| 38 |
PROCESSING_NAMESPACE_INFO = "Processing namespace: {namespace}" |
| 39 |
PROCESSING_PVCDATA_START_INFO = "Start backup volume claim: {pvc}" |
| 40 |
PROCESSING_PVCDATA_STOP_INFO = "Finish backup volume claim: {pvc}" |
| 41 |
PROCESSING_PODBACKUP_PHASE_START_INFO = "Start backup phase of pvcs that they are in pod annotations" |
| 42 |
PROCESSING_PODBACKUP_PHASE_FINISH_INFO = "Finish backup phase of pvcs that they are in pod annotations" |
| 43 |
PROCESSING_PODBACKUP_START_INFO = "Start backup Pod: {namespace}/{podname}" |
| 44 |
PROCESSING_PODBACKUP_FINISH_INFO = "Finish backup Pod: {namespace}/{podname}" |
| 45 |
SKIP_PVCDATA_SECOND_BACKUP_INFO = "Skip backup of pvc `{}` because it did previously with a pod" |
| 46 |
SECOND_TRY_PVCDATA_INFO = "Generic try to do backup of this pvc `{}`" |
| 47 |
BA_PVC_NOT_FOUND_ERROR = "Requested volume claim: {pvc} on {namespace}/{podname} not found!" |
| 48 |
BA_PVCNAME_ERROR = "Invalid annotations for Pod: {namespace}/{podname}. {bavol} Required!" |
| 49 |
PROCESS_POD_PVCDATA_ERROR = "Cannot process Pod PVC Data backup: {namespace}/{podname}!" |
| 50 |
LABEL_PARAM_INVALID_ERROR = "Label parameter ({}) is invalid!" |
| 51 |
|
| 52 |
|
| 53 |
class EstimationJob(JobPodBacula): |
| 54 |
""" |
| 55 |
Job that contains the business logic |
| 56 |
related to the estimation mode of the Backend. |
| 57 |
It depends upon a Plugin Class implementation |
| 58 |
that retrieves estimation data from the Plugins Data Source |
| 59 |
""" |
| 60 |
|
| 61 |
def __init__(self, plugin, params, start_packet=None): |
| 62 |
if not start_packet: |
| 63 |
start_packet = ESTIMATION_START_PACKET |
| 64 |
self.__start_packet = start_packet |
| 65 |
super().__init__(plugin, DefaultIO(), params) |
| 66 |
self.include_matches = [] |
| 67 |
self.regex_include_matches = [] |
| 68 |
self.exclude_matches = [] |
| 69 |
self.regex_exclude_matches = [] |
| 70 |
_sc = params.get('storageclass', []) |
| 71 |
self.storageclassparam = _sc if len(_sc) > 0 else None |
| 72 |
_pv = params.get('persistentvolume', []) |
| 73 |
self.persistentvolumeparam = _pv if len(_pv) > 0 else None |
| 74 |
|
| 75 |
def execute(self): |
| 76 |
self._start(self.__start_packet) |
| 77 |
self.execution_loop() |
| 78 |
self.__verify_all_matches() |
| 79 |
self._io.send_eod() |
| 80 |
|
| 81 |
def execution_loop(self): |
| 82 |
return self.processing_loop(estimate=True) |
| 83 |
|
| 84 |
def processing_loop(self, estimate=False): |
| 85 |
sc_list = self._plugin.list_all_storageclass(estimate=estimate) |
| 86 |
logging.debug("processing list_all_storageclass:{}:nrfound:{}".format(self.storageclassparam, len(sc_list))) |
| 87 |
if isinstance(sc_list, dict) and sc_list.get('exception'): |
| 88 |
self._handle_error(SC_LIST_ERROR.format(parse_json_descr(sc_list))) |
| 89 |
|
| 90 |
else: |
| 91 |
if self.storageclassparam is not None and len(sc_list) == 0: |
| 92 |
self._handle_error(NO_SC_FOUND) |
| 93 |
|
| 94 |
for sc in sc_list: |
| 95 |
logging.debug('processing sc:{}'.format(sc)) |
| 96 |
self.process_file(sc_list.get(sc)) |
| 97 |
|
| 98 |
pv_list = self._plugin.list_all_persistentvolumes(estimate=estimate) |
| 99 |
logging.debug("processing list_all_persistentvolumes:{}:nrfound:{}".format(self.persistentvolumeparam, len(pv_list))) |
| 100 |
if isinstance(pv_list, dict) and pv_list.get('exception'): |
| 101 |
self._handle_error(PV_LIST_ERROR.format(parse_json_descr(pv_list))) |
| 102 |
|
| 103 |
else: |
| 104 |
if self.persistentvolumeparam is not None and len(pv_list) == 0: |
| 105 |
self._handle_error(NO_PV_FOUND) |
| 106 |
|
| 107 |
for pv in pv_list: |
| 108 |
logging.debug("processing pv: {}".format(pv)) |
| 109 |
self.process_file(pv_list.get(pv)) |
| 110 |
|
| 111 |
ns_list = self._plugin.list_all_namespaces(estimate=estimate) |
| 112 |
logging.debug("processing list_all_namespaces:nrfound:{}".format(len(ns_list))) |
| 113 |
if isinstance(ns_list, dict) and ns_list.get('exception'): |
| 114 |
self._handle_error(NS_LIST_ERROR.format(parse_json_descr(ns_list))) |
| 115 |
|
| 116 |
else: |
| 117 |
if len(self._params.get('namespace')) != 0 and len(ns_list) == 0: |
| 118 |
self._handle_error(NO_NS_FOUND) |
| 119 |
|
| 120 |
for nsname in ns_list: |
| 121 |
ns = ns_list.get(nsname) |
| 122 |
logging.debug('processing ns: {}'.format(ns.get('name'))) |
| 123 |
if not estimate: |
| 124 |
self._io.send_info(PROCESSING_NAMESPACE_INFO.format(namespace=ns['name'])) |
| 125 |
self.process_file(ns) |
| 126 |
nsdata = self._plugin.list_namespaced_objects_before_pvcdata(nsname, estimate=estimate) |
| 127 |
logging.debug('Before PVCData NSDATA:{}'.format([ns.keys() for ns in nsdata])) # limit debug output |
| 128 |
for sub in nsdata: |
| 129 |
# sub is a list of different resource types |
| 130 |
if isinstance(sub, dict) and sub.get('exception'): |
| 131 |
self._handle_error(RES_LIST_ERROR.format(parse_json_descr(sub))) |
| 132 |
else: |
| 133 |
for res in sub: |
| 134 |
self.process_file(sub.get(res)) |
| 135 |
|
| 136 |
podsannotated = self._plugin.get_annotated_namespaced_pods_data(nsname, estimate=estimate) |
| 137 |
logging.debug("processing get_annotated_namespaced_pods_data:{}:nrfound:{}".format(nsname, |
| 138 |
len(podsannotated))) |
| 139 |
# We can define what pvc backup in two places: fileset and pod annotation. So, we save the pod annotations to we don't backup two times the same. |
| 140 |
pvc_backed={} |
| 141 |
# here we have a list of pods which are anotated |
| 142 |
if podsannotated is not None and self._plugin.config.get('pvcdata') is not None: |
| 143 |
if isinstance(podsannotated, dict) and podsannotated.get('exception'): |
| 144 |
self._handle_error(PODS_LIST_ERROR.format(parse_json_descr(podsannotated))) |
| 145 |
else: |
| 146 |
if len(podsannotated) > 0 and not estimate: |
| 147 |
self._io.send_info(PROCESSING_PODBACKUP_PHASE_START_INFO) |
| 148 |
for pod in podsannotated: |
| 149 |
logging.debug('PODDATA:{}'.format(pod)) |
| 150 |
# this is required parameter! |
| 151 |
pvcnames = pod.get(BaculaAnnotationsClass.BackupVolume) |
| 152 |
if pvcnames is None: |
| 153 |
self._handle_error(BA_PVCNAME_ERROR.format(namespace=nsname, |
| 154 |
podname=pod.get('name'), |
| 155 |
bavol=BaculaAnnotationsClass.BaculaPrefix + |
| 156 |
BaculaAnnotationsClass.BackupVolume)) |
| 157 |
continue |
| 158 |
|
| 159 |
podname = pod.get('name') |
| 160 |
if not estimate: |
| 161 |
self._io.send_info(PROCESSING_PODBACKUP_START_INFO.format(namespace=nsname, |
| 162 |
podname=podname)) |
| 163 |
|
| 164 |
status = self.process_pod_pvcdata(nsname, pod, pvcnames) |
| 165 |
logging.debug("Status in processing_loop: {}".format(status)) |
| 166 |
if status is None: |
| 167 |
for pvc_name in pvcnames.split(','): |
| 168 |
pvc_backed[pvc_name] = 'error' |
| 169 |
logging.error("Some unknown error at {namespace}/{podname}!".format(namespace=nsname, podname=podname)) |
| 170 |
self._handle_error(PROCESS_POD_PVCDATA_ERROR.format(namespace=nsname, podname=podname)) |
| 171 |
break |
| 172 |
if not estimate: |
| 173 |
for pvc_name in pvcnames.split(','): |
| 174 |
pvc_backed[pvc_name] = 'ok' |
| 175 |
self._io.send_info(PROCESSING_PODBACKUP_FINISH_INFO.format(namespace=nsname, |
| 176 |
podname=podname)) |
| 177 |
if len(podsannotated) > 0 and not estimate: |
| 178 |
self._io.send_info(PROCESSING_PODBACKUP_PHASE_FINISH_INFO) |
| 179 |
logging.debug("PVCs backed through pod annotations: {}".format(pvc_backed)) |
| 180 |
|
| 181 |
pvcdatalist = self._plugin.list_pvcdata_for_namespace(nsname, estimate=estimate) |
| 182 |
logging.debug("processing list_pvcdata_for_namespace:{}:nrfound:{}".format(nsname, len(pvcdatalist))) |
| 183 |
if pvcdatalist is not None: |
| 184 |
if isinstance(pvcdatalist, dict) and pvcdatalist.get('exception'): |
| 185 |
self._handle_error(PV_LIST_ERROR.format(parse_json_descr(pvcdatalist))) |
| 186 |
else: |
| 187 |
for pvc in pvcdatalist: |
| 188 |
pvcdata = pvcdatalist.get(pvc) |
| 189 |
logging.debug('PVCDATA:{}:{}'.format(pvc, pvcdata)) |
| 190 |
if not estimate: |
| 191 |
logging.debug("Previous pvc_backed: {} /{}".format(pvc_backed, pvc)) |
| 192 |
# If a pod has annotations to backup a pvc, it did it previously. So, skip if it was ok, if not second try. |
| 193 |
if pvc_backed.get(pvc) == 'ok': |
| 194 |
self._io.send_info(SKIP_PVCDATA_SECOND_BACKUP_INFO.format(pvc)) |
| 195 |
continue |
| 196 |
elif pvc_backed.get(pvc) == 'error': |
| 197 |
if self._plugin.pvc_is_terminating(nsname, pvcdata): |
| 198 |
logging.debug("Skip pvc. Cause Terminating status") |
| 199 |
self._io.send_warning("Skip pvc of second try `{}` because it is in Terminating status.".format(pvc)) |
| 200 |
continue |
| 201 |
if self._plugin.pvc_is_pending(nsname, pvcdata): |
| 202 |
logging.debug("Skip pvc. Cause Pending status") |
| 203 |
self._io.send_warning("Skip pvc `{}` because it is in Pending status.".format(pvc)) |
| 204 |
continue |
| 205 |
self._io.send_warning(SECOND_TRY_PVCDATA_INFO.format(pvc)) |
| 206 |
self._io.send_info(PROCESSING_PVCDATA_START_INFO.format(pvc=pvc)) |
| 207 |
if self._plugin.pvc_is_terminating(nsname, pvcdata): |
| 208 |
logging.debug("Skip pvc. Cause Terminating status") |
| 209 |
self._io.send_warning("Skip pvc `{}` because it is in Terminating status.".format(pvc)) |
| 210 |
continue |
| 211 |
if self._plugin.pvc_is_pending(nsname, pvcdata): |
| 212 |
logging.debug("Skip pvc. Cause Pending status") |
| 213 |
self._io.send_warning("Skip pvc `{}` because it is in Pending status.".format(pvc)) |
| 214 |
continue |
| 215 |
status = self.process_pvcdata(nsname, pvcdata) |
| 216 |
if status is None: |
| 217 |
# None means unable to prepare listening service during backup |
| 218 |
break |
| 219 |
if not estimate and status: |
| 220 |
self._io.send_info(PROCESSING_PVCDATA_STOP_INFO.format(pvc=pvc)) |
| 221 |
logging.debug("Finish pvcdatalist") |
| 222 |
|
| 223 |
nsdata = self._plugin.list_namespaced_objects_after_pvcdata(nsname, estimate=estimate) |
| 224 |
logging.debug('After PVCDATA NSDATA:{}'.format([ns.keys() for ns in nsdata])) # limit debug output |
| 225 |
for sub in nsdata: |
| 226 |
# sub is a list of different resource types |
| 227 |
if isinstance(sub, dict) and sub.get('exception'): |
| 228 |
self._handle_error(RES_LIST_ERROR.format(parse_json_descr(sub))) |
| 229 |
else: |
| 230 |
for res in sub: |
| 231 |
self.process_file(sub.get(res)) |
| 232 |
|
| 233 |
def _estimate_file(self, data): |
| 234 |
logging.debug('{}'.format(data)) |
| 235 |
if isinstance(data, dict): |
| 236 |
file_info = data.get('fi') |
| 237 |
elif isinstance(data, FileInfo): |
| 238 |
file_info = data |
| 239 |
else: |
| 240 |
raise ValueError('Invalid data in estimate_file') |
| 241 |
logging.debug('file_info: {}'.format(file_info)) |
| 242 |
self._io.send_file_info(file_info) |
| 243 |
|
| 244 |
def process_file(self, data): |
| 245 |
return self._estimate_file(data) |
| 246 |
|
| 247 |
def process_pvcdata(self, namespace, pvcdata): |
| 248 |
return self._estimate_file(pvcdata) |
| 249 |
|
| 250 |
def process_pod_pvcdata(self, namespace, pod, pvcnames): |
| 251 |
# iterate on requested pvc |
| 252 |
logging.debug("process_pod_pvcdata in Estimate mode") |
| 253 |
for pvc in pvcnames.split(','): |
| 254 |
if self._plugin.pvc_is_terminating(namespace, pvc): |
| 255 |
logging.debug("Skip pvc. Cause Terminating status") |
| 256 |
self._io.send_warning("Skip pvc `{}` because it is in Terminating status.".format(pvc)) |
| 257 |
continue |
| 258 |
if self._plugin.pvc_is_pending(namespace, pvc): |
| 259 |
logging.debug("Skip pvc. Cause Pending status") |
| 260 |
self._io.send_warning("Skip pvc `{}` because it is in Pending status.".format(pvc)) |
| 261 |
continue |
| 262 |
# get pvcdata for this volume |
| 263 |
pvcdata = self._plugin.get_pvcdata_namespaced(namespace, pvc) |
| 264 |
if isinstance(pvcdata, dict) and 'exception' in pvcdata: |
| 265 |
self._handle_error(PVCDATA_GET_ERROR.format(parse_json_descr(pvcdata))) |
| 266 |
else: |
| 267 |
logging.debug('PVCDATA:{}:{}'.format(pvc, pvcdata)) |
| 268 |
if len(pvcdata) > 0: |
| 269 |
self._estimate_file(pvcdata) |
| 270 |
return True |
| 271 |
|
| 272 |
def __match_includes(self, info): |
| 273 |
if len(self._params.get("includes", [])) <= 0: |
| 274 |
return True |
| 275 |
|
| 276 |
any_match = info.match_any_glob( |
| 277 |
self._params["includes"], |
| 278 |
self.include_matches |
| 279 |
) |
| 280 |
|
| 281 |
return any_match |
| 282 |
|
| 283 |
def __match_regex_includes(self, info): |
| 284 |
if len(self._params.get("regex_includes", [])) <= 0: |
| 285 |
return True |
| 286 |
|
| 287 |
any_match = info.match_any_regex( |
| 288 |
self._params["regex_includes"], |
| 289 |
self.regex_include_matches |
| 290 |
) |
| 291 |
|
| 292 |
return any_match |
| 293 |
|
| 294 |
def __match_excludes(self, info): |
| 295 |
if len(self._params.get("excludes", [])) <= 0: |
| 296 |
return False |
| 297 |
|
| 298 |
any_match = info.match_any_glob( |
| 299 |
self._params["excludes"], |
| 300 |
self.exclude_matches |
| 301 |
) |
| 302 |
|
| 303 |
return any_match |
| 304 |
|
| 305 |
def __match_regex_excludes(self, info): |
| 306 |
if len(self._params.get("regex_excludes", [])) <= 0: |
| 307 |
return False |
| 308 |
|
| 309 |
any_match = info.match_any_regex( |
| 310 |
self._params["regex_excludes"], |
| 311 |
self.regex_exclude_matches |
| 312 |
) |
| 313 |
|
| 314 |
return any_match |
| 315 |
|
| 316 |
def __verify_all_matches(self): |
| 317 |
self.__verify_matches("includes", self.include_matches) |
| 318 |
self.__verify_matches("regex_includes", self.regex_include_matches) |
| 319 |
self.__verify_matches("excludes", self.exclude_matches) |
| 320 |
self.__verify_matches("regex_excludes", self.regex_exclude_matches) |
| 321 |
|
| 322 |
def __verify_matches(self, pattern_type, matches): |
| 323 |
|
| 324 |
if len(self._params.get(pattern_type, [])) <= 0: |
| 325 |
return |
| 326 |
|
| 327 |
patterns = self._params[pattern_type] |
| 328 |
|
| 329 |
# Assures that all patterns got at least one match |
| 330 |
for pattern in patterns: |
| 331 |
if pattern not in matches: |
| 332 |
error_msg = PATTERN_NOT_FOUND.format(pattern) |
| 333 |
self._handle_error(error_msg) |