Skip to content

Commit 6778a4d

Browse files
Merge pull request #341 from kstrenkova/fix-ansible-workload-key
Refactor AnsibleTest code
2 parents 4e5ccbc + 1cc44ce commit 6778a4d

File tree

3 files changed

+17
-30
lines changed

3 files changed

+17
-30
lines changed

controllers/ansibletest_controller.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ func (r *AnsibleTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
207207
// Create a new pod
208208
mountCerts := r.CheckSecretExists(ctx, instance, "combined-ca-bundle")
209209
podName := r.GetPodName(instance, nextWorkflowStep)
210-
envVars, workflowOverrideParams := r.PrepareAnsibleEnv(instance)
210+
envVars := r.PrepareAnsibleEnv(instance)
211211
logsPVCName := r.GetPVCLogsName(instance, 0)
212-
containerImage, err := r.GetContainerImage(ctx, workflowOverrideParams["ContainerImage"], instance)
212+
containerImage, err := r.GetContainerImage(ctx, instance.Spec.ContainerImage, instance)
213213
if err != nil {
214214
return ctrl.Result{}, err
215215
}
@@ -221,7 +221,6 @@ func (r *AnsibleTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
221221
logsPVCName,
222222
mountCerts,
223223
envVars,
224-
workflowOverrideParams,
225224
nextWorkflowStep,
226225
containerImage,
227226
)
@@ -268,23 +267,12 @@ func (r *AnsibleTestReconciler) SetupWithManager(mgr ctrl.Manager) error {
268267
// PrepareAnsibleEnv prepares environment variables for a single workflow step
269268
func (r *AnsibleTestReconciler) PrepareAnsibleEnv(
270269
instance *testv1beta1.AnsibleTest,
271-
) (map[string]env.Setter, map[string]string) {
270+
) map[string]env.Setter {
272271
// Prepare env vars
273272
envVars := make(map[string]env.Setter)
274-
workflowOverrideParams := make(map[string]string)
275-
276-
// volumes workflow override
277-
workflowOverrideParams["WorkloadSSHKeySecretName"] = instance.Spec.WorkloadSSHKeySecretName
278-
workflowOverrideParams["ComputeSSHKeySecretName"] = instance.Spec.ComputeSSHKeySecretName
279-
workflowOverrideParams["ContainerImage"] = instance.Spec.ContainerImage
280273

281274
// bool
282-
debug := instance.Spec.Debug
283-
if debug {
284-
envVars["POD_DEBUG"] = env.SetValue("true")
285-
} else {
286-
envVars["POD_DEBUG"] = env.SetValue("false")
287-
}
275+
envVars["POD_DEBUG"] = env.SetValue(strconv.FormatBool(instance.Spec.Debug))
288276

289277
// strings
290278
envVars["POD_ANSIBLE_EXTRA_VARS"] = env.SetValue(instance.Spec.AnsibleExtraVars)
@@ -294,5 +282,5 @@ func (r *AnsibleTestReconciler) PrepareAnsibleEnv(
294282
envVars["POD_ANSIBLE_PLAYBOOK"] = env.SetValue(instance.Spec.AnsiblePlaybookPath)
295283
envVars["POD_INSTALL_COLLECTIONS"] = env.SetValue(instance.Spec.AnsibleCollections)
296284

297-
return envVars, workflowOverrideParams
285+
return envVars
298286
}

pkg/ansibletest/pod.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func Pod(
1717
logsPVCName string,
1818
mountCerts bool,
1919
envVars map[string]env.Setter,
20-
workflowOverrideParams map[string]string,
2120
externalWorkflowCounter int,
2221
containerImage string,
2322
) *corev1.Pod {
@@ -60,7 +59,6 @@ func Pod(
6059
logsPVCName,
6160
mountCerts,
6261
AnsibleTestPropagation,
63-
workflowOverrideParams,
6462
externalWorkflowCounter,
6563
),
6664
},

pkg/ansibletest/volumes.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func GetVolumes(
1313
logsPVCName string,
1414
mountCerts bool,
1515
svc []storage.PropagationType,
16-
workflowOverrideParams map[string]string,
1716
externalWorkflowCounter int,
1817
) []corev1.Volume {
1918

@@ -85,20 +84,20 @@ func GetVolumes(
8584
Name: "compute-ssh-secret",
8685
VolumeSource: corev1.VolumeSource{
8786
Secret: &corev1.SecretVolumeSource{
88-
SecretName: workflowOverrideParams["ComputeSSHKeySecretName"],
87+
SecretName: instance.Spec.ComputeSSHKeySecretName,
8988
DefaultMode: &privateKeyMode,
9089
},
9190
},
9291
}
9392

9493
volumes = append(volumes, keysVolume)
9594

96-
if workflowOverrideParams["WorkloadSSHKeySecretName"] != "" {
95+
if instance.Spec.WorkloadSSHKeySecretName != "" {
9796
keysVolume = corev1.Volume{
9897
Name: "workload-ssh-secret",
9998
VolumeSource: corev1.VolumeSource{
10099
Secret: &corev1.SecretVolumeSource{
101-
SecretName: workflowOverrideParams["WorkloadSSHKeySecretName"],
100+
SecretName: instance.Spec.WorkloadSSHKeySecretName,
102101
DefaultMode: &privateKeyMode,
103102
},
104103
},
@@ -219,14 +218,16 @@ func GetVolumeMounts(
219218
volumeMounts = append(volumeMounts, caCertVolumeMount)
220219
}
221220

222-
workloadSSHKeyMount := corev1.VolumeMount{
223-
Name: "workload-ssh-secret",
224-
MountPath: "/var/lib/ansible/test_keypair.key",
225-
SubPath: "ssh-privatekey",
226-
ReadOnly: true,
227-
}
221+
if instance.Spec.WorkloadSSHKeySecretName != "" {
222+
workloadSSHKeyMount := corev1.VolumeMount{
223+
Name: "workload-ssh-secret",
224+
MountPath: "/var/lib/ansible/test_keypair.key",
225+
SubPath: "ssh-privatekey",
226+
ReadOnly: true,
227+
}
228228

229-
volumeMounts = append(volumeMounts, workloadSSHKeyMount)
229+
volumeMounts = append(volumeMounts, workloadSSHKeyMount)
230+
}
230231

231232
computeSSHKeyMount := corev1.VolumeMount{
232233
Name: "compute-ssh-secret",

0 commit comments

Comments
 (0)