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

azure_rm_aks: support system-assigned (managed) identity, #514

Merged
merged 11 commits into from
Jul 22, 2021

Conversation

geekq
Copy link
Contributor

@geekq geekq commented Apr 23, 2021

... make linux_profile and service_principal optional

SUMMARY

Motivation:

while current azure documentation tells

The cluster infrastructure authentication specified is used by Azure Kubernetes Service to manage cloud resources attached to the cluster. This can be either a service principal or a system-assigned managed identity.

the current azcollection only supports former, not latter

required: true
declares service_principal as required and allows no system-assigned managed identity.

An explicit linux_profile, service_principal require additional work at the beginning, but especially later, for rotation credentials for the service principal. The usual (and also Microsoft-recommended) approach nowadays is to use system-assigned managed identity. Also added an example for creating a cluster using minimal number of parameters.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

azure_rm_aks

ADDITIONAL INFORMATION

If you just start with trying to automate the Azure k8s cluster creation with ansible,
it was surprising to see many parameters required by this ansible module.

While az cli only requires name, group name and node count
az aks create -g myGroup -n myCluster --node-count 3 -l westeurope - you just need an existing resource group,
I saw no way to create a cluster with a single azure_rm_aks ansible module call.

This pull request aims to change that, enabling an easy start, making following example work

    - name: Use minimal parameters and system-assigned identity
      azure_rm_aks:
        name: myMinimalCluster
        location: eastus
        resource_group: myExistingResourceGroup
        dns_prefix: akstest
        agent_pool_profiles:
          - name: default
            count: 1
            vm_size: Standard_D2_v2

Copy link
Collaborator

@Fred-sun Fred-sun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@geekq Thank for your contribution! Would you add test case for the request? Thank you very much!

plugins/modules/azure_rm_aks.py Outdated Show resolved Hide resolved
suboptions:
client_id:
description:
- The ID for the Service Principal.
required: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The required is not match with argument_spec. The argument_spec is required!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure, if I should remove required: true for the suboptions too. Current state of PR seems to work as expected. I understand it so: linux_profile is optional, but if linux_profile: is given, then suboptions admin_username and ssh_key must be provided. This is why I kept required: true for admin_username and ssh_key. Or do you mean something else with "The argument_spec is required"?

@Fred-sun Fred-sun added medium_priority Medium priority work in In trying to solve, or in working with contributors labels Apr 28, 2021
@geekq
Copy link
Contributor Author

geekq commented Apr 28, 2021

Would you add test case for the request?

Would love to provide tests! From CONTRIBUTING.md or README.md I did not understand, what tests are needed, how they are supposed to be run: do I have to run the whole suit against a real Azure account? Maybe you can provide some guidance on this, thanks! Otherwise I'll try to dive in into the code and may be extend https://github.com/ansible-collections/azure/blob/dev/tests/integration/targets/azure_rm_aks/tasks/main.yml - or can I (preferably) create a smaller, isolated test in the same folder?

P.S. Can I see the test results in the CI? Looks like it requires additional authorization?

@geekq
Copy link
Contributor Author

geekq commented Apr 29, 2021

I've added an integration test for this minimal-parameters-cluster-definition.
But how I can run it with tests/utils/ado/ado.sh locally?

Or maybe @Fred-sun you can see the test results in the CI (I seem to have no access to the linked pipelines above)

@Xiuxi-Sun
Copy link

I've added an integration test for this minimal-parameters-cluster-definition.
But how I can run it with tests/utils/ado/ado.sh locally?

Or maybe @Fred-sun you can see the test results in the CI (I seem to have no access to the linked pipelines above)

@geekq You can follow the link below to perform the Sanity test. Thank you very much!

link:  https://docs.ansible.com/ansible/latest/dev_guide/testing_sanity.html
command:   ansible-test sanity --color -v --junit

@Fred-sun
Copy link
Collaborator

Fred-sun commented Jun 8, 2021

@geekq Why don't you place the test(minimal-cluster.yml) in main.yml so that the automated test can cover the feature! Thank you very much!

@Fred-sun
Copy link
Collaborator

Fred-sun commented Jun 8, 2021

@geekq In addition, please help to share the results of the test cases you added, which will help to advance the PR merge. Thank you very much!

