Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Change API to allow env from secrets
Browse files Browse the repository at this point in the history
Signed-off-by: Fabricio Aguiar <[email protected]>
  • Loading branch information
fao89 committed Feb 29, 2024
1 parent d53e4f4 commit d481c39
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
26 changes: 23 additions & 3 deletions api/bases/ansibleee.openstack.org_openstackansibleees.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,29 @@ spec:
- name
type: object
type: array
envConfigMapName:
default: openstack-aee-default-env
type: string
envFrom:
items:
properties:
configMapRef:
properties:
name:
type: string
optional:
type: boolean
type: object
x-kubernetes-map-type: atomic
prefix:
type: string
secretRef:
properties:
name:
type: string
optional:
type: boolean
type: object
x-kubernetes-map-type: atomic
type: object
type: array
extraMounts:
items:
properties:
Expand Down
9 changes: 6 additions & 3 deletions api/v1beta1/openstack_ansibleee_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ type OpenStackAnsibleEESpec struct {
// Name is the name of the internal container inside the pod
// +kubebuilder:default:="openstackansibleee"
Name string `json:"name,omitempty"`
// EnvConfigMapName is the name of the k8s config map that contains the ansible env variables
// +kubebuilder:default:="openstack-aee-default-env"
EnvConfigMapName string `json:"envConfigMapName,omitempty"`
// List of sources to populate environment variables in the container.
// The keys defined within a source must be a C_IDENTIFIER. All invalid keys
// will be reported as an event when the container is starting. When a key exists in multiple
// sources, the value associated with the last source will take precedence.
// Values defined by an Env with a duplicate key will take precedence.
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
// Env is a list containing the environment variables to pass to the pod
Env []corev1.EnvVar `json:"env,omitempty"`
// RestartPolicy is the policy applied to the Job on whether it needs to restart the Pod. It can be "OnFailure" or "Never".
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions config/crd/bases/ansibleee.openstack.org_openstackansibleees.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,29 @@ spec:
- name
type: object
type: array
envConfigMapName:
default: openstack-aee-default-env
type: string
envFrom:
items:
properties:
configMapRef:
properties:
name:
type: string
optional:
type: boolean
type: object
x-kubernetes-map-type: atomic
prefix:
type: string
secretRef:
properties:
name:
type: string
optional:
type: boolean
type: object
x-kubernetes-map-type: atomic
type: object
type: array
extraMounts:
items:
properties:
Expand Down
23 changes: 3 additions & 20 deletions controllers/openstack_ansibleee_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"context"

Expand Down Expand Up @@ -184,15 +183,6 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R
return ctrl.Result{}, err
}

configMap := &corev1.ConfigMap{}
err = r.Get(ctx, types.NamespacedName{Name: instance.Spec.EnvConfigMapName, Namespace: instance.Namespace}, configMap)
if err != nil && !errors.IsNotFound(err) {
Log.Error(err, err.Error())
return ctrl.Result{}, err
} else if err == nil {
addEnvFrom(instance, jobDef)
}

ansibleeeJob := job.NewJob(
jobDef,
ansibleeeJobType,
Expand Down Expand Up @@ -344,6 +334,9 @@ func (r *OpenStackAnsibleEEReconciler) jobForOpenStackAnsibleEE(ctx context.Cont
if len(instance.Spec.ServiceAccountName) > 0 {
job.Spec.Template.Spec.ServiceAccountName = instance.Spec.ServiceAccountName
}
if len(instance.Spec.EnvFrom) > 0 {
job.Spec.Template.Spec.Containers[0].EnvFrom = instance.Spec.EnvFrom
}
// Set primary inventory if specified as string
var existingInventoryMounts string = ""
if len(instance.Spec.Inventory) > 0 {
Expand Down Expand Up @@ -452,16 +445,6 @@ func labelsForOpenStackAnsibleEE(name string, labels map[string]string) map[stri
return ls
}

func addEnvFrom(instance *ansibleeev1.OpenStackAnsibleEE, job *batchv1.Job) {
job.Spec.Template.Spec.Containers[0].EnvFrom = []corev1.EnvFromSource{
{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{Name: instance.Spec.EnvConfigMapName},
},
},
}
}

func addMounts(instance *ansibleeev1.OpenStackAnsibleEE, job *batchv1.Job) {
var volumeMounts []corev1.VolumeMount
var volumes []corev1.Volume
Expand Down
6 changes: 3 additions & 3 deletions docs/assemblies/openstack_ansibleee.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ OpenStackAnsibleEESpec defines the desired state of OpenStackAnsibleEE
| string
| false

| envConfigMapName
| EnvConfigMapName is the name of the k8s config map that contains the ansible env variables
| string
| envFrom
| List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence.
| []corev1.EnvFromSource
| false

| env
Expand Down

0 comments on commit d481c39

Please sign in to comment.