Skip to main content

Molecule aids in the development and testing of Ansible roles

Project description

PyPI Package https://github.com/jseguillon/molecule-kubevirt/workflows/tox/badge.svg Python Black Code Style Repository License

Molecule KubeVirt Plugin is designed to allow use of KubeVirt containers for provisioning test resources.

Scope

Molecule-kubevirt enables running ansible roles tests in a Kubernetes cluster.

Usage

To use this plugin, you’ll need to set the driver and platform variables in your molecule.yml:

driver:
  name: kubevirt
platforms:
  - name: instance
    image: quay.io/kubevirt/fedora-cloud-container-disk-demo

Installation

Driver

This driver supports Ansible 2, 3 and 4.

# Ansible >2
python3 -m pip install molecule-kubevirt

# Ansible 2
python3 -m pip install molecule-kubevirt 'openshift<0.12.0' 'kubernetes<12.0'

KubeVirt Installation

Follow KubeVirt guides for kind, minkube, or cloud providers

SSH access

By default, the driver connects onto ssh via VirtualMachineInstance Pod ip and molecule needs to be able to ssh directly to Pod ip:

  • if running local Kubernetes with kind:

IP=$(docker container inspect kind-control-plane --format '{{ .NetworkSettings.Networks.kind.IPAddress }}')
sudo ip route add 10.244.0.0/16 via $IP # Linux
# sudo route -n add 10.244.0.0/16 $IP # MacOSX
  • if running local Kubernetes with minikube:

sudo ip route add 172.17.0.0/16 via $(minikube ip)
# sudo route -n add 172.17.0.0/16 $(minikube ip) # MacOSX
  • if running molecule inside the target Kubernetes cluster, routing is ensured by CNI.

A Kubernetes Service can de created by the driver for SSH access. Current supported Services are ClusterIP and NodePort.

NodePort

NodePort can be set. Static nodePort can be defined, also host target for port can be set:

ssh_service:
  type: NodePort
  # optional static port
  nodePort: 32569
  # host where nodePort can be reached
  nodePort_host: localhost

ClusterIP

Default SSH Service is ClusterIP and a static clusterIP can be set:

ssh_service:
  type: ClusterIP
  clusterIP: 10.96.102.231

Molecule then needs to be able to ssh on the ClusterIP ip:

  • if running local Kubernetes with Kind:

IP=$(docker container inspect kind-control-plane   --format '{{ .NetworkSettings.Networks.kind.IPAddress }}')
sudo ip route add 10.96.0.0/12 via $IP # Linux
# sudo route -n add 10.96.0.0/12 $IP # MacOSX
  • if running local Kubernetes with Minikube, no known solution yet.

  • if running molecule inside the target Kubernetes cluster, routing is ensured by CNI.

Virtual machines customisation

A few defaults are created if not provided in platfom definition:

  • if no interface with name: default is defined in domain.devices.interfaces, then a default one is created with brige: {} and bus: virtio,

  • if no disk with name: boot is defined in domain.devices.disks, then a default one is created with bus: virtio,

  • if no network with name: default is defined in networks, then a default one is created with pod: {} and model: virtio,

  • if no volume with name: boot is defined in volumes, then a default one is created as:

    • a containerDisk

    • with image, path and imagePullPolicy respectively set to plaform image, image_path and image_pull_policy

  • if cloud-config is defined in user_data it is merged default one wich sets ssh public key for ‘molecule’ user.

Customisation example

This example configuration demonstrates how to:

  • use Kubevirt’s CDI in place of an image using dataVolumeTemplates and overriding default boot volume.

  • set customs ressources and annotation

  • and a second interface/network

  • adds a second disk/volume

  • make use of cloud-config to format and mount additional disk

---
dependency:
  name: galaxy
driver:
  name: kubevirt
platforms:
  - name: instance
    # annotate for calico static ip
    annotations:
      cni.projectcalico.org/ipAddrs: "[\"10.244.25.25\"]"
    # use data volume facility in place of using 'image:'
    dataVolumeTemplates:
      - metadata:
          name: disk-dv
        spec:
          pvc:
            accessModes:
            - ReadWriteOnce
            resources:
              requests:
                storage: 10Gi
          preallocation: true
          source:
            http:
              url: https://download.fedoraproject.org/pub/fedora/linux/releases/35/Cloud/x86_64/images/Fedora-Cloud-Base-35-1.2.x86_64.raw.xz
    domain:
      resources:
        limits:
          cpu: "1"
          memory: 3Gi
        requests:
          cpu: 200m
          memory: 1Gi
      devices:
        interfaces:
          # add a second device interface
          - bridge: {}
            name: multus
            model: virtio
            ports:
              - port: 22
        disks:
          # add a second device disk
          - name: emptydisk
            disk:
              bus: virtio
    volumes:
        # override default 'boot' volume with cdi data volume template source
      - name: boot
        dataVolume:
          name: disk-dv
      # add a second volume, must be same name as defined in device
      - name: emptydisk
        emptyDisk:
          capacity: 2Gi
    networks:
      # add a second network for added device interface
      - name: multus
        multus:
          # use a NetworkAttachement
          networkName: macvlan-conf
    # cloud-config format and mount additional disk
    user_data:
      # format additional disk
      fs_setup:
        - label: data_disk
          filesystem: 'ext4'
          device: /dev/vdb
          overwrite: true
      # mount additional disk
      mounts:
        - [ /dev/vdb, /var/lib/software, "auto", "defaults,nofail", "0", "0" ]

See molecule/tests/molecule.yml from source code for full example.

Run from inside Kubernetes cluster

You can run this driver with a container running tox and/or molecule. Take a look at:

Demo

Testing nginx ansible role with KubeVirt, via github actions: jseguillon/ansible-role-nginx

Get Involved

License

The MIT License.

The logo is licensed under the Creative Commons NoDerivatives 4.0 License.

If you have some other use in mind, contact us.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

molecule-kubevirt-0.0.6.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

molecule_kubevirt-0.0.6-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file molecule-kubevirt-0.0.6.tar.gz.

File metadata

  • Download URL: molecule-kubevirt-0.0.6.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for molecule-kubevirt-0.0.6.tar.gz
Algorithm Hash digest
SHA256 947e2ea0f875480638d345854d037cd78661e830ce7740ed220d0691cb946ad6
MD5 1ec1b0387ed0cfeaeada724368678732
BLAKE2b-256 8109179f430d672df4e9e8dae09e1a3ee2b33acc086239db0cd4d0f3f2f46993

See more details on using hashes here.

File details

Details for the file molecule_kubevirt-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for molecule_kubevirt-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 df6b961f75eb7d7441270e88a9fc9afbc6a1e487924572b4cf12e91bfcb8e908
MD5 b5dd22696390c3540bfee55bae701525
BLAKE2b-256 9f4a7979014ef8aff9db5d72ff44fd59228d5a4a253df8d852a68fc1ed213c9d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page