Skip to content

Customized Molecule-Kubernetes is not working with molecule 6.0.3 #291

Open
@kiranashav

Description

We have upgraded our molecule version from 4.0.0 to 6.0.3 and the molecule commands are not working.
We were able to execute all the molecule commands using version 4.0.0 and were able to launch pods on kubernetes. But with 6.0.3 we're not sure what changes need to be made

here's the customized tree structure
`tree molecule_kubernetes molecule_kubernetes ├── cookiecutter │ ├── cookiecutter.json │ └── {{cookiecutter.molecule_directory}} │ └── {{cookiecutter.scenario_name}} │ ├── collections.yml │ ├── converge.yml │ ├── LICENSE │ ├── molecule.yml │ └── README.md ├── driver.py ├── init.py ├── playbooks │ ├── create.yml │ └── destroy.yml └── pycache ├── driver.cpython-39.pyc └── init.cpython-39.pyc 5 directories, 12 files

Customized molecule.yml which was working for version 4.0.0

cat molecule.yml
`

dependency:
{%- if cookiecutter.dependency_name == 'galaxy' %}
name: shell
command: |
# build and install current collection and its dependencies
if test -f "../../galaxy.yml"; then
ansible-galaxy collection build ../.. --output-path $MOLECULE_EPHEMERAL_DIRECTORY --force
ansible-galaxy collection install $$(ls -1 $MOLECULE_EPHEMERAL_DIRECTORY/*.tar.gz) --force
fi
# install scenario role dependencies
if test -f "$MOLECULE_SCENARIO_DIRECTORY/requirements.yml"; then
ansible-galaxy role install -r $MOLECULE_SCENARIO_DIRECTORY/requirements.yml
fi
# install scenario collection dependencies
if test -f "$MOLECULE_SCENARIO_DIRECTORY/collections.yml"; then
ansible-galaxy collection install -r $MOLECULE_SCENARIO_DIRECTORY/collections.yml
fi
{%- else %}
name: {{ cookiecutter.dependency_name }}
{%- endif %}
driver:
name: kubernetes
namespace: ${BUILD_KUBERNETES_NAMESPACE:-molecule}
resources:
requests:
cpu: 500m
memory: 1Gi
#shares:

- name: ${BUILD_TAG}-{{ cookiecutter.role_name|replace('', '-') }}-{{ cookiecutter.scenario_name|replace('', '-') }}

mountPath: /path/to/your/shared/folder

storageClass: managed-nfs-storage

size: 10Mi

templates:
centos7:
image: abc.com/abf-centos7-init:latest
mountHostEntitlements: true
nodeSelector:
kubernetes.com/distribution: redhat7
#kubernetes.com/storage: 'true'
centos8:
image: abc.com/abf-centos8-init:latest
mountHostEntitlements: true
nodeSelector:
kubernetes.com/distribution: redhat8
#kubernetes.com/storage: 'true'
rhel7:
image: abc.com/abf-rhel7-init:latest
nodeSelector:
kubernetes.com/distribution: redhat7
#kubernetes.com/storage: 'true'
rhel8:
image: abc.com/abf-rhel8-init:latest
nodeSelector:
kubernetes.com/distribution: redhat8
#kubernetes.com/storage: 'true'
platforms:

  • name: ${BUILD_TAG}-{{ cookiecutter.role_name|replace('', '-')|lower }}-{{ cookiecutter.scenario_name|replace('', '-'
    )|lower }}
    inherit_from: rhel8
    prerun: false
    provisioner:
    name: ansible
    inventory:
    group_vars:
    all:
    stage: molecule
    verifier:
    name: {{ cookiecutter.verifier_name }}
    (ansible-2.14.18)
    `

Customized driver.py
`
import os

from molecule import logger, util
from molecule.api import Driver
from molecule.util import sysexit_with_message
from ansible_compat.ports import cache

log = logger.get_logger(name)

class Kubernetes(Driver):
def init(self, config=None):
super(Kubernetes, self).init(config)
self._name = "kubernetes"

@property
def name(self):
    return self._name

@name.setter
def name(self, value):
    self._name = value

@property
def login_cmd_template(self):
    return "kubectl exec -n {namespace} -c {instance} -it {address} -- bash"

@property
def default_safe_files(self):
    return [self.instance_config]

@property
def default_ssh_connection_options(self):
    return []

def login_options(self, instance_name):
    d = {"instance": instance_name}
    return util.merge_dicts(d, self._get_instance_config(instance_name))

def _get_instance_config(self, instance_name):
    instance_config_dict = util.safe_load_file(self._config.driver.instance_config)

    return next(
        item for item in instance_config_dict if item["instance"] == instance_name
    )

def ansible_connection_options(self, instance_name):
    try:
        d = self._get_instance_config(instance_name)

        return {"ansible_connection": "community.kubernetes.kubectl", "ansible_host": d["address"]}
    except StopIteration:
        return {}
    except IOError:
        return {}

def template_dir(self):
    """Return path to its own cookiecutterm templates. It is used by init
    command in order to figure out where to load the templates from.
    """
    return os.path.join(os.path.dirname(__file__), "cookiecutter")

@cache
def sanity_checks(self):
    """Kubernetes driver sanity checks."""

    log.info("Sanity checks: '{}'".format(self._name))

    try:
        import openshift  # noqa
    except ImportError:
        msg = (
            "Missing Kubernetes driver dependency. Please "
            "install the 'molecule-kubernetes' package or "
            "refer to your INSTALL.rst driver documentation file"
        )
        sysexit_with_message(msg)

def reset(self):
    """Destroy all resources managed by this plugin."""
    pass

(ansible-2.14.18)
`

Customized Collections.yml
`

collections:

  • name: community.kubernetes
    version: '>=1.1.1,<2.0.0'
  • name: community.windows
    version: '>=1.0.0,<2.0.0'
    `

Error:

WARNING Driver kubernetes does not provide a schema.
CRITICAL Failed to validate /home/monitoring/roles/prometheus/molecule/default/molecule.yml

["'kubernetes' is not one of ['azure', 'ec2', 'delegated', 'docker', 'containers', 'openstack', 'podman', 'vagrant', 'digitalocean', 'gce', 'libvirt', 'lxd', 'molecule-', 'molecule_', 'custom-', 'custom_']"]

molecule drivers

╶────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╴
default
kubernetes

molecule --version
molecule 6.0.3 using python 3.9
ansible:2.14.18
default:6.0.3 from molecule
kubernetes:0.7.2.dev2 from molecule_kubernetes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions