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

Add args to namespaces context to specify custom ns #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion samples/scenario/contexts/namespaces.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@
"with_serviceaccount": true
}
}
},
{
"title": "Run a single workload with create/read/delete pod",
"scenario": {
"Kubernetes.create_and_delete_pod": {
"image": "kubernetes/pause"
}
},
"runner": {
"constant": {
"concurrency": 2,
"times": 10
}
},
"contexts": {
"namespaces": {
"namespaces": [
"default"
]
}
}
}
]
}
}
12 changes: 12 additions & 0 deletions samples/scenario/contexts/namespaces.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ subtasks:
namespaces:
count: 4
with_serviceaccount: true
- title: Run a single workload with create/read/delete pod
scenario:
Kubernetes.create_and_delete_pod:
image: kubernetes/pause
runner:
constant:
concurrency: 2
times: 10
contexts:
namespaces:
namespaces:
- default
17 changes: 17 additions & 0 deletions tests/unit/tasks/contexts/test_namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,20 @@ def test_create_several_namespaces_with_sa_and_round_choice(self):
self.assertEqual(3, self.client.create_namespace.call_count)
self.assertEqual(3, self.client.create_serviceaccount.call_count)
self.assertEqual(3, self.client.create_secret.call_count)

def test_namespaces_context_specify_namespaces(self):
old_config = copy.deepcopy(self.ctx.config)
old_config.update({
"namespaces": ["default", "non-default"],
"namespace_choice_method": "round_robin"
})
self.ctx.config = old_config
self.ctx.setup()

self.assertEqual(["default", "non-default"],
self.ctx.context["kubernetes"]["namespaces"])
self.assertEqual(
"round_robin",
self.ctx.context["kubernetes"]["namespace_choice_method"]
)
self.assertFalse(self.ctx.context["kubernetes"]["serviceaccounts"])
64 changes: 45 additions & 19 deletions xrally_kubernetes/tasks/contexts/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,39 @@ class NamespaceContext(common_context.BaseKubernetesContext):

CONFIG_SCHEMA = {
"type": "object",
"additionalProperties": False,
"properties": {
"count": {
"type": "integer",
"minimum": 1
"oneOf": [
{
"description": "Create number of namespaces in kubernetes env",
"properties": {
"count": {
"type": "integer",
"minimum": 1
},
"with_serviceaccount": {
"type": "boolean"
},
"namespace_choice_method": {
"enum": ["random", "round_robin"]
}
},
"required": ["count"],
"additionalProperties": False
},
"with_serviceaccount": {
"type": "boolean"
{
"description": "Select from specified namespaces, which "
"already exists in kubernetes env",
"properties": {
"namespaces": {
"type": "array"
},
"namespace_choice_method": {
"enum": ["random", "round_robin"]
}
},
"required": ["namespaces"],
"additionalProperties": False
},
"namespace_choice_method": {
"enum": ["random", "round_robin"]
}
}
]
}

DEFAULT_CONFIG = {"namespace_choice_method": "random"}
Expand All @@ -47,13 +67,19 @@ def setup(self):
})

self.context["kubernetes"].setdefault("namespaces", [])
for _ in range(self.config.get("count")):
name = self.client.create_namespace(status_wait=False)
self.context["kubernetes"]["namespaces"].append(name)
if self.config.get("with_serviceaccount"):
self.client.create_serviceaccount(name, namespace=name)
self.client.create_secret(name, namespace=name)
if self.config.get("count"):
for _ in range(self.config.get("count")):
name = self.client.create_namespace(status_wait=False)
self.context["kubernetes"]["namespaces"].append(name)
if self.config.get("with_serviceaccount"):
self.client.create_serviceaccount(name, namespace=name)
self.client.create_secret(name, namespace=name)
else:
self.context["kubernetes"]["namespaces"].extend(
self.config.get("namespaces")
)

def cleanup(self):
for name in self.context["kubernetes"].get("namespaces"):
self.client.delete_namespace(name)
if self.config.get("count"):
for name in self.context["kubernetes"].get("namespaces"):
self.client.delete_namespace(name)