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

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

Open
kiranashav opened this issue Jan 13, 2025 · 3 comments
Open

Comments

@kiranashav
Copy link

kiranashav commented Jan 13, 2025

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

@kiranashav
Copy link
Author

Here's the molecule.yml under the scenario directory which the molecule is failing to validate

cat molecule.yml

dependency:
name: shell
command: bash ${MOLECULE_SCENARIO_DIRECTORY}/dependency.sh
driver:
name: kubernetes
namespace: ${BUILD_KUBERNETES_NAMESPACE:-molecule}
resources:
requests:
cpu: 500m
memory: 1Gi
#shares:

- name: ${BUILD_TAG}-prometheus-default

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

storageClass: managed-nfs-storage

size: 10Mi

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

  • name: ${BUILD_TAG}-prometheus-default-server-r7
    groups:
    • prometheus_server
    • prometheus
    • us
      inherit_from: rhel7
  • name: ${BUILD_TAG}-prometheus-default-client-r7
    groups:
    • client
      inherit_from: rhel7
  • name: ${BUILD_TAG}-prometheus-default-satclient-r7
    groups:
    • client
    • satellite
      inherit_from: rhel7
      prerun: false
      provisioner:
      name: ansible
      inventory:
      group_vars:
      all:
      stage: docker
      artifactory_base_url: https://abc.com/abf/artifactory
      region: us
      prometheus_server_hosts: '{{ groups.prometheus_server|difference(groups.dmz|default(omit))|intersect(groups[region] or groups.us)|list }}'
      prometheus:
      labels:
      - name: region
      value: us
      - name: group
      value: us
      prometheus_grafana_plugins:
      • percona-percona-app
      • grafana-piechart-panel
      • Vonage-Grafana_Status_panel
        grafana_bitbucket_user: ${BITBUCKET_USERNAME}
        grafana_bitbucket_pass: ${BITBUCKET_PASSWORD}
        grafana_repo_url: "https://{{ grafana_bitbucket_user | urlencode }}:{{ grafana_bitbucket_pass | urlencode }}@bitbucket.abc.com/scm/it/abc.git"
        us:
        region: us
        verifier:
        name: testinfra
        (ansible-2.14.18)

@kiranashav
Copy link
Author

cat dependency.sh
#!/bin/bash

build and install current collection and its dependencies

if test -f "../../galaxy.yml"; then
if ls $MOLECULE_EPHEMERAL_DIRECTORY/.tar.gz 1>/dev/null 2>&1; then
echo -e "\e[33m-> Delete existing local collection installation\e[0m"
rm -rf $MOLECULE_EPHEMERAL_DIRECTORY/collections/ansible_collections/$(basename $MOLECULE_EPHEMERAL_DIRECTORY/
.tar.gz | cut -d '-' --output-delimiter '/' -f 1,2)
echo -e "\e[33m-> Delete existing local collection build\e[0m"
rm -f $MOLECULE_EPHEMERAL_DIRECTORY/.tar.gz
fi
echo -e "\e[33m-> Build local collection\e[0m"
if ! ansible-galaxy collection build ../.. --output-path $MOLECULE_EPHEMERAL_DIRECTORY --force; then
echo -e "\e[31m-> ERROR build failed\e[0m"
exit 1
fi
echo -e "\e[33m-> Install local collection and it's dependencies\e[0m"
if ! ansible-galaxy collection install $(ls -1 $MOLECULE_EPHEMERAL_DIRECTORY/
.tar.gz) --force; then
echo -e "\e[31m-> ERROR install failed\e[0m"
exit 1
fi
fi

install scenario role dependencies

if test -f "$MOLECULE_SCENARIO_DIRECTORY/requirements.yml"; then
echo -e "\e[33m-> Install scenario role dependencies\e[0m"
if ! ansible-galaxy role install -r $MOLECULE_SCENARIO_DIRECTORY/requirements.yml; then
echo -e "\e[31m-> ERROR install failed\e[0m"
exit 1
fi
fi

install scenario collection dependencies

if test -f "$MOLECULE_SCENARIO_DIRECTORY/collections.yml"; then
echo -e "\e[33m-> Install scenario collection dependencies\e[0m"
if ! ansible-galaxy collection install -r $MOLECULE_SCENARIO_DIRECTORY/collections.yml; then
echo -e "\e[31m-> ERROR install failed\e[0m"
exit 1
fi
fi
(ansible-2.14.18)

@apatard
Copy link
Member

apatard commented Jan 24, 2025

I'm not sure this place is the best one, as there's no kubernetes plugin. Anyway, several comments:

There may be other issues but these two ones are important. The second one being a blocker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants