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

karellen / kubernator / 19201075326

09 Nov 2025 12:59AM UTC coverage: 75.318% (+0.3%) from 75.065%
19201075326

push

github

web-flow
Merge pull request #91 from karellen/helm_version_check

Add Helm ability to check latest versions of the charts used

650 of 1022 branches covered (63.6%)

Branch coverage included in aggregate %.

25 of 25 new or added lines in 1 file covered. (100.0%)

5 existing lines in 3 files now uncovered.

2545 of 3220 relevant lines covered (79.04%)

4.74 hits per line

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

76.79
/src/main/python/kubernator/plugins/k8s.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

20
import json
6✔
21
import logging
6✔
22
import re
6✔
23
import sys
6✔
24
import types
6✔
25
from collections.abc import Mapping
6✔
26
from functools import partial
6✔
27
from importlib.metadata import version as pkg_version
6✔
28
from pathlib import Path
6✔
29
from typing import Iterable, Callable, Sequence
6✔
30

31
import jsonpatch
6✔
32
import yaml
6✔
33
from kubernetes.client import ApiException
6✔
34

35
from kubernator.api import (KubernatorPlugin,
6✔
36
                            Globs,
37
                            scan_dir,
38
                            load_file,
39
                            FileType,
40
                            load_remote_file,
41
                            StripNL,
42
                            install_python_k8s_client,
43
                            TemplateEngine,
44
                            sleep)
45
from kubernator.merge import extract_merge_instructions, apply_merge_instructions
6✔
46
from kubernator.plugins.k8s_api import (K8SResourcePluginMixin,
6✔
47
                                        K8SResource,
48
                                        K8SResourcePatchType,
49
                                        K8SPropagationPolicy)
50

51
logger = logging.getLogger("kubernator.k8s")
6✔
52
proc_logger = logger.getChild("proc")
6✔
53
stdout_logger = StripNL(proc_logger.info)
6✔
54
stderr_logger = StripNL(proc_logger.warning)
6✔
55

56
FIELD_VALIDATION_STRICT_MARKER = "strict decoding error: "
6✔
57
VALID_FIELD_VALIDATION = ("Ignore", "Warn", "Strict")
6✔
58

59

60
def final_resource_validator(resources: Sequence[K8SResource],
6✔
61
                             resource: K8SResource,
62
                             error: Callable[..., Exception]) -> Iterable[Exception]:
63
    final_key = resource.get_manifest_key(resource.manifest)
6✔
64
    if final_key != resource.key:
6!
65
        yield error("Illegal change of identifiers of the resource "
×
66
                    "%s from %s have been changed to %s",
67
                    resource.key, resource.source, final_key)
68

69
    if resource.rdef.namespaced and not resource.namespace:
6!
70
        yield error("Namespaced resource %s from %s is missing the required namespace",
×
71
                    resource, resource.source)
72

73

74
def normalize_pkg_version(v: str):
6✔
75
    v_split = v.split(".")
6✔
76
    rev = v_split[-1]
6✔
77
    if not rev.isdigit():
6✔
78
        new_rev = ""
6✔
79
        for c in rev:
6!
80
            if not c.isdigit():
6✔
81
                break
6✔
82
            new_rev += c
6✔
83
        v_split[-1] = new_rev
6✔
84
    return tuple(map(int, v_split))
6✔
85

86

87
def api_exc_normalize_body(e: "ApiException"):
6✔
88
    if e.headers and "content-type" in e.headers:
6!
89
        content_type = e.headers["content-type"]
6✔
90
        if content_type == "application/json" or content_type.endswith("+json"):
6!
91
            e.body = json.loads(e.body)
6✔
92
        elif (content_type in ("application/yaml", "application/x-yaml", "text/yaml",
×
93
                               "text/x-yaml") or content_type.endswith("+yaml")):
94
            e.body = yaml.safe_load(e.body)
×
95

96

97
def api_exc_format_body(e: ApiException):
6✔
98
    if not isinstance(e.body, (str, bytes)):
6!
99
        e.body = json.dumps(e.body, indent=4)
6✔
100

101