azure_rm_aks:
name: "minimal{{ rpfx }}"
location: eastus
resource_group: "{{ resource_group }}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add "kubernetes_version" parameter when create kubernets service! Thank you very much!

Suggested change
resource_group: "{{ resource_group }}"
resource_group: "{{ resource_group }}"
kubernetes_version: **

@geekq
Copy link
Contributor Author

geekq commented Jun 18, 2021

@geekq Why don't you place the test(minimal-cluster.yml in main.yml

My motivation for putting it to a separate file was to depict, that those tasks are self-contained and do not depend on the other steps in main.yml
Such separation in smaller, self contained chunks would improve the maintenance. So anybody working on a particular feature could run just a single file (much faster, costs less azure resource). I would even recommend to break the existing main.yml into small, independent chunks! But I do not mind, if you merge it to the big main.yml ;-)

so that the automated test can cover the feature!

Maybe this automated test could run multiple files. It would be also great to be able to see it's output published somewhere.

Please add "kubernetes_version" parameter when create kubernets service!

What about maintenance? azure constantly changes the list of kubernetes versions available. You will need to adjust this file continuously. But I do not mind! :-) Please adjust as you see fit. It is maybe less work for you to adjust this PR to your taste, than make me change each line ;-) And thank you for maintaining this library!

@Fred-sun
Copy link
Collaborator

@geekq Thank you for your reply.

First, kubernetes_version is a required parameter when creating aks, and we can get the version of kubernetes_version from azure_rm_aksversion_info and pass it in.

Second, the main.yml is an automated playbook. Test /// Tasks /*.yml will be executed on a weekly basis to ensure that the latest repo is working properly.

In the end, we combined the two PlayBooks. It doesn't have to be interdependent, it can be a separate test module, with different functions for the test.

@Fred-sun
Copy link
Collaborator

@geekq Could you please help to authorize me to update this PR? Thank you very much!

@Fred-sun
Copy link
Collaborator

@geekq Please make the final changes. Thank you very much!

File: tests/integration/targets/azure_rm_aks/tasks/main.yml
 - set_fact:
     rpfx: "{{ resource_group | hash('md5') | truncate(8, True, '') }}"
     noderpfx: "{{ resource_group | hash('md5') | truncate(4, True, '') }}"

 - include: minimal-cluster.yml

 - name: Find available k8s version
   azure_rm_aksversion_info:
     location: eastus
   register: versions

File :tests/integration/targets/azure_rm_aks/tasks/minimal-cluster.yml
- set_fact:
    rpfx: "{{ resource_group | hash('md5') | truncate(8, True, '') }}"
    noderpfx: "{{ resource_group | hash('md5') | truncate(4, True, '') }}"

- name: Find available k8s version
  azure_rm_aksversion_info:
    location: eastus
  register: versions

- name: Use minimal parameters and system-assigned identity
  azure_rm_aks:
    name: "minimal{{ rpfx }}"
    location: eastus
    resource_group: "{{ resource_group }}"
    dns_prefix: "aks{{ rpfx }}"
    kubernetes_version: "{{ versions.azure_aks_versions[0] }}"
    agent_pool_profiles:
      - name: default
        count: 1
        vm_size: Standard_DS1_v2
  register: output

- name: Assert the AKS instance is well created
  assert:
    that:
      - output.changed
      - output.provisioning_state == 'Succeeded'

- name: Get AKS fact
  azure_rm_aks_info:
     name: "minimal{{ rpfx }}"
     resource_group: "{{ resource_group }}"
  register: fact

- name: Assert fact returns the created one
  assert:
    that:
      - "fact.aks | length == 1"
      - fact.aks[0].id == output.id

- name: Use minimal parameters and system-assigned identity (idempotent)
  azure_rm_aks:
    name: "minimal{{ rpfx }}"
    location: eastus
    resource_group: "{{ resource_group }}"
    dns_prefix: "aks{{ rpfx }}"
    kubernetes_version: "{{ versions.azure_aks_versions[0] }}"
    agent_pool_profiles:
      - name: default
        count: 1
        vm_size: Standard_DS1_v2
  register: output

- name: Assert idempotent
  assert:
    that:
      - not output.changed

- name: Delete the AKS instance
  azure_rm_aks:
    name: "minimal{{ rpfx }}"
    resource_group: "{{ resource_group }}"
    state: absent
  register: output

- name: Assert the AKS instance is well deleted
  assert:
    that:
      - output.changed

- name: Get AKS fact
  azure_rm_aks_info:
    name: "minimal{{ rpfx }}"
    resource_group: "{{ resource_group }}"
  register: fact

- name: Assert fact returns empty
  assert:
    that:
      - "fact.aks | length == 0"

@xuzhang3 xuzhang3 merged commit bd40c1d into ansible-collections:dev Jul 22, 2021
Fred-sun added a commit to Fred-sun/ansible_collections_azure that referenced this pull request Aug 5, 2021
add new change

add new change

add new change

add new update

Update last changed

Update test case

Enable azure_rm_privateendpoint test

azure_rm_aks: support system-assigned (managed) identity, (ansible-collections#514)

* azure_rm_aks: make linux_profile and service_principal optional, support system-assigned (managed) identity

* azure_rm_aks: adjust docs formatting

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

* azure_rm_aks: add a test for the minimal parameters cluster definition

* azure_rm_aks: fix sanity-checks / pep8 requirements

* Add instructions for tests / sanity checks

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

Upddate test case (ansible-collections#585)

Add new feature storage_profile (ansible-collections#563)

* Add new feature storage_profile

* remove ignore

* remove ignore 02

Bump version to v1.8.0 (ansible-collections#586)

* Bump version to v1.8.0

* Update CHANGELOG.md

update release date

Co-authored-by: xuzhang3 <57888764+xuzhang3@users.noreply.github.com>

add runtime.yml (ansible-collections#587)

fix sanity error

fix santiy error 02

fix sanity error 04

fix sanity error 03

fix sanity error 05

fix sanity error 06

fix sanity error 07

Add resource tags (ansible-collections#592)

* add resource tags

* add resource tags
Fred-sun added a commit to Fred-sun/ansible_collections_azure that referenced this pull request Aug 11, 2021
* add new paramter node_labels for agent_pool

* remove space

Add return value for azure_rm_containerregistry idempotent test (ansible-collections#578)

* Add return value for azure_rm_containerregistry idempotent test

* fix sanity error:

* remove require set

azure_rm_roleassignment bugfix (ansible-collections#464)

* pushing fixes.

* whitespace

* whitespace

* Update aliases

* add assignee filter to triplet lookup

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

azure_rm_aks: support system-assigned (managed) identity, (ansible-collections#514)

* azure_rm_aks: make linux_profile and service_principal optional, support system-assigned (managed) identity

* azure_rm_aks: adjust docs formatting

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

* azure_rm_aks: add a test for the minimal parameters cluster definition

* azure_rm_aks: fix sanity-checks / pep8 requirements

* Add instructions for tests / sanity checks

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

Upddate test case (ansible-collections#585)
xuzhang3 pushed a commit that referenced this pull request Aug 12, 2021
* add new module azure_rm_privateendpoint_info.py

* add azu_rm_privateendpoint.py

* update new

add new change

add new change

add new change

add new update

Update last changed

Update test case

Enable azure_rm_privateendpoint test

azure_rm_aks: support system-assigned (managed) identity, (#514)

* azure_rm_aks: make linux_profile and service_principal optional, support system-assigned (managed) identity

* azure_rm_aks: adjust docs formatting

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

* azure_rm_aks: add a test for the minimal parameters cluster definition

* azure_rm_aks: fix sanity-checks / pep8 requirements

* Add instructions for tests / sanity checks

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

Upddate test case (#585)

Add new feature storage_profile (#563)

* Add new feature storage_profile

* remove ignore

* remove ignore 02

Bump version to v1.8.0 (#586)

* Bump version to v1.8.0

* Update CHANGELOG.md

update release date

Co-authored-by: xuzhang3 <57888764+xuzhang3@users.noreply.github.com>

add runtime.yml (#587)

fix sanity error

fix santiy error 02

fix sanity error 04

fix sanity error 03

fix sanity error 05

fix sanity error 06

fix sanity error 07

Add resource tags (#592)

* add resource tags

* add resource tags

* Update Copyright

* add supports check mode for azure_rm_privateendpoint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
medium_priority Medium priority work in In trying to solve, or in working with contributors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants