• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

karellen / kubernator / 18180360618

02 Oct 2025 01:15AM UTC coverage: 75.914% (-0.1%) from 76.019%
18180360618

push

github

arcivanov
Release 1.0.21

615 of 961 branches covered (64.0%)

Branch coverage included in aggregate %.

2395 of 3004 relevant lines covered (79.73%)

4.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.52
/src/main/python/kubernator/api.py
1
# -*- coding: utf-8 -*-
2
#
3
#   Copyright 2020 Express Systems USA, Inc
4
#   Copyright 2021 Karellen, Inc.
5
#
6
#   Licensed under the Apache License, Version 2.0 (the "License");
7
#   you may not use this file except in compliance with the License.
8
#   You may obtain a copy of the License at
9
#
10
#       http://www.apache.org/licenses/LICENSE-2.0
11
#
12
#   Unless required by applicable law or agreed to in writing, software
13
#   distributed under the License is distributed on an "AS IS" BASIS,
14
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
#   See the License for the specific language governing permissions and
16
#   limitations under the License.
17
#
18

19
import fnmatch
6✔
20
import json
6✔
21
import logging
6✔
22
import os
6✔
23
import platform
6✔
24
import re
6✔
25
import sys
6✔
26
import traceback
6✔
27
import urllib.parse
6✔
28
from collections.abc import Callable
6✔
29
from collections.abc import Iterable, MutableSet, Reversible
6✔
30
from enum import Enum
6✔
31
from hashlib import sha256
6✔
32
from io import StringIO as io_StringIO
6✔
33
from pathlib import Path
6✔
34
from shutil import rmtree
6✔
35
from subprocess import CalledProcessError
6✔
36
from types import GeneratorType
6✔
37
from typing import Optional, Union, MutableSequence
6✔
38

39
import requests
6✔
40
import yaml
6✔
41
from diff_match_patch import diff_match_patch
6✔
42
from gevent import sleep
6✔
43
from jinja2 import (Environment,
6✔
44
                    ChainableUndefined,
45
                    make_logging_undefined,
46
                    Template as JinjaTemplate,
47
                    pass_context)
48
from jsonschema import validators
6✔
49
from platformdirs import user_cache_dir
6✔
50

51
from kubernator._json_path import jp  # noqa: F401
6✔
52
from kubernator._k8s_client_patches import (URLLIB_HEADERS_PATCH,
6✔
53
                                            CUSTOM_OBJECT_PATCH_23,
54
                                            CUSTOM_OBJECT_PATCH_25)
55

56
_CACHE_HEADER_TRANSLATION = {"etag": "if-none-match",
6✔
57
                             "last-modified": "if-modified-since"}
58
_CACHE_HEADERS = ("etag", "last-modified")
6✔
59

60

61
def calling_frame_source(depth=2):
6✔
62
    f = traceback.extract_stack(limit=depth + 1)[0]
×
63
    return f"file {f.filename}, line {f.lineno} in {f.name}"
×
64

65

66
def re_filter(name: str, patterns: Iterable[re.Pattern]):
6✔
67
    for pattern in patterns:
6✔
68
        if pattern.match(name):
6✔
69
            return True
6✔
70

71

72
def to_patterns(*patterns):
6✔
73
    return [re.compile(fnmatch.translate(p)) for p in patterns]
×
74

75

76
def scan_dir(logger, path: Path, path_filter: Callable[[os.DirEntry], bool], excludes, includes):
6✔
77
    logger.debug("Scanning %s, excluding %s, including %s", path, excludes, includes)
6✔
78
    with os.scandir(path) as it:  # type: Iterable[os.DirEntry]
6✔
79
        files = {f: f for f in
6✔
80
                 sorted(d.name for d in it if path_filter(d) and not re_filter(d.name, excludes))}
81

82
    for include in includes:
6✔
83
        logger.trace("Considering include %s in %s", include, path)
6✔
84
        for f in list(files.keys()):
6✔
85
            if include.match(f):
6✔
86
                del files[f]
6✔
87
                logger.debug("Selecting %s in %s as it matches %s", f, path, include)
6✔
88
                yield path / f
6✔
89

90

91
class FileType(Enum):
6✔
92
    JSON = (json.load,)
6✔
93
    YAML = (yaml.safe_load_all,)
6✔
94

95
    def __init__(self, func):
6✔
96
        self.func = func
6✔
97

98