102
class KubernetesPlugin(KubernatorPlugin, K8SResourcePluginMixin):
6✔
103
    logger = logger
6✔
104

105
    _name = "k8s"
6✔
106

107
    def __init__(self):
6✔
108
        super().__init__()
6✔
109
        self.context = None
6✔
110

111
        self.embedded_pkg_version = self._get_kubernetes_client_version()
6✔
112

113
        self._transformers = []
6✔
114
        self._validators = []
6✔
115
        self._manifest_patchers = []
6✔
116
        self._summary = 0, 0, 0
6✔
117
        self._template_engine = TemplateEngine(logger)
6✔
118

119
    def set_context(self, context):
6✔
120
        self.context = context
6✔
121

122
    def register(self,
6✔
123
                 field_validation="Warn",
124
                 field_validation_warn_fatal=True,
125
                 disable_client_patches=False):
126
        self.context.app.register_plugin("kubeconfig")
6✔
127

128
        if field_validation not in VALID_FIELD_VALIDATION:
6!
129
            raise ValueError("'field_validation' must be one of %s" % (", ".join(VALID_FIELD_VALIDATION)))
×
130

131
        context = self.context
6✔
132
        context.globals.k8s = dict(patch_field_excludes=("^/metadata/managedFields",
6✔
133
                                                         "^/metadata/generation",
134
                                                         "^/metadata/creationTimestamp",
135
                                                         "^/metadata/resourceVersion",
136
                                                         ),
137
                                   immutable_changes={("apps", "DaemonSet"): K8SPropagationPolicy.BACKGROUND,
138
                                                      ("apps", "StatefulSet"): K8SPropagationPolicy.ORPHAN,
139
                                                      ("apps", "Deployment"): K8SPropagationPolicy.ORPHAN,
140
                                                      ("storage.k8s.io", "StorageClass"): K8SPropagationPolicy.ORPHAN,
141
                                                      (None, "Pod"): K8SPropagationPolicy.BACKGROUND,
142
                                                      ("batch", "Job"): K8SPropagationPolicy.ORPHAN,
143
                                                      },
144
                                   default_includes=Globs(["*.yaml", "*.yml"], True),
145
                                   default_excludes=Globs([".*"], True),
146
                                   add_resources=self.add_resources,
147
                                   load_resources=self.api_load_resources,
148
                                   load_remote_resources=self.api_load_remote_resources,
149
                                   load_crds=self.api_load_crds,
150
                                   load_remote_crds=self.api_load_remote_crds,
151
                                   add_transformer=self.api_add_transformer,
152
                                   remove_transformer=self.api_remove_transformer,
153
                                   add_validator=self.api_remove_validator,
154
                                   add_manifest_patcher=self.api_add_manifest_patcher,
155
                                   get_api_versions=self.get_api_versions,
156
                                   create_resource=self.create_resource,
157
                                   disable_client_patches=disable_client_patches,
158
                                   field_validation=field_validation,
159
                                   field_validation_warn_fatal=field_validation_warn_fatal,
160
                                   field_validation_warnings=0,
161
                                   conflict_retry_delay=0.3,
162
                                   _k8s=self,
163
                                   )
164
        context.k8s = dict(default_includes=Globs(context.globals.k8s.default_includes),
6✔
165
                           default_excludes=Globs(context.globals.k8s.default_excludes)
166
                           )
167
        self.api_add_validator(final_resource_validator)
6✔
168

169
    def handle_init(self):
6✔
170
        pass
6✔
171

172
    def handle_start(self):
6✔
173
        self.context.kubeconfig.register_change_notifier(self._kubeconfig_changed)
6✔
174
        self.setup_client()
6✔
175

176
    def _kubeconfig_changed(self):
6✔
177
        self.setup_client()
×
178

179
    def _get_kubernetes_client_version(self):
6✔
180
        return pkg_version("kubernetes").split(".")
6✔
181

182
    def setup_client(self):
6✔
183
        k8s = self.context.k8s
6✔
184
        if "server_version" not in k8s:
6!
185
            self._setup_client()
6✔
186

187
        server_minor = k8s.server_version[1]
6✔
188

189
        logger.info("Using Kubernetes client version =~%s.0 for server version %s",
6✔
190
                    server_minor, ".".join(k8s.server_version))
191
        pkg_dir = install_python_k8s_client(self.context.app.run_passthrough_capturing, server_minor, logger,
6✔
192
                                            stdout_logger, stderr_logger, k8s.disable_client_patches)
193

194
        modules_to_delete = []
6✔
195
        for k, v in sys.modules.items():
6✔
196
            if k == "kubernetes" or k.startswith("kubernetes."):
6✔
197
                modules_to_delete.append(k)
6✔
198
        for k in modules_to_delete:
6✔
199
            del sys.modules[k]
6✔
200

201
        logger.info("Adding sys.path reference to %s", pkg_dir)
6✔
202
        sys.path.insert(0, str(pkg_dir))
6✔
203
        self.embedded_pkg_version = self._get_kubernetes_client_version()
6✔
204
        logger.info("Switching to Kubernetes client version %s", ".".join(self.embedded_pkg_version))
6✔
205
        self._setup_client()
6✔
206

207
        logger.debug("Reading Kubernetes OpenAPI spec for %s", k8s.server_git_version)
6✔
208

209
        k8s_def = load_remote_file(logger, f"https://raw.githubusercontent.com/kubernetes/kubernetes/"
6✔
210
                                           f"{k8s.server_git_version}/api/openapi-spec/swagger.json",
211
                                   FileType.JSON)
212
        self.resource_definitions_schema = k8s_def
6✔
213

214
        self._populate_resource_definitions()
6✔
215

216
    def _setup_client(self):
6✔
217
        from kubernetes import client
6✔
218

219
        context = self.context
6✔
220
        k8s = context.k8s
6✔
221

222
        k8s.client = self._setup_k8s_client()
6✔
223
        version = client.VersionApi(k8s.client).get_code()
6✔
224
        if "-eks-" or "-gke" in version.git_version:
6!
225
            git_version = version.git_version.split("-")[0]
6✔
226
        else:
UNCOV
227
            git_version = version.git_version
228

229
        k8s.server_version = git_version[1:].split(".")
6✔
230
        k8s.server_git_version = git_version
6✔
231

232
        logger.info("Found Kubernetes %s on %s", k8s.server_git_version, k8s.client.configuration.host)
6✔
233

234
        K8SResource._k8s_client_version = normalize_pkg_version(pkg_version("kubernetes"))
6✔
235
        K8SResource._k8s_field_validation = k8s.field_validation
6✔
236
        K8SResource._k8s_field_validation_patched = not k8s.disable_client_patches
6✔
237
        K8SResource._logger = self.logger
6✔
238
        K8SResource._api_warnings = self._api_warnings
6✔
239

240
    def _api_warnings(self, resource, warn):
6✔
241
        k8s = self.context.k8s
6✔
242
        self.context.globals.k8s.field_validation_warnings += 1
6✔
243

244
        log = self.logger.warning
6✔
245
        if k8s.field_validation_warn_fatal:
6✔
246
            log = self.logger.error
6✔
247

248
        log("FAILED FIELD VALIDATION on resource %s from %s: %s", resource, resource.source, warn)
6✔
249

250
    def handle_before_dir(self, cwd: Path):
6✔
251
        context = self.context
6✔
252
        context.k8s.default_includes = Globs(context.k8s.default_includes)
6✔
253
        context.k8s.default_excludes = Globs(context.k8s.default_excludes)
6✔
254
        context.k8s.includes = Globs(context.k8s.default_includes)
6✔
255
        context.k8s.excludes = Globs(context.k8s.default_excludes)
6✔
256

257
    def handle_after_dir(self, cwd: Path):
6✔
258
        context = self.context
6✔
259
        k8s = context.k8s
6✔
260

261
        for f in scan_dir(logger, cwd, lambda d: d.is_file(), k8s.excludes, k8s.includes):
6✔
262
            p = cwd / f.name
6✔
263
            display_p = context.app.display_path(p)
6✔
264
            logger.debug("Adding Kubernetes manifest from %s", display_p)
6✔
265

266
            manifests = load_file(logger, p, FileType.YAML, display_p,
6✔
267
                                  self._template_engine,
268
                                  {"ktor": context}
269
                                  )
270

271
            for manifest in manifests:
6✔
272
                if manifest:
6!
273
                    self.add_resource(manifest, display_p)
6✔
274

275
    def handle_apply(self):
6✔
276
        context = self.context
6✔
277
        k8s = context.k8s
6✔
278

279
        self._validate_resources()
6✔
280

281
        cmd = context.app.args.command
6✔
282
        file = context.app.args.file
6✔
283
        file_format = context.app.args.output_format
6✔
284
        dry_run = context.app.args.dry_run
6✔
285
        dump = cmd == "dump"
6✔
286

287
        status_msg = f"{' (dump only)' if dump else ' (dry run)' if dry_run else ''}"
6✔
288
        if dump:
6✔
289
            logger.info("Will dump the changes into a file %s in %s format", file, file_format)
6✔
290

291
        patch_field_excludes = [re.compile(e) for e in context.globals.k8s.patch_field_excludes]
6✔
292
        dump_results = []
6✔
293
        total_created, total_patched, total_deleted = 0, 0, 0
6✔
294
        for resource in self.resources.values():
6✔
295
            if dump:
6✔
296
                resource_id = {"apiVersion": resource.api_version,
6✔
297
                               "kind": resource.kind,
298
                               "name": resource.name
299
                               }
300

301
                def patch_func(patch):
6✔
302
                    if resource.rdef.namespaced:
6!
303
                        resource_id["namespace"] = resource.namespace
×
304
                    method_descriptor = {"method": "patch",
6✔
305
                                         "resource": resource_id,
306
                                         "body": patch
307
                                         }
308
                    dump_results.append(method_descriptor)
6✔
309

310
                def create_func():
6✔
311
                    method_descriptor = {"method": "create",
6✔
312
                                         "body": resource.manifest}
313
                    dump_results.append(method_descriptor)
6✔
314

315
                def delete_func(*, propagation_policy):
6✔
316
                    method_descriptor = {"method": "delete",
×
317
                                         "resource": resource_id,
318
                                         "propagation_policy": propagation_policy.policy
319
                                         }
320
                    dump_results.append(method_descriptor)
×
321
            else:
322
                patch_func = partial(resource.patch, patch_type=K8SResourcePatchType.JSON_PATCH, dry_run=dry_run)
6✔
323
                create_func = partial(resource.create, dry_run=dry_run)
6✔
324
                delete_func = partial(resource.delete, dry_run=dry_run)
6✔
325

326
            created, patched, deleted = self._apply_resource(dry_run,
6✔
327
                                                             patch_field_excludes,
328
                                                             resource,
329
                                                             patch_func,
330
                                                             create_func,
331
                                                             delete_func,
332
                                                             status_msg)
333

334
            total_created += created
6✔
335
            total_patched += patched
6✔
336
            total_deleted += deleted
6✔
337

338
        if ((dump or dry_run) and
6✔
339
                k8s.field_validation_warn_fatal and self.context.globals.k8s.field_validation_warnings):
340
            msg = ("There were %d field validation warnings and the warnings are fatal!" %
6✔
341
                   self.context.globals.k8s.field_validation_warnings)
342
            logger.fatal(msg)
6✔
343
            raise RuntimeError(msg)
6✔
344

345
        if dump:
6✔
346
            if file_format in ("json", "json-pretty"):
6!
347
                json.dump(dump_results, file, sort_keys=True,
×
348
                          indent=4 if file_format == "json-pretty" else None)
349
            else:
350
                yaml.safe_dump(dump_results, file)
6✔
351
        else:
352
            self._summary = total_created, total_patched, total_deleted
6✔
353

354
    def handle_summary(self):
6✔
355
        total_created, total_patched, total_deleted = self._summary
6✔
356
        logger.info("Created %d, patched %d, deleted %d resources", total_created, total_patched, total_deleted)
6✔
357

