Skip to content

Commit

Permalink
Get working with k8s.core 2 and above
Browse files Browse the repository at this point in the history
- Drop Python 2 support
- Use kubernetes.core
- Migrate from OpenShift to Kubernetes for some tests
  • Loading branch information
snecklifter committed Sep 21, 2021
1 parent 3414a94 commit 17ece39
Show file tree
Hide file tree
Showing 23 changed files with 225 additions and 144 deletions.
25 changes: 17 additions & 8 deletions .github/workflows/ansible-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
sanity:
name: Sanity (Ⓐ${{ matrix.ansible }})
strategy:
fail-fast: true
matrix:
ansible:
# It's important that Sanity is tested against all stable-X.Y branches
Expand Down Expand Up @@ -56,8 +57,11 @@ jobs:
- name: Install ansible-base (${{ matrix.ansible }})
run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check

- name: Install deps
run: pip install kubernetes --disable-pip-version-check

- name: Install collection dependencies
run: ansible-galaxy collection install 'community.kubernetes:<2.0.0' -p .
run: ansible-galaxy collection install 'kubernetes.core' -p .

# run ansible-test sanity inside of Docker.
# The docker container has all the pinned dependencies that are required
Expand Down Expand Up @@ -97,12 +101,18 @@ jobs:
- name: Install ansible-base (${{ matrix.ansible }})
run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check

- name: Remove old pyyaml
run: pip uninstall -y pyyaml

- name: Install deps
run: pip install kubernetes --disable-pip-version-check

- name: Install collection dependencies
run: ansible-galaxy collection install 'community.kubernetes:<2.0.0' -p .
run: ansible-galaxy collection install 'kubernetes.core' -p .

# Run the unit tests
- name: Run unit test
run: ansible-test units -v --color --docker --coverage
run: ansible-test units -v --color --docker --python 3.8 --coverage
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

# ansible-test support producing code coverage date
Expand Down Expand Up @@ -137,10 +147,6 @@ jobs:
- stable-2.11
- devel
python:
- 2.7
- 3.5
- 3.6
- 3.7
- 3.8
- 3.9
exclude:
Expand All @@ -161,8 +167,11 @@ jobs:
- name: Install ansible-base (${{ matrix.ansible }})
run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check

- name: Install deps
run: pip install kubernetes --disable-pip-version-check

- name: Install collection dependencies
run: ansible-galaxy collection install 'community.kubernetes:<2.0.0' -p .
run: ansible-galaxy collection install 'kubernetes.core' -p .

# Run the integration tests
- name: Run integration test
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ Every voice is important and every idea is valuable. If you have something on yo

## External requirements

- python >= 2.7
- openshift >= 0.8.2
- python >= 3.6
- kubernetes >= 12.0.1

## Included Content

Expand Down
4 changes: 4 additions & 0 deletions changelogs/fragments/k8s-core-2-compatibility.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
major_changes:
- Drop Python 2.7 compatibility (https://github.com/ansible-collections/community.kubevirt/issues/33).
- Migrate from the ``community.kubernetes`` to ``kubernetes.core`` collection dependency (https://github.com/ansible-collections/community.kubevirt/issues/33).
- Change references from ``OpenShift`` to ``Kubernetes`` in the documentation (https://github.com/ansible-collections/community.kubevirt/issues/33).
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors:
- kubevirt (github.com/kubevirt)
- tima (github.com/tima)
dependencies:
community.kubernetes: '>=1.0.0,<2.0.0'
kubernetes.core: '>=2.0.0'
description:
license_file: LICENSE
tags:
Expand Down
8 changes: 4 additions & 4 deletions plugins/doc_fragments/kubevirt_common_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ class ModuleDocFragment(object):
the virtual machine will not have a domain name at all. The DNS entry will resolve to the virtual machine, no matter if the virtual machine
itself can pick up a hostname."
requirements:
- python >= 2.7
- openshift >= 0.8.2
- python >= 3.6
- kubernetes >= 12.0.1
notes:
- "In order to use this module you have to install Openshift Python SDK.
- "In order to use this module you have to install Kubernetes Python SDK.
To ensure it's installed with correct version you can create the following task:
I(pip: name=openshift>=0.8.2)"
I(pip: name=kubernetes>=12.0.1)"
'''
11 changes: 6 additions & 5 deletions plugins/inventory/kubevirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
kubeconfig:
description:
- Path to an existing Kubernetes config file. If not provided, and no other connection
options are provided, the OpenShift client will attempt to load the default
options are provided, the Kubernetes client will attempt to load the default
configuration file from I(~/.kube/config.json). Can also be specified via K8S_AUTH_KUBECONFIG
environment variable.
type: str
Expand Down Expand Up @@ -115,7 +115,7 @@
type: str
default: 'ansible'
requirements:
- "openshift >= 0.6"
- "kubernetes >= 12.0.1"
- "PyYAML >= 3.11"
'''

Expand All @@ -140,7 +140,8 @@

import json

from ansible_collections.community.kubernetes.plugins.inventory.k8s import K8sInventoryException, InventoryModule as K8sInventoryModule, format_dynamic_api_exc
from ansible_collections.kubernetes.core.plugins.inventory.k8s import K8sInventoryException, InventoryModule as K8sInventoryModule, format_dynamic_api_exc
from ansible_collections.kubernetes.core.plugins.module_utils.common import get_api_client

try:
from openshift.dynamic.exceptions import DynamicApiError
Expand All @@ -159,12 +160,12 @@ def setup(self, config_data, cache, cache_key):
super(InventoryModule, self).setup(config_data, cache, cache_key)

def fetch_objects(self, connections):
client = self.get_api_client()
client = get_api_client()
vm_format = self.config_data.get('host_format', '{namespace}-{name}-{uid}')

if connections:
for connection in connections:
client = self.get_api_client(**connection)
client = get_api_client(**connection)
name = connection.get('name', self.get_default_host_name(client.configuration.host))
if connection.get('namespaces'):
namespaces = connection['namespaces']
Expand Down
56 changes: 48 additions & 8 deletions plugins/module_utils/kubevirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import traceback
import copy
import re

from collections import defaultdict
from distutils.version import Version

from ansible.module_utils.common import dict_transformations
from ansible.module_utils.common._collections_compat import Sequence
from ansible_collections.community.kubernetes.plugins.module_utils.common import list_dict_str
from ansible_collections.community.kubernetes.plugins.module_utils.raw import KubernetesRawModule

import copy
import re
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native

from ansible_collections.kubernetes.core.plugins.module_utils.args_common import AUTH_ARG_SPEC, list_dict_str
from ansible_collections.kubernetes.core.plugins.module_utils.common import K8sAnsibleMixin, get_api_client

try:
import yaml
from kubernetes.dynamic.exceptions import DynamicApiError, NotFoundError, ForbiddenError
except ImportError:
# Exceptions handled in common
pass

MAX_SUPPORTED_API_VERSION = 'v1alpha3'
API_GROUP = 'kubevirt.io'
Expand Down Expand Up @@ -132,9 +144,37 @@ def __cmp__(self, other):
return self._cmp(other)


class KubeVirtRawModule(KubernetesRawModule):
def __init__(self, *args, **kwargs):
super(KubeVirtRawModule, self).__init__(*args, **kwargs)
class KubeVirtRawModule(K8sAnsibleMixin):
def __init__(self, k8s_kind=None, *args, **kwargs):
module = AnsibleModule(
argument_spec=self.argspec,
supports_check_mode=True,
)

self.module = module
self.check_mode = self.module.check_mode
self.params = self.module.params
self.fail_json = self.module.fail_json
self.fail = self.module.fail_json
self.exit_json = self.module.exit_json

super(KubeVirtRawModule, self).__init__(module, *args, **kwargs)

self.client = get_api_client(self.module)
self.warnings = []

self.kind = k8s_kind or self.params.get('kind')
self.api_version = self.params.get('api_version')
self.name = self.params.get('name')
self.namespace = self.params.get('namespace')

self.check_library_version()
self.set_resource_definitions(module)

@property
def argspec(self):
argument_spec = copy.deepcopy(AUTH_ARG_SPEC)
return argument_spec

@staticmethod
def merge_dicts(base_dict, merging_dicts):
Expand Down Expand Up @@ -306,7 +346,7 @@ def find_supported_resource(self, kind):
str([r.api_version for r in sr]), API_GROUP, MAX_SUPPORTED_API_VERSION))

def _construct_vm_definition(self, kind, definition, template, params, defaults=None):
self.client = self.get_api_client()
self.client = get_api_client(self.module)

disks = params.get('disks', [])
memory = params.get('memory')
Expand Down
26 changes: 17 additions & 9 deletions plugins/modules/kubevirt_cdi_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
description:
- Use Openshift Python SDK to create UploadTokenRequest objects.
- Use Kubernetes Python SDK to create UploadTokenRequest objects.
- Transfer contents of local files to the CDI Upload Proxy.
options:
Expand Down Expand Up @@ -59,12 +59,12 @@
choices: [ json, merge, strategic-merge ]
extends_documentation_fragment:
- community.kubernetes.k8s_auth_options
- kubernetes.core.k8s_auth_options
requirements:
- python >= 2.7
- openshift >= 0.8.2
- python >= 3.6
- kubernetes >= 12.0.1
- requests >= 2.0.0
'''

Expand All @@ -84,9 +84,11 @@
import traceback

from collections import defaultdict
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native

from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC
from ansible_collections.community.kubernetes.plugins.module_utils.raw import KubernetesRawModule
from ansible_collections.kubernetes.core.plugins.module_utils.common import K8sAnsibleMixin, get_api_client
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import AUTH_ARG_SPEC

# 3rd party imports
try:
Expand All @@ -113,9 +115,15 @@
}


class KubeVirtCDIUpload(KubernetesRawModule):
class KubeVirtCDIUpload(K8sAnsibleMixin):
def __init__(self, *args, **kwargs):
super(KubeVirtCDIUpload, self).__init__(*args, k8s_kind='UploadTokenRequest', **kwargs)
self.module = AnsibleModule(
argument_spec=self.argspec,
supports_check_mode=True,
)
self.fail_json = self.module.fail_json

super(KubeVirtCDIUpload, self).__init__(self.module, *args, k8s_kind='UploadTokenRequest', **kwargs)

if not HAS_REQUESTS:
self.fail("This module requires the python 'requests' package. Try `pip install requests`.")
Expand All @@ -133,7 +141,7 @@ def execute_module(self):
API = 'v1alpha1'
KIND = 'UploadTokenRequest'

self.client = self.get_api_client()
self.client = get_api_client(self.module)

api_version = 'upload.cdi.kubevirt.io/{0}'.format(API)
pvc_name = self.params.get('pvc_name')
Expand Down
13 changes: 6 additions & 7 deletions plugins/modules/kubevirt_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
short_description: Manage KubeVirt virtual machine presets
description:
- Use Openshift Python SDK to manage the state of KubeVirt virtual machine presets.
- Use Kubernetes Python SDK to manage the state of KubeVirt virtual machine presets.
author: KubeVirt Team (@kubevirt)
Expand Down Expand Up @@ -45,14 +45,14 @@
type: dict
extends_documentation_fragment:
- community.kubernetes.k8s_auth_options
- kubernetes.core.k8s_auth_options
- community.kubevirt.kubevirt_vm_options
- community.kubevirt.kubevirt_common_options
requirements:
- python >= 2.7
- openshift >= 0.8.2
- python >= 3.6
- kubernetes >= 12.0.1
'''

EXAMPLES = '''
Expand Down Expand Up @@ -87,8 +87,7 @@
import copy
import traceback


from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import AUTH_ARG_SPEC

from ansible_collections.community.kubevirt.plugins.module_utils.kubevirt import (
virtdict,
Expand Down Expand Up @@ -128,7 +127,7 @@ def execute_module(self):
# defaults for template
defaults = {'disks': [], 'volumes': [], 'interfaces': [], 'networks': []}

# Execute the CURD of VM:
# Execute the CRUD of VM:
dummy, definition = self.construct_vm_definition(KIND, definition, definition, defaults)
result_crud = self.execute_crud(KIND, definition)
changed = result_crud['changed']
Expand Down
Loading

0 comments on commit 17ece39

Please sign in to comment.