99
def _load_file(logger, path: Path, file_type: FileType, source=None) -> Iterable[dict]:
6✔
100
    with open(path, "rb") as f:
6✔
101
        try:
6✔
102
            data = file_type.func(f)
6✔
103
            if isinstance(data, GeneratorType):
6✔
104
                data = list(data)
6✔
105
            return data
6✔
106
        except Exception as e:
×
107
            logger.error("Failed parsing %s using %s", source or path, file_type, exc_info=e)
×
108
            raise
×
109

110

111
def _download_remote_file(url, file_name, cache: dict):
6✔
112
    retry_delay = 0
6✔
113
    while True:
5✔
114
        if retry_delay:
6!
115
            sleep(retry_delay)
×
116

117
        with requests.get(url, headers=cache, stream=True) as r:
6!
118
            if r.status_code == 429:
6!
119
                if not retry_delay:
×
120
                    retry_delay = 0.2
×
121
                else:
122
                    retry_delay *= 2.0
×
123
                if retry_delay > 2.5:
×
124
                    retry_delay = 2.5
×
125
                continue
×
126

127
            r.raise_for_status()
6✔
128
            if r.status_code != 304:
6!
129
                with open(file_name, "wb") as out:
×
130
                    for chunk in r.iter_content(chunk_size=65535):
×
131
                        out.write(chunk)
×
132
                return dict(r.headers)
×
133
            else:
134
                return None
6✔
135

136

137
def get_app_cache_dir():
6✔
138
    return Path(user_cache_dir("kubernator", "karellen"))
6✔
139

140

141
def get_cache_dir(category: str, sub_category: str = None):
6✔
142
    cache_dir = get_app_cache_dir() / category
6✔
143
    if sub_category:
6!
144
        cache_dir = cache_dir / sub_category
×
145
    if not cache_dir.exists():
6✔
146
        cache_dir.mkdir(parents=True)
6✔
147

148
    return cache_dir
6✔
149

150

151
def download_remote_file(logger, url: str, category: str = "k8s", sub_category: str = None,
6✔
152
                         downloader=_download_remote_file):
153
    config_dir = get_cache_dir(category, sub_category)
6✔
154

155
    file_name = config_dir / sha256(url.encode("UTF-8")).hexdigest()
6✔
156
    cache_file_name = file_name.with_suffix(".cache")
6✔
157
    logger.trace("Cache file for %s is %s.cache", url, file_name)
6✔
158

159
    cache = {}
6✔
160
    if cache_file_name.exists():
6!
161
        logger.trace("Loading cache file from %s", cache_file_name)
6✔
162
        try:
6✔
163
            with open(cache_file_name, "rb") as cache_f:
6✔
164
                cache = json.load(cache_f)
6✔
165
        except (IOError, ValueError) as e:
×
166
            logger.trace("Failed loading cache file from %s (cleaning up)", cache_file_name, exc_info=e)
×
167
            cache_file_name.unlink(missing_ok=True)
×
168

169
    logger.trace("Downloading %s into %s%s", url, file_name, " (caching)" if cache else "")
6✔
170
    headers = downloader(url, file_name, cache)
6✔
171
    up_to_date = False
6✔
172
    if not headers:
6!
173
        logger.trace("File %s(%s) is up-to-date", url, file_name.name)
6✔
174
        up_to_date = True
6✔
175
    else:
176
        cache = {_CACHE_HEADER_TRANSLATION.get(k.lower(), k): v
×
177
                 for k, v in headers.items()
178
                 if k.lower() in _CACHE_HEADERS}
179

180
        logger.trace("Update cache file in %s: %r", cache_file_name, cache)
×
181
        with open(cache_file_name, "wt") as cache_f:
×
182
            json.dump(cache, cache_f)
×
183

184
    return file_name, up_to_date
6✔
185

186

187
def load_remote_file(logger, url, file_type: FileType, category: str = "k8s", sub_category: str = None,
6✔
188
                     downloader=_download_remote_file):
189
    file_name, _ = download_remote_file(logger, url, category, sub_category, downloader=downloader)
6✔
190
    logger.debug("Loading %s from %s using %s", url, file_name, file_type.name)
6✔
191
    return _load_file(logger, file_name, file_type, url)
6✔
192

193

194
def load_file(logger, path: Path, file_type: FileType, source=None) -> Iterable[dict]:
6✔
195
    logger.debug("Loading %s using %s", source or path, file_type.name)