358
    def api_load_resources(self, path: Path, file_type: str):
6✔
359
        return self.add_local_resources(path, FileType[file_type.upper()])
×
360

361
    def api_load_remote_resources(self, url: str, file_type: str, file_category=None):
6✔
362
        return self.add_remote_resources(url, FileType[file_type.upper()], sub_category=file_category)
×
363

364
    def api_load_crds(self, path: Path, file_type: str):
6✔
365
        return self.add_local_crds(path, FileType[file_type.upper()])
6✔
366

367
    def api_load_remote_crds(self, url: str, file_type: str, file_category=None):
6✔
368
        return self.add_remote_crds(url, FileType[file_type.upper()], sub_category=file_category)
6✔
369

370
    def api_add_transformer(self, transformer):
6✔
371
        if transformer not in self._transformers:
6!
372
            self._transformers.append(transformer)
6✔
373

374
    def api_add_validator(self, validator):
6✔
375
        if validator not in self._validators:
6!
376
            self._validators.append(validator)
6✔
377

378
    def api_add_manifest_patcher(self, patcher):
6✔
379
        if patcher not in self._manifest_patchers:
×
380
            self._manifest_patchers.append(patcher)
×
381

382
    def api_remove_transformer(self, transformer):
6✔
383
        if transformer in self._transformers:
6!
384
            self._transformers.remove(transformer)
6✔
385

386
    def api_remove_validator(self, validator):
6✔
387
        if validator not in self._validators:
×
388
            self._validators.remove(validator)
×
389

390
    def api_validation_error(self, msg, *args):
6✔
391
        frame = sys._getframe().f_back
×
392
        tb = None
×
393
        while True:
394
            if not frame:
×
395
                break
×
396
            tb = types.TracebackType(tb, frame, frame.f_lasti, frame.f_lineno)
×
397
            frame = frame.f_back
×
398
        return ValueError((msg % args) if args else msg).with_traceback(tb)
×
399

400
    def _patch_manifest(self,
6✔
401
                        manifest: dict,
402
                        resource_description: str):
403
        for patcher in reversed(self._manifest_patchers):
6!
404
            logger.debug("Applying patcher %s to %s",
×
405
                         getattr(patcher, "__name__", patcher),
406
                         resource_description)
407
            manifest = patcher(manifest, resource_description) or manifest
×
408

409
        return manifest
6✔
410

411
    def _transform_resource(self, resources: Sequence[K8SResource], resource: K8SResource) -> K8SResource:
6✔
412
        for transformer in reversed(self._transformers):
6✔
413
            logger.debug("Applying transformer %s to %s from %s",
6✔
414
                         getattr(transformer, "__name__", transformer),
415
                         resource, resource.source)
416
            resource = transformer(resources, resource) or resource
6✔
417

418
        return resource
6✔
419

420
    def _validate_resources(self):
6✔
421
        errors: list[Exception] = []
6✔
422
        for resource in self.resources.values():
6✔
423
            for validator in reversed(self._validators):
6✔
424
                logger.debug("Applying validator %s to %s from %s",
6✔
425
                             getattr(validator, "__name__", validator),
426
                             resource, resource.source)
427
                errors.extend(validator(self.resources, resource, self.api_validation_error))
6✔
428
        if errors:
6!
429
            for error in errors:
×
430
                logger.error("Validation error: %s", error)
×
431
            raise errors[0]
×
432

433
    def _apply_resource(self,
6✔
434
                        dry_run,
435
                        patch_field_excludes: Iterable[re.compile],
436
                        resource: K8SResource,
437
                        patch_func: Callable[[Iterable[dict]], None],
438
                        create_func: Callable[[], None],
439
                        delete_func: Callable[[K8SPropagationPolicy], None],
440
                        status_msg):
441
        from kubernetes import client
6✔
442
        from kubernetes.client.rest import ApiException
6✔
443

444
        rdef = resource.rdef
6✔
445
        rdef.populate_api(client, self.context.k8s.client)
6✔
446

447
        def handle_400_strict_validation_error(e: ApiException):
6✔
448
            if e.status == 400:
