From 0006c011e204318d75fe53d22040dccb3d2e4973 Mon Sep 17 00:00:00 2001 From: Fabricio Aguiar Date: Thu, 2 Jan 2025 13:48:49 -0300 Subject: [PATCH] Support Roles on OpenStackDataPlaneService Additionally to playbooks, it enables running roles directly with ansible-runner closes OSPRH-12358 Signed-off-by: Fabricio Aguiar --- ...nstack.org_openstackdataplaneservices.yaml | 2 ++ .../openstackdataplaneservice_types.go | 3 +++ ...nstack.org_openstackdataplaneservices.yaml | 2 ++ pkg/dataplane/util/ansible_execution.go | 3 +++ pkg/dataplane/util/ansibleee.go | 19 +++++++++++++------ .../00-assert.yaml | 9 ++------- .../00-dataplane-create.yaml | 9 +-------- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/apis/bases/dataplane.openstack.org_openstackdataplaneservices.yaml b/apis/bases/dataplane.openstack.org_openstackdataplaneservices.yaml index 79c24c635..d61c82967 100644 --- a/apis/bases/dataplane.openstack.org_openstackdataplaneservices.yaml +++ b/apis/bases/dataplane.openstack.org_openstackdataplaneservices.yaml @@ -78,6 +78,8 @@ spec: type: string playbookContents: type: string + role: + type: string tlsCerts: additionalProperties: properties: diff --git a/apis/dataplane/v1beta1/openstackdataplaneservice_types.go b/apis/dataplane/v1beta1/openstackdataplaneservice_types.go index 475446b37..8c90a9776 100644 --- a/apis/dataplane/v1beta1/openstackdataplaneservice_types.go +++ b/apis/dataplane/v1beta1/openstackdataplaneservice_types.go @@ -72,6 +72,9 @@ type OpenStackDataPlaneServiceSpec struct { // Playbook is a path to the playbook that ansible will run on this execution Playbook string `json:"playbook,omitempty"` + // Role is a path to the role that ansible will run on this execution + Role string `json:"role,omitempty"` + // CACerts - Secret containing the CA certificate chain // +kubebuilder:validation:Optional // +kubebuilder:validation:MaxLength:=253 diff --git a/config/crd/bases/dataplane.openstack.org_openstackdataplaneservices.yaml b/config/crd/bases/dataplane.openstack.org_openstackdataplaneservices.yaml index 79c24c635..d61c82967 100644 --- a/config/crd/bases/dataplane.openstack.org_openstackdataplaneservices.yaml +++ b/config/crd/bases/dataplane.openstack.org_openstackdataplaneservices.yaml @@ -78,6 +78,8 @@ spec: type: string playbookContents: type: string + role: + type: string tlsCerts: additionalProperties: properties: diff --git a/pkg/dataplane/util/ansible_execution.go b/pkg/dataplane/util/ansible_execution.go index e86b6d6d7..fb79174c8 100644 --- a/pkg/dataplane/util/ansible_execution.go +++ b/pkg/dataplane/util/ansible_execution.go @@ -218,6 +218,9 @@ func (a *EEJob) BuildAeeJobSpec( if len(service.Spec.Playbook) > 0 { a.Playbook = service.Spec.Playbook } + if len(service.Spec.Role) > 0 { + a.Role = service.Spec.Role + } a.BackoffLimit = deployment.Spec.BackoffLimit a.PreserveJobs = deployment.Spec.PreserveJobs diff --git a/pkg/dataplane/util/ansibleee.go b/pkg/dataplane/util/ansibleee.go index 0dff7b072..669a807bf 100644 --- a/pkg/dataplane/util/ansibleee.go +++ b/pkg/dataplane/util/ansibleee.go @@ -22,6 +22,8 @@ type EEJob struct { PlaybookContents string `json:"playbookContents,omitempty"` // Playbook is the playbook that ansible will run on this execution, accepts path or FQN from collection Playbook string `json:"playbook,omitempty"` + // Role is the role that ansible will run on this execution, accepts path or FQN from collection + Role string `json:"role,omitempty"` // Image is the container image that will execute the ansible command Image string `json:"image,omitempty"` // Name is the name of the execution job @@ -78,12 +80,17 @@ func (a *EEJob) JobForOpenStackAnsibleEE(h *helper.Helper) (*batchv1.Job, error) args := a.Args - playbook := a.Playbook + artifact := a.Playbook + param := "-p" if len(args) == 0 { - if len(playbook) == 0 { - playbook = CustomPlaybook + if len(a.PlaybookContents) > 0 { + artifact = CustomPlaybook } - args = []string{"ansible-runner", "run", "/runner", "-p", playbook} + if len(a.Role) > 0 { + artifact = a.Role + param = "-r" + } + args = []string{"ansible-runner", "run", "/runner", param, artifact} } // ansible runner identifier @@ -171,10 +178,10 @@ func (a *EEJob) JobForOpenStackAnsibleEE(h *helper.Helper) (*batchv1.Job, error) if len(a.PlaybookContents) > 0 { setRunnerEnvVar(h, "RUNNER_PLAYBOOK", a.PlaybookContents, "playbookContents", job, hashes) - } else if len(playbook) > 0 { + } else if len(a.Playbook) > 0 { // As we set "playbook.yaml" as default // we need to ensure that PlaybookContents is empty before adding playbook - setRunnerEnvVar(h, "RUNNER_PLAYBOOK", playbook, "playbooks", job, hashes) + setRunnerEnvVar(h, "RUNNER_PLAYBOOK", a.Playbook, "playbooks", job, hashes) } if len(a.CmdLine) > 0 { diff --git a/tests/kuttl/tests/dataplane-service-custom-image/00-assert.yaml b/tests/kuttl/tests/dataplane-service-custom-image/00-assert.yaml index ebce91149..6102c0b80 100644 --- a/tests/kuttl/tests/dataplane-service-custom-image/00-assert.yaml +++ b/tests/kuttl/tests/dataplane-service-custom-image/00-assert.yaml @@ -95,16 +95,11 @@ spec: - ansible-runner - run - /runner - - -p - - playbook.yaml + - -r + - test role - -i - custom-img-svc-edpm-compute-no-nodes-edpm-no-nodes-custom-svc env: - - name: RUNNER_PLAYBOOK - value: |2+ - - playbook.yaml - - name: RUNNER_EXTRA_VARS value: |2+ diff --git a/tests/kuttl/tests/dataplane-service-custom-image/00-dataplane-create.yaml b/tests/kuttl/tests/dataplane-service-custom-image/00-dataplane-create.yaml index 358f0c4c9..6b5630fc1 100644 --- a/tests/kuttl/tests/dataplane-service-custom-image/00-dataplane-create.yaml +++ b/tests/kuttl/tests/dataplane-service-custom-image/00-dataplane-create.yaml @@ -4,14 +4,7 @@ metadata: name: custom-img-svc spec: openStackAnsibleEERunnerImage: example.com/repo/runner-image:latest - role: - name: "test role" - hosts: "all" - strategy: "linear" - tasks: - - name: "test task" - import_role: - name: "test role" + role: "test role" --- apiVersion: dataplane.openstack.org/v1beta1 kind: OpenStackDataPlaneNodeSet