Skip to content

Commit

Permalink
Add project_name parameter to the kubernetes module (#333)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Mercado <mamercad@gmail.com>
  • Loading branch information
raman-babich and mamercad authored Jan 2, 2024
1 parent 23f775f commit c09252e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 15 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/264-kubernetes-project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- digital_ocean_kubernetes - add project_name parameter
(https://github.com/ansible-collections/community.digitalocean/issues/264).
1 change: 1 addition & 0 deletions plugins/module_utils/digital_ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def assign_to_project(self, project_name, urn):
Domain | do:domain:example.com
Droplet | do:droplet:4126873
Floating IP | do:floatingip:192.168.99.100
Kubernetes | do:kubernetes:bd5f5959-5e1e-4205-a714-a914373942af
Load Balancer | do:loadbalancer:39052d89-8dd4-4d49-8d5a-3c3b6b365b5b
Space | do:space:my-website-assets
Volume | do:volume:6fc4c277-ea5c-448a-93cd-dd496cfef71f
Expand Down
69 changes: 65 additions & 4 deletions plugins/modules/digital_ocean_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@
- Highly available control planes incur less downtime.
type: bool
default: false
project_name:
aliases: ["project"]
description:
- Project to assign the resource to (project name, not UUID).
- Defaults to the default project of the account (empty string).
type: str
required: false
default: ""
"""


Expand All @@ -171,7 +179,7 @@
count: 3
return_kubeconfig: true
wait_timeout: 600
register: my_cluster
register: my_cluster
- name: Show the kubeconfig for the cluster we just created
debug:
Expand All @@ -182,6 +190,21 @@
state: absent
oauth_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
name: hacktoberfest
- name: Create a new DigitalOcean Kubernetes cluster assigned to Project "test"
community.digitalocean.digital_ocean_kubernetes:
state: present
oauth_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
name: hacktoberfest
region: nyc1
node_pools:
- name: hacktoberfest-workers
size: s-1vcpu-2gb
count: 3
return_kubeconfig: true
project: test
wait_timeout: 600
register: my_cluster
"""


Expand Down Expand Up @@ -263,6 +286,7 @@
from ansible.module_utils.basic import AnsibleModule, env_fallback
from ansible_collections.community.digitalocean.plugins.module_utils.digital_ocean import (
DigitalOceanHelper,
DigitalOceanProjects,
)


Expand All @@ -276,6 +300,8 @@ def __init__(self, module):
self.wait_timeout = self.module.params.pop("wait_timeout", 600)
self.module.params.pop("oauth_token")
self.cluster_id = None
if self.module.params.get("project_name"):
self.projects = DigitalOceanProjects(module, self.rest)

def get_by_id(self):
"""Returns an existing DigitalOcean Kubernetes cluster matching on id"""
Expand Down Expand Up @@ -374,16 +400,32 @@ def create(self):
node_pool["size"], ", ".join(valid_sizes)
)
)

if self.module.check_mode:
self.module.exit_json(changed=True)
# Create the Kubernetes cluster
json_data = self.get_kubernetes()
if json_data:
# Add the kubeconfig to the return
if self.return_kubeconfig:
json_data["kubeconfig"] = self.get_kubernetes_kubeconfig()
# Assign kubernetes to project
project_name = self.module.params.get("project_name")
# empty string is the default project, skip project assignment
if project_name:
urn = "do:kubernetes:{0}".format(self.cluster_id)
(
assign_status,
error_message,
resources,
) = self.projects.assign_to_project(project_name, urn)
if assign_status not in {"ok", "assigned", "already_assigned"}:
self.module.fail_json(
changed=False,
msg=error_message,
assign_status=assign_status,
resources=resources,
)
self.module.exit_json(changed=False, data=json_data)
if self.module.check_mode:
self.module.exit_json(changed=True)
request_params = dict(self.module.params)
response = self.rest.post("kubernetes/clusters", data=request_params)
json_data = response.json
Expand All @@ -395,6 +437,22 @@ def create(self):
json_data = self.ensure_running()
# Add the kubeconfig to the return
if self.return_kubeconfig:
json_data["kubeconfig"] = self.get_kubernetes_kubeconfig()
# Assign kubernetes to project
project_name = self.module.params.get("project_name")
# empty string is the default project, skip project assignment
if project_name:
urn = "do:kubernetes:{0}".format(self.cluster_id)
assign_status, error_message, resources = self.projects.assign_to_project(
project_name, urn
)
if assign_status not in {"ok", "assigned", "already_assigned"}:
self.module.fail_json(
changed=True,
msg=error_message,
assign_status=assign_status,
resources=resources,
)
json_data["kubernetes_cluster"][
"kubeconfig"
] = self.get_kubernetes_kubeconfig()
Expand Down Expand Up @@ -474,6 +532,9 @@ def main():
wait=dict(type="bool", default=True),
wait_timeout=dict(type="int", default=600),
ha=dict(type="bool", default=False),
project_name=dict(
type="str", aliases=["project"], required=False, default=""
),
),
required_if=(
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
do_region: nyc1
test_project_name: test-kubernetes

cluster_name: gh-ci-k8s-0-{{ pr_number }}
cluster_version: latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@
- result.failed
- result.msg == "Kubernetes cluster not found"

- name: Ensure the test project is absent
community.digitalocean.digital_ocean_project:
oauth_token: "{{ do_api_key }}"
state: absent
name: "{{ test_project_name }}"
register: project

- name: Verify test project is absent
ansible.builtin.assert:
that:
- not project.changed

- name: Create test project
community.digitalocean.digital_ocean_project:
oauth_token: "{{ do_api_key }}"
state: present
name: "{{ test_project_name }}"
purpose: Just trying out DigitalOcean
description: This is a test project
environment: Development
register: project

- name: Create the Kubernetes cluster
community.digitalocean.digital_ocean_kubernetes:
oauth_token: "{{ do_api_key }}"
Expand All @@ -65,7 +87,8 @@
version: "{{ cluster_version }}"
region: "{{ do_region }}"
node_pools: "{{ cluster_node_pools }}"
return_kubeconfig: true
project_name: "{{ test_project_name }}"
return_kubeconfig: false
wait_timeout: 600
register: result

Expand Down Expand Up @@ -105,6 +128,19 @@
- result.data.kubeconfig is defined
- result.data.kubeconfig | length > 0

- name: Get test project resources
community.digitalocean.digital_ocean_project_resource_info:
oauth_token: "{{ do_api_key }}"
name: "{{ test_project_name }}"
register: resources

- name: Verify kubernetes cluster is present
ansible.builtin.assert:
that:
- resources.data is defined
- resources.data | length == 1
- resources.data[0].urn == 'do:kubernetes:' + result.data.id

- name: Give the cloud a minute to settle
ansible.builtin.pause:
minutes: 1
Expand Down Expand Up @@ -229,3 +265,10 @@
return_kubeconfig: false
wait_timeout: 600
ignore_errors: true # Should this fail, we'll clean it up next run

- name: Delete test project
community.digitalocean.digital_ocean_project:
oauth_token: "{{ do_api_key }}"
state: absent
name: "{{ test_project_name }}"
ignore_errors: true # Should this fail, we'll clean it up next run
Loading

0 comments on commit c09252e

Please sign in to comment.