6!
449
                # Assumes the body has been parsed
450
                status = e.body
6✔
451
                if status["status"] == "Failure":
6!
452
                    if FIELD_VALIDATION_STRICT_MARKER in status["message"]:
6!
453
                        message = status["message"]
6✔
454
                        messages = message[message.find(FIELD_VALIDATION_STRICT_MARKER) +
6✔
455
                                           len(FIELD_VALIDATION_STRICT_MARKER):].split(",")
456
                        for m in messages:
6✔
457
                            self._api_warnings(resource, m.strip())
6✔
458

459
                        raise e from None
6✔
460
                    else:
461
                        logger.error("FAILED MODIFYING resource %s from %s: %s",
×
462
                                     resource, resource.source, status["message"])
463
                        raise e from None
×
464

465
        def create(exists_ok=False, wait_for_delete=False):
6✔
466
            logger.info("Creating resource %s%s%s", resource, status_msg,
6✔
467
                        " (ignoring existing)" if exists_ok else "")
468
            while True:
5✔
469
                try:
6✔
470
                    create_func()
6✔
471
                    return
6✔
472
                except ApiException as __e:
6✔
473
                    api_exc_normalize_body(__e)
6✔
474
                    try:
6✔
475
                        if exists_ok or wait_for_delete:
6!
476
                            if __e.status == 409:
×
477
                                status = __e.body
×
478
                                if status["reason"] == "AlreadyExists":
×
479
                                    if wait_for_delete:
×
480
                                        sleep(self.context.k8s.conflict_retry_delay)
×
481
                                        logger.info("Retry creating resource %s%s%s", resource, status_msg,
×
482
                                                    " (ignoring existing)" if exists_ok else "")
483
                                        continue
×
484
                                    else:
485
                                        return
×
486
                        raise
6✔
487
                    except ApiException as ___e:
6✔
488
                        api_exc_format_body(___e)
6✔
489
                        raise
6✔
490

491
        merge_instrs, normalized_manifest = extract_merge_instructions(resource.manifest, resource)
6✔
492
        if merge_instrs:
6✔
493
            logger.trace("Normalized manifest (no merge instructions) for resource %s: %s", resource,
6✔
494
                         normalized_manifest)
495
        else:
496
            normalized_manifest = resource.manifest
6✔
497

498
        logger.debug("Applying resource %s%s", resource, status_msg)
6✔
499
        try:
6✔
500
            remote_resource = resource.get()
6✔
501
            logger.trace("Current resource %s: %s", resource, remote_resource)
6✔
502
        except ApiException as e:
6✔
503
            api_exc_normalize_body(e)
6✔
504
            try:
6✔
505
                if e.status == 404:
6!
506
                    try:
6✔
507
                        create()
6✔
508
                        return 1, 0, 0
6✔
509
                    except ApiException as e:
6✔
510
                        api_exc_normalize_body(e)
6✔
511
                        if not handle_400_strict_validation_error(e):
6!
512
                            raise
1✔
513
                else:
514
                    raise
×
515
            except ApiException as _e:
6✔
516
                api_exc_format_body(_e)
6✔
517
                raise
6✔
518
        else:
519
            logger.trace("Attempting to retrieve a normalized patch for resource %s: %s", resource, normalized_manifest)
6✔
520
            try:
6✔
521
                merged_resource = resource.patch(normalized_manifest,
6✔
522
                                                 patch_type=K8SResourcePatchType.SERVER_SIDE_PATCH,
523
                                                 dry_run=True,
524
                                                 force=True)
525
            except ApiException as e:
×
526
                try:
×
527
                    api_exc_normalize_body(e)
×
528

529
                    if e.status == 422:
×
530
                        status = e.body
×
531
                        # Assumes the body has been unmarshalled
532
                        details = status["details"]
×
533
                        immutable_key = details.get("group"), details["kind"]
×
534

535
                        try:
×
536
                            propagation_policy = self.context.k8s.immutable_changes[immutable_key]
×
537
                        except KeyError:
×
538
                            raise e from None
×
539
                        else:
540
                            for cause in details["causes"]:
×
541
                                if (
×
542
                                        cause["reason"] == "FieldValueInvalid" and
543
                                        "field is immutable" in cause["message"]
544
                                        or
545
                                        cause["reason"] == "FieldValueForbidden" and
546
                                        ("Forbidden: updates to" in cause["message"]
547
                                         or
548
                                         "Forbidden: pod updates" in cause["message"])
549
                                ):
550
                                    logger.info("Deleting resource %s (cascade %s)%s", resource,
×
551
                                                propagation_policy.policy,
552
                                                status_msg)
553
                                    delete_func(propagation_policy=propagation_policy)
×
554
                                    create(exists_ok=dry_run, wait_for_delete=not dry_run)
×
555
                                    return 1, 0, 1
×
556
                            raise
×
557
                    else:
558
                        if not handle_400_strict_validation_error(e):
×
559
                            raise
×
560
                except ApiException as _e:
×
561
                    api_exc_format_body(_e)
×
562
                    raise
×
563

564
            else:
565
                logger.trace("Merged resource %s: %s", resource, merged_resource)
6✔
566
                if merge_instrs:
6✔
567
                    apply_merge_instructions(merge_instrs, normalized_manifest, merged_resource, logger, resource)
6✔
568

569
                patch = jsonpatch.make_patch(remote_resource, merged_resource)
6✔
570
                logger.trace("Resource %s initial patches are: %s", resource, patch)
6✔
571
                patch = self._filter_resource_patch(patch, patch_field_excludes)
6✔
572
                logger.trace("Resource %s final patches are: %s", resource, patch)
6✔
573
                if patch:
6✔
574
                    logger.info("Patching resource %s%s", resource, status_msg)
6✔
575
                    patch_func(patch)
6✔
576
                    return 0, 1, 0
6✔
577
                else:
578
                    logger.info("Nothing to patch for resource %s", resource)
6✔
579
                    return 0, 0, 0
6✔
580

581
    def _filter_resource_patch(self, patch: Iterable[Mapping], excludes: Iterable[re.compile]):
6✔
582
        result = []
6✔
583
        for op in patch:
6✔
584
            path = op["path"]
6✔
585
            excluded = False
6✔
586
            for exclude in excludes:
6✔
587
                if exclude.match(path):
6✔
588
                    logger.trace("Excluding %r from patch %s", op, patch)
6✔
589
                    excluded = True
6✔
590
                    break
6✔
591
            if excluded:
6✔
592
                continue
6✔
593
            result.append(op)
6✔
594
        return result
6✔
595

596
    def _setup_k8s_client(self):
6✔
597
        from kubernetes import client
6✔
598
        from kubernetes.config import load_incluster_config, load_kube_config, ConfigException
6✔
599

600
        try:
6✔
601
            logger.debug("Trying K8S in-cluster configuration")
6✔
602
            load_incluster_config()
6✔
603
            logger.info("Running K8S with in-cluster configuration")
×
604
        except ConfigException as e:
6✔
605
            logger.trace("K8S in-cluster configuration failed", exc_info=e)
6✔
606
            logger.debug("Initializing K8S with kubeconfig configuration")
6✔
607
            load_kube_config(config_file=self.context.kubeconfig.kubeconfig)
6✔
608

609
        k8s_client = client.ApiClient()
6✔
610

611
        # Patch the header content type selector to allow json patch
612
        k8s_client._select_header_content_type = k8s_client.select_header_content_type
6✔
613
        k8s_client.select_header_content_type = self._select_header_content_type_patch
6✔
614

615
        return k8s_client
6✔
616

617
    def _select_header_content_type_patch(self, content_types):
6✔
618
        """Returns `Content-Type` based on an array of content_types provided.
619
        :param content_types: List of content-types.
620
        :return: Content-Type (e.g. application/json).
621
        """
622

623
        content_type = self.context.k8s.client._select_header_content_type(content_types)
×
624
        if content_type == "application/merge-patch+json":
×
625
            return "application/json-patch+json"
×
626
        return content_type
×
627

628
    def __repr__(self):
6✔
629
        return "Kubernetes Plugin"
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