6✔
196
    return _load_file(logger, path, file_type)
6✔
197

198

199
def validator_with_defaults(validator_class):
6✔
200
    validate_properties = validator_class.VALIDATORS["properties"]
6✔
201

202
    def set_defaults(validator, properties, instance, schema):
6✔
203
        for property, subschema in properties.items():
6✔
204
            if "default" in subschema:
6✔
205
                instance.setdefault(property, subschema["default"])
6✔
206

207
        for error in validate_properties(validator, properties, instance, schema):
6!
208
            yield error
×
209

210
    return validators.extend(validator_class, {"properties": set_defaults})
6✔
211

212

213
class _PropertyList(MutableSequence):
6✔
214

215
    def __init__(self, seq, read_parent, name):
6✔
216
        self.__read_seq = seq
6✔
217
        self.__read_parent = read_parent
6✔
218
        self.__write_parent = None
6✔
219
        self.__write_seq = None
6✔
220
        self.__name = name
6✔
221

222
    def __iter__(self):
6✔
223
        return self.__read_seq.__iter__()
6✔
224

225
    def __mul__(self, __n):
6✔
226
        return self.__read_seq.__mul__(__n)
×
227

228
    def __rmul__(self, __n):
6✔
229
        return self.__read_seq.__rmul__(__n)
×
230

231
    def __imul__(self, __n):
6✔
232
        return self.__read_seq.__imul__(__n)
×
233

234
    def __contains__(self, __o):
6✔
235
        return self.__read_seq.__contains__(__o)
×
236

237
    def __reversed__(self):
6✔
238
        return self.__read_seq.__reversed__()
6✔
239

240
    def __gt__(self, __x):
6✔
241
        return self.__read_seq.__gt__(__x)
×
242

243
    def __ge__(self, __x):
6✔
244
        return self.__read_seq.__ge__(__x)
×
245

246
    def __lt__(self, __x):
6✔
247
        return self.__read_seq.__lt__(__x)
×
248

249
    def __le__(self, __x):
6✔
250
        return self.__read_seq.__le__(__x)
×
251

252
    def __len__(self):
6✔
253
        return self.__read_seq.__len__()
6✔
254

255
    def count(self, __value):
6✔
256
        return self.__read_seq.count(__value)
×
257

258
    def copy(self):
6✔
259
        while True:
260
            try:
×
261
                return self.__write_seq.copy()
×
262
            except AttributeError:
×
263
                self.__clone()
×
264

265
    def __getitem__(self, __i):
6✔
266
        return self.__read_seq.__getitem__(__i)
6✔
267

268
    def append(self, __object):
6✔
269
        while True:
5✔
270
            try:
6✔
271
                return self.__write_seq.append(__object)
6✔
272
            except AttributeError:
6✔
273
                self.__clone()
6✔
274

275
    def extend(self, __iterable):
6✔
276
        while True:
277
            try:
×
278
                return self.__write_seq.extend(__iterable)
×
279
            except AttributeError:
×
280
                self.__clone()
×
281

282
    def pop(self, __index=None):
6✔
283
        while True:
284
            try:
×
285
                return self.__write_seq.pop(__index)
×
286
            except AttributeError:
×
287
                self.__clone()
×
288

289
    def insert(self, __index, __object):
6✔
290
        while True:
291
            try:
×
292
                return self.__write_seq.insert(__index, __object)
×
293
            except AttributeError:
×
294
                self.__clone()
×
295

296
    def remove(self, __value):
6✔
297
        while True:
298
            try:
×
299
                return self.__write_seq.remove(__value)
×
300
            except AttributeError:
×
301
                self.__clone()
×
302

303
    def sort(self, *, key=None, reverse=False):
6✔
304
        while True:
305
            try:
×
306
                return self.__write_seq.sort(key=key, reverse=reverse)
×
307
            except AttributeError:
×
308
                self.__clone()
×
309

310
    def __setitem__(self, __i, __o):
6✔
311
        while True:
312
            try:
×
313
                return self.__write_seq.__setitem__(__i, __o)
×
314
            except AttributeError:
×
315
                self.__clone()
×
316

317
    def __delitem__(self, __i):
6✔
318
        while True:
319
            try:
×
320
                return self.__write_seq.__delitem__(__i)
×
321
            except AttributeError:
×
322
                self.__clone()
×
323

324
    def __add__(self, __x):
6✔
325
        while True:
326
            try:
×
327
                return self.__write_seq.__add__(__x)
×
328
            except AttributeError:
×
329
                self.__clone()
×
330

331
    def __iadd__(self, __x):
6✔
332
        while True:
333
            try:
×
334
                return self.__write_seq.__iadd__(__x)
×
335
            except AttributeError:
×
336
                self.__clone()
×
337

338
    def clear(self):
6✔
339
        while True:
340
            try:
×
341
                return self.__write_seq.clear()
×
342
            except AttributeError:
×
343
                self.__clone()
×
344

345
    def reverse(self):
6✔
346
        while True:
347
            try:
×
348
                return self.__write_seq.reverse()
×
349
            except AttributeError:
×
350
                self.__clone()
×
351

352
    def __clone(self):
6✔
353
        if self.__read_parent == self.__write_parent:
6✔
354
            self.__write_seq = self.__read_seq
6✔
355
        else:
356
            self.__write_seq = self.__read_seq.copy()
6✔
357
            self.__read_seq = self.__write_seq
6✔
358

359
            setattr(self.__write_parent, self.__name, self.__write_seq)
6✔
360

361

362
class PropertyDict:
6✔
363
    def __init__(self, _dict=None, _parent=None):
6✔
364
        self.__dict__["_PropertyDict__dict"] = _dict or {}
6✔
365
        self.__dict__["_PropertyDict__parent"] = _parent
6✔
366

367
    def __getattr__(self, item):
6✔
368
        v = self.__getattr(item)
6✔
369
        if isinstance(v, _PropertyList):
6✔
370
            v._PropertyList__write_parent = self
6✔
371
        return v
6✔
372

373
    def __getattr(self, item):
6✔
374
        try:
6✔
375
            v = self.__dict[item]
6✔
376
            if isinstance(v, list):
6✔
377
                v = _PropertyList(v, self, item)
6✔
378
            return v
6✔
379
        except KeyError:
6✔
380
            parent = self.__parent
6✔
381
            if parent is not None:
6!
382
                return parent.__getattr(item)
6✔
383
            raise AttributeError("no attribute %r" % item) from None
×
384

385
    def __setattr__(self, key, value):
6✔
386
        if key.startswith("_PropertyDict__"):
6!
387
            raise AttributeError("prohibited attribute %r" % key)
×
388
        if isinstance(value, dict):
6✔
389
            parent_dict = None
6✔
390
            if self.__parent is not None:
6✔
391
                try:
6✔
392
                    parent_dict = self.__parent.__getattr__(key)
6✔
393
                    if not isinstance(parent_dict, PropertyDict):
6!
394
                        raise ValueError("cannot override a scalar with a synthetic object for attribute %s", key)
×
395
                except AttributeError:
×
396
                    pass
×
397
            value = PropertyDict(value, _parent=parent_dict)
6✔
398
        self.__dict[key] = value
6✔
399

400
    def __delattr__(self, item):
6✔
401
        del self.__dict[item]
6✔
402

403
    def __len__(self):
6✔
404
        return len(self.__dir__())
×
405

406
    def __getitem__(self, item):
6✔
407
        return self.__dict.__getitem__(item)
×
408

409
    def __setitem__(self, key, value):
6✔
410
        self.__dict.__setitem__(key, value)
×
411

412
    def __delitem__(self, key):
6✔
413
        self.__dict.__delitem__(key)
×
414

415
    def __contains__(self, item):
6✔
416
        try:
6✔
417
            self.__dict[item]
6✔
418
            return True
6✔
419
        except KeyError:
6✔
420
            parent = self.__parent
6✔
421
            if parent is not None:
6✔
422
                return parent.__contains__(item)
6✔
423
            return False
6✔
424

425
    def __dir__(self) -> Iterable[str]:
6✔
426
        result: set[str] = set()
×
427
        result.update(self.__dict.keys())
×
428
        if self.__parent is not None:
×
429
            result.update(self.__parent.__dir__())
×
430
        return result
×
431

432
    def __repr__(self):
6✔
433
        return "PropertyDict[%r]" % self.__dict
×
434

435

436
def config_parent(config: PropertyDict):
6✔
437
    return config._PropertyDict__parent
×
438

439

440
def config_as_dict(config: PropertyDict):
6✔
441
    return {k: config[k] for k in dir(config)}
×
442

443

444
def config_get(config: PropertyDict, key: str, default=None):
6✔
445
    try:
×
446
        return config[key]
