Skip to content

Commit

Permalink
Merge pull request #23 from tsladecek/22-enforce-value-type
Browse files Browse the repository at this point in the history
allow enforcing integer values in patches
  • Loading branch information
tsladecek committed Oct 30, 2023
2 parents 563339b + 891fc35 commit 58365c0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ ops:
path: <path to the part of the manifest to be patched>
value: <value which should be replaced or added>
action: <add|replace|remove>
enforce_integer: <Optional - bool. whether exported value must be an integer in output json. Defaults to "false">
list_key: <Optional - key by which an element in list should be targeted. Defaults to "name">
```

Expand Down
30 changes: 30 additions & 0 deletions examples/base/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa
minReplicas: 1
maxReplicas: 1
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
behavior:
scaleDown:
policies:
- type: Percent
value: 50
periodSeconds: 30
11 changes: 11 additions & 0 deletions examples/patches/hpa_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: horizontal pod autoscaler patch
ops:
- action: replace
name: replace max replicas value
path: /spec/maxReplicas
value: 3
enforce_integer: true
target:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
name: hpa
7 changes: 7 additions & 0 deletions src/ksux/src/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def validate_patch(patch: dict) -> Patch:
logging.error(f'Patch {patch} did not pass validation. See required fields')
exit(1)
else:
for op in validated.ops:
if op.enforce_integer:
try:
op.value = int(op.value)
except ValueError:
logging.error(f'Cannot convert "{op.value}" to int for patch: {validated}')

return validated


Expand Down
1 change: 1 addition & 0 deletions src/ksux/src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Op(BaseModel):
name: str
path: str
value: Optional[Union[str, int, dict, list]]
enforce_integer: bool = False
action: Action
list_key: str = 'name'

Expand Down
16 changes: 14 additions & 2 deletions src/ksux/tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from pydantic import ValidationError

from src.ksux.src.patch import read_patches, get_real_path, apply_patch
from src.ksux.src.patch import read_patches, get_real_path, apply_patch, validate_patch
from src.ksux.src.schemas import Patch, Op, Action
from src.ksux.tests.config import settings

Expand All @@ -22,7 +22,8 @@ def get_valid_patch() -> dict:
"name": "example op",
"path": "/spec/replicas",
"action": "replace",
"value": 2
"value": "2",
"enforce_integer": False
}
]
}
Expand All @@ -32,6 +33,17 @@ def test_validate_patch_valid_patch():
valid = get_valid_patch()
assert Patch(**valid)

# Test enforce integer False
patch = get_valid_patch()
patch["ops"][0]["enforce_integer"] = False
validated = validate_patch(patch)
assert isinstance(validated.ops[0].value, str)

# Test enforce integer True
patch["ops"][0]["enforce_integer"] = True
validated = validate_patch(patch)
assert isinstance(validated.ops[0].value, int)

# value can be omitted for action remove
valid_patch_action_remove_missing_value = get_valid_patch()
valid_patch_action_remove_missing_value['ops'][0]['action'] = 'remove'
Expand Down

0 comments on commit 58365c0

Please sign in to comment.