Skip to content

K8s

Kubernetes Module to implement functions to read / write Kubernetes objects.

This includes as Pods, Stateful Sets, Config Maps, ...

https://github.com/kubernetes-client/python https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md https://github.com/kubernetes-client/python/tree/master/examples

K8s

Provides an interface to interact with the Kubernetes API.

This class can run both in-cluster and locally using kubeconfig. It offers methods to interact with Kubernetes namespaces, pods, and various API objects like CoreV1, AppsV1, and NetworkingV1.

Attributes:

Name Type Description
logger Logger

Logger for the class.

_core_v1_api CoreV1Api

API client for Kubernetes Core V1.

_apps_v1_api AppsV1Api

API client for Kubernetes Apps V1.

_networking_v1_api NetworkingV1Api

API client for Kubernetes Networking V1.

_namespace str

The namespace in which operations are performed.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
class K8s:
    """Provides an interface to interact with the Kubernetes API.

    This class can run both in-cluster and locally using kubeconfig.
    It offers methods to interact with Kubernetes namespaces, pods,
    and various API objects like CoreV1, AppsV1, and NetworkingV1.

    Attributes:
        logger (logging.Logger): Logger for the class.
        _core_v1_api (CoreV1Api): API client for Kubernetes Core V1.
        _apps_v1_api (AppsV1Api): API client for Kubernetes Apps V1.
        _networking_v1_api (NetworkingV1Api): API client for Kubernetes Networking V1.
        _namespace (str): The namespace in which operations are performed.

    """

    logger: logging.Logger = default_logger

    _core_v1_api = None
    _apps_v1_api = None
    _networking_v1_api = None
    _namespace = None

    def __init__(
        self,
        kubeconfig_file: str | None = None,
        namespace: str = "default",
        logger: logging.Logger = default_logger,
    ) -> None:
        """Initialize the Kubernetes object.

        Args:
            kubeconfig_file (str | None, optional):
                Path to a kubeconfig file. Defaults to None.
            namespace (str, optional):
                The Kubernetes name space. Defaults to "default".
            logger (logging.Logger, optional):
                The logger object. Defaults to default_logger.

        """

        if logger != default_logger:
            self.logger = logger.getChild("k8s")
            for logfilter in logger.filters:
                self.logger.addFilter(logfilter)

        # Configure Kubernetes API authentication to use pod serviceAccount

        try:
            config.load_incluster_config()
            configured = True
        except ConfigException:
            configured = False
            self.logger.info("Failed to load in-cluster config")

        if kubeconfig_file is None:
            kubeconfig_file = os.getenv(
                "KUBECONFIG",
                os.path.expanduser("~/.kube/config"),
            )

        if not configured:
            try:
                config.load_kube_config(config_file=kubeconfig_file)
            except ConfigException:
                self.logger.info(
                    "Failed to load kubernetes config with file -> '%s'",
                    kubeconfig_file,
                )

        self._core_v1_api = CoreV1Api()
        self._apps_v1_api = AppsV1Api()
        self._networking_v1_api = NetworkingV1Api()

        if namespace == "default":
            # Read current namespace
            try:
                with open(
                    "/var/run/secrets/kubernetes.io/serviceaccount/namespace",
                    encoding="utf-8",
                ) as namespace_file:
                    self._namespace = namespace_file.read()
            except FileNotFoundError:
                self._namespace = namespace
        else:
            self._namespace = namespace

    # end method definition

    def get_core_v1_api(self) -> CoreV1Api:
        """Return Kubernetes Core V1 API object.

        Returns:
            object: Kubernetes API object

        """

        return self._core_v1_api

    # end method definition

    def get_apps_v1_api(self) -> AppsV1Api:
        """Return Kubernetes Apps V1 API object.

        Returns:
            object: Kubernetes API object

        """

        return self._apps_v1_api

    # end method definition

    def get_networking_v1_api(self) -> NetworkingV1Api:
        """Return Kubernetes Networking V1 API object.

        Returns:
            object: Kubernetes API object

        """

        return self._networking_v1_api

    # end method definition

    def get_namespace(self) -> str:
        """Return Kubernetes Namespace.

        Returns:
            str: Kubernetes namespace

        """

        return self._namespace

    # end method definition

    def get_pod(self, pod_name: str) -> V1Pod:
        """Get a pod in the configured namespace (the namespace is defined in the class constructor).

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_pod

        Args:
            pod_name (str):
                The name of the Kubernetes pod in the current namespace.

        Returns:
            V1Pod (object) or None if the call fails.
            - api_version='v1',
            - kind='Pod',
            - metadata=V1ObjectMeta(...),
            - spec=V1PodSpec(...),
            - status=V1PodStatus(...)

        """

        try:
            response = self.get_core_v1_api().read_namespaced_pod(
                name=pod_name,
                namespace=self.get_namespace(),
            )
        except ApiException as e:
            if e.status == 404:
                self.logger.debug("Pod -> '%s' not found (may be deleted).", pod_name)
                return None
            else:
                self.logger.error("Failed to get pod -> '%s'!", pod_name)
                return None  # Unexpected error, return None
        return response

    # end method definition

    def list_pods(
        self,
        field_selector: str = "",
        label_selector: str = "",
    ) -> V1PodList:
        """List all Kubernetes pods in a given namespace.

        The list can be further restricted by specifying a field or label selector.

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_pod

        Args:
            field_selector (str): filter result based on fields
            label_selector (str): filter result based on labels

        Returns:
            V1PodList (object) or None if the call fails
            Properties can be accessed with the "." notation (this is an object not a dict!):
            - api_version: The Kubernetes API version.
            - items: A list of V1Pod objects, each representing a pod. You can access the fields of a
                    V1Pod object using dot notation, for example, pod.metadata.name to access the name of the pod
            - kind: The Kubernetes object kind, which is always "PodList".
            - metadata: Additional metadata about the pod list, such as the resource version.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1PodList.md

        """

        try:
            response = self.get_core_v1_api().list_namespaced_pod(
                field_selector=field_selector,
                label_selector=label_selector,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to list pods with field selector -> '%s' and label selector -> '%s'",
                field_selector,
                label_selector,
            )
            return None

        return response

    # end method definition

    def wait_pod_condition(
        self,
        pod_name: str,
        condition_name: str,
        sleep_time: int = 30,
    ) -> None:
        """Wait for the pod to reach a defined condition (e.g. "Ready").

        Args:
            pod_name (str):
                The name of the Kubernetes pod in the current namespace.
            condition_name (str):
                The name of the condition, e.g. "Ready".
            sleep_time (int):
                The number of seconds to wait between repetitive status checks.

        Returns:
            None

        """

        ready = False
        while not ready:
            try:
                pod_status = self.get_core_v1_api().read_namespaced_pod_status(
                    pod_name,
                    self.get_namespace(),
                )

                # Check if the pod has reached the defined condition:
                for cond in pod_status.status.conditions:
                    if cond.type == condition_name and cond.status == "True":
                        self.logger.info(
                            "Pod -> '%s' is in state -> '%s'!",
                            pod_name,
                            condition_name,
                        )
                        ready = True
                        break
                else:
                    self.logger.info(
                        "Pod -> '%s' is not yet in state -> '%s'. Waiting...",
                        pod_name,
                        condition_name,
                    )
                    time.sleep(sleep_time)
                    continue

            except ApiException:
                self.logger.error(
                    "Failed to wait for pod -> '%s' to reach state -> '%s'.",
                    pod_name,
                    condition_name,
                )

    # end method definition

    def exec_pod_command(
        self,
        pod_name: str,
        command: list,
        max_retry: int = 3,
        time_retry: int = 10,
        container: str | None = None,
        timeout: int = 60,
    ) -> str | None:
        """Execute a command inside a Kubernetes Pod (similar to kubectl exec on command line).

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#connect_get_namespaced_pod_exec

        Args:
            pod_name (str):
                The name of the Kubernetes pod in the current namespace.
            command (list):
                A list of command and its parameters, e.g. ["/bin/bash", "-c", "pwd"]
                The "-c" is required to make the shell executing the command.
            max_retry (int):
                The maximum number of attempts to execute the command.
            time_retry (int):
                Wait time in seconds between retries.
            container (str):
                The container name if the pod runs multiple containers inside.
            timeout (int):
                Timeout duration that is waited for any response in seconds.
                Each time a response is found in stdout or stderr we wait another timeout duration [60]

        Returns:
            str | None:
                Response of the command or None if the call fails.

        """

        pod = self.get_pod(pod_name=pod_name)
        if not pod:
            self.logger.error("Pod -> '%s' does not exist. Cannot execute command -> %s", pod_name, command)
            return None

        self.logger.debug("Execute command -> %s in pod -> '%s'", command, pod_name)

        retry_counter = 1

        while retry_counter <= max_retry:
            try:
                response = stream(
                    self.get_core_v1_api().connect_get_namespaced_pod_exec,
                    pod_name,
                    self.get_namespace(),
                    command=command,
                    container=container,
                    stderr=True,
                    stdin=False,
                    stdout=True,
                    tty=False,
                    _request_timeout=timeout,
                )
            except ApiException as exc:
                self.logger.warning(
                    "Failed to execute command, retry (%s/%s) -> %s in pod -> '%s'; error -> %s",
                    retry_counter,
                    max_retry,
                    command,
                    pod_name,
                    str(exc),
                )
                retry_counter = retry_counter + 1
                exception = exc
                self.logger.debug(
                    "Wait %s seconds before next retry...",
                    str(time_retry),
                )
                time.sleep(time_retry)
                continue
            else:
                self.logger.debug("Command execution response -> %s", response if response else "<empty>")
                return response

        self.logger.error(
            "Failed to execute command -> %s in pod -> '%s' after %d retries; error -> %s",
            command,
            pod_name,
            max_retry,
            str(exception),
        )

        return None

    # end method definition

    # Some commands like the OTAC spawner need to run interactive - otherwise the command "hangs"
    def exec_pod_command_interactive(
        self,
        pod_name: str,
        commands: list,
        timeout: int = 30,
        write_stderr_to_error_log: bool = True,
    ) -> str | None:
        """Execute a command inside a Kubernetes pod (similar to kubectl exec on command line).

        Other than exec_pod_command() method above this is an interactive execution using
        stdin and reading the output from stdout and stderr. This is required for longer
        running commands. It is currently used for restarting the spawner of Archive Center.
        The output of the command is pushed into the logging.

        Args:
            pod_name (str):
                The name of the Kubernetes pod in the current namespace
            commands (list):
                A list of command and its parameters, e.g. ["/bin/bash", "/etc/init.d/spawner restart"]
                Here we should NOT have a "-c" parameter!
            timeout (int):
                Timeout duration that is waited for any response.
                Each time a resonse is found in stdout or stderr we wait another timeout duration
                to make sure we get the full output of the command.
            write_stderr_to_error_log (bool):
                Flag to control if output in stderr should be written to info or error log stream.
                Default is write to error log (True).

        Returns:
            str | None:
                Response of the command or None if the call fails.

        """

        if not commands:
            self.logger.error("No commands to execute on pod ->'%s'!", pod_name)
            return None

        pod = self.get_pod(pod_name=pod_name)
        if not pod:
            self.logger.error("Pod -> '%s' does not exist. Cannot execute commands -> %s", pod_name, commands)
            return None

        # Get first command - this should be the shell:
        command = commands.pop(0)

        try:
            response = stream(
                self.get_core_v1_api().connect_get_namespaced_pod_exec,
                pod_name,
                self.get_namespace(),
                command=command,
                stderr=True,
                stdin=True,  # This is important!
                stdout=True,
                tty=False,
                _preload_content=False,  # This is important!
                _request_timeout=timeout,
            )
        except ApiException:
            self.logger.error(
                "Failed to execute command -> %s in pod -> '%s'",
                command,
                pod_name,
            )
            return None

        while response.is_open():
            got_response = False
            response.update(timeout=timeout)
            if response.peek_stdout():
                self.logger.debug(response.read_stdout().replace("\n", " "))
                got_response = True
            if response.peek_stderr():
                if write_stderr_to_error_log:
                    self.logger.error(response.read_stderr().replace("\n", " "))
                else:
                    self.logger.debug(response.read_stderr().replace("\n", " "))
                got_response = True
            if commands:
                command = commands.pop(0)
                self.logger.debug(
                    "Execute command -> %s in pod -> '%s'",
                    command,
                    pod_name,
                )
                response.write_stdin(command + "\n")
            # We continue as long as we get some response during timeout period
            elif not got_response:
                break

        response.close()

        return response

    # end method definition

    def delete_pod(self, pod_name: str) -> V1Pod | None:
        """Delete a pod in the configured namespace (the namespace is defined in the class constructor).

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#delete_namespaced_pod

        Args:
            pod_name (str):
                The name of the Kubernetes pod in the current namespace.

        Returns:
            V1Pod (object) or None if the call fails.

        """

        pod = self.get_pod(pod_name=pod_name)
        if not pod:
            self.logger.error("Pod -> '%s' does not exist! Cannot delete it.", pod_name)
            return None

        try:
            response = self.get_core_v1_api().delete_namespaced_pod(
                pod_name,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to delete pod -> '%s'",
                pod_name,
            )
            return None

        return response

    # end method definition

    def get_config_map(self, config_map_name: str) -> V1ConfigMap:
        """Get a config map in the configured namespace (the namespace is defined in the class constructor).

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_config_map

        Args:
            config_map_name (str):
                The name of the Kubernetes config map in the current namespace.

        Returns:
            V1ConfigMap (object):
                Kubernetes Config Map object that includes these fields:
                - api_version:
                    The Kubernetes API version.
                - metadata:
                    A V1ObjectMeta object representing metadata about the V1ConfigMap object,
                    such as its name, labels, and annotations.
                - data:
                    A dictionary containing the non-binary data stored in the ConfigMap,
                    where the keys represent the keys of the data items and the values represent
                    the values of the data items.
                - binary_data:
                    A dictionary containing the binary data stored in the ConfigMap,
                    where the keys represent the keys of the binary data items and the values
                    represent the values of the binary data items. Binary data is encoded as base64
                    strings in the dictionary values.

        """

        try:
            response = self.get_core_v1_api().read_namespaced_config_map(
                name=config_map_name,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to get config map -> '%s'",
                config_map_name,
            )
            return None

        return response

    # end method definition

    def list_config_maps(
        self,
        field_selector: str | None = None,
        label_selector: str | None = None,
    ) -> V1ConfigMapList:
        """List all Kubernetes Config Maps in the current namespace.

        The list can be filtered by providing field selectors and label selectors.
        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_config_map

        Args:
            field_selector (str):
                To filter the result based on fields.
            label_selector (str):
                To filter result based on labels.

        Returns:
            V1ConfigMapList (object) or None if the call fails
            Properties can be accessed with the "." notation (this is an object not a dict!):
            - api_version: The Kubernetes API version.
            - items: A list of V1ConfigMap objects, each representing a config map. You can access the fields of a
                     V1Pod object using dot notation, for example, cm.metadata.name to access the name of the config map
            - kind: The Kubernetes object kind, which is always "ConfigMapList".
            - metadata: Additional metadata about the config map list, such as the resource version.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ConfigMapList.md

        """

        try:
            response = self.get_core_v1_api().list_namespaced_config_map(
                field_selector=field_selector,
                label_selector=label_selector,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to list config maps with field selector -> '%s' and label selector -> '%s'",
                field_selector,
                label_selector,
            )
            return None

        return response

    # end method definition

    def find_config_map(self, config_map_name: str) -> V1ConfigMapList:
        """Find a Kubernetes Config Map based on its name.

        This is just a wrapper method for list_config_maps()
        that uses the name as a field selector.

        Args:
            config_map_name (str):
                The name of the Kubernetes Config Map to search for.

        Returns:
            object:
                V1ConfigMapList (object) or None if the call fails.

        """

        try:
            response = self.list_config_maps(
                field_selector="metadata.name={}".format(config_map_name),
            )
        except ApiException:
            self.logger.error(
                "Failed to find config map -> '%s'",
                config_map_name,
            )
            return None

        return response

    # end method definition

    def replace_config_map(
        self,
        config_map_name: str,
        config_map_data: dict,
    ) -> V1ConfigMap:
        """Replace a Config Map with a new specification.

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#replace_namespaced_config_map

        Args:
            config_map_name (str):
                The name of the Kubernetes Config Map to replace.
            config_map_data (dict):
                The updated specification of the Config Map.

        Returns:
            V1ConfigMap (object):
                Updated Kubernetes Config Map object or None if the call fails.
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ConfigMap.md

        """

        try:
            response = self.get_core_v1_api().replace_namespaced_config_map(
                name=config_map_name,
                namespace=self.get_namespace(),
                body=client.V1ConfigMap(
                    metadata=client.V1ObjectMeta(
                        name=config_map_name,
                    ),
                    data=config_map_data,
                ),
            )
        except ApiException:
            self.logger.error(
                "Failed to replace config map -> '%s'",
                config_map_name,
            )
            return None

        return response

    # end method definition

    def get_stateful_set(self, sts_name: str) -> V1StatefulSet:
        """Get a Kubernetes Stateful Set based on its name.

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#read_namespaced_stateful_set

        Args:
            sts_name (str):
                The name of the Kubernetes stateful set

        Returns:
            V1StatefulSet (object):
                Kubernetes Stateful Set object or None if the call fails.
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

        """

        try:
            response = self.get_apps_v1_api().read_namespaced_stateful_set(
                name=sts_name,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to get stateful set -> '%s'",
                sts_name,
            )
            return None

        return response

    # end method definition

    def get_stateful_set_scale(self, sts_name: str) -> V1Scale:
        """Get the number of replicas for a Kubernetes Stateful Set.

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#read_namespaced_stateful_set_scale

        Args:
            sts_name (str):
                The name of the Kubernetes Stateful Set.

        Returns:
            V1Scale (object):
                Kubernetes Scale object or None if the call fails.
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Scale.md

        """

        try:
            response = self.get_apps_v1_api().read_namespaced_stateful_set_scale(
                name=sts_name,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to get scaling (replicas) of stateful set -> '%s'",
                sts_name,
            )
            return None

        return response

    # end method definition

    def patch_stateful_set(self, sts_name: str, sts_body: dict) -> V1StatefulSet:
        """Patch a Stateful set with new values.

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#patch_namespaced_stateful_set

        Args:
            sts_name (str):
                The name of the Kubernetes stateful set in the current namespace.
            sts_body (str):
                The patch string.

        Returns:
            V1StatefulSet (object):
                The patched Kubernetes Stateful Set object or None if the call fails.
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

        """

        try:
            response = self.get_apps_v1_api().patch_namespaced_stateful_set(
                name=sts_name,
                namespace=self.get_namespace(),
                body=sts_body,
            )
        except ApiException:
            self.logger.error(
                "Failed to patch stateful set -> '%s' with -> %s",
                sts_name,
                str(sts_body),
            )
            return None

        return response

    # end method definition

    def scale_stateful_set(self, sts_name: str, scale: int) -> V1StatefulSet:
        """Scale a stateful set to a specific number of replicas.

        It uses the class method patch_stateful_set() above.

        Args:
            sts_name (str):
                The name of the Kubernetes stateful set in the current namespace.
            scale (int):
                The number of replicas (pods) the stateful set shall be scaled to.

        Returns:
            V1StatefulSet (object):
                Kubernetes Stateful Set object or None if the call fails.
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

        """

        try:
            response = self.patch_stateful_set(
                sts_name,
                sts_body={"spec": {"replicas": scale}},
            )
        except ApiException:
            self.logger.error(
                "Failed to scale stateful set -> '%s' to -> %s replicas",
                sts_name,
                scale,
            )
            return None

        return response

    # end method definition

    def get_service(self, service_name: str) -> V1Service:
        """Get a Kubernetes Service with a defined name in the current namespace.

        Args:
            service_name (str):
                The name of the Kubernetes Service in the current namespace.

        Returns:
            V1Service (object):
                Kubernetes Service object or None if the call fails
                This is NOT a dict but an object - the you have to use the "." syntax to access to returned elements.
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Service.md

        """

        try:
            response = self.get_core_v1_api().read_namespaced_service(
                name=service_name,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to get service -> '%s'",
                service_name,
            )
            return None

        return response

    # end method definition

    def list_services(self, field_selector: str = "", label_selector: str = "") -> None:
        """List all Kubernetes Service in the current namespace.

        The list can be filtered by providing field selectors and label selectors.
        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_service

        Args:
            field_selector (str):
                To filter result based on fields.
            label_selector (str):
                To filter result based on labels.

        Returns:
            V1ServiceList (object):
                A list of Kubernetes Services or None if the call fails.
                Properties can be accessed with the "." notation (this is an object not a dict!):
                - api_version: The Kubernetes API version.
                - items: A list of V1Service objects, each representing a service.
                        You can access the fields of a V1Service object using dot notation,
                        for example, service.metadata.name to access the name of the service
                - kind: The Kubernetes object kind, which is always "ServiceList".
                - metadata: Additional metadata about the pod list, such as the resource version.
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ServiceList.md

        """

        try:
            response = self.get_core_v1_api().list_namespaced_service(
                field_selector=field_selector,
                label_selector=label_selector,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to list services with field selector -> '%s' and label selector -> '%s'",
                field_selector,
                label_selector,
            )
            return None

        return response

    # end method definition

    def patch_service(self, service_name: str, service_body: dict) -> V1Service:
        """Patch a Kubernetes Service with an updated spec.

        Args:
            service_name (str):
                The name of the Kubernetes Ingress in the current namespace.
            service_body (dict):
                The new / updated Service body spec.
                (will be merged with existing values)

        Returns:
            V1Service (object):
                The patched Kubernetes Service or None if the call fails.
                This is NOT a dict but an object - you have to use the "." syntax
                to access to returned elements
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Service.md

        """

        try:
            response = self.get_core_v1_api().patch_namespaced_service(
                name=service_name,
                namespace=self.get_namespace(),
                body=service_body,
            )
        except ApiException:
            self.logger.error(
                "Failed to patch service -> '%s' with -> %s",
                service_name,
                service_body,
            )
            return None

        return response

    # end method definition

    def get_ingress(self, ingress_name: str) -> V1Ingress:
        """Get a Kubernetes Ingress with a defined name in the current namespace.

        Args:
            ingress_name (str):
                The name of the Kubernetes Ingress in the current namespace.

        Returns:
            V1Ingress (object):
                Kubernetes Ingress or None if the call fails
                This is NOT a dict but an object - the you have to use the "." syntax to access to returned elements.
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

        """

        try:
            response = self.get_networking_v1_api().read_namespaced_ingress(
                name=ingress_name,
                namespace=self.get_namespace(),
            )
        except ApiException:
            self.logger.error(
                "Failed to get ingress -> '%s'!",
                ingress_name,
            )
            return None

        return response

    # end method definition

    def patch_ingress(self, ingress_name: str, ingress_body: dict) -> V1Ingress:
        """Patch a Kubernetes Ingress with a updated spec.

        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/NetworkingV1Api.md#patch_namespaced_ingress

        Args:
            ingress_name (str):
                The name of the Kubernetes Ingress in the current namespace.
            ingress_body (dict):
                The new / updated ingress body spec.
                (will be merged with existing values)

        Returns:
            V1Ingress (object):
                The patched Kubernetes Ingress object or None if the call fails
                This is NOT a dict but an object - you have to use the
                "." syntax to access to returned elements
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

        """

        try:
            response = self.get_networking_v1_api().patch_namespaced_ingress(
                name=ingress_name,
                namespace=self.get_namespace(),
                body=ingress_body,
            )
        except ApiException:
            self.logger.error(
                "Failed to patch ingress -> '%s' with -> %s",
                ingress_name,
                ingress_body,
            )
            return None

        return response

    # end method definition

    def update_ingress_backend_services(
        self,
        ingress_name: str,
        hostname: str,
        service_name: str,
        service_port: int,
    ) -> V1Ingress:
        """Update a backend service and port of an Kubernetes Ingress.

        "spec": {
            "rules": [
                {
                    "host": host,
                    "http": {
                        "paths": [
                            {
                                "path": "/",
                                "pathType": "Prefix",
                                "backend": {
                                    "service": {
                                        "name": <service_name>,
                                        "port": {
                                            "name": None,
                                            "number": <service_port>,
                                        },
                                    },
                                },
                            }
                        ]
                    },
                }
            ]
        }

        Args:
            ingress_name (str):
                The name of the Kubernetes Ingress in the current namespace.
            hostname (str):
                The hostname that should get an updated backend service / port.
            service_name (str):
                The new backend service name.
            service_port (int):
                The new backend service port.

        Returns:
            V1Ingress (object):
                The updated Kubernetes Ingress object or None if the call fails.
                This is NOT a dict but an object - you have to use the "." syntax
                to access to returned elements
                See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

        """

        ingress = self.get_ingress(ingress_name=ingress_name)
        if not ingress:
            return None

        host = ""
        rules = ingress.spec.rules
        rule_index = 0
        for rule in rules:
            if hostname in rule.host:
                host = rule.host
                path = rule.http.paths[0]
                backend = path.backend
                service = backend.service

                self.logger.debug(
                    "Replace backend service -> '%s' (%s) with new backend service -> '%s' (%s)",
                    service.name,
                    service.port.number,
                    service_name,
                    service_port,
                )

                service.name = service_name
                service.port.number = service_port
                break
            rule_index += 1

        if not host:
            self.logger.error("Cannot find host to upgrade the Kubernetes Ingress -> '%s'", ingress_name)
            return None

        body = [
            {
                "op": "replace",
                "path": "/spec/rules/{}/http/paths/0/backend/service/name".format(
                    rule_index,
                ),
                "value": service_name,
            },
            {
                "op": "replace",
                "path": "/spec/rules/{}/http/paths/0/backend/service/port/number".format(
                    rule_index,
                ),
                "value": service_port,
            },
        ]

        return self.patch_ingress(ingress_name, body)

    # end method definition

    def verify_pod_status(
        self,
        pod_name: str,
        timeout: int = 1800,
        total_containers: int = 1,
        ready_containers: int = 1,
        retry_interval: int = 30,
    ) -> bool:
        """Verify if a pod is in a 'Ready' state by checking the status of its containers.

        This function waits for a Kubernetes pod to reach the 'Ready' state, where a specified number
        of containers are ready. It checks the pod status at regular intervals and reports the status
        using logs. If the pod does not reach the 'Ready' state within the specified timeout,
        it returns `False`.

        Args:
            pod_name (str):
                The name of the pod to check the status for.
            timeout (int, optional):
                The maximum time (in seconds) to wait for the pod to become ready. Defaults to 1800.
            total_containers (int, optional):
                The total number of containers expected to be running in the pod. Defaults to 1.
            ready_containers (int, optional):
                The minimum number of containers that need to be in a ready state. Defaults to 1.
            retry_interval (int, optional):
                Time interval (in seconds) between each retry to check pod readiness. Defaults to 30.

        Returns:
            bool:
                Returns `True` if the pod reaches the 'Ready' state with the specified number of containers ready
                within the timeout. Otherwise, returns `False`.

        """

        def wait_for_pod_ready(pod_name: str, timeout: int) -> bool:
            """Wait until the pod is in the 'Ready' state with the specified number of containers ready.

            This sub method repeatedly checks the readiness of the pod, logging the
            status of the containers. If the pod does not exist, it retries after waiting
            and logs detailed information at each step.

            Args:
                pod_name (str):
                    The name of the pod to check the status for.
                timeout (int):
                    The maximum time (in seconds) to wait for the pod to become ready.

            Returns:
                bool:
                    Returns `True` if the pod is ready with the specified number of containers in a 'Ready' state.
                    Otherwise, returns `False`.

            """

            elapsed_time = 0  # Initialize elapsed time

            while elapsed_time < timeout:
                pod = self.get_pod(pod_name=pod_name)

                if not pod:
                    self.logger.warning(
                        "Pod -> '%s' does not exist, waiting 300 seconds to retry.",
                        pod_name,
                    )
                    time.sleep(300)
                    pod = self.get_pod(pod_name=pod_name)

                if not pod:
                    self.logger.error(
                        "Pod -> '%s' still does not exist after retry!",
                        pod_name,
                    )
                    return False

                # Get the ready status of containers
                container_statuses = pod.status.container_statuses
                if container_statuses and all(container.ready for container in container_statuses):
                    current_ready_containers = sum(1 for c in container_statuses if c.ready)
                    total_containers_in_pod = len(container_statuses)

                    if current_ready_containers >= ready_containers and total_containers_in_pod == total_containers:
                        self.logger.info(
                            "Pod -> '%s' is ready with %d/%d containers.",
                            pod_name,
                            current_ready_containers,
                            total_containers_in_pod,
                        )
                        return True
                    else:
                        self.logger.debug(
                            "Pod -> '%s' is not yet ready (%d/%d).",
                            pod_name,
                            current_ready_containers,
                            total_containers_in_pod,
                        )
                else:
                    self.logger.debug("Pod -> '%s' is not yet ready.", pod_name)

                self.logger.info(
                    "Waiting %s seconds before next pod status check.",
                    retry_interval,
                )
                time.sleep(
                    retry_interval,
                )  # Sleep for the retry interval before checking again
                elapsed_time += retry_interval

            self.logger.error(
                "Pod -> '%s' is not ready after %d seconds.",
                pod_name,
                timeout,
            )
            return False

        # end method definition

        # Wait until the pod is ready
        return wait_for_pod_ready(pod_name=pod_name, timeout=timeout)

    # end method definition

    def verify_pod_deleted(
        self,
        pod_name: str,
        timeout: int = 300,
        retry_interval: int = 30,
    ) -> bool:
        """Verify if a pod is deleted within the specified timeout.

        Args:
            pod_name (str):
                The name of the pod to check.
            timeout (int):
                Maximum time to wait for the pod to be deleted (in seconds).
            retry_interval:
                Time interval between retries (in seconds).

        Returns:
            bool:
                True if the pod is deleted, False otherwise.

        """

        elapsed_time = 0  # Initialize elapsed time

        while elapsed_time < timeout:
            pod = self.get_pod(pod_name=pod_name)

            if not pod:
                self.logger.info("Pod -> '%s' has been deleted successfully.", pod_name)
                return True

            pod_status = self.get_core_v1_api().read_namespaced_pod_status(
                pod_name,
                self.get_namespace(),
            )

            self.logger.info(
                "Pod -> '%s' still exists with conditions -> %s. Waiting %s seconds before next check.",
                pod_name,
                str(
                    [
                        pod_condition.type
                        for pod_condition in pod_status.status.conditions
                        if pod_condition.status == "True"
                    ]
                ),
                retry_interval,
            )
            time.sleep(retry_interval)
            elapsed_time += retry_interval

        self.logger.error("Pod -> '%s' was not deleted within %d seconds.", pod_name, timeout)

        return False

    # end method definition

    def restart_deployment(
        self, deployment_name: str, force: bool = False, wait: bool = False, wait_timeout: int = 1800
    ) -> bool:
        """Restart a Kubernetes deployment using rolling restart.

        Args:
            deployment_name (str):
                Name of the Kubernetes deployment.
            force (bool):
                If True, all pod instances will be forcefully deleted. [False]
            wait (bool):
                If True, wait for the stateful set to be ready again. [False]
            wait_timeout (int):
                Maximum time to wait for the stateful set to be ready again (in seconds). [1800]

        Returns:
            bool:
                True if successful, False otherwise.

        """

        success = True

        if not force:
            now = datetime.now(UTC).isoformat(timespec="seconds") + "Z"

            body = {
                "spec": {
                    "template": {
                        "metadata": {
                            "annotations": {
                                "kubectl.kubernetes.io/restartedAt": now,
                            },
                        },
                    },
                },
            }
            try:
                self.get_apps_v1_api().patch_namespaced_deployment(
                    deployment_name,
                    self.get_namespace(),
                    body,
                    pretty="true",
                )
                self.logger.debug("Triggered restart of deployment -> '%s'.", deployment_name)

            except ApiException as api_exception:
                self.logger.error(
                    "Failed to restart deployment -> '%s'; error -> %s!", deployment_name, str(api_exception)
                )
                return False

        # If force is set, all pod instances will be forcefully deleted.
        elif force:
            self.logger.info("Force deleting all pods of deployment -> '%s'.", deployment_name)

            try:
                # Get the Deployment to retrieve its pod labels
                deployment = self.get_apps_v1_api().read_namespaced_deployment(
                    name=deployment_name, namespace=self.get_namespace()
                )

                # Get the label selector for the Deployment
                label_selector = deployment.spec.selector.match_labels

                # List pods matching the label selector
                pods = (
                    self.get_core_v1_api()
                    .list_namespaced_pod(
                        namespace=self.get_namespace(),
                        label_selector=",".join([f"{k}={v}" for k, v in label_selector.items()]),
                    )
                    .items
                )

                # Loop through the pods and delete each one
                for pod in pods:
                    pod_name = pod.metadata.name
                    try:
                        # Define the delete options with force and grace period set to 0
                        body = client.V1DeleteOptions(grace_period_seconds=0, propagation_policy="Foreground")

                        # Call the delete_namespaced_pod method
                        self.get_core_v1_api().delete_namespaced_pod(
                            name=pod_name, namespace=self.get_namespace(), body=body
                        )
                        self.logger.info(
                            "Pod '%s' in namespace '%s' has been deleted forcefully.", pod_name, self.get_namespace()
                        )
                    except Exception as e:
                        self.logger.error("Error occurred while deleting pod '%s': %s", pod_name, e)
                        success = False

            except Exception as e:
                self.logger.error("Error occurred while getting Deployment '%s': %s", deployment_name, e)
                success = False

        start_time = time.time()
        while wait:
            self.logger.info("Waiting for restart of deployment -> '%s' to complete.", deployment_name)
            # Get the deployment
            deployment = self.get_apps_v1_api().read_namespaced_deployment_status(deployment_name, self.get_namespace())

            # Check the availability status
            ready_replicas = deployment.status.ready_replicas or 0
            updated_replicas = deployment.status.updated_replicas or 0
            unavailable_replicas = deployment.status.unavailable_replicas or 0
            total_replicas = deployment.status.replicas or 0
            desired_replicas = deployment.spec.replicas or 0

            self.logger.debug(
                "Deployment status -> updated pods: %s/%s -> ready replicas: %s/%s",
                updated_replicas,
                desired_replicas,
                ready_replicas,
                total_replicas,
            )

            if (
                updated_replicas == desired_replicas
                and unavailable_replicas == 0
                and total_replicas == desired_replicas
            ):
                self.logger.info("Restart of deployment -> '%s' completed successfully", deployment_name)
                break

            if (time.time() - start_time) > wait_timeout:
                self.logger.error("Timed out waiting for restart of deployment -> '%s' to complete.", deployment_name)
                success = False
                break

            # Sleep for a while before checking again
            time.sleep(20)

        return success

    # end method definition

    def restart_stateful_set(
        self, sts_name: str, force: bool = False, wait: bool = False, wait_timeout: int = 1800
    ) -> bool:
        """Restart a Kubernetes stateful set using rolling restart.

        Args:
            sts_name (str):
                Name of the Kubernetes statefulset.
            force (bool, optional):
                If True, all pod instances will be forcefully deleted. [False]
            wait (bool, optional):
                If True, wait for the stateful set to be ready again. [False]
            wait_timeout (int, optional):
                Maximum time to wait for the stateful set to be ready again (in seconds). [1800]

        Returns:
            bool:
                True if successful, False otherwise.

        """

        success = True

        now = datetime.now(UTC).isoformat(timespec="seconds") + "Z"

        body = {
            "spec": {
                "template": {
                    "metadata": {
                        "annotations": {
                            "kubectl.kubernetes.io/restartedAt": now,
                        },
                    },
                },
            },
        }

        try:
            self.get_apps_v1_api().patch_namespaced_stateful_set(sts_name, self.get_namespace(), body, pretty="true")
            self.logger.debug("Triggered restart of stateful set -> '%s'.", sts_name)

        except ApiException as api_exception:
            self.logger.error("Failed to restart stateful set -> '%s'; error -> %s!", sts_name, str(api_exception))
            return False

        # If force is set, all pod instances will be forcefully deleted.
        if force:
            self.logger.info("Force deleting all pods of stateful set -> '%s'.", sts_name)

            try:
                # Get the StatefulSet
                statefulset = self.get_apps_v1_api().read_namespaced_stateful_set(
                    name=sts_name, namespace=self.get_namespace()
                )

                # Loop through the replicas of the StatefulSet
                for i in range(statefulset.spec.replicas):
                    pod_name = f"{statefulset.metadata.name}-{i}"
                    try:
                        # Define the delete options with force and grace period set to 0
                        body = client.V1DeleteOptions(grace_period_seconds=0, propagation_policy="Foreground")

                        # Call the delete_namespaced_pod method
                        self.get_core_v1_api().delete_namespaced_pod(
                            name=pod_name, namespace=self.get_namespace(), body=body
                        )
                        self.logger.info(
                            "Pod -> '%s' in namespace -> '%s' has been deleted forcefully.",
                            pod_name,
                            self.get_namespace(),
                        )

                    except Exception as e:
                        self.logger.error("Error occurred while deleting pod -> '%s': %s", pod_name, str(e))
                        success = False

            except Exception as e:
                self.logger.error("Error occurred while getting stateful set -> '%s': %s", sts_name, str(e))
                success = False

        start_time = time.time()

        while wait:
            time.sleep(10)  # Add delay before checking that the stateful set is ready again.
            self.logger.info("Waiting for restart of stateful set -> '%s' to complete...", sts_name)
            # Get the deployment
            statefulset = self.get_apps_v1_api().read_namespaced_stateful_set_status(sts_name, self.get_namespace())

            # Check the availability status
            available_replicas = statefulset.status.available_replicas or 0
            desired_replicas = statefulset.spec.replicas or 0

            current_revision = statefulset.status.current_revision or ""
            update_revision = statefulset.status.update_revision or ""

            self.logger.debug(
                "Stateful set status -> available pods: %s/%s, revision updated: %s",
                available_replicas,
                desired_replicas,
                current_revision == update_revision,
            )

            if available_replicas == desired_replicas and update_revision == current_revision:
                self.logger.info("Stateful set -> '%s' completed restart successfully", sts_name)
                break

            if (time.time() - start_time) > wait_timeout:
                self.logger.error("Timed out waiting for restart of stateful set -> '%s' to complete.", sts_name)
                success = False
                break

            # Sleep for a while before checking again
            time.sleep(10)

        return success

__init__(kubeconfig_file=None, namespace='default', logger=default_logger)

Initialize the Kubernetes object.

Parameters:

Name Type Description Default
kubeconfig_file str | None

Path to a kubeconfig file. Defaults to None.

None
namespace str

The Kubernetes name space. Defaults to "default".

'default'
logger Logger

The logger object. Defaults to default_logger.

default_logger
Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def __init__(
    self,
    kubeconfig_file: str | None = None,
    namespace: str = "default",
    logger: logging.Logger = default_logger,
) -> None:
    """Initialize the Kubernetes object.

    Args:
        kubeconfig_file (str | None, optional):
            Path to a kubeconfig file. Defaults to None.
        namespace (str, optional):
            The Kubernetes name space. Defaults to "default".
        logger (logging.Logger, optional):
            The logger object. Defaults to default_logger.

    """

    if logger != default_logger:
        self.logger = logger.getChild("k8s")
        for logfilter in logger.filters:
            self.logger.addFilter(logfilter)

    # Configure Kubernetes API authentication to use pod serviceAccount

    try:
        config.load_incluster_config()
        configured = True
    except ConfigException:
        configured = False
        self.logger.info("Failed to load in-cluster config")

    if kubeconfig_file is None:
        kubeconfig_file = os.getenv(
            "KUBECONFIG",
            os.path.expanduser("~/.kube/config"),
        )

    if not configured:
        try:
            config.load_kube_config(config_file=kubeconfig_file)
        except ConfigException:
            self.logger.info(
                "Failed to load kubernetes config with file -> '%s'",
                kubeconfig_file,
            )

    self._core_v1_api = CoreV1Api()
    self._apps_v1_api = AppsV1Api()
    self._networking_v1_api = NetworkingV1Api()

    if namespace == "default":
        # Read current namespace
        try:
            with open(
                "/var/run/secrets/kubernetes.io/serviceaccount/namespace",
                encoding="utf-8",
            ) as namespace_file:
                self._namespace = namespace_file.read()
        except FileNotFoundError:
            self._namespace = namespace
    else:
        self._namespace = namespace