×
447
    except KeyError:
×
448
        return default
×
449

450

451
class Globs(MutableSet[Union[str, re.Pattern]]):
6✔
452
    def __init__(self, source: Optional[list[Union[str, re.Pattern]]] = None,
6✔
453
                 immutable=False):
454
        self._immutable = immutable
6✔
455
        if source:
6!
456
            self._list = [self.__wrap__(v) for v in source]
6✔
457
        else:
458
            self._list = []
×
459

460
    def __wrap__(self, item: Union[str, re.Pattern]):
6✔
461
        if isinstance(item, re.Pattern):
6✔
462
            return item
6✔
463
        return re.compile(fnmatch.translate(item))
6✔
464

465
    def __contains__(self, item: Union[str, re.Pattern]):
6✔
466
        return self._list.__contains__(self.__wrap__(item))
×
467

468
    def __iter__(self):
6✔
469
        return self._list.__iter__()
6✔
470

471
    def __len__(self):
6✔
472
        return self._list.__len__()
6✔
473

474
    def add(self, value: Union[str, re.Pattern]):
6✔
475
        if self._immutable:
6!
476
            raise RuntimeError("immutable")
×
477

478
        _list = self._list
6✔
479
        value = self.__wrap__(value)
6✔
480
        if value not in _list:
6!
481
            _list.append(value)
6✔
482

483
    def extend(self, values: Iterable[Union[str, re.Pattern]]):
6✔
484
        for v in values:
×
485
            self.add(v)
×
486

487
    def discard(self, value: Union[str, re.Pattern]):
6✔
488
        if self._immutable:
6!
489
            raise RuntimeError("immutable")
×
490

491
        _list = self._list
6✔
492
        value = self.__wrap__(value)
6✔
493
        if value in _list:
6!
494
            _list.remove(value)
6✔
495

496
    def add_first(self, value: Union[str, re.Pattern]):
6✔
497
        if self._immutable:
6!
498
            raise RuntimeError("immutable")
×
499

500
        _list = self._list
×
501
        value = self.__wrap__(value)
×
502
        if value not in _list:
×
503
            _list.insert(0, value)
×
504

505
    def extend_first(self, values: Reversible[Union[str, re.Pattern]]):
6✔
506
        for v in reversed(values):
×
507
            self.add_first(v)
×
508

509
    def __str__(self):
6✔
510
        return self._list.__str__()
6✔
511

512
    def __repr__(self):
6✔
513
        return f"Globs[{self._list}]"
×
514

515

516
class TemplateEngine:
6✔
517
    VARIABLE_START_STRING = "{${"
6✔
518
    VARIABLE_END_STRING = "}$}"
6✔
519

520
    def __init__(self, logger):
6✔
521
        self.template_failures = 0
6✔
522
        self.templates = {}
6✔
523

524
        class CollectingUndefined(ChainableUndefined):
6✔
525
            __slots__ = ()
6✔
526

527
            def __str__(self):
6✔
528
                self.template_failures += 1
×
529
                return super().__str__()
×
530

531
        logging_undefined = make_logging_undefined(
6✔
532
            logger=logger,
533
            base=CollectingUndefined
534
        )
535

536
        @pass_context
6✔
537
        def variable_finalizer(ctx, value):
6✔
538
            normalized_value = str(value)
6✔
539
            if self.VARIABLE_START_STRING in normalized_value and self.VARIABLE_END_STRING in normalized_value:
6✔
540
                value_template_content = sys.intern(normalized_value)
6✔
541
                env: Environment = ctx.environment
6✔
542
                value_template = self.templates.get(value_template_content)
6✔
543
                if not value_template:
6!
544
                    value_template = env.from_string(value_template_content, env.globals)
6✔
545
                    self.templates[value_template_content] = value_template
6✔
546
                return value_template.render(ctx.parent)
6✔
547

548
            return normalized_value
6✔
549

550
        self.env = Environment(variable_start_string=self.VARIABLE_START_STRING,
6✔
551
                               variable_end_string=self.VARIABLE_END_STRING,
552
                               autoescape=False,
553
                               finalize=variable_finalizer,
554
                               undefined=logging_undefined)
555

556
    def from_string(self, template):
6✔
557
        return self.env.from_string(template)
6✔
558

559
    def failures(self):
6✔
560
        return self.template_failures
6✔
561

562

563
class Template:
6✔
564
    def __init__(self, name: str, template: JinjaTemplate, defaults: dict = None, path=None, source=None):
6✔
565
        self.name = name
6✔
566
        self.source = source
6✔
567
        self.path = path
6✔
568
        self.template = template
6✔
569
        self.defaults = defaults
6✔
570

571
    def render(self, context: dict, values: dict):
6✔
572
        variables = {"ktor": context,
6✔
573
                     "values": (self.defaults or {}) | values}
574
        return self.template.render(variables)
6✔
575

576

577
class StringIO:
6✔
578
    def __init__(self, trimmed=True):
6✔
579
        self.write = self.write_trimmed if trimmed else self.write_untrimmed
6✔
580
        self._buf = io_StringIO()
6✔
581

582
    def write_untrimmed(self, line):
6✔
583
        self._buf.write(line)
6✔
584

585
    def write_trimmed(self, line):
6✔
586
        self._buf.write(f"{line}\n")
×
587

588
    def getvalue(self):
6✔
589
        return self._buf.getvalue()
6✔
590

591

592
class StripNL:
6✔
593
    def __init__(self, func):
6✔
594
        self._func = func
6✔
595

596
    def __call__(self, line: str):
6✔
597
        return self._func(line.rstrip("\r\n"))
6✔
598

599

600
def log_level_to_verbosity_count(level: int):
6✔
601
    return int(-level / 10 + 6)
6✔
602

603

604
def clone_url_str(url):
6✔
605
    return urllib.parse.urlunsplit(url[:3] + ("", ""))  # no query or fragment
6✔
606

607

608
def prepend_os_path(path):
6✔
609
    path = str(path)
6✔
610
    paths = os.environ["PATH"].split(os.pathsep)
6✔
611
    if path not in paths:
6!
612
        paths.insert(0, path)
6✔
613
        os.environ["PATH"] = os.pathsep.join(paths)
6✔
614
        return True
6✔
615
    return False
×
616

617

618
_GOLANG_MACHINE = platform.machine().lower()
6✔
619
if _GOLANG_MACHINE == "x86_64":
6!
620
    _GOLANG_MACHINE = "amd64"
6✔
621

622
_GOLANG_OS = platform.system().lower()
6✔
623

624

625
def get_golang_machine():
6✔
626
    return _GOLANG_MACHINE
6✔
627

628

629
def get_golang_os():
6✔
630
    return _GOLANG_OS
6✔
631

632

633
def sha256_file_digest(path):
6✔
634
    h = sha256()
×
635
    with open(path, "rb") as f:
×
636
        h.update(f.read(65535))
×
637
    return h.hexdigest()
×
638

639

640
class Repository:
6✔
641
    logger = logging.getLogger("kubernator.repository")
6✔
642
    git_logger = logger.getChild("git")
6✔
643

644
    def __init__(self, repo, cred_aug=None):
6✔
645
        repo = str(repo)  # in case this is a Path
6✔
646
        url = urllib.parse.urlsplit(repo)
6✔
647

648
        if not url.scheme and not url.netloc and Path(url.path).exists():
6!
649
            url = url._replace(scheme="file")  # In case it's a local repository
6✔
650

651
        self.url = url
6✔
652
        self.url_str = urllib.parse.urlunsplit(url[:4] + ("",))
6✔
653
        self._cred_aug = cred_aug
6✔
654
        self._hash_obj = (url.hostname if url.username or url.password else url.netloc,
6✔
655
                          url.path,
656
                          url.query)
657

658
        self.clone_url = None  # Actual URL components used in cloning operations
6✔
659
        self.clone_url_str = None  # Actual URL string used in cloning operations
6✔
660
        self.ref = None
6✔
661
        self.local_dir = None
6✔
662

663
    def __eq__(self, o: object) -> bool:
6✔
664
        if isinstance(o, Repository):
×
665
            return self._hash_obj == o._hash_obj
×
666

667
    def __hash__(self) -> int:
6✔
668
        return hash(self._hash_obj)
6✔
669

670
    def init(self, logger, context):
6✔
671
        run = context.app.run
6✔
672
        run_capturing_out = context.app.run_capturing_out
6✔
673

674
        url = self.url
6✔
675
        if self._cred_aug:
6!
676
            url = self._cred_aug(url)
6✔
677

678
        self.clone_url = url
6✔
679
        self.clone_url_str = clone_url_str(url)
6✔
680

681
        query = urllib.parse.parse_qs(self.url.query)
