diff --git a/controllers/dataplane/openstackdataplanedeployment_controller.go b/controllers/dataplane/openstackdataplanedeployment_controller.go index 0f8234e2a..5a9c4a459 100644 --- a/controllers/dataplane/openstackdataplanedeployment_controller.go +++ b/controllers/dataplane/openstackdataplanedeployment_controller.go @@ -22,6 +22,7 @@ import ( "time" "github.com/go-playground/validator/v10" + batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" k8s_errors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -35,7 +36,6 @@ import ( condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" "github.com/openstack-k8s-operators/lib-common/modules/common/helper" "github.com/openstack-k8s-operators/lib-common/modules/common/util" - ansibleeev1 "github.com/openstack-k8s-operators/openstack-ansibleee-operator/api/v1beta1" dataplanev1 "github.com/openstack-k8s-operators/openstack-operator/apis/dataplane/v1beta1" deployment "github.com/openstack-k8s-operators/openstack-operator/pkg/dataplane" dataplaneutil "github.com/openstack-k8s-operators/openstack-operator/pkg/dataplane/util" @@ -427,6 +427,6 @@ func (r *OpenStackDataPlaneDeploymentReconciler) setHashes( func (r *OpenStackDataPlaneDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&dataplanev1.OpenStackDataPlaneDeployment{}). - Owns(&ansibleeev1.OpenStackAnsibleEE{}). + Owns(&batchv1.Job{}). Complete(r) } diff --git a/controllers/dataplane/openstackdataplanenodeset_controller.go b/controllers/dataplane/openstackdataplanenodeset_controller.go index 8f5cf2bab..98f236ea9 100644 --- a/controllers/dataplane/openstackdataplanenodeset_controller.go +++ b/controllers/dataplane/openstackdataplanenodeset_controller.go @@ -24,6 +24,7 @@ import ( "github.com/go-playground/validator/v10" "golang.org/x/exp/slices" + batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" k8s_errors "k8s.io/apimachinery/pkg/api/errors" @@ -48,7 +49,6 @@ import ( "github.com/openstack-k8s-operators/lib-common/modules/common/secret" "github.com/openstack-k8s-operators/lib-common/modules/common/serviceaccount" "github.com/openstack-k8s-operators/lib-common/modules/common/util" - ansibleeev1 "github.com/openstack-k8s-operators/openstack-ansibleee-operator/api/v1beta1" baremetalv1 "github.com/openstack-k8s-operators/openstack-baremetal-operator/api/v1beta1" openstackv1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1" dataplanev1 "github.com/openstack-k8s-operators/openstack-operator/apis/dataplane/v1beta1" @@ -576,7 +576,8 @@ func checkDeployment(ctx context.Context, helper *helper.Helper, // SetupWithManager sets up the controller with the Manager. func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager( - ctx context.Context, mgr ctrl.Manager) error { + ctx context.Context, mgr ctrl.Manager, +) error { // index for ConfigMaps listed on ansibleVarsFrom if err := mgr.GetFieldIndexer().IndexField(ctx, &dataplanev1.OpenStackDataPlaneNodeSet{}, "spec.ansibleVarsFrom.ansible.configMaps", @@ -629,7 +630,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager( } return ctrl.NewControllerManagedBy(mgr). For(&dataplanev1.OpenStackDataPlaneNodeSet{}). - Owns(&ansibleeev1.OpenStackAnsibleEE{}). + Owns(&batchv1.Job{}). Owns(&baremetalv1.OpenStackBaremetalSet{}). Owns(&infranetworkv1.IPSet{}). Owns(&infranetworkv1.DNSData{}). diff --git a/go.mod b/go.mod index 2a181530e..e05a803c0 100644 --- a/go.mod +++ b/go.mod @@ -130,3 +130,5 @@ replace github.com/openshift/api => github.com/openshift/api v0.0.0-202304141430 // custom RabbitmqClusterSpecCore for OpenStackControlplane (v2.6.0_patches_tag) replace github.com/rabbitmq/cluster-operator/v2 => github.com/openstack-k8s-operators/rabbitmq-cluster-operator/v2 v2.6.1-0.20240719064129-c0201810c8f5 //allow-merging + +replace github.com/openstack-k8s-operators/lib-common/modules/ansible => github.com/bshephar/lib-common/modules/ansible v0.0.0-20240725003125-09533a78d1b6 //allow-merging diff --git a/go.sum b/go.sum index 4e2351e05..365f0fa4b 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +github.com/bshephar/lib-common/modules/ansible v0.0.0-20240725003125-09533a78d1b6 h1:WSujmLriV4qeXhgIoEhISIw/uv6b8/d/2LNKnK5T+Qw= +github.com/bshephar/lib-common/modules/ansible v0.0.0-20240725003125-09533a78d1b6/go.mod h1:0hwGtk2X58sW4RDZdbN4vSILBUNLX4tIC1hVur8xwAo= github.com/cert-manager/cert-manager v1.13.6 h1:yngKM4ZQoyFQ3LGHTx95fWqyiNJP11UM5PWq7pAr3T0= github.com/cert-manager/cert-manager v1.13.6/go.mod h1:iWFePja8XKEl+Dv1kZtwPshT8D0SmC4Hyu5Qc5KS0tM= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= @@ -116,8 +118,6 @@ github.com/openstack-k8s-operators/ironic-operator/api v0.4.1-0.20240710183123-6 github.com/openstack-k8s-operators/ironic-operator/api v0.4.1-0.20240710183123-6fe249a4f5c9/go.mod h1:2UuZV86J1RNXy04nPsGMtk3OPLaZzt68qG2y6ZgKAIg= github.com/openstack-k8s-operators/keystone-operator/api v0.4.1-0.20240722124319-feff284667dc h1:QRqVnx9wukVufEatrY5LkLvOdcWnUQLnC23HcG2fn7U= github.com/openstack-k8s-operators/keystone-operator/api v0.4.1-0.20240722124319-feff284667dc/go.mod h1:ths7iE+y6SBmCM2quoqdhkQuEMtZlWCgLSak0j5zltM= -github.com/openstack-k8s-operators/lib-common/modules/ansible v0.4.1-0.20240722053056-bd202c880706 h1:ThnTuIl4c0CHMyB0uNkTAf+VbCzeVZTPB10i7fZw05E= -github.com/openstack-k8s-operators/lib-common/modules/ansible v0.4.1-0.20240722053056-bd202c880706/go.mod h1:tP+nxk95PisCKJaXE/an2igG9lluxuOVhdmV9WtkR2s= github.com/openstack-k8s-operators/lib-common/modules/certmanager v0.4.1-0.20240722053056-bd202c880706 h1:QTBp/hAEUKslmtW3gFc26SgOf6iy3dYQSon7U6lFqVg= github.com/openstack-k8s-operators/lib-common/modules/certmanager v0.4.1-0.20240722053056-bd202c880706/go.mod h1:ja6CQm/06ZWyTDp0e+VM2kHy0k7jc9qMV252pVqzmQA= github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240722053056-bd202c880706 h1:GHlfRpEk41YZxxszhGpw03b8Te5BdvTUctQTzp43p/o= diff --git a/pkg/dataplane/deployment.go b/pkg/dataplane/deployment.go index 28e237504..dd24b0a45 100644 --- a/pkg/dataplane/deployment.go +++ b/pkg/dataplane/deployment.go @@ -24,6 +24,9 @@ import ( "sort" "strconv" + batchv1 "k8s.io/api/batch/v1" + corev1 "k8s.io/api/core/v1" + slices "golang.org/x/exp/slices" k8s_errors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" @@ -35,11 +38,9 @@ import ( "github.com/openstack-k8s-operators/lib-common/modules/common/helper" "github.com/openstack-k8s-operators/lib-common/modules/common/util" "github.com/openstack-k8s-operators/lib-common/modules/storage" - ansibleeev1 "github.com/openstack-k8s-operators/openstack-ansibleee-operator/api/v1beta1" openstackv1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1" dataplanev1 "github.com/openstack-k8s-operators/openstack-operator/apis/dataplane/v1beta1" dataplaneutil "github.com/openstack-k8s-operators/openstack-operator/pkg/dataplane/util" - corev1 "k8s.io/api/core/v1" ) // Deployer defines a data structure with all of the relevant objects required for a full deployment. @@ -195,7 +196,7 @@ func (d *Deployer) ConditionalDeploy( } if nsConditions.IsFalse(readyCondition) { - var ansibleEE *ansibleeev1.OpenStackAnsibleEE + var ansibleEE *batchv1.Job _, labelSelector := dataplaneutil.GetAnsibleExecutionNameAndLabels(&foundService, d.Deployment.Name, d.NodeSet.Name) ansibleEE, err = dataplaneutil.GetAnsibleExecution(d.Ctx, d.Helper, d.Deployment, labelSelector) if err != nil { @@ -213,15 +214,15 @@ func (d *Deployer) ConditionalDeploy( err.Error())) } - if ansibleEE.Status.JobStatus == ansibleeev1.JobStatusSucceeded { + if ansibleEE.Status.Succeeded != 0 { log.Info(fmt.Sprintf("Condition %s ready", readyCondition)) nsConditions.Set(condition.TrueCondition( readyCondition, readyMessage)) } - if ansibleEE.Status.JobStatus == ansibleeev1.JobStatusRunning || ansibleEE.Status.JobStatus == ansibleeev1.JobStatusPending { - log.Info(fmt.Sprintf("AnsibleEE job is not yet completed: Execution: %s, Status: %s", ansibleEE.Name, ansibleEE.Status.JobStatus)) + if ansibleEE.Status.Succeeded == 0 || ansibleEE.Status.Active != 0 && ansibleEE.Status.Failed != 0 { + log.Info(fmt.Sprintf("AnsibleEE job is not yet completed: Execution: %s, Active pods: %d", ansibleEE.Name, ansibleEE.Status.Active)) nsConditions.Set(condition.FalseCondition( readyCondition, condition.RequestedReason, @@ -229,18 +230,23 @@ func (d *Deployer) ConditionalDeploy( readyWaitingMessage)) } - if ansibleEE.Status.JobStatus == ansibleeev1.JobStatusFailed { - errorMsg := fmt.Sprintf("execution.name %s execution.namespace %s execution.status.jobstatus: %s", ansibleEE.Name, ansibleEE.Namespace, ansibleEE.Status.JobStatus) - ansibleCondition := ansibleEE.Status.Conditions.Get(condition.ReadyCondition) + var ansibleCondition *batchv1.JobCondition + if ansibleEE.Status.Failed != 0 { + errorMsg := fmt.Sprintf("execution.name %s execution.namespace %s failed pods: %d", ansibleEE.Name, ansibleEE.Namespace, ansibleEE.Status.Failed) + for _, condition := range ansibleEE.Status.Conditions { + if condition.Type == batchv1.JobFailed { + ansibleCondition = &condition + } + } if ansibleCondition.Reason == condition.JobReasonBackoffLimitExceeded { - errorMsg = fmt.Sprintf("backoff limit reached for execution.name %s execution.namespace %s execution.status.jobstatus: %s", ansibleEE.Name, ansibleEE.Namespace, ansibleEE.Status.JobStatus) + errorMsg = fmt.Sprintf("backoff limit reached for execution.name %s execution.namespace %s execution.condition.message: %s", ansibleEE.Name, ansibleEE.Namespace, ansibleCondition.Message) } log.Info(fmt.Sprintf("Condition %s error", readyCondition)) err = fmt.Errorf(errorMsg) nsConditions.Set(condition.FalseCondition( readyCondition, - ansibleCondition.Reason, - ansibleCondition.Severity, + condition.Reason(ansibleCondition.Reason), + condition.SeverityError, readyErrorMessage, err.Error())) } diff --git a/pkg/dataplane/service.go b/pkg/dataplane/service.go index f44bb2c95..83e2f370f 100644 --- a/pkg/dataplane/service.go +++ b/pkg/dataplane/service.go @@ -47,7 +47,6 @@ type ServiceYAML struct { // DeployService service deployment func (d *Deployer) DeployService(foundService dataplanev1.OpenStackDataPlaneService) error { err := dataplaneutil.AnsibleExecution( - d.Ctx, d.Helper, d.Deployment, &foundService, @@ -55,7 +54,6 @@ func (d *Deployer) DeployService(foundService dataplanev1.OpenStackDataPlaneServ d.InventorySecrets, d.AeeSpec, d.NodeSet) - if err != nil { d.Helper.GetLogger().Error(err, fmt.Sprintf("Unable to execute Ansible for %s", foundService.Name)) return err @@ -160,8 +158,9 @@ func EnsureServices(ctx context.Context, helper *helper.Helper, instance *datapl // Global Services in multiple NodeSets for a deployment func DedupeServices(ctx context.Context, helper *helper.Helper, nodesets []dataplanev1.OpenStackDataPlaneNodeSet, - serviceOverride []string) (map[string][]string, error) { - var nodeSetServiceMap = make(map[string][]string, 0) + serviceOverride []string, +) (map[string][]string, error) { + nodeSetServiceMap := make(map[string][]string, 0) var globalServices []string if len(serviceOverride) != 0 { services, err := dedupe(ctx, helper, serviceOverride, globalServices) @@ -182,7 +181,8 @@ func DedupeServices(ctx context.Context, helper *helper.Helper, } func dedupe(ctx context.Context, helper *helper.Helper, - services []string, globalServices []string) ([]string, error) { + services []string, globalServices []string, +) ([]string, error) { var dedupedServices []string var nodeSetServiceTypes []string for _, svc := range services { diff --git a/pkg/dataplane/util/ansible_execution.go b/pkg/dataplane/util/ansible_execution.go index f778ed94b..ef22bff5d 100644 --- a/pkg/dataplane/util/ansible_execution.go +++ b/pkg/dataplane/util/ansible_execution.go @@ -24,23 +24,20 @@ import ( "strings" appsv1 "k8s.io/api/apps/v1" + batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" apimachineryvalidation "k8s.io/apimachinery/pkg/util/validation" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + ansible "github.com/openstack-k8s-operators/lib-common/modules/ansible" "github.com/openstack-k8s-operators/lib-common/modules/common/helper" - "github.com/openstack-k8s-operators/lib-common/modules/common/util" "github.com/openstack-k8s-operators/lib-common/modules/storage" - ansibleeev1 "github.com/openstack-k8s-operators/openstack-ansibleee-operator/api/v1beta1" dataplanev1 "github.com/openstack-k8s-operators/openstack-operator/apis/dataplane/v1beta1" ) // AnsibleExecution creates a OpenStackAnsiblEE CR func AnsibleExecution( - ctx context.Context, helper *helper.Helper, deployment *dataplanev1.OpenStackDataPlaneDeployment, service *dataplanev1.OpenStackDataPlaneService, @@ -61,181 +58,165 @@ func AnsibleExecution( ansibleEEMounts := storage.VolMounts{} executionName, labels := GetAnsibleExecutionNameAndLabels(service, deployment.GetName(), nodeSet.GetName()) - ansibleEE, err := GetAnsibleExecution(ctx, helper, deployment, labels) - if err != nil && !k8serrors.IsNotFound(err) { - return err - } - if ansibleEE == nil { - ansibleEE = &ansibleeev1.OpenStackAnsibleEE{ - ObjectMeta: metav1.ObjectMeta{ - Name: executionName, - Namespace: deployment.GetNamespace(), - Labels: labels, - }, - } + // ansibleEE, err := GetAnsibleExecution(ctx, helper, deployment, labels) + //if err != nil && !k8serrors.IsNotFound(err) { + // return err + //} + ansibleEE := ansible.EEJob{ + Name: executionName, + Namespace: deployment.GetNamespace(), + Labels: labels, } - _, err = controllerutil.CreateOrPatch(ctx, helper.GetClient(), ansibleEE, func() error { - ansibleEE.Spec.NetworkAttachments = aeeSpec.NetworkAttachments - if aeeSpec.DNSConfig != nil { - ansibleEE.Spec.DNSConfig = aeeSpec.DNSConfig - } - if len(aeeSpec.OpenStackAnsibleEERunnerImage) > 0 { - ansibleEE.Spec.Image = aeeSpec.OpenStackAnsibleEERunnerImage - } - if len(aeeSpec.ExtraVars) > 0 { - ansibleEE.Spec.ExtraVars = aeeSpec.ExtraVars - } - if len(aeeSpec.AnsibleTags) > 0 { - fmt.Fprintf(&cmdLineArguments, "--tags %s ", aeeSpec.AnsibleTags) - } - if len(aeeSpec.AnsibleLimit) > 0 { - fmt.Fprintf(&cmdLineArguments, "--limit %s ", aeeSpec.AnsibleLimit) - } - if len(aeeSpec.AnsibleSkipTags) > 0 { - fmt.Fprintf(&cmdLineArguments, "--skip-tags %s ", aeeSpec.AnsibleSkipTags) - } - if len(aeeSpec.ServiceAccountName) > 0 { - ansibleEE.Spec.ServiceAccountName = aeeSpec.ServiceAccountName - } - if cmdLineArguments.Len() > 0 { - ansibleEE.Spec.CmdLine = strings.TrimSpace(cmdLineArguments.String()) - } + ansibleEE.NetworkAttachments = aeeSpec.NetworkAttachments + if aeeSpec.DNSConfig != nil { + ansibleEE.DNSConfig = aeeSpec.DNSConfig + } + if len(aeeSpec.OpenStackAnsibleEERunnerImage) > 0 { + ansibleEE.Image = aeeSpec.OpenStackAnsibleEERunnerImage + } + if len(aeeSpec.ExtraVars) > 0 { + ansibleEE.ExtraVars = aeeSpec.ExtraVars + } + if len(aeeSpec.AnsibleTags) > 0 { + fmt.Fprintf(&cmdLineArguments, "--tags %s ", aeeSpec.AnsibleTags) + } + if len(aeeSpec.AnsibleLimit) > 0 { + fmt.Fprintf(&cmdLineArguments, "--limit %s ", aeeSpec.AnsibleLimit) + } + if len(aeeSpec.AnsibleSkipTags) > 0 { + fmt.Fprintf(&cmdLineArguments, "--skip-tags %s ", aeeSpec.AnsibleSkipTags) + } + if len(aeeSpec.ServiceAccountName) > 0 { + ansibleEE.ServiceAccountName = aeeSpec.ServiceAccountName + } + if cmdLineArguments.Len() > 0 { + ansibleEE.CmdLine = strings.TrimSpace(cmdLineArguments.String()) + } - if len(service.Spec.PlaybookContents) > 0 { - ansibleEE.Spec.PlaybookContents = service.Spec.PlaybookContents - } - if len(service.Spec.Playbook) > 0 { - ansibleEE.Spec.Playbook = service.Spec.Playbook - } - ansibleEE.Spec.BackoffLimit = deployment.Spec.BackoffLimit + if len(service.Spec.PlaybookContents) > 0 { + ansibleEE.PlaybookContents = service.Spec.PlaybookContents + } + if len(service.Spec.Playbook) > 0 { + ansibleEE.Playbook = service.Spec.Playbook + } + ansibleEE.BackoffLimit = deployment.Spec.BackoffLimit - // If we have a service that ought to be deployed everywhere - // substitute the existing play target with 'all' - // Check if we have ExtraVars before accessing it - if ansibleEE.Spec.ExtraVars == nil { - ansibleEE.Spec.ExtraVars = make(map[string]json.RawMessage) - } - if service.Spec.DeployOnAllNodeSets { - ansibleEE.Spec.ExtraVars["edpm_override_hosts"] = json.RawMessage([]byte("\"all\"")) - util.LogForObject(helper, fmt.Sprintf("for service %s, substituting existing ansible play host with 'all'.", service.Name), ansibleEE) - } else { - ansibleEE.Spec.ExtraVars["edpm_override_hosts"] = json.RawMessage([]byte(fmt.Sprintf("\"%s\"", nodeSet.GetName()))) - util.LogForObject(helper, - fmt.Sprintf("for service %s, substituting existing ansible play host with '%s'.", service.Name, nodeSet.GetName()), ansibleEE) - } - if service.Spec.EDPMServiceType != "" { - ansibleEE.Spec.ExtraVars["edpm_service_type"] = json.RawMessage([]byte(fmt.Sprintf("\"%s\"", service.Spec.EDPMServiceType))) - } else { - ansibleEE.Spec.ExtraVars["edpm_service_type"] = json.RawMessage([]byte(fmt.Sprintf("\"%s\"", service.Name))) - } + // If we have a service that ought to be deployed everywhere + // substitute the existing play target with 'all' + // Check if we have ExtraVars before accessing it + if ansibleEE.ExtraVars == nil { + ansibleEE.ExtraVars = make(map[string]json.RawMessage) + } + if service.Spec.DeployOnAllNodeSets { + ansibleEE.ExtraVars["edpm_override_hosts"] = json.RawMessage([]byte("\"all\"")) + } else { + ansibleEE.ExtraVars["edpm_override_hosts"] = json.RawMessage([]byte(fmt.Sprintf("\"%s\"", nodeSet.GetName()))) + } + if service.Spec.EDPMServiceType != "" { + ansibleEE.ExtraVars["edpm_service_type"] = json.RawMessage([]byte(fmt.Sprintf("\"%s\"", service.Spec.EDPMServiceType))) + } else { + ansibleEE.ExtraVars["edpm_service_type"] = json.RawMessage([]byte(fmt.Sprintf("\"%s\"", service.Name))) + } - if len(deployment.Spec.ServicesOverride) > 0 { - ansibleEE.Spec.ExtraVars["edpm_services_override"] = json.RawMessage([]byte(fmt.Sprintf("\"%s\"", deployment.Spec.ServicesOverride))) - } + if len(deployment.Spec.ServicesOverride) > 0 { + ansibleEE.ExtraVars["edpm_services_override"] = json.RawMessage([]byte(fmt.Sprintf("\"%s\"", deployment.Spec.ServicesOverride))) + } - // Sort keys of the ssh secret map - sshKeys := make([]string, 0) - for k := range sshKeySecrets { - sshKeys = append(sshKeys, k) - } - sort.Strings(sshKeys) + // Sort keys of the ssh secret map + sshKeys := make([]string, 0) + for k := range sshKeySecrets { + sshKeys = append(sshKeys, k) + } + sort.Strings(sshKeys) - for _, sshKeyNodeName := range sshKeys { - sshKeySecret := sshKeySecrets[sshKeyNodeName] - if service.Spec.DeployOnAllNodeSets { - sshKeyName = fmt.Sprintf("ssh-key-%s", sshKeyNodeName) - sshKeyMountSubPath = fmt.Sprintf("ssh_key_%s", sshKeyNodeName) - sshKeyMountPath = fmt.Sprintf("/runner/env/ssh_key/%s", sshKeyMountSubPath) - } else { - if sshKeyNodeName != nodeSet.GetName() { - continue - } - sshKeyName = "ssh-key" - sshKeyMountSubPath = "ssh_key" - sshKeyMountPath = "/runner/env/ssh_key" + for _, sshKeyNodeName := range sshKeys { + sshKeySecret := sshKeySecrets[sshKeyNodeName] + if service.Spec.DeployOnAllNodeSets { + sshKeyName = fmt.Sprintf("ssh-key-%s", sshKeyNodeName) + sshKeyMountSubPath = fmt.Sprintf("ssh_key_%s", sshKeyNodeName) + sshKeyMountPath = fmt.Sprintf("/runner/env/ssh_key/%s", sshKeyMountSubPath) + } else { + if sshKeyNodeName != nodeSet.GetName() { + continue } - sshKeyVolume := corev1.Volume{ - Name: sshKeyName, - VolumeSource: corev1.VolumeSource{ - Secret: &corev1.SecretVolumeSource{ - SecretName: sshKeySecret, - Items: []corev1.KeyToPath{ - { - Key: "ssh-privatekey", - Path: sshKeyMountSubPath, - }, + sshKeyName = "ssh-key" + sshKeyMountSubPath = "ssh_key" + sshKeyMountPath = "/runner/env/ssh_key" + } + sshKeyVolume := corev1.Volume{ + Name: sshKeyName, + VolumeSource: corev1.VolumeSource{ + Secret: &corev1.SecretVolumeSource{ + SecretName: sshKeySecret, + Items: []corev1.KeyToPath{ + { + Key: "ssh-privatekey", + Path: sshKeyMountSubPath, }, }, }, - } - sshKeyMount := corev1.VolumeMount{ - Name: sshKeyName, - MountPath: sshKeyMountPath, - SubPath: sshKeyMountSubPath, - } - // Mount ssh secrets - ansibleEEMounts.Mounts = append(ansibleEEMounts.Mounts, sshKeyMount) - ansibleEEMounts.Volumes = append(ansibleEEMounts.Volumes, sshKeyVolume) + }, } - - // order the inventory keys otherwise it could lead to changing order and mount order changing - invKeys := make([]string, 0) - for k := range inventorySecrets { - invKeys = append(invKeys, k) + sshKeyMount := corev1.VolumeMount{ + Name: sshKeyName, + MountPath: sshKeyMountPath, + SubPath: sshKeyMountSubPath, } - sort.Strings(invKeys) + // Mount ssh secrets + ansibleEEMounts.Mounts = append(ansibleEEMounts.Mounts, sshKeyMount) + ansibleEEMounts.Volumes = append(ansibleEEMounts.Volumes, sshKeyVolume) + } - // Mounting inventory and secrets - for inventoryIndex, nodeName := range invKeys { - if service.Spec.DeployOnAllNodeSets { - inventoryName = fmt.Sprintf("inventory-%d", inventoryIndex) - inventoryMountPath = fmt.Sprintf("/runner/inventory/%s", inventoryName) - } else { - if nodeName != nodeSet.GetName() { - continue - } - inventoryName = "inventory" - inventoryMountPath = "/runner/inventory/hosts" + // order the inventory keys otherwise it could lead to changing order and mount order changing + invKeys := make([]string, 0) + for k := range inventorySecrets { + invKeys = append(invKeys, k) + } + sort.Strings(invKeys) + + // Mounting inventory and secrets + for inventoryIndex, nodeName := range invKeys { + if service.Spec.DeployOnAllNodeSets { + inventoryName = fmt.Sprintf("inventory-%d", inventoryIndex) + inventoryMountPath = fmt.Sprintf("/runner/inventory/%s", inventoryName) + } else { + if nodeName != nodeSet.GetName() { + continue } + inventoryName = "inventory" + inventoryMountPath = "/runner/inventory/hosts" + } - inventoryVolume = corev1.Volume{ - Name: inventoryName, - VolumeSource: corev1.VolumeSource{ - Secret: &corev1.SecretVolumeSource{ - SecretName: inventorySecrets[nodeName], - Items: []corev1.KeyToPath{ - { - Key: "inventory", - Path: inventoryName, - }, + inventoryVolume = corev1.Volume{ + Name: inventoryName, + VolumeSource: corev1.VolumeSource{ + Secret: &corev1.SecretVolumeSource{ + SecretName: inventorySecrets[nodeName], + Items: []corev1.KeyToPath{ + { + Key: "inventory", + Path: inventoryName, }, }, }, - } - inventoryMount := corev1.VolumeMount{ - Name: inventoryName, - MountPath: inventoryMountPath, - SubPath: inventoryName, - } - // Inventory mount - ansibleEEMounts.Mounts = append(ansibleEEMounts.Mounts, inventoryMount) - ansibleEEMounts.Volumes = append(ansibleEEMounts.Volumes, inventoryVolume) + }, } - - ansibleEE.Spec.ExtraMounts = append(aeeSpec.ExtraMounts, []storage.VolMounts{ansibleEEMounts}...) - ansibleEE.Spec.Env = aeeSpec.Env - - err := controllerutil.SetControllerReference(deployment, ansibleEE, helper.GetScheme()) - if err != nil { - return err + inventoryMount := corev1.VolumeMount{ + Name: inventoryName, + MountPath: inventoryMountPath, + SubPath: inventoryName, } + // Inventory mount + ansibleEEMounts.Mounts = append(ansibleEEMounts.Mounts, inventoryMount) + ansibleEEMounts.Volumes = append(ansibleEEMounts.Volumes, inventoryVolume) + } - return nil - }) + ansibleEE.ExtraMounts = append(aeeSpec.ExtraMounts, []storage.VolMounts{ansibleEEMounts}...) + ansibleEE.Env = aeeSpec.Env + _, err = ansibleEE.JobForOpenStackAnsibleEE(helper) if err != nil { - util.LogErrorForObject(helper, err, fmt.Sprintf("Unable to create AnsibleEE %s", ansibleEE.Name), ansibleEE) return err } @@ -249,9 +230,10 @@ func AnsibleExecution( // "openstackdataplanenodeset": , // If none or more than one is found, return nil and error func GetAnsibleExecution(ctx context.Context, - helper *helper.Helper, obj client.Object, labelSelector map[string]string) (*ansibleeev1.OpenStackAnsibleEE, error) { + helper *helper.Helper, obj client.Object, labelSelector map[string]string, +) (*batchv1.Job, error) { var err error - ansibleEEs := &ansibleeev1.OpenStackAnsibleEEList{} + ansibleEEs := &batchv1.JobList{} listOpts := []client.ListOption{ client.InNamespace(obj.GetNamespace()), @@ -265,7 +247,7 @@ func GetAnsibleExecution(ctx context.Context, return nil, err } - var ansibleEE *ansibleeev1.OpenStackAnsibleEE + var ansibleEE *batchv1.Job if len(ansibleEEs.Items) == 0 { return nil, k8serrors.NewNotFound(appsv1.Resource("OpenStackAnsibleEE"), fmt.Sprintf("with label %s", labelSelector)) } else if len(ansibleEEs.Items) == 1 { @@ -292,7 +274,8 @@ func getAnsibleExecutionNamePrefix(serviceName string) string { // GetAnsibleExecutionNameAndLabels Name and Labels of AnsibleEE func GetAnsibleExecutionNameAndLabels(service *dataplanev1.OpenStackDataPlaneService, deploymentName string, - nodeSetName string) (string, map[string]string) { + nodeSetName string, +) (string, map[string]string) { executionName := fmt.Sprintf("%s-%s", getAnsibleExecutionNamePrefix(service.Name), deploymentName) if !service.Spec.DeployOnAllNodeSets { executionName = fmt.Sprintf("%s-%s", executionName, nodeSetName) diff --git a/pkg/dataplane/util/version.go b/pkg/dataplane/util/version.go index 93e53199b..6a883075b 100644 --- a/pkg/dataplane/util/version.go +++ b/pkg/dataplane/util/version.go @@ -39,7 +39,7 @@ func GetVersion(ctx context.Context, helper *helper.Helper, namespace string) (* return nil, err } if len(versions.Items) > 1 { - errorMsg := "Found multiple OpenStackVersions when at most 1 should exist" + errorMsg := "found multiple OpenStackVersions when at most 1 should exist" err := errors.New(errorMsg) log.Error(err, errorMsg) return nil, err @@ -55,7 +55,6 @@ func GetVersion(ctx context.Context, helper *helper.Helper, namespace string) (* // GetContainerImages - get the container image values considering either the // OpenStackVersion or the defaults func GetContainerImages(version *openstackv1.OpenStackVersion) openstackv1.ContainerImages { - var containerImages openstackv1.ContainerImages // Set the containerImages variable for the container images If there is an