delete_pod(pod_name)

Delete a pod in the configured namespace (the namespace is defined in the class constructor).

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#delete_namespaced_pod

Parameters:

Name Type Description Default
pod_name str

The name of the Kubernetes pod in the current namespace.

required

Returns:

Type Description
V1Pod | None

V1Pod (object) or None if the call fails.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def delete_pod(self, pod_name: str) -> V1Pod | None:
    """Delete a pod in the configured namespace (the namespace is defined in the class constructor).

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#delete_namespaced_pod

    Args:
        pod_name (str):
            The name of the Kubernetes pod in the current namespace.

    Returns:
        V1Pod (object) or None if the call fails.

    """

    pod = self.get_pod(pod_name=pod_name)
    if not pod:
        self.logger.error("Pod -> '%s' does not exist! Cannot delete it.", pod_name)
        return None

    try:
        response = self.get_core_v1_api().delete_namespaced_pod(
            pod_name,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to delete pod -> '%s'",
            pod_name,
        )
        return None

    return response

exec_pod_command(pod_name, command, max_retry=3, time_retry=10, container=None, timeout=60)

Execute a command inside a Kubernetes Pod (similar to kubectl exec on command line).

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#connect_get_namespaced_pod_exec

Parameters:

Name Type Description Default
pod_name str