6✔
682
        ref = query.get("ref")
6✔
683
        if ref:
6✔
684
            self.ref = ref[0]
6✔
685

686
        config_dir = get_cache_dir("git")
6✔
687

688
        git_cache = config_dir / sha256(self.clone_url_str.encode("UTF-8")).hexdigest()
6✔
689

690
        if git_cache.exists() and git_cache.is_dir() and (git_cache / ".git").exists():
6✔
691
            try:
6✔
692
                run(["git", "status"], None, None, cwd=git_cache).wait()
6✔
693
            except CalledProcessError:
×
694
                rmtree(git_cache)
×
695

696
        self.local_dir = git_cache
6✔
697

698
        stdout_logger = StripNL(self.git_logger.debug)
6✔
699
        stderr_logger = StripNL(self.git_logger.info)
6✔
700
        if git_cache.exists():
6✔
701
            if not self.ref:
6!
702
                ref = run_capturing_out(["git", "symbolic-ref", "refs/remotes/origin/HEAD", "--short"],
×
703
                                        stderr_logger, cwd=git_cache).strip()[7:]  # Remove prefix "origin/"
704
            else:
705
                ref = self.ref
6✔
706
            self.logger.info("Using %s%s cached in %s", self.url_str,
6✔
707
                             f"?ref={ref}" if not self.ref else "",
708
                             self.local_dir)
709
            run(["git", "config", "remote.origin.fetch", f"+refs/heads/{ref}:refs/remotes/origin/{ref}"],
6✔
710
                stdout_logger, stderr_logger, cwd=git_cache).wait()
711
            run(["git", "fetch", "-pPt", "--force"], stdout_logger, stderr_logger, cwd=git_cache).wait()
6✔
712
            run(["git", "checkout", ref], stdout_logger, stderr_logger, cwd=git_cache).wait()
6✔
713
            run(["git", "clean", "-f"], stdout_logger, stderr_logger, cwd=git_cache).wait()
6✔
714
            run(["git", "reset", "--hard", ref, "--"], stdout_logger, stderr_logger, cwd=git_cache).wait()
6✔
715
            run(["git", "pull"], stdout_logger, stderr_logger, cwd=git_cache).wait()
6✔
716
        else:
717
            self.logger.info("Initializing %s -> %s", self.url_str, self.local_dir)
6✔
718
            args = (["git", "clone", "--depth", "1",
6✔
719
                     "-" + ("v" * log_level_to_verbosity_count(logger.getEffectiveLevel()))] +
720
                    (["-b", self.ref] if self.ref else []) +
721
                    ["--", self.clone_url_str, str(self.local_dir)])
722
            safe_args = [c if c != self.clone_url_str else self.url_str for c in args]
6✔
723
            run(args, stdout_logger, stderr_logger, safe_args=safe_args).wait()
6✔
724

725
    def cleanup(self):
6✔
726
        if False and self.local_dir:
6!
727
            self.logger.info("Cleaning up %s -> %s", self.url_str, self.local_dir)
728
            rmtree(self.local_dir)
729

730

731
class KubernatorPlugin:
6✔
732
    _name = None
6✔
733

734
    def set_context(self, context):
6✔
735
        raise NotImplementedError
×
736

737
    def register(self, **kwargs):
6✔
738
        pass
6✔
739

740
    def handle_init(self):
6✔
741
        pass
6✔
742

743
    def handle_start(self):
6✔
744
        pass
6✔
745

746
    def handle_before_dir(self, cwd: Path):
6✔
747
        pass
6✔
748

749
    def handle_before_script(self, cwd: Path):
6✔
750
        pass
6✔
751

752
    def handle_after_script(self, cwd: Path):
6✔
753
        pass
6✔
754

755
    def handle_after_dir(self, cwd: Path):
6✔
756
        pass
6✔
757

758
    def handle_apply(self):
6✔
759
        pass
6✔
760

761
    def handle_verify(self):
6✔
762
        pass
6✔
763

764
    def handle_shutdown(self):
6✔
765
        pass
6✔
766

767
    def handle_summary(self):
6✔
768
        pass
6✔
769

770

771
def install_python_k8s_client(run, package_major, logger, logger_stdout, logger_stderr, disable_patching,
6✔
772
                              fallback=False):
773
    cache_dir = get_cache_dir("python")
6✔
774
    package_major_dir = cache_dir / str(package_major)
6✔
775
    package_major_dir_str = str(package_major_dir)
6✔
776
    patch_indicator = package_major_dir / ".patched"
6✔
777

778
    if disable_patching and package_major_dir.exists() and patch_indicator.exists():
6✔
779
        logger.info("Patching is disabled, existing Kubernetes Client %s (%s) was patched - "
6✔
780
                    "deleting current client",
781
                    str(package_major), package_major_dir)
782
        rmtree(package_major_dir)
6✔
783

784
    if not package_major_dir.exists() or not len(os.listdir(package_major_dir)):
6✔
785
        package_major_dir.mkdir(parents=True, exist_ok=True)
6✔
786
        try:
6✔
787
            run([sys.executable, "-m", "pip", "install", "--no-deps", "--no-input",
6✔
788
                 "--root-user-action=ignore", "--break-system-packages", "--disable-pip-version-check",
789
                 "--target", package_major_dir_str, f"kubernetes>={package_major!s}dev0,<{int(package_major) + 1!s}"],
790
                logger_stdout, logger_stderr)
791
        except CalledProcessError as e:
×
792
            if not fallback and "No matching distribution found for" in e.stderr:
×
793
                logger.warning("Kubernetes Client %s (%s) failed to install because the version wasn't found. "
×
794
                               "Falling back to a client of the previous version - %s",
795
                               str(package_major), package_major_dir, int(package_major) - 1)
796
                return install_python_k8s_client(run,
×
797
                                                 int(package_major) - 1,
798
                                                 logger,
799
                                                 logger_stdout,
800
                                                 logger_stderr,
801
                                                 disable_patching,
802
                                                 fallback=True)
803
            else:
804
                raise
×
805

806
    if not patch_indicator.exists() and not disable_patching:
6✔
807
        if not fallback and not len(os.listdir(package_major_dir)):
6!
808
            # Directory is empty
809
            logger.warning("Kubernetes Client %s (%s) directory is empty - the client was not installed. "
×
810
                           "Falling back to a client of the previous version - %s",
811
                           str(package_major), package_major_dir, int(package_major) - 1)
812

813
            return install_python_k8s_client(run,
×
814
                                             int(package_major) - 1,
815
                                             logger,
816
                                             logger_stdout,
817
                                             logger_stderr,
818
                                             disable_patching,
819
                                             fallback=True)
820

821
        for patch_text, target_file, skip_if_found, min_version, max_version, name in (
6✔
822
                URLLIB_HEADERS_PATCH, CUSTOM_OBJECT_PATCH_23, CUSTOM_OBJECT_PATCH_25):
823
            patch_target = package_major_dir / target_file
6✔
824
            logger.info("Applying patch %s to %s...", name, patch_target)
6✔
825
            if min_version and int(package_major) < min_version:
6✔
826
                logger.info("Skipping patch %s on %s due to package major version %s below minimum %d!",
6✔
827
                            name, patch_target, package_major, min_version)
828
                continue
6✔
829
            if max_version and int(package_major) > max_version:
6✔
830
                logger.info("Skipping patch %s on %s due to package major version %s above maximum %d!",
6✔
831
                            name, patch_target, package_major, max_version)
832
                continue
6✔
833

834
            with open(patch_target, "rt") as f:
6✔
835
                target_file_original = f.read()
6✔
836
            if skip_if_found in target_file_original:
6✔
837
                logger.info("Skipping patch %s on %s, as it already appears to be patched!", name,
6✔
838
                            patch_target)
839
                continue
6✔
840

841
            dmp = diff_match_patch()
6✔
842
            patches = dmp.patch_fromText(patch_text)
6✔
843
            target_file_patched, results = dmp.patch_apply(patches, target_file_original)
6✔
844
            failed_patch = False
6✔
845
            for idx, result in enumerate(results):
6✔
846
                if not result:
6!
847
                    failed_patch = True
×
848
                    msg = ("Failed to apply a patch to Kubernetes Client API %s, hunk #%d, patch: \n%s" % (
×
849
                        patch_target, idx, patches[idx]))
850
                    logger.fatal(msg)
×
851
            if failed_patch:
6!
852
                raise RuntimeError(f"Failed to apply some Kubernetes Client API {patch_target} patches")
×
853

854
            with open(patch_target, "wt") as f:
6✔
855
                f.write(target_file_patched)
6✔
856

857
        patch_indicator.touch(exist_ok=False)
6✔
858

859
    return package_major_dir
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc