|
| 1 | +# Migrate from kubeconfig to Official Kubernetes Python Client |
| 2 | + |
| 3 | +## Objective |
| 4 | + |
| 5 | +Replace the unmaintained `kubeconfig` package with the official `kubernetes` Python client to ensure Python 3.12+ compatibility, active maintenance, and eliminate dependency on external kubectl binary. |
| 6 | + |
| 7 | +## Critical Rules |
| 8 | + |
| 9 | +- Maintain backward compatibility - function signatures must remain unchanged |
| 10 | +- All existing tests must pass without modification |
| 11 | +- Validate with `black` and `flake8` after each code change |
| 12 | +- No functional changes - only replace the underlying implementation |
| 13 | +- Update copyright headers to 2026 where files are modified |
| 14 | + |
| 15 | +## Execution Plan |
| 16 | + |
| 17 | +### Phase 1: Dependency Management |
| 18 | + |
| 19 | +- [ ] **1.1** Remove `kubeconfig` from [`setup.py`](setup.py:57) |
| 20 | +- [ ] **1.2** Validate: Run `python setup.py check` to ensure setup.py is valid |
| 21 | + |
| 22 | +### Phase 2: Refactor `ocp.py` |
| 23 | + |
| 24 | +- [ ] **2.1** Update imports in [`src/mas/devops/ocp.py`](src/mas/devops/ocp.py:14-15) |
| 25 | + - [ ] Replace `from kubeconfig import KubeConfig` with `from kubernetes import config` |
| 26 | + - [ ] Replace `from kubeconfig.exceptions import KubectlNotFoundError` with `from kubernetes.config.config_exception import ConfigException` |
| 27 | + - [ ] Add `import tempfile` and `import os` for temp file handling |
| 28 | + |
| 29 | +- [ ] **2.2** Refactor [`connect()`](src/mas/devops/ocp.py:28) function |
| 30 | + - [ ] Create kubeconfig dict structure with cluster, user, and context |
| 31 | + - [ ] Write dict to temporary file using `tempfile.NamedTemporaryFile` |
| 32 | + - [ ] Load config using `config.load_kube_config(config_file=temp_kubeconfig)` |
| 33 | + - [ ] Clean up temporary file with `os.unlink()` |
| 34 | + - [ ] Update exception handling from `KubectlNotFoundError` to `ConfigException` |
| 35 | + - [ ] Update docstring to reflect new implementation (remove kubectl references) |
| 36 | + |
| 37 | +- [ ] **2.3** Update copyright header to include 2026 |
| 38 | + |
| 39 | +- [ ] **2.4** Validate Phase 2 |
| 40 | + - [ ] Run `wsl bash -lc "black src/mas/devops/ocp.py"` |
| 41 | + - [ ] Run `wsl bash -lc "flake8 src/mas/devops/ocp.py"` |
| 42 | + - [ ] Verify no syntax errors |
| 43 | + |
| 44 | +### Phase 3: Refactor `tekton.py` |
| 45 | + |
| 46 | +- [ ] **3.1** Update imports in [`src/mas/devops/tekton.py`](src/mas/devops/tekton.py:21) |
| 47 | + - [ ] Remove `from kubeconfig import kubectl` |
| 48 | + - [ ] Add `from kubernetes import client, utils` |
| 49 | + - [ ] Ensure `import yaml` is present |
| 50 | + |
| 51 | +- [ ] **3.2** Refactor [`updateTektonDefinitions()`](src/mas/devops/tekton.py:333) function |
| 52 | + - [ ] Create `k8s_client = client.ApiClient()` |
| 53 | + - [ ] Read YAML file and parse with `yaml.safe_load_all()` |
| 54 | + - [ ] Iterate through YAML objects and apply with `utils.create_from_dict()` |
| 55 | + - [ ] Set namespace in metadata if not present |
| 56 | + - [ ] Add error handling for `FileNotFoundError`, `yaml.YAMLError`, and API exceptions |
| 57 | + - [ ] Update docstring to reflect new implementation and exceptions |
| 58 | + |
| 59 | +- [ ] **3.3** Update copyright header to include 2026 |
| 60 | + |
| 61 | +- [ ] **3.4** Validate Phase 3 |
| 62 | + - [ ] Run `wsl bash -lc "black src/mas/devops/tekton.py"` |
| 63 | + - [ ] Run `wsl bash -lc "flake8 src/mas/devops/tekton.py"` |
| 64 | + - [ ] Verify no syntax errors |
| 65 | + |
| 66 | +### Phase 4: Testing |
| 67 | + |
| 68 | +- [ ] **4.1** Create unit tests for `ocp.connect()` in `test/src/test_ocp_connect.py` |
| 69 | + - [ ] Test successful connection |
| 70 | + - [ ] Test connection with TLS skip |
| 71 | + - [ ] Test connection failure handling |
| 72 | + - [ ] Test ConfigException handling |
| 73 | + |
| 74 | +- [ ] **4.2** Create unit tests for `tekton.updateTektonDefinitions()` in `test/src/test_tekton_update.py` |
| 75 | + - [ ] Test successful YAML application |
| 76 | + - [ ] Test FileNotFoundError handling |
| 77 | + - [ ] Test invalid YAML handling |
| 78 | + - [ ] Test multiple resources in single file |
| 79 | + |
| 80 | +- [ ] **4.3** Validate Phase 4 |
| 81 | + - [ ] Run `wsl bash -lc "pytest test/src/test_ocp_connect.py -v"` |
| 82 | + - [ ] Run `wsl bash -lc "pytest test/src/test_tekton_update.py -v"` |
| 83 | + - [ ] Verify all new tests pass |
| 84 | + |
| 85 | +### Phase 5: Integration Testing |
| 86 | + |
| 87 | +- [ ] **5.1** Run full existing test suite |
| 88 | + - [ ] Run `wsl bash -lc "pytest test/ -v"` |
| 89 | + - [ ] Verify all existing tests still pass |
| 90 | + - [ ] Document any test failures and root cause |
| 91 | + |
| 92 | +- [ ] **5.2** Run code quality checks |
| 93 | + - [ ] Run `wsl bash -lc "black src/mas/devops/ocp.py src/mas/devops/tekton.py"` |
| 94 | + - [ ] Run `wsl bash -lc "flake8 src/mas/devops/ocp.py src/mas/devops/tekton.py"` |
| 95 | + - [ ] Verify no violations |
| 96 | + |
| 97 | +- [ ] **5.3** Validate Phase 5 |
| 98 | + - [ ] All tests pass |
| 99 | + - [ ] No flake8 violations |
| 100 | + - [ ] No black formatting issues |
| 101 | + |
| 102 | +### Phase 6: Documentation |
| 103 | + |
| 104 | +- [ ] **6.1** Review and update documentation files |
| 105 | + - [ ] Check [`README.md`](README.md:1) for kubeconfig references |
| 106 | + - [ ] Check [`CONTRIBUTING.md`](CONTRIBUTING.md:1) for setup instructions |
| 107 | + - [ ] Update if any references to kubeconfig exist |
| 108 | + |
| 109 | +- [ ] **6.2** Validate Phase 6 |
| 110 | + - [ ] Documentation is accurate and up-to-date |
| 111 | + - [ ] No broken references or outdated instructions |
| 112 | + |
| 113 | +## Validation |
| 114 | + |
| 115 | +### Success Criteria |
| 116 | + |
| 117 | +1. **Dependency removed**: `kubeconfig` no longer in [`setup.py`](setup.py:57) |
| 118 | +2. **Code quality**: All files pass `black` and `flake8` validation |
| 119 | +3. **Tests pass**: All existing tests pass without modification |
| 120 | +4. **New tests**: New unit tests for refactored functions pass |
| 121 | +5. **Copyright updated**: Modified files have 2026 in copyright header |
| 122 | +6. **Documentation**: No references to kubeconfig package remain |
| 123 | + |
| 124 | +### Commands to Run |
| 125 | + |
| 126 | +```bash |
| 127 | +# Code formatting and linting |
| 128 | +wsl bash -lc "black src/mas/devops/ocp.py src/mas/devops/tekton.py" |
| 129 | +wsl bash -lc "flake8 src/mas/devops/ocp.py src/mas/devops/tekton.py" |
| 130 | + |
| 131 | +# Run all tests |
| 132 | +wsl bash -lc "pytest test/ -v" |
| 133 | + |
| 134 | +# Verify setup.py |
| 135 | +python setup.py check |
| 136 | +``` |
| 137 | + |
| 138 | +### Expected Results |
| 139 | + |
| 140 | +- Black: No files reformatted |
| 141 | +- Flake8: No violations |
| 142 | +- Pytest: All tests pass (exact count TBD based on existing test suite) |
| 143 | +- Setup.py: No errors or warnings |
0 commit comments