The name of the Kubernetes pod in the current namespace.

required
command list

A list of command and its parameters, e.g. ["/bin/bash", "-c", "pwd"] The "-c" is required to make the shell executing the command.

required
max_retry int

The maximum number of attempts to execute the command.

3
time_retry int

Wait time in seconds between retries.

10
container str

The container name if the pod runs multiple containers inside.

None
timeout int

Timeout duration that is waited for any response in seconds. Each time a response is found in stdout or stderr we wait another timeout duration [60]

60

Returns:

Type Description
str | None

str | None: Response of the command or None if the call fails.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def exec_pod_command(
    self,
    pod_name: str,
    command: list,
    max_retry: int = 3,
    time_retry: int = 10,
    container: str | None = None,
    timeout: int = 60,
) -> str | None:
    """Execute a command inside a Kubernetes Pod (similar to kubectl exec on command line).

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#connect_get_namespaced_pod_exec

    Args:
        pod_name (str):
            The name of the Kubernetes pod in the current namespace.
        command (list):
            A list of command and its parameters, e.g. ["/bin/bash", "-c", "pwd"]
            The "-c" is required to make the shell executing the command.
        max_retry (int):
            The maximum number of attempts to execute the command.
        time_retry (int):
            Wait time in seconds between retries.
        container (str):
            The container name if the pod runs multiple containers inside.
        timeout (int):
            Timeout duration that is waited for any response in seconds.
            Each time a response is found in stdout or stderr we wait another timeout duration [60]

    Returns:
        str | None:
            Response of the command or None if the call fails.

    """

    pod = self.get_pod(pod_name=pod_name)
    if not pod:
        self.logger.error("Pod -> '%s' does not exist. Cannot execute command -> %s", pod_name, command)
        return None

    self.logger.debug("Execute command -> %s in pod -> '%s'", command, pod_name)

    retry_counter = 1

    while retry_counter <= max_retry:
        try:
            response = stream(
                self.get_core_v1_api().connect_get_namespaced_pod_exec,
                pod_name,
                self.get_namespace(),
                command=command,
                container=container,
                stderr=True,
                stdin=False,
                stdout=True,
                tty=False,
                _request_timeout=timeout,
            )
        except ApiException as exc:
            self.logger.warning(
                "Failed to execute command, retry (%s/%s) -> %s in pod -> '%s'; error -> %s",
                retry_counter,
                max_retry,
                command,
                pod_name,
                str(exc),
            )
            retry_counter = retry_counter + 1
            exception = exc
            self.logger.debug(
                "Wait %s seconds before next retry...",
                str(time_retry),
            )
            time.sleep(time_retry)
            continue
        else:
            self.logger.debug("Command execution response -> %s", response if response else "<empty>")
            return response

    self.logger.error(
        "Failed to execute command -> %s in pod -> '%s' after %d retries; error -> %s",
        command,
        pod_name,
        max_retry,
        str(exception),
    )

    return None

