Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 50 - load_all_yml supports List objects #51

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lightkube/codecs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import importlib
from typing import Union, TextIO, List
from typing import Union, TextIO, Iterator, List, Mapping

import yaml

Expand Down Expand Up @@ -52,6 +52,8 @@ def from_dict(d: dict) -> AnyResource:
* **d** - A dictionary representing a Kubernetes resource. Keys `apiVersion` and `kind` are
always required.
"""
if not isinstance(d, Mapping):
raise LoadResourceError(f"Invalid resource definition, not a dict.")
for attr in REQUIRED_ATTR:
if attr not in d:
raise LoadResourceError(f"Invalid resource definition, key '{attr}' missing.")
Expand All @@ -63,7 +65,7 @@ def from_dict(d: dict) -> AnyResource:
def load_all_yaml(stream: Union[str, TextIO], context: dict = None, template_env = None, create_resources_for_crds: bool = False) -> List[AnyResource]:
"""Load kubernetes resource objects defined as YAML. See `from_dict` regarding how resource types are detected.
Returns a list of resource objects or raise a `LoadResourceError`. Skips any empty YAML documents in the
stream, returning an empty list if all YAML documents are empty.
stream, returning an empty list if all YAML documents are empty. Deep parse any items from .*List resources.

**parameters**

Expand All @@ -83,15 +85,24 @@ def load_all_yaml(stream: Union[str, TextIO], context: dict = None, template_env
"""
if context is not None:
stream = _template(stream, context=context, template_env=template_env)
resources = []
for obj in yaml.safe_load_all(stream):
if obj is not None:
res = from_dict(obj)
resources.append(res)

if create_resources_for_crds is True and res.kind == "CustomResourceDefinition":
create_resources_from_crd(res)
return resources

def _flatten(objects: Iterator) -> List[AnyResource]:
"""Flatten resources which hae a kind = *List."""
addyess marked this conversation as resolved.
Show resolved Hide resolved
resources = []
for obj in objects:
if obj is None:
continue
if isinstance(obj, Mapping) and obj.get("kind").endswith("List"):
addyess marked this conversation as resolved.
Show resolved Hide resolved
resources += _flatten(obj.get("items") or [])
else:
res = from_dict(obj)
resources.append(res)

if create_resources_for_crds is True and res.kind == "CustomResourceDefinition":
create_resources_from_crd(res)
return resources

return _flatten(yaml.safe_load_all(stream))


def dump_all_yaml(resources: List[AnyResource], stream: TextIO = None, indent=2):
Expand Down
84 changes: 84 additions & 0 deletions tests/data/example-def-with-lists.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
apiVersion: v1
kind: SecretList
items:
- apiVersion: v1
kind: Secret
metadata:
name: "nginxsecret"
namespace: "default"
type: kubernetes.io/tls
data:
tls.crt: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURIekNDQWdW...Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0t"
tls.key: "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2UUlCQURB...TkJnVW1VbGc9Ci0tLS0tRU5EIFBSSVZB"
---
apiVersion: v1
kind: List
gtsystem marked this conversation as resolved.
Show resolved Hide resolved
items:
- apiVersion: myapp.com/v1
kind: Mydb
metadata:
name: bla
a: xx
b: yy
---
apiVersion: v1
kind: ServiceList
items:
- apiVersion: v1
kind: ServiceList
metadata: {}
items:
- apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
type: NodePort
ports:
- port: 8080
targetPort: 80
protocol: TCP
name: http
- port: 443
protocol: TCP
name: https
selector:
run: my-nginx
---
apiVersion: apps/v1
kind: DeploymentList
items:
- apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 1
template:
metadata:
labels:
run: my-nginx
spec:
volumes:
- name: secret-volume
secret:
secretName: nginxsecret
- name: configmap-volume
configMap:
name: nginxconfigmap
containers:
- name: nginxhttps
image: bprashanth/nginxhttps:1.0
ports:
- containerPort: 443
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/ssl
name: secret-volume
- mountPath: /etc/nginx/conf.d
name: configmap-volume
5 changes: 5 additions & 0 deletions tests/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def test_from_dict():


def test_from_dict_wrong_model():
# provided argument must actually be a dict
with pytest.raises(LoadResourceError, match='.*not a dict'):
codecs.from_dict([])

# apiVersion and kind are required
with pytest.raises(LoadResourceError, match=".*key 'apiVersion' missing"):
codecs.from_dict({
Expand Down Expand Up @@ -123,6 +127,7 @@ def test_from_dict_not_found():
(
"example-def.yaml",
"example-def-with-nulls.yaml",
"example-def-with-lists.yaml"
)
)
def test_load_all_yaml_static(yaml_file):
Expand Down