Skip to content

Commit

Permalink
Provide env_vars fields to add custom env vars
Browse files Browse the repository at this point in the history
closes: #1161
  • Loading branch information
git-hyagi committed Dec 8, 2023
1 parent 324e3bd commit 3780ab5
Show file tree
Hide file tree
Showing 14 changed files with 2,591 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES/1161.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `env_vars` field to define custom environment variables.
15 changes: 15 additions & 0 deletions apis/repo-manager.pulpproject.org/v1beta2/pulp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ type Api struct {

// InitContainer defines configuration of the init-containers that run in pulpcore pods
InitContainer PulpContainer `json:"init_container,omitempty"`

// Environment variables to add to pulpcore-api container
EnvVars []corev1.EnvVar `json:"env_vars,omitempty"`
}

// Content defines desired state of pulpcore-content resources
Expand Down Expand Up @@ -538,6 +541,9 @@ type Content struct {

// InitContainer defines configuration of the init-containers that run in pulpcore pods
InitContainer PulpContainer `json:"init_container,omitempty"`

// Environment variables to add to pulpcore-content container
EnvVars []corev1.EnvVar `json:"env_vars,omitempty"`
}

// Worker defines desired state of pulpcore-worker resources
Expand Down Expand Up @@ -599,6 +605,9 @@ type Worker struct {

// InitContainer defines configuration of the init-containers that run in pulpcore pods
InitContainer PulpContainer `json:"init_container,omitempty"`

// Environment variables to add to pulpcore-worker container
EnvVars []corev1.EnvVar `json:"env_vars,omitempty"`
}

// Web defines desired state of pulpcore-web (reverse-proxy) resources
Expand Down Expand Up @@ -654,6 +663,9 @@ type Web struct {
// +kubebuilder:validation:Enum:=edge;Edge;passthrough;Passthrough
// +operator-sdk:csv:customresourcedefinitions:type=spec,xDescriptors={"urn:alm:descriptor:com.tectonic.ui:advanced"}
TLSTerminationMechanism string `json:"tls_termination_mechanism,omitempty"`

// Environment variables to add to pulpcore-web container
EnvVars []corev1.EnvVar `json:"env_vars,omitempty"`
}

// Database defines desired state of postgres
Expand Down Expand Up @@ -880,6 +892,9 @@ type PulpContainer struct {
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,xDescriptors={"urn:alm:descriptor:com.tectonic.ui:resourceRequirements","urn:alm:descriptor:com.tectonic.ui:advanced"}
ResourceRequirements corev1.ResourceRequirements `json:"resource_requirements,omitempty"`

// Environment variables to add to the container
EnvVars []corev1.EnvVar `json:"env_vars,omitempty"`
}

// PulpJob defines the jobs used by pulpcore containers to run single-shot administrative tasks
Expand Down
35 changes: 35 additions & 0 deletions apis/repo-manager.pulpproject.org/v1beta2/zz_generated.deepcopy.go

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

1,165 changes: 1,165 additions & 0 deletions bundle/manifests/repo-manager.pulpproject.org_pulps.yaml

Large diffs are not rendered by default.

1,165 changes: 1,165 additions & 0 deletions config/crd/bases/repo-manager.pulpproject.org_pulps.yaml

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions controllers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (d *CommonDeployment) setEnvVars(resources any, pulpcoreType settings.Pulpc
pulp := resources.(FunctionResources).Pulp
pulpcoreTypeField := reflect.ValueOf(pulp.Spec).FieldByName(string(pulpcoreType))

var envVars []corev1.EnvVar
envVars := SetPulpcoreCustomEnvVars(*pulp, pulpcoreType)

if pulpcoreType != settings.WORKER {
// gunicornWorkers definition
Expand All @@ -237,10 +237,11 @@ func (d *CommonDeployment) setEnvVars(resources any, pulpcoreType settings.Pulpc
// gunicornTimeout definition
gunicornTimeout := strconv.FormatInt(pulpcoreTypeField.FieldByName("GunicornTimeout").Int(), 10)

envVars = []corev1.EnvVar{
gunicornEnvVars := []corev1.EnvVar{
{Name: "PULP_GUNICORN_TIMEOUT", Value: gunicornTimeout},
{Name: "PULP_" + strings.ToUpper(string(pulpcoreType)) + "_WORKERS", Value: gunicornWorkers},
}
envVars = append(envVars, gunicornEnvVars...)
}

// add postgres env vars
Expand Down Expand Up @@ -329,9 +330,9 @@ func (d *CommonDeployment) setEnvVars(resources any, pulpcoreType settings.Pulpc
}

// setInitContainerEnvVars defines the list of init-containers' environment variables
func (d *CommonDeployment) setInitContainerEnvVars(resources any) {
func (d *CommonDeployment) setInitContainerEnvVars(resources any, pulpcoreType settings.PulpcoreType) {
pulp := resources.(FunctionResources).Pulp
d.initContainerEnvVars = append([]corev1.EnvVar(nil), GetPostgresEnvVars(*pulp)...)
d.initContainerEnvVars = append(GetPostgresEnvVars(*pulp), SetPulpcoreCustomEnvVars(*pulp, pulpcoreType)...)
}

// GetPostgresEnvVars return the list of postgres environment variables to use in containers
Expand Down Expand Up @@ -1068,7 +1069,6 @@ func (d *CommonDeployment) setLDAPConfigs(resources any) {
ReadOnly: true,
}
d.volumeMounts = append(d.volumeMounts, volumeMount)

}

// build constructs the fields used in the deployment specification
Expand All @@ -1093,7 +1093,7 @@ func (d *CommonDeployment) build(resources any, pulpcoreType settings.PulpcoreTy
d.setInitContainerResourceRequirements(*pulp, pulpcoreType)
d.setInitContainerImage(*pulp, pulpcoreType)
d.setInitContainerVolumeMounts(*pulp)
d.setInitContainerEnvVars(resources)
d.setInitContainerEnvVars(resources, pulpcoreType)
d.setLDAPConfigs(resources)
d.setInitContainers(resources, *pulp, pulpcoreType)
d.setContainers(*pulp, pulpcoreType)
Expand Down
5 changes: 5 additions & 0 deletions controllers/repo_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Api defines desired state of pulpcore-api resources
| pdb | PodDisruptionBudget is an object to define the max disruption that can be caused to a collection of pods | *policy.PodDisruptionBudgetSpec | false |
| strategy | The deployment strategy to use to replace existing pods with new ones. | appsv1.DeploymentStrategy | false |
| init_container | InitContainer defines configuration of the init-containers that run in pulpcore pods | [PulpContainer](#pulpcontainer) | false |
| env_vars | Environment variables to add to pulpcore-api container | []corev1.EnvVar | false |

[Back to Custom Resources](#custom-resources)

Expand Down Expand Up @@ -82,6 +83,7 @@ Content defines desired state of pulpcore-content resources
| pdb | PodDisruptionBudget is an object to define the max disruption that can be caused to a collection of pods | *policy.PodDisruptionBudgetSpec | false |
| strategy | The deployment strategy to use to replace existing pods with new ones. | appsv1.DeploymentStrategy | false |
| init_container | InitContainer defines configuration of the init-containers that run in pulpcore pods | [PulpContainer](#pulpcontainer) | false |
| env_vars | Environment variables to add to pulpcore-content container | []corev1.EnvVar | false |

[Back to Custom Resources](#custom-resources)

Expand Down Expand Up @@ -143,6 +145,7 @@ PulpContainer defines configuration of the \"auxiliary\" containers that run in
| ----- | ----------- | ------ | -------- |
| image | The image name for the container. By default, if not provided, it will use the same image from .Spec.Image. WARN: defining a different image than the one used by API pods can cause unexpected behaviors! | string | false |
| resource_requirements | Resource requirements for pulpcore aux container. | corev1.ResourceRequirements | false |
| env_vars | Environment variables to add to the container | []corev1.EnvVar | false |

[Back to Custom Resources](#custom-resources)

Expand Down Expand Up @@ -292,6 +295,7 @@ Web defines desired state of pulpcore-web (reverse-proxy) resources
| strategy | The deployment strategy to use to replace existing pods with new ones. | appsv1.DeploymentStrategy | false |
| service_annotations | Annotations for the service | map[string]string | false |
| tls_termination_mechanism | The secure TLS termination mechanism to use Default: \"edge\" | string | false |
| env_vars | Environment variables to add to pulpcore-web container | []corev1.EnvVar | false |

[Back to Custom Resources](#custom-resources)

Expand All @@ -312,5 +316,6 @@ Worker defines desired state of pulpcore-worker resources
| pdb | PodDisruptionBudget is an object to define the max disruption that can be caused to a collection of pods | *policy.PodDisruptionBudgetSpec | false |
| strategy | The deployment strategy to use to replace existing pods with new ones. | appsv1.DeploymentStrategy | false |
| init_container | InitContainer defines configuration of the init-containers that run in pulpcore pods | [PulpContainer](#pulpcontainer) | false |
| env_vars | Environment variables to add to pulpcore-worker container | []corev1.EnvVar | false |

[Back to Custom Resources](#custom-resources)
8 changes: 8 additions & 0 deletions controllers/repo_manager/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ var _ = Describe("Pulp controller", Ordered, func() {
{Name: "POSTGRES_HOST_AUTH_METHOD", Value: "scram-sha-256"},
}

customEnvVar := corev1.EnvVar{Name: "TEST", Value: "test ok"}
envVarsInitContainer := []corev1.EnvVar{
{Name: "POSTGRES_SERVICE_HOST", Value: PulpName + "-database-svc"},
{Name: "POSTGRES_SERVICE_PORT", Value: "5432"},
customEnvVar,
}

envVarsApi := []corev1.EnvVar{
customEnvVar,
{Name: "PULP_GUNICORN_TIMEOUT", Value: strconv.Itoa(90)},
{Name: "PULP_API_WORKERS", Value: strconv.Itoa(2)},
{Name: "POSTGRES_SERVICE_HOST", Value: PulpName + "-database-svc"},
Expand All @@ -177,6 +180,7 @@ var _ = Describe("Pulp controller", Ordered, func() {
}

envVarsContent := []corev1.EnvVar{
customEnvVar,
{Name: "PULP_GUNICORN_TIMEOUT", Value: strconv.Itoa(90)},
{Name: "PULP_CONTENT_WORKERS", Value: strconv.Itoa(2)},
{Name: "POSTGRES_SERVICE_HOST", Value: PulpName + "-database-svc"},
Expand All @@ -186,6 +190,7 @@ var _ = Describe("Pulp controller", Ordered, func() {
}

envVarsWorker := []corev1.EnvVar{
customEnvVar,
{Name: "POSTGRES_SERVICE_HOST", Value: PulpName + "-database-svc"},
{Name: "POSTGRES_SERVICE_PORT", Value: "5432"},
{Name: "REDIS_SERVICE_HOST", Value: PulpName + "-redis-svc." + PulpNamespace},
Expand Down Expand Up @@ -820,12 +825,15 @@ var _ = Describe("Pulp controller", Ordered, func() {
ImageWebVersion: "latest",
Api: repomanagerpulpprojectorgv1beta2.Api{
Replicas: 1,
EnvVars: []corev1.EnvVar{customEnvVar},
},
Content: repomanagerpulpprojectorgv1beta2.Content{
Replicas: 1,
EnvVars: []corev1.EnvVar{customEnvVar},
},
Worker: repomanagerpulpprojectorgv1beta2.Worker{
Replicas: 1,
EnvVars: []corev1.EnvVar{customEnvVar},
},
Web: repomanagerpulpprojectorgv1beta2.Web{
Replicas: 1,
Expand Down
10 changes: 9 additions & 1 deletion controllers/repo_manager/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func pulpcoreVolumeMounts(pulp *repomanagerpulpprojectorgv1beta2.Pulp) []corev1.
func resetAdminPasswordContainer(pulp *repomanagerpulpprojectorgv1beta2.Pulp) corev1.Container {
// env vars
envVars := controllers.GetPostgresEnvVars(*pulp)
envVars = append(envVars, controllers.SetCustomEnvVars(*pulp, "AdminPasswordJob")...)

// volume mounts
volumeMounts := pulpcoreVolumeMounts(pulp)
Expand Down Expand Up @@ -230,6 +231,7 @@ func (r *RepoManagerReconciler) migrationJob(ctx context.Context, pulp *repomana
func migrationContainer(pulp *repomanagerpulpprojectorgv1beta2.Pulp) corev1.Container {
// env vars
envVars := controllers.GetPostgresEnvVars(*pulp)
envVars = append(envVars, controllers.SetCustomEnvVars(*pulp, "MigrationJob")...)

// volume mounts
volumeMounts := pulpcoreVolumeMounts(pulp)
Expand Down Expand Up @@ -303,6 +305,7 @@ func (r *RepoManagerReconciler) updateContentChecksumsJob(ctx context.Context, p
func contentChecksumsContainer(pulp *repomanagerpulpprojectorgv1beta2.Pulp) corev1.Container {
// env vars
envVars := controllers.GetPostgresEnvVars(*pulp)
envVars = append(envVars, controllers.SetCustomEnvVars(*pulp, string(settings.API))...)

// volume mounts
volumeMounts := pulpcoreVolumeMounts(pulp)
Expand Down Expand Up @@ -390,6 +393,7 @@ func signingScriptContainer(pulp *repomanagerpulpprojectorgv1beta2.Pulp, scripts

// env vars
envVars := controllers.GetPostgresEnvVars(*pulp)
envVars = append(envVars, controllers.SetCustomEnvVars(*pulp, "SigningJob")...)
envVars = append(envVars, corev1.EnvVar{Name: "PULP_SIGNING_KEY_FINGERPRINT", Value: fingerprint})
envVars = append(envVars, corev1.EnvVar{Name: "HOME", Value: "/var/lib/pulp"})

Expand Down Expand Up @@ -525,6 +529,10 @@ func initContainer(pulp *repomanagerpulpprojectorgv1beta2.Pulp, resources corev1
/usr/bin/wait_on_database_migrations.sh`,
}

// env vars
envVars := controllers.GetPostgresEnvVars(*pulp)
envVars = append(envVars, controllers.SetCustomEnvVars(*pulp, "SigningJob")...)

volumeMounts := []corev1.VolumeMount{
{
Name: pulp.Name + "-server",
Expand Down Expand Up @@ -556,7 +564,7 @@ func initContainer(pulp *repomanagerpulpprojectorgv1beta2.Pulp, resources corev1
Name: "init-container",
Image: image,
ImagePullPolicy: corev1.PullPolicy(pulp.Spec.ImagePullPolicy),
Env: controllers.GetPostgresEnvVars(*pulp),
Env: envVars,
Command: []string{"/bin/sh"},
Args: args,
VolumeMounts: volumeMounts,
Expand Down
23 changes: 13 additions & 10 deletions controllers/repo_manager/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ func (r *RepoManagerReconciler) deploymentForPulpWeb(m *repomanagerpulpprojector
nodeSelector = m.Spec.Web.NodeSelector
}

envVars := []corev1.EnvVar{
{
Name: "NODE_IP",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "status.hostIP",
},
},
},
}
envVars = append(envVars, m.Spec.Web.EnvVars...)

dep := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: settings.WEB.DeploymentName(m.Name),
Expand All @@ -208,16 +220,7 @@ func (r *RepoManagerReconciler) deploymentForPulpWeb(m *repomanagerpulpprojector
Image: ImageWeb,
Name: "web",
Resources: resources,
Env: []corev1.EnvVar{
{
Name: "NODE_IP",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "status.hostIP",
},
},
},
},
Env: envVars,
Ports: []corev1.ContainerPort{{
ContainerPort: 8080,
Protocol: "TCP",
Expand Down
Loading

0 comments on commit 3780ab5

Please sign in to comment.