exec_pod_command_interactive(pod_name, commands, timeout=30, write_stderr_to_error_log=True)

Execute a command inside a Kubernetes pod (similar to kubectl exec on command line).

Other than exec_pod_command() method above this is an interactive execution using stdin and reading the output from stdout and stderr. This is required for longer running commands. It is currently used for restarting the spawner of Archive Center. The output of the command is pushed into the logging.

Parameters:

Name Type Description Default
pod_name str

The name of the Kubernetes pod in the current namespace

required
commands list

A list of command and its parameters, e.g. ["/bin/bash", "/etc/init.d/spawner restart"] Here we should NOT have a "-c" parameter!

required
timeout int

Timeout duration that is waited for any response. Each time a resonse is found in stdout or stderr we wait another timeout duration to make sure we get the full output of the command.

30
write_stderr_to_error_log bool

Flag to control if output in stderr should be written to info or error log stream. Default is write to error log (True).

True

Returns:

Type Description
str | None

str | None: Response of the command or None if the call fails.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def exec_pod_command_interactive(
    self,
    pod_name: str,
    commands: list,
    timeout: int = 30,
    write_stderr_to_error_log: bool = True,
) -> str | None:
    """Execute a command inside a Kubernetes pod (similar to kubectl exec on command line).

    Other than exec_pod_command() method above this is an interactive execution using
    stdin and reading the output from stdout and stderr. This is required for longer
    running commands. It is currently used for restarting the spawner of Archive Center.
    The output of the command is pushed into the logging.

    Args:
        pod_name (str):
            The name of the Kubernetes pod in the current namespace
        commands (list):
            A list of command and its parameters, e.g. ["/bin/bash", "/etc/init.d/spawner restart"]
            Here we should NOT have a "-c" parameter!
        timeout (int):
            Timeout duration that is waited for any response.
            Each time a resonse is found in stdout or stderr we wait another timeout duration
            to make sure we get the full output of the command.
        write_stderr_to_error_log (bool):
            Flag to control if output in stderr should be written to info or error log stream.
            Default is write to error log (True).

    Returns:
        str | None:
            Response of the command or None if the call fails.

    """

    if not commands:
        self.logger.error("No commands to execute on pod ->'%s'!", pod_name)
        return None

    pod = self.get_pod(pod_name=pod_name)
    if not pod:
        self.logger.error("Pod -> '%s' does not exist. Cannot execute commands -> %s", pod_name, commands)
        return None

    # Get first command - this should be the shell:
    command = commands.pop(0)

    try:
        response = stream(
            self.get_core_v1_api().connect_get_namespaced_pod_exec,
            pod_name,
            self.get_namespace(),
            command=command,
            stderr=True,
            stdin=True,  # This is important!
            stdout=True,
            tty=False,
            _preload_content=False,  # This is important!
            _request_timeout=timeout,
        )
    except ApiException:
        self.logger.error(
            "Failed to execute command -> %s in pod -> '%s'",
            command,
            pod_name,
        )
        return None

    while response.is_open():
        got_response = False
        response.update(timeout=timeout)
        if response.peek_stdout():
            self.logger.debug(response.read_stdout().replace("\n", " "))
            got_response = True
        if response.peek_stderr():
            if write_stderr_to_error_log:
                self.logger.error(response.read_stderr().replace("\n", " "))
            else:
                self.logger.debug(response.read_stderr().replace("\n", " "))
            got_response = True
        if commands:
            command = commands.pop(0)
            self.logger.debug(
                "Execute command -> %s in pod -> '%s'",
                command,
                pod_name,
            )
            response.write_stdin(command + "\n")
        # We continue as long as we get some response during timeout period
        elif not got_response:
            break

    response.close()

    return response

find_config_map(config_map_name)

Find a Kubernetes Config Map based on its name.

This is just a wrapper method for list_config_maps() that uses the name as a field selector.

Parameters:

Name Type Description Default
config_map_name str

The name of the Kubernetes Config Map to search for.

required

Returns:

Name Type Description
object V1ConfigMapList

V1ConfigMapList (object) or None if the call fails.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def find_config_map(self, config_map_name: str) -> V1ConfigMapList:
    """Find a Kubernetes Config Map based on its name.

    This is just a wrapper method for list_config_maps()
    that uses the name as a field selector.

    Args:
        config_map_name (str):
            The name of the Kubernetes Config Map to search for.

    Returns:
        object:
            V1ConfigMapList (object) or None if the call fails.

    """

    try:
        response = self.list_config_maps(
            field_selector="metadata.name={}".format(config_map_name),
        )
    except ApiException:
        self.logger.error(
            "Failed to find config map -> '%s'",
            config_map_name,
        )
        return None

    return response

get_apps_v1_api()

Return Kubernetes Apps V1 API object.

Returns:

Name Type Description
object AppsV1Api

Kubernetes API object

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_apps_v1_api(self) -> AppsV1Api:
    """Return Kubernetes Apps V1 API object.

    Returns:
        object: Kubernetes API object

    """

    return self._apps_v1_api

get_config_map(config_map_name)

Get a config map in the configured namespace (the namespace is defined in the class constructor).

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_config_map

Parameters:

Name Type Description Default
config_map_name str

The name of the Kubernetes config map in the current namespace.

required

Returns:

Name Type Description
V1ConfigMap object

Kubernetes Config Map object that includes these fields: - api_version: The Kubernetes API version. - metadata: A V1ObjectMeta object representing metadata about the V1ConfigMap object, such as its name, labels, and annotations. - data: A dictionary containing the non-binary data stored in the ConfigMap, where the keys represent the keys of the data items and the values represent the values of the data items. - binary_data: A dictionary containing the binary data stored in the ConfigMap, where the keys represent the keys of the binary data items and the values represent the values of the binary data items. Binary data is encoded as base64 strings in the dictionary values.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_config_map(self, config_map_name: str) -> V1ConfigMap:
    """Get a config map in the configured namespace (the namespace is defined in the class constructor).

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_config_map

    Args:
        config_map_name (str):
            The name of the Kubernetes config map in the current namespace.

    Returns:
        V1ConfigMap (object):
            Kubernetes Config Map object that includes these fields:
            - api_version:
                The Kubernetes API version.
            - metadata:
                A V1ObjectMeta object representing metadata about the V1ConfigMap object,
                such as its name, labels, and annotations.
            - data:
                A dictionary containing the non-binary data stored in the ConfigMap,
                where the keys represent the keys of the data items and the values represent
                the values of the data items.
            - binary_data:
                A dictionary containing the binary data stored in the ConfigMap,
                where the keys represent the keys of the binary data items and the values
                represent the values of the binary data items. Binary data is encoded as base64
                strings in the dictionary values.

    """

    try:
        response = self.get_core_v1_api().read_namespaced_config_map(
            name=config_map_name,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to get config map -> '%s'",
            config_map_name,
        )
        return None

    return response

get_core_v1_api()

Return Kubernetes Core V1 API object.

Returns:

Name Type Description
object CoreV1Api

Kubernetes API object

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_core_v1_api(self) -> CoreV1Api:
    """Return Kubernetes Core V1 API object.

    Returns:
        object: Kubernetes API object

    """

    return self._core_v1_api

get_ingress(ingress_name)

Get a Kubernetes Ingress with a defined name in the current namespace.

Parameters:

Name Type Description Default
ingress_name str

The name of the Kubernetes Ingress in the current namespace.

required

Returns:

Name Type Description
V1Ingress object

Kubernetes Ingress or None if the call fails This is NOT a dict but an object - the you have to use the "." syntax to access to returned elements. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_ingress(self, ingress_name: str) -> V1Ingress:
    """Get a Kubernetes Ingress with a defined name in the current namespace.

    Args:
        ingress_name (str):
            The name of the Kubernetes Ingress in the current namespace.

    Returns:
        V1Ingress (object):
            Kubernetes Ingress or None if the call fails
            This is NOT a dict but an object - the you have to use the "." syntax to access to returned elements.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

    """

    try:
        response = self.get_networking_v1_api().read_namespaced_ingress(
            name=ingress_name,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to get ingress -> '%s'!",
            ingress_name,
        )
        return None

    return response

get_namespace()

Return Kubernetes Namespace.

Returns:

Name Type Description
str str

Kubernetes namespace

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_namespace(self) -> str:
    """Return Kubernetes Namespace.

    Returns:
        str: Kubernetes namespace

    """

    return self._namespace

get_networking_v1_api()

Return Kubernetes Networking V1 API object.

Returns:

Name Type Description
object NetworkingV1Api

Kubernetes API object

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_networking_v1_api(self) -> NetworkingV1Api:
    """Return Kubernetes Networking V1 API object.

    Returns:
        object: Kubernetes API object

    """

    return self._networking_v1_api

get_pod(pod_name)

Get a pod in the configured namespace (the namespace is defined in the class constructor).

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_pod

Parameters:

Name Type Description Default
pod_name str

The name of the Kubernetes pod in the current namespace.

required

Returns:

Type Description
V1Pod

V1Pod (object) or None if the call fails.

V1Pod
  • api_version='v1',
V1Pod
  • kind='Pod',
V1Pod
  • metadata=V1ObjectMeta(...),
V1Pod
  • spec=V1PodSpec(...),
V1Pod
  • status=V1PodStatus(...)
Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_pod(self, pod_name: str) -> V1Pod:
    """Get a pod in the configured namespace (the namespace is defined in the class constructor).

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_pod

    Args:
        pod_name (str):
            The name of the Kubernetes pod in the current namespace.

    Returns:
        V1Pod (object) or None if the call fails.
        - api_version='v1',
        - kind='Pod',
        - metadata=V1ObjectMeta(...),
        - spec=V1PodSpec(...),
        - status=V1PodStatus(...)

    """

    try:
        response = self.get_core_v1_api().read_namespaced_pod(
            name=pod_name,
            namespace=self.get_namespace(),
        )
    except ApiException as e:
        if e.status == 404:
            self.logger.debug("Pod -> '%s' not found (may be deleted).", pod_name)
            return None
        else:
            self.logger.error("Failed to get pod -> '%s'!", pod_name)
            return None  # Unexpected error, return None
    return response

get_service(service_name)

Get a Kubernetes Service with a defined name in the current namespace.

Parameters:

Name Type Description Default
service_name str

The name of the Kubernetes Service in the current namespace.

required

Returns:

Name Type Description
V1Service object

Kubernetes Service object or None if the call fails This is NOT a dict but an object - the you have to use the "." syntax to access to returned elements. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Service.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_service(self, service_name: str) -> V1Service:
    """Get a Kubernetes Service with a defined name in the current namespace.

    Args:
        service_name (str):
            The name of the Kubernetes Service in the current namespace.

    Returns:
        V1Service (object):
            Kubernetes Service object or None if the call fails
            This is NOT a dict but an object - the you have to use the "." syntax to access to returned elements.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Service.md

    """

    try:
        response = self.get_core_v1_api().read_namespaced_service(
            name=service_name,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to get service -> '%s'",
            service_name,
        )
        return None

    return response

get_stateful_set(sts_name)

Get a Kubernetes Stateful Set based on its name.

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#read_namespaced_stateful_set

Parameters:

Name Type Description Default
sts_name str

The name of the Kubernetes stateful set

required

Returns:

Name Type Description
V1StatefulSet object

Kubernetes Stateful Set object or None if the call fails. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_stateful_set(self, sts_name: str) -> V1StatefulSet:
    """Get a Kubernetes Stateful Set based on its name.

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#read_namespaced_stateful_set

    Args:
        sts_name (str):
            The name of the Kubernetes stateful set

    Returns:
        V1StatefulSet (object):
            Kubernetes Stateful Set object or None if the call fails.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

    """

    try:
        response = self.get_apps_v1_api().read_namespaced_stateful_set(
            name=sts_name,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to get stateful set -> '%s'",
            sts_name,
        )
        return None

    return response

get_stateful_set_scale(sts_name)

Get the number of replicas for a Kubernetes Stateful Set.

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#read_namespaced_stateful_set_scale

Parameters:

Name Type Description Default
sts_name str

The name of the Kubernetes Stateful Set.

required

Returns:

Name Type Description
V1Scale object

Kubernetes Scale object or None if the call fails. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Scale.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def get_stateful_set_scale(self, sts_name: str) -> V1Scale:
    """Get the number of replicas for a Kubernetes Stateful Set.

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#read_namespaced_stateful_set_scale

    Args:
        sts_name (str):
            The name of the Kubernetes Stateful Set.

    Returns:
        V1Scale (object):
            Kubernetes Scale object or None if the call fails.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Scale.md

    """

    try:
        response = self.get_apps_v1_api().read_namespaced_stateful_set_scale(
            name=sts_name,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to get scaling (replicas) of stateful set -> '%s'",
            sts_name,
        )
        return None

    return response

list_config_maps(field_selector=None, label_selector=None)

List all Kubernetes Config Maps in the current namespace.

The list can be filtered by providing field selectors and label selectors. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_config_map

Parameters:

Name Type Description Default
field_selector str

To filter the result based on fields.

None
label_selector str

To filter result based on labels.

None

Returns:

Name Type Description
V1ConfigMapList

V1ConfigMapList (object) or None if the call fails

V1ConfigMapList

Properties can be accessed with the "." notation (this is an object not a dict!):

V1ConfigMapList
  • api_version: The Kubernetes API version.
V1ConfigMapList
  • items: A list of V1ConfigMap objects, each representing a config map. You can access the fields of a V1Pod object using dot notation, for example, cm.metadata.name to access the name of the config map
V1ConfigMapList
  • kind: The Kubernetes object kind, which is always "ConfigMapList".
V1ConfigMapList
  • metadata: Additional metadata about the config map list, such as the resource version.
See V1ConfigMapList

https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ConfigMapList.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def list_config_maps(
    self,
    field_selector: str | None = None,
    label_selector: str | None = None,
) -> V1ConfigMapList:
    """List all Kubernetes Config Maps in the current namespace.

    The list can be filtered by providing field selectors and label selectors.
    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_config_map

    Args:
        field_selector (str):
            To filter the result based on fields.
        label_selector (str):
            To filter result based on labels.

    Returns:
        V1ConfigMapList (object) or None if the call fails
        Properties can be accessed with the "." notation (this is an object not a dict!):
        - api_version: The Kubernetes API version.
        - items: A list of V1ConfigMap objects, each representing a config map. You can access the fields of a
                 V1Pod object using dot notation, for example, cm.metadata.name to access the name of the config map
        - kind: The Kubernetes object kind, which is always "ConfigMapList".
        - metadata: Additional metadata about the config map list, such as the resource version.
        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ConfigMapList.md

    """

    try:
        response = self.get_core_v1_api().list_namespaced_config_map(
            field_selector=field_selector,
            label_selector=label_selector,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to list config maps with field selector -> '%s' and label selector -> '%s'",
            field_selector,
            label_selector,
        )
        return None

    return response

list_pods(field_selector='', label_selector='')

List all Kubernetes pods in a given namespace.

The list can be further restricted by specifying a field or label selector.

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_pod

Parameters:

Name Type Description Default
field_selector str

filter result based on fields

''
label_selector str

filter result based on labels

''

Returns:

Name Type Description
V1PodList

V1PodList (object) or None if the call fails

V1PodList

Properties can be accessed with the "." notation (this is an object not a dict!):

V1PodList
  • api_version: The Kubernetes API version.
V1PodList
  • items: A list of V1Pod objects, each representing a pod. You can access the fields of a V1Pod object using dot notation, for example, pod.metadata.name to access the name of the pod
V1PodList
  • kind: The Kubernetes object kind, which is always "PodList".
V1PodList
  • metadata: Additional metadata about the pod list, such as the resource version.
See V1PodList

https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1PodList.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def list_pods(
    self,
    field_selector: str = "",
    label_selector: str = "",
) -> V1PodList:
    """List all Kubernetes pods in a given namespace.

    The list can be further restricted by specifying a field or label selector.

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_pod

    Args:
        field_selector (str): filter result based on fields
        label_selector (str): filter result based on labels

    Returns:
        V1PodList (object) or None if the call fails
        Properties can be accessed with the "." notation (this is an object not a dict!):
        - api_version: The Kubernetes API version.
        - items: A list of V1Pod objects, each representing a pod. You can access the fields of a
                V1Pod object using dot notation, for example, pod.metadata.name to access the name of the pod
        - kind: The Kubernetes object kind, which is always "PodList".
        - metadata: Additional metadata about the pod list, such as the resource version.
        See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1PodList.md

    """

    try:
        response = self.get_core_v1_api().list_namespaced_pod(
            field_selector=field_selector,
            label_selector=label_selector,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to list pods with field selector -> '%s' and label selector -> '%s'",
            field_selector,
            label_selector,
        )
        return None

    return response

list_services(field_selector='', label_selector='')

List all Kubernetes Service in the current namespace.

The list can be filtered by providing field selectors and label selectors. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_service

Parameters:

Name Type Description Default
field_selector str

To filter result based on fields.

''
label_selector str

To filter result based on labels.

''

Returns:

Name Type Description
V1ServiceList object

A list of Kubernetes Services or None if the call fails. Properties can be accessed with the "." notation (this is an object not a dict!): - api_version: The Kubernetes API version. - items: A list of V1Service objects, each representing a service. You can access the fields of a V1Service object using dot notation, for example, service.metadata.name to access the name of the service - kind: The Kubernetes object kind, which is always "ServiceList". - metadata: Additional metadata about the pod list, such as the resource version. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ServiceList.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def list_services(self, field_selector: str = "", label_selector: str = "") -> None:
    """List all Kubernetes Service in the current namespace.

    The list can be filtered by providing field selectors and label selectors.
    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#list_namespaced_service

    Args:
        field_selector (str):
            To filter result based on fields.
        label_selector (str):
            To filter result based on labels.

    Returns:
        V1ServiceList (object):
            A list of Kubernetes Services or None if the call fails.
            Properties can be accessed with the "." notation (this is an object not a dict!):
            - api_version: The Kubernetes API version.
            - items: A list of V1Service objects, each representing a service.
                    You can access the fields of a V1Service object using dot notation,
                    for example, service.metadata.name to access the name of the service
            - kind: The Kubernetes object kind, which is always "ServiceList".
            - metadata: Additional metadata about the pod list, such as the resource version.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ServiceList.md

    """

    try:
        response = self.get_core_v1_api().list_namespaced_service(
            field_selector=field_selector,
            label_selector=label_selector,
            namespace=self.get_namespace(),
        )
    except ApiException:
        self.logger.error(
            "Failed to list services with field selector -> '%s' and label selector -> '%s'",
            field_selector,
            label_selector,
        )
        return None

    return response

patch_ingress(ingress_name, ingress_body)

Patch a Kubernetes Ingress with a updated spec.

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/NetworkingV1Api.md#patch_namespaced_ingress

Parameters:

Name Type Description Default
ingress_name str

The name of the Kubernetes Ingress in the current namespace.

required
ingress_body dict

The new / updated ingress body spec. (will be merged with existing values)

required

Returns:

Name Type Description
V1Ingress object

The patched Kubernetes Ingress object or None if the call fails This is NOT a dict but an object - you have to use the "." syntax to access to returned elements See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def patch_ingress(self, ingress_name: str, ingress_body: dict) -> V1Ingress:
    """Patch a Kubernetes Ingress with a updated spec.

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/NetworkingV1Api.md#patch_namespaced_ingress

    Args:
        ingress_name (str):
            The name of the Kubernetes Ingress in the current namespace.
        ingress_body (dict):
            The new / updated ingress body spec.
            (will be merged with existing values)

    Returns:
        V1Ingress (object):
            The patched Kubernetes Ingress object or None if the call fails
            This is NOT a dict but an object - you have to use the
            "." syntax to access to returned elements
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

    """

    try:
        response = self.get_networking_v1_api().patch_namespaced_ingress(
            name=ingress_name,
            namespace=self.get_namespace(),
            body=ingress_body,
        )
    except ApiException:
        self.logger.error(
            "Failed to patch ingress -> '%s' with -> %s",
            ingress_name,
            ingress_body,
        )
        return None

    return response

patch_service(service_name, service_body)

Patch a Kubernetes Service with an updated spec.

Parameters:

Name Type Description Default
service_name str

The name of the Kubernetes Ingress in the current namespace.

required
service_body dict

The new / updated Service body spec. (will be merged with existing values)

required

Returns:

Name Type Description
V1Service object

The patched Kubernetes Service or None if the call fails. This is NOT a dict but an object - you have to use the "." syntax to access to returned elements See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Service.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def patch_service(self, service_name: str, service_body: dict) -> V1Service:
    """Patch a Kubernetes Service with an updated spec.

    Args:
        service_name (str):
            The name of the Kubernetes Ingress in the current namespace.
        service_body (dict):
            The new / updated Service body spec.
            (will be merged with existing values)

    Returns:
        V1Service (object):
            The patched Kubernetes Service or None if the call fails.
            This is NOT a dict but an object - you have to use the "." syntax
            to access to returned elements
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Service.md

    """

    try:
        response = self.get_core_v1_api().patch_namespaced_service(
            name=service_name,
            namespace=self.get_namespace(),
            body=service_body,
        )
    except ApiException:
        self.logger.error(
            "Failed to patch service -> '%s' with -> %s",
            service_name,
            service_body,
        )
        return None

    return response

patch_stateful_set(sts_name, sts_body)

Patch a Stateful set with new values.

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#patch_namespaced_stateful_set

Parameters:

Name Type Description Default
sts_name str

The name of the Kubernetes stateful set in the current namespace.

required
sts_body str

The patch string.

required

Returns:

Name Type Description
V1StatefulSet object

The patched Kubernetes Stateful Set object or None if the call fails. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def patch_stateful_set(self, sts_name: str, sts_body: dict) -> V1StatefulSet:
    """Patch a Stateful set with new values.

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#patch_namespaced_stateful_set

    Args:
        sts_name (str):
            The name of the Kubernetes stateful set in the current namespace.
        sts_body (str):
            The patch string.

    Returns:
        V1StatefulSet (object):
            The patched Kubernetes Stateful Set object or None if the call fails.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

    """

    try:
        response = self.get_apps_v1_api().patch_namespaced_stateful_set(
            name=sts_name,
            namespace=self.get_namespace(),
            body=sts_body,
        )
    except ApiException:
        self.logger.error(
            "Failed to patch stateful set -> '%s' with -> %s",
            sts_name,
            str(sts_body),
        )
        return None

    return response

replace_config_map(config_map_name, config_map_data)

Replace a Config Map with a new specification.

See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#replace_namespaced_config_map

Parameters:

Name Type Description Default
config_map_name str

The name of the Kubernetes Config Map to replace.

required
config_map_data dict

The updated specification of the Config Map.

required

Returns:

Name Type Description
V1ConfigMap object

Updated Kubernetes Config Map object or None if the call fails. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ConfigMap.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def replace_config_map(
    self,
    config_map_name: str,
    config_map_data: dict,
) -> V1ConfigMap:
    """Replace a Config Map with a new specification.

    See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#replace_namespaced_config_map

    Args:
        config_map_name (str):
            The name of the Kubernetes Config Map to replace.
        config_map_data (dict):
            The updated specification of the Config Map.

    Returns:
        V1ConfigMap (object):
            Updated Kubernetes Config Map object or None if the call fails.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ConfigMap.md

    """

    try:
        response = self.get_core_v1_api().replace_namespaced_config_map(
            name=config_map_name,
            namespace=self.get_namespace(),
            body=client.V1ConfigMap(
                metadata=client.V1ObjectMeta(
                    name=config_map_name,
                ),
                data=config_map_data,
            ),
        )
    except ApiException:
        self.logger.error(
            "Failed to replace config map -> '%s'",
            config_map_name,
        )
        return None

    return response

restart_deployment(deployment_name, force=False, wait=False, wait_timeout=1800)

Restart a Kubernetes deployment using rolling restart.

Parameters:

Name Type Description Default
deployment_name str

Name of the Kubernetes deployment.

required
force bool

If True, all pod instances will be forcefully deleted. [False]

False
wait bool

If True, wait for the stateful set to be ready again. [False]

False
wait_timeout int

Maximum time to wait for the stateful set to be ready again (in seconds). [1800]

1800

Returns:

Name Type Description
bool bool

True if successful, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def restart_deployment(
    self, deployment_name: str, force: bool = False, wait: bool = False, wait_timeout: int = 1800
) -> bool:
    """Restart a Kubernetes deployment using rolling restart.

    Args:
        deployment_name (str):
            Name of the Kubernetes deployment.
        force (bool):
            If True, all pod instances will be forcefully deleted. [False]
        wait (bool):
            If True, wait for the stateful set to be ready again. [False]
        wait_timeout (int):
            Maximum time to wait for the stateful set to be ready again (in seconds). [1800]

    Returns:
        bool:
            True if successful, False otherwise.

    """

    success = True

    if not force:
        now = datetime.now(UTC).isoformat(timespec="seconds") + "Z"

        body = {
            "spec": {
                "template": {
                    "metadata": {
                        "annotations": {
                            "kubectl.kubernetes.io/restartedAt": now,
                        },
                    },
                },
            },
        }
        try:
            self.get_apps_v1_api().patch_namespaced_deployment(
                deployment_name,
                self.get_namespace(),
                body,
                pretty="true",
            )
            self.logger.debug("Triggered restart of deployment -> '%s'.", deployment_name)

        except ApiException as api_exception:
            self.logger.error(
                "Failed to restart deployment -> '%s'; error -> %s!", deployment_name, str(api_exception)
            )
            return False

    # If force is set, all pod instances will be forcefully deleted.
    elif force:
        self.logger.info("Force deleting all pods of deployment -> '%s'.", deployment_name)

        try:
            # Get the Deployment to retrieve its pod labels
            deployment = self.get_apps_v1_api().read_namespaced_deployment(
                name=deployment_name, namespace=self.get_namespace()
            )

            # Get the label selector for the Deployment
            label_selector = deployment.spec.selector.match_labels

            # List pods matching the label selector
            pods = (
                self.get_core_v1_api()
                .list_namespaced_pod(
                    namespace=self.get_namespace(),
                    label_selector=",".join([f"{k}={v}" for k, v in label_selector.items()]),
                )
                .items
            )

            # Loop through the pods and delete each one
            for pod in pods:
                pod_name = pod.metadata.name
                try:
                    # Define the delete options with force and grace period set to 0
                    body = client.V1DeleteOptions(grace_period_seconds=0, propagation_policy="Foreground")

                    # Call the delete_namespaced_pod method
                    self.get_core_v1_api().delete_namespaced_pod(
                        name=pod_name, namespace=self.get_namespace(), body=body
                    )
                    self.logger.info(
                        "Pod '%s' in namespace '%s' has been deleted forcefully.", pod_name, self.get_namespace()
                    )
                except Exception as e:
                    self.logger.error("Error occurred while deleting pod '%s': %s", pod_name, e)
                    success = False

        except Exception as e:
            self.logger.error("Error occurred while getting Deployment '%s': %s", deployment_name, e)
            success = False

    start_time = time.time()
    while wait:
        self.logger.info("Waiting for restart of deployment -> '%s' to complete.", deployment_name)
        # Get the deployment
        deployment = self.get_apps_v1_api().read_namespaced_deployment_status(deployment_name, self.get_namespace())

        # Check the availability status
        ready_replicas = deployment.status.ready_replicas or 0
        updated_replicas = deployment.status.updated_replicas or 0
        unavailable_replicas = deployment.status.unavailable_replicas or 0
        total_replicas = deployment.status.replicas or 0
        desired_replicas = deployment.spec.replicas or 0

        self.logger.debug(
            "Deployment status -> updated pods: %s/%s -> ready replicas: %s/%s",
            updated_replicas,
            desired_replicas,
            ready_replicas,
            total_replicas,
        )

        if (
            updated_replicas == desired_replicas
            and unavailable_replicas == 0
            and total_replicas == desired_replicas
        ):
            self.logger.info("Restart of deployment -> '%s' completed successfully", deployment_name)
            break

        if (time.time() - start_time) > wait_timeout:
            self.logger.error("Timed out waiting for restart of deployment -> '%s' to complete.", deployment_name)
            success = False
            break

        # Sleep for a while before checking again
        time.sleep(20)

    return success

restart_stateful_set(sts_name, force=False, wait=False, wait_timeout=1800)

Restart a Kubernetes stateful set using rolling restart.

Parameters:

Name Type Description Default
sts_name str

Name of the Kubernetes statefulset.

required
force bool

If True, all pod instances will be forcefully deleted. [False]

False
wait bool

If True, wait for the stateful set to be ready again. [False]

False
wait_timeout int

Maximum time to wait for the stateful set to be ready again (in seconds). [1800]

1800

Returns:

Name Type Description
bool bool

True if successful, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def restart_stateful_set(
    self, sts_name: str, force: bool = False, wait: bool = False, wait_timeout: int = 1800
) -> bool:
    """Restart a Kubernetes stateful set using rolling restart.

    Args:
        sts_name (str):
            Name of the Kubernetes statefulset.
        force (bool, optional):
            If True, all pod instances will be forcefully deleted. [False]
        wait (bool, optional):
            If True, wait for the stateful set to be ready again. [False]
        wait_timeout (int, optional):
            Maximum time to wait for the stateful set to be ready again (in seconds). [1800]

    Returns:
        bool:
            True if successful, False otherwise.

    """

    success = True

    now = datetime.now(UTC).isoformat(timespec="seconds") + "Z"

    body = {
        "spec": {
            "template": {
                "metadata": {
                    "annotations": {
                        "kubectl.kubernetes.io/restartedAt": now,
                    },
                },
            },
        },
    }

    try:
        self.get_apps_v1_api().patch_namespaced_stateful_set(sts_name, self.get_namespace(), body, pretty="true")
        self.logger.debug("Triggered restart of stateful set -> '%s'.", sts_name)

    except ApiException as api_exception:
        self.logger.error("Failed to restart stateful set -> '%s'; error -> %s!", sts_name, str(api_exception))
        return False

    # If force is set, all pod instances will be forcefully deleted.
    if force:
        self.logger.info("Force deleting all pods of stateful set -> '%s'.", sts_name)

        try:
            # Get the StatefulSet
            statefulset = self.get_apps_v1_api().read_namespaced_stateful_set(
                name=sts_name, namespace=self.get_namespace()
            )

            # Loop through the replicas of the StatefulSet
            for i in range(statefulset.spec.replicas):
                pod_name = f"{statefulset.metadata.name}-{i}"
                try:
                    # Define the delete options with force and grace period set to 0
                    body = client.V1DeleteOptions(grace_period_seconds=0, propagation_policy="Foreground")

                    # Call the delete_namespaced_pod method
                    self.get_core_v1_api().delete_namespaced_pod(
                        name=pod_name, namespace=self.get_namespace(), body=body
                    )
                    self.logger.info(
                        "Pod -> '%s' in namespace -> '%s' has been deleted forcefully.",
                        pod_name,
                        self.get_namespace(),
                    )

                except Exception as e:
                    self.logger.error("Error occurred while deleting pod -> '%s': %s", pod_name, str(e))
                    success = False

        except Exception as e:
            self.logger.error("Error occurred while getting stateful set -> '%s': %s", sts_name, str(e))
            success = False

    start_time = time.time()

    while wait:
        time.sleep(10)  # Add delay before checking that the stateful set is ready again.
        self.logger.info("Waiting for restart of stateful set -> '%s' to complete...", sts_name)
        # Get the deployment
        statefulset = self.get_apps_v1_api().read_namespaced_stateful_set_status(sts_name, self.get_namespace())

        # Check the availability status
        available_replicas = statefulset.status.available_replicas or 0
        desired_replicas = statefulset.spec.replicas or 0

        current_revision = statefulset.status.current_revision or ""
        update_revision = statefulset.status.update_revision or ""

        self.logger.debug(
            "Stateful set status -> available pods: %s/%s, revision updated: %s",
            available_replicas,
            desired_replicas,
            current_revision == update_revision,
        )

        if available_replicas == desired_replicas and update_revision == current_revision:
            self.logger.info("Stateful set -> '%s' completed restart successfully", sts_name)
            break

        if (time.time() - start_time) > wait_timeout:
            self.logger.error("Timed out waiting for restart of stateful set -> '%s' to complete.", sts_name)
            success = False
            break

        # Sleep for a while before checking again
        time.sleep(10)

    return success

scale_stateful_set(sts_name, scale)

Scale a stateful set to a specific number of replicas.

It uses the class method patch_stateful_set() above.

Parameters:

Name Type Description Default
sts_name str

The name of the Kubernetes stateful set in the current namespace.

required
scale int

The number of replicas (pods) the stateful set shall be scaled to.

required

Returns:

Name Type Description
V1StatefulSet object

Kubernetes Stateful Set object or None if the call fails. See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def scale_stateful_set(self, sts_name: str, scale: int) -> V1StatefulSet:
    """Scale a stateful set to a specific number of replicas.

    It uses the class method patch_stateful_set() above.

    Args:
        sts_name (str):
            The name of the Kubernetes stateful set in the current namespace.
        scale (int):
            The number of replicas (pods) the stateful set shall be scaled to.

    Returns:
        V1StatefulSet (object):
            Kubernetes Stateful Set object or None if the call fails.
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1StatefulSet.md

    """

    try:
        response = self.patch_stateful_set(
            sts_name,
            sts_body={"spec": {"replicas": scale}},
        )
    except ApiException:
        self.logger.error(
            "Failed to scale stateful set -> '%s' to -> %s replicas",
            sts_name,
            scale,
        )
        return None

    return response

update_ingress_backend_services(ingress_name, hostname, service_name, service_port)

Update a backend service and port of an Kubernetes Ingress.

"spec": { "rules": [ { "host": host, "http": { "paths": [ { "path": "/", "pathType": "Prefix", "backend": { "service": { "name": , "port": { "name": None, "number": , }, }, }, } ] }, } ] }

Parameters:

Name Type Description Default
ingress_name str

The name of the Kubernetes Ingress in the current namespace.

required
hostname str

The hostname that should get an updated backend service / port.

required
service_name str

The new backend service name.

required
service_port int

The new backend service port.

required

Returns:

Name Type Description
V1Ingress object

The updated Kubernetes Ingress object or None if the call fails. This is NOT a dict but an object - you have to use the "." syntax to access to returned elements See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def update_ingress_backend_services(
    self,
    ingress_name: str,
    hostname: str,
    service_name: str,
    service_port: int,
) -> V1Ingress:
    """Update a backend service and port of an Kubernetes Ingress.

    "spec": {
        "rules": [
            {
                "host": host,
                "http": {
                    "paths": [
                        {
                            "path": "/",
                            "pathType": "Prefix",
                            "backend": {
                                "service": {
                                    "name": <service_name>,
                                    "port": {
                                        "name": None,
                                        "number": <service_port>,
                                    },
                                },
                            },
                        }
                    ]
                },
            }
        ]
    }

    Args:
        ingress_name (str):
            The name of the Kubernetes Ingress in the current namespace.
        hostname (str):
            The hostname that should get an updated backend service / port.
        service_name (str):
            The new backend service name.
        service_port (int):
            The new backend service port.

    Returns:
        V1Ingress (object):
            The updated Kubernetes Ingress object or None if the call fails.
            This is NOT a dict but an object - you have to use the "." syntax
            to access to returned elements
            See: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Ingress.md

    """

    ingress = self.get_ingress(ingress_name=ingress_name)
    if not ingress:
        return None

    host = ""
    rules = ingress.spec.rules
    rule_index = 0
    for rule in rules:
        if hostname in rule.host:
            host = rule.host
            path = rule.http.paths[0]
            backend = path.backend
            service = backend.service

            self.logger.debug(
                "Replace backend service -> '%s' (%s) with new backend service -> '%s' (%s)",
                service.name,
                service.port.number,
                service_name,
                service_port,
            )

            service.name = service_name
            service.port.number = service_port
            break
        rule_index += 1

    if not host:
        self.logger.error("Cannot find host to upgrade the Kubernetes Ingress -> '%s'", ingress_name)
        return None

    body = [
        {
            "op": "replace",
            "path": "/spec/rules/{}/http/paths/0/backend/service/name".format(
                rule_index,
            ),
            "value": service_name,
        },
        {
            "op": "replace",
            "path": "/spec/rules/{}/http/paths/0/backend/service/port/number".format(
                rule_index,
            ),
            "value": service_port,
        },
    ]

    return self.patch_ingress(ingress_name, body)

verify_pod_deleted(pod_name, timeout=300, retry_interval=30)

Verify if a pod is deleted within the specified timeout.

Parameters:

Name Type Description Default
pod_name str

The name of the pod to check.

required
timeout int

Maximum time to wait for the pod to be deleted (in seconds).

300
retry_interval int

Time interval between retries (in seconds).

30

Returns:

Name Type Description
bool bool

True if the pod is deleted, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def verify_pod_deleted(
    self,
    pod_name: str,
    timeout: int = 300,
    retry_interval: int = 30,
) -> bool:
    """Verify if a pod is deleted within the specified timeout.

    Args:
        pod_name (str):
            The name of the pod to check.
        timeout (int):
            Maximum time to wait for the pod to be deleted (in seconds).
        retry_interval:
            Time interval between retries (in seconds).

    Returns:
        bool:
            True if the pod is deleted, False otherwise.

    """

    elapsed_time = 0  # Initialize elapsed time

    while elapsed_time < timeout:
        pod = self.get_pod(pod_name=pod_name)

        if not pod:
            self.logger.info("Pod -> '%s' has been deleted successfully.", pod_name)
            return True

        pod_status = self.get_core_v1_api().read_namespaced_pod_status(
            pod_name,
            self.get_namespace(),
        )

        self.logger.info(
            "Pod -> '%s' still exists with conditions -> %s. Waiting %s seconds before next check.",
            pod_name,
            str(
                [
                    pod_condition.type
                    for pod_condition in pod_status.status.conditions
                    if pod_condition.status == "True"
                ]
            ),
            retry_interval,
        )
        time.sleep(retry_interval)
        elapsed_time += retry_interval

    self.logger.error("Pod -> '%s' was not deleted within %d seconds.", pod_name, timeout)

    return False

verify_pod_status(pod_name, timeout=1800, total_containers=1, ready_containers=1, retry_interval=30)

Verify if a pod is in a 'Ready' state by checking the status of its containers.

This function waits for a Kubernetes pod to reach the 'Ready' state, where a specified number of containers are ready. It checks the pod status at regular intervals and reports the status using logs. If the pod does not reach the 'Ready' state within the specified timeout, it returns False.

Parameters:

Name Type Description Default
pod_name str

The name of the pod to check the status for.

required
timeout int

The maximum time (in seconds) to wait for the pod to become ready. Defaults to 1800.

1800
total_containers int

The total number of containers expected to be running in the pod. Defaults to 1.

1
ready_containers int

The minimum number of containers that need to be in a ready state. Defaults to 1.

1
retry_interval int

Time interval (in seconds) between each retry to check pod readiness. Defaults to 30.

30

Returns:

Name Type Description
bool bool

Returns True if the pod reaches the 'Ready' state with the specified number of containers ready within the timeout. Otherwise, returns False.

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def verify_pod_status(
    self,
    pod_name: str,
    timeout: int = 1800,
    total_containers: int = 1,
    ready_containers: int = 1,
    retry_interval: int = 30,
) -> bool:
    """Verify if a pod is in a 'Ready' state by checking the status of its containers.

    This function waits for a Kubernetes pod to reach the 'Ready' state, where a specified number
    of containers are ready. It checks the pod status at regular intervals and reports the status
    using logs. If the pod does not reach the 'Ready' state within the specified timeout,
    it returns `False`.

    Args:
        pod_name (str):
            The name of the pod to check the status for.
        timeout (int, optional):
            The maximum time (in seconds) to wait for the pod to become ready. Defaults to 1800.
        total_containers (int, optional):
            The total number of containers expected to be running in the pod. Defaults to 1.
        ready_containers (int, optional):
            The minimum number of containers that need to be in a ready state. Defaults to 1.
        retry_interval (int, optional):
            Time interval (in seconds) between each retry to check pod readiness. Defaults to 30.

    Returns:
        bool:
            Returns `True` if the pod reaches the 'Ready' state with the specified number of containers ready
            within the timeout. Otherwise, returns `False`.

    """

    def wait_for_pod_ready(pod_name: str, timeout: int) -> bool:
        """Wait until the pod is in the 'Ready' state with the specified number of containers ready.

        This sub method repeatedly checks the readiness of the pod, logging the
        status of the containers. If the pod does not exist, it retries after waiting
        and logs detailed information at each step.

        Args:
            pod_name (str):
                The name of the pod to check the status for.
            timeout (int):
                The maximum time (in seconds) to wait for the pod to become ready.

        Returns:
            bool:
                Returns `True` if the pod is ready with the specified number of containers in a 'Ready' state.
                Otherwise, returns `False`.

        """

        elapsed_time = 0  # Initialize elapsed time

        while elapsed_time < timeout:
            pod = self.get_pod(pod_name=pod_name)

            if not pod:
                self.logger.warning(
                    "Pod -> '%s' does not exist, waiting 300 seconds to retry.",
                    pod_name,
                )
                time.sleep(300)
                pod = self.get_pod(pod_name=pod_name)

            if not pod:
                self.logger.error(
                    "Pod -> '%s' still does not exist after retry!",
                    pod_name,
                )
                return False

            # Get the ready status of containers
            container_statuses = pod.status.container_statuses
            if container_statuses and all(container.ready for container in container_statuses):
                current_ready_containers = sum(1 for c in container_statuses if c.ready)
                total_containers_in_pod = len(container_statuses)

                if current_ready_containers >= ready_containers and total_containers_in_pod == total_containers:
                    self.logger.info(
                        "Pod -> '%s' is ready with %d/%d containers.",
                        pod_name,
                        current_ready_containers,
                        total_containers_in_pod,
                    )
                    return True
                else:
                    self.logger.debug(
                        "Pod -> '%s' is not yet ready (%d/%d).",
                        pod_name,
                        current_ready_containers,
                        total_containers_in_pod,
                    )
            else:
                self.logger.debug("Pod -> '%s' is not yet ready.", pod_name)

            self.logger.info(
                "Waiting %s seconds before next pod status check.",
                retry_interval,
            )
            time.sleep(
                retry_interval,
            )  # Sleep for the retry interval before checking again
            elapsed_time += retry_interval

        self.logger.error(
            "Pod -> '%s' is not ready after %d seconds.",
            pod_name,
            timeout,
        )
        return False

    # end method definition

    # Wait until the pod is ready
    return wait_for_pod_ready(pod_name=pod_name, timeout=timeout)

wait_pod_condition(pod_name, condition_name, sleep_time=30)

Wait for the pod to reach a defined condition (e.g. "Ready").

Parameters:

Name Type Description Default
pod_name str

The name of the Kubernetes pod in the current namespace.

required
condition_name str

The name of the condition, e.g. "Ready".

required
sleep_time int

The number of seconds to wait between repetitive status checks.

30

Returns:

Type Description
None

None

Source code in packages/pyxecm/src/pyxecm_customizer/k8s.py
def wait_pod_condition(
    self,
    pod_name: str,
    condition_name: str,
    sleep_time: int = 30,
) -> None:
    """Wait for the pod to reach a defined condition (e.g. "Ready").

    Args:
        pod_name (str):
            The name of the Kubernetes pod in the current namespace.
        condition_name (str):
            The name of the condition, e.g. "Ready".
        sleep_time (int):
            The number of seconds to wait between repetitive status checks.

    Returns:
        None

    """

    ready = False
    while not ready:
        try:
            pod_status = self.get_core_v1_api().read_namespaced_pod_status(
                pod_name,
                self.get_namespace(),
            )

            # Check if the pod has reached the defined condition:
            for cond in pod_status.status.conditions:
                if cond.type == condition_name and cond.status == "True":
                    self.logger.info(
                        "Pod -> '%s' is in state -> '%s'!",
                        pod_name,
                        condition_name,
                    )
                    ready = True
                    break
            else:
                self.logger.info(
                    "Pod -> '%s' is not yet in state -> '%s'. Waiting...",
                    pod_name,
                    condition_name,
                )
                time.sleep(sleep_time)
                continue

        except ApiException:
            self.logger.error(
                "Failed to wait for pod -> '%s' to reach state -> '%s'.",
                pod_name,
                condition_name,
            )