Skip to content

Commit 3fb4708

Browse files
Merge pull request #1227 from fao89/OSPRH-12358
Support Roles on OpenStackDataPlaneService
2 parents 800f323 + e0b4391 commit 3fb4708

11 files changed

Lines changed: 68 additions & 26 deletions

File tree

.github/workflows/docs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
with:
2727
# this fetches all branches. Needed because we need gh-pages branch for deploy to work
2828
fetch-depth: 0
29-
- uses: ruby/setup-ruby@v1.160.0
29+
- uses: ruby/setup-ruby@v1
3030
with:
3131
ruby-version: '3.2'
3232

apis/bases/dataplane.openstack.org_openstackdataplaneservices.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ spec:
7878
type: string
7979
playbookContents:
8080
type: string
81+
role:
82+
type: string
8183
tlsCerts:
8284
additionalProperties:
8385
properties:

apis/dataplane/v1beta1/openstackdataplaneservice_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ type OpenStackDataPlaneServiceSpec struct {
7272
// Playbook is a path to the playbook that ansible will run on this execution
7373
Playbook string `json:"playbook,omitempty"`
7474

75+
// Role is a path to the role that ansible will run on this execution
76+
Role string `json:"role,omitempty"`
77+
7578
// CACerts - Secret containing the CA certificate chain
7679
// +kubebuilder:validation:Optional
7780
// +kubebuilder:validation:MaxLength:=253

apis/dataplane/v1beta1/openstackdataplaneservice_webhook.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,31 @@ func (r *OpenStackDataPlaneService) ValidateCreate() (admission.Warnings, error)
7878
return nil, nil
7979
}
8080

81-
func (r *OpenStackDataPlaneServiceSpec) ValidateCreate() field.ErrorList {
82-
// TODO(user): fill in your validation logic upon object creation.
81+
func (r *OpenStackDataPlaneServiceSpec) ValidateArtifact() field.ErrorList {
82+
if len(r.Playbook) == len(r.PlaybookContents) && len(r.Playbook) == len(r.Role) && len(r.Playbook) == 0 {
83+
return field.ErrorList{
84+
field.Invalid(
85+
field.NewPath("Playbook"),
86+
r.Playbook, "Playbook, PlaybookContents and Role cannot be empty at the same time",
87+
),
88+
field.Invalid(
89+
field.NewPath("PlaybookContents"),
90+
r.Playbook, "Playbook, PlaybookContents and Role cannot be empty at the same time",
91+
),
92+
field.Invalid(
93+
field.NewPath("Role"),
94+
r.Playbook, "Playbook, PlaybookContents and Role cannot be empty at the same time",
95+
),
96+
}
97+
}
8398

8499
return field.ErrorList{}
85100
}
86101

102+
func (r *OpenStackDataPlaneServiceSpec) ValidateCreate() field.ErrorList {
103+
return r.ValidateArtifact()
104+
}
105+
87106
func (r *OpenStackDataPlaneService) ValidateUpdate(original runtime.Object) (admission.Warnings, error) {
88107
openstackdataplaneservicelog.Info("validate update", "name", r.Name)
89108
errors := r.Spec.ValidateUpdate()
@@ -100,9 +119,7 @@ func (r *OpenStackDataPlaneService) ValidateUpdate(original runtime.Object) (adm
100119
}
101120

102121
func (r *OpenStackDataPlaneServiceSpec) ValidateUpdate() field.ErrorList {
103-
// TODO(user): fill in your validation logic upon object creation.
104-
105-
return field.ErrorList{}
122+
return r.ValidateArtifact()
106123
}
107124

108125
func (r *OpenStackDataPlaneService) ValidateDelete() (admission.Warnings, error) {

config/crd/bases/dataplane.openstack.org_openstackdataplaneservices.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ spec:
7878
type: string
7979
playbookContents:
8080
type: string
81+
role:
82+
type: string
8183
tlsCerts:
8284
additionalProperties:
8385
properties:

pkg/dataplane/util/ansible_execution.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ func (a *EEJob) BuildAeeJobSpec(
218218
if len(service.Spec.Playbook) > 0 {
219219
a.Playbook = service.Spec.Playbook
220220
}
221+
if len(service.Spec.Role) > 0 {
222+
a.Role = service.Spec.Role
223+
}
221224

222225
a.BackoffLimit = deployment.Spec.BackoffLimit
223226
a.PreserveJobs = deployment.Spec.PreserveJobs

pkg/dataplane/util/ansibleee.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type EEJob struct {
2222
PlaybookContents string `json:"playbookContents,omitempty"`
2323
// Playbook is the playbook that ansible will run on this execution, accepts path or FQN from collection
2424
Playbook string `json:"playbook,omitempty"`
25+
// Role is the role that ansible will run on this execution, accepts path or FQN from collection
26+
Role string `json:"role,omitempty"`
2527
// Image is the container image that will execute the ansible command
2628
Image string `json:"image,omitempty"`
2729
// Name is the name of the execution job
@@ -78,12 +80,20 @@ func (a *EEJob) JobForOpenStackAnsibleEE(h *helper.Helper) (*batchv1.Job, error)
7880

7981
args := a.Args
8082

81-
playbook := a.Playbook
8283
if len(args) == 0 {
83-
if len(playbook) == 0 {
84-
playbook = CustomPlaybook
84+
artifact := a.Playbook
85+
param := "-p"
86+
if len(artifact) == 0 {
87+
if len(a.PlaybookContents) > 0 {
88+
artifact = CustomPlaybook
89+
} else if len(a.Role) > 0 {
90+
artifact = a.Role
91+
param = "-r"
92+
} else {
93+
return nil, fmt.Errorf("no playbook, playbookContents or role specified")
94+
}
8595
}
86-
args = []string{"ansible-runner", "run", "/runner", "-p", playbook}
96+
args = []string{"ansible-runner", "run", "/runner", param, artifact}
8797
}
8898

8999
// ansible runner identifier
@@ -169,12 +179,15 @@ func (a *EEJob) JobForOpenStackAnsibleEE(h *helper.Helper) (*batchv1.Job, error)
169179
}
170180
}
171181

182+
if len(a.Role) > 0 {
183+
setRunnerEnvVar(h, "RUNNER_ROLE", a.Role, "role", job, hashes)
184+
}
172185
if len(a.PlaybookContents) > 0 {
173186
setRunnerEnvVar(h, "RUNNER_PLAYBOOK", a.PlaybookContents, "playbookContents", job, hashes)
174-
} else if len(playbook) > 0 {
187+
} else if len(a.Playbook) > 0 {
175188
// As we set "playbook.yaml" as default
176189
// we need to ensure that PlaybookContents is empty before adding playbook
177-
setRunnerEnvVar(h, "RUNNER_PLAYBOOK", playbook, "playbooks", job, hashes)
190+
setRunnerEnvVar(h, "RUNNER_PLAYBOOK", a.Playbook, "playbooks", job, hashes)
178191
}
179192

180193
if len(a.CmdLine) > 0 {

tests/functional/dataplane/base_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ func CreateDataplaneServicesWithSameServiceType(name types.NamespacedName) {
105105

106106
// Create an OpenStackDataPlaneService with a given NamespacedName, and a given unstructured spec
107107
func CreateDataPlaneServiceFromSpec(name types.NamespacedName, spec map[string]interface{}) *unstructured.Unstructured {
108+
if spec["playbook"] == nil && spec["playbookContents"] == nil && spec["role"] == nil {
109+
spec["playbook"] = "test"
110+
}
108111
raw := map[string]interface{}{
109112

110113
"apiVersion": "dataplane.openstack.org/v1beta1",
@@ -514,6 +517,9 @@ func DefaultDataplaneService(name types.NamespacedName) map[string]interface{} {
514517
"metadata": map[string]interface{}{
515518
"name": name.Name,
516519
"namespace": name.Namespace,
520+
},
521+
"spec": map[string]interface{}{
522+
"playbook": "test",
517523
}}
518524
}
519525

@@ -531,6 +537,7 @@ func DefaultDataplaneGlobalService(name types.NamespacedName) map[string]interfa
531537
},
532538
"spec": map[string]interface{}{
533539
"deployOnAllNodeSets": true,
540+
"playbook": "test",
534541
},
535542
}
536543
}

tests/functional/dataplane/openstackdataplaneservice_controller_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ var _ = Describe("OpenstackDataplaneService Test", func() {
4242
It("spec fields are set up", func() {
4343
service := GetService(dataplaneServiceName)
4444
Expect(service.Spec.DataSources).To(BeEmpty())
45-
Expect(service.Spec.Playbook).To(BeEmpty())
45+
Expect(service.Spec.PlaybookContents).To(BeEmpty())
46+
Expect(service.Spec.Role).To(BeEmpty())
4647
Expect(service.Spec.DeployOnAllNodeSets).To(BeFalse())
4748
})
4849
})
@@ -57,7 +58,8 @@ var _ = Describe("OpenstackDataplaneService Test", func() {
5758
It("spec fields are set up", func() {
5859
service := GetService(dataplaneServiceName)
5960
Expect(service.Spec.DataSources).To(BeEmpty())
60-
Expect(service.Spec.Playbook).To(BeEmpty())
61+
Expect(service.Spec.PlaybookContents).To(BeEmpty())
62+
Expect(service.Spec.Role).To(BeEmpty())
6163
Expect(service.Spec.DeployOnAllNodeSets).To(BeTrue())
6264
})
6365
})

tests/kuttl/tests/dataplane-service-custom-image/00-assert.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ spec:
9595
- ansible-runner
9696
- run
9797
- /runner
98-
- -p
99-
- playbook.yaml
98+
- -r
99+
- test role
100100
- -i
101101
- custom-img-svc-edpm-compute-no-nodes-edpm-no-nodes-custom-svc
102102
env:
103-
- name: RUNNER_PLAYBOOK
103+
- name: RUNNER_ROLE
104104
value: |2+
105105
106-
playbook.yaml
106+
test role
107107
108108
- name: RUNNER_EXTRA_VARS
109109
value: |2+

0 commit comments

Comments
 (0)