Skip to content

Commit d7de943

Browse files
committed
refactor db construction
1 parent 8c01c84 commit d7de943

File tree

2 files changed

+173
-132
lines changed

2 files changed

+173
-132
lines changed

controllers/operator/construct/database_construction.go

Lines changed: 169 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,8 @@ func getVolumesAndVolumeMounts(mdb databaseStatefulSetSource, databaseOpts Datab
667667

668668
// buildMongoDBPodTemplateSpec constructs the podTemplateSpec for the MongoDB resource
669669
func buildMongoDBPodTemplateSpec(opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource) podtemplatespec.Modification {
670+
serviceAccountName := getServiceAccountName(opts)
671+
670672
// scripts volume is shared by the init container and the AppDB, so the startup
671673
// script can be copied over
672674
scriptsVolume := statefulset.CreateVolumeFromEmptyDir("database-scripts")
@@ -675,74 +677,186 @@ func buildMongoDBPodTemplateSpec(opts DatabaseStatefulSetOptions, mdb databaseSt
675677
volumes := []corev1.Volume{scriptsVolume}
676678
volumeMounts := []corev1.VolumeMount{databaseScriptsVolumeMount}
677679

678-
initContainerModifications := []func(*corev1.Container){buildDatabaseInitContainer(opts.InitDatabaseImage)}
679-
databaseContainerModifications := []func(*corev1.Container){container.Apply(
680-
container.WithName(util.DatabaseContainerName),
681-
container.WithImage(opts.DatabaseNonStaticImage),
682-
container.WithEnvs(databaseEnvVars(opts)...),
683-
container.WithCommand([]string{"/opt/scripts/agent-launcher.sh"}),
684-
container.WithVolumeMounts(volumeMounts),
685-
)}
686-
687-
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
680+
// Add hostname override volume if specified
681+
if opts.HostNameOverrideConfigmapName != "" {
682+
volumes = append(volumes, statefulset.CreateVolumeFromConfigMap(opts.HostNameOverrideConfigmapName, opts.HostNameOverrideConfigmapName))
683+
}
688684

689-
staticContainerMongodContainerModification := podtemplatespec.NOOP()
690-
if architectures.IsRunningStaticArchitecture(mdb.GetAnnotations()) {
691-
// we don't use initContainers therefore, we reset it here
692-
initContainerModifications = []func(*corev1.Container){}
693-
mongodModification := []func(*corev1.Container){container.Apply(
694-
container.WithName(util.DatabaseContainerName),
695-
container.WithArgs([]string{""}),
696-
container.WithImage(opts.MongodbImage),
697-
container.WithEnvs(databaseEnvVars(opts)...),
698-
container.WithCommand([]string{"bash", "-c", "tail -F -n0 ${MDB_LOG_FILE_MONGODB} mongodb_marker"}),
699-
containerSecurityContext,
700-
)}
701-
staticContainerMongodContainerModification = podtemplatespec.WithContainerByIndex(1, mongodModification...)
702-
703-
// We are not setting the database-scripts volume on purpose,
704-
// since we don't need to copy things from the init container over.
705-
databaseContainerModifications = []func(*corev1.Container){container.Apply(
706-
container.WithName(util.AgentContainerName),
707-
container.WithImage(opts.AgentImage),
708-
container.WithEnvs(databaseEnvVars(opts)...),
709-
containerSecurityContext,
710-
)}
685+
configurePodSpecSecurityContext, _ := podtemplatespec.WithDefaultSecurityContextsModifications()
686+
pullSecretsConfigurationFunc := podtemplatespec.NOOP()
687+
if pullSecrets, ok := env.Read(util.ImagePullSecrets); ok { // nolint:forbidigo
688+
pullSecretsConfigurationFunc = podtemplatespec.WithImagePullSecrets(pullSecrets)
711689
}
712690

713-
if opts.HostNameOverrideConfigmapName != "" {
714-
volumes = append(volumes, statefulset.CreateVolumeFromConfigMap(opts.HostNameOverrideConfigmapName, opts.HostNameOverrideConfigmapName))
715-
modification := container.WithVolumeMounts([]corev1.VolumeMount{
716-
{
717-
Name: opts.HostNameOverrideConfigmapName,
718-
MountPath: "/opt/scripts/config",
719-
},
720-
})
691+
return podtemplatespec.Apply(
692+
podtemplatespec.WithPodLabels(defaultPodLabels(opts.ServiceName, opts.Name)),
693+
podtemplatespec.WithTerminationGracePeriodSeconds(util.DefaultPodTerminationPeriodSeconds),
694+
pullSecretsConfigurationFunc,
695+
configurePodSpecSecurityContext,
696+
podtemplatespec.WithAffinity(opts.Name, PodAntiAffinityLabelKey, 100),
697+
podtemplatespec.WithTopologyKey(opts.PodSpec.GetTopologyKeyOrDefault(), 0),
698+
podtemplatespec.WithServiceAccount(serviceAccountName),
699+
podtemplatespec.WithVolumes(volumes),
700+
buildContainers(opts, mdb, volumeMounts),
701+
)
702+
}
703+
704+
// buildContainers directly creates and configures all containers based on architecture
705+
func buildContainers(opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource, volumeMounts []corev1.VolumeMount) func(*corev1.PodTemplateSpec) {
706+
return func(podTemplateSpec *corev1.PodTemplateSpec) {
707+
isStaticArchitecture := architectures.IsRunningStaticArchitecture(mdb.GetAnnotations())
721708

722-
// we only need to add the volume modification if we actually use an init container
723-
if len(initContainerModifications) > 0 {
724-
initContainerModifications = append(initContainerModifications, modification)
709+
if isStaticArchitecture {
710+
buildStaticArchitectureContainers(podTemplateSpec, opts, mdb)
711+
} else {
712+
buildNonStaticArchitectureContainers(podTemplateSpec, opts, volumeMounts)
725713
}
726714

727-
databaseContainerModifications = append(databaseContainerModifications, modification)
715+
// Apply hostname override volume mounts if specified
716+
if opts.HostNameOverrideConfigmapName != "" {
717+
applyHostnameOverrideVolumeMounts(podTemplateSpec, opts.HostNameOverrideConfigmapName)
718+
}
728719
}
720+
}
729721

730-
serviceAccountName := getServiceAccountName(opts)
722+
// buildStaticArchitectureContainers creates containers for static architecture
723+
func buildStaticArchitectureContainers(podTemplateSpec *corev1.PodTemplateSpec, opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource) {
724+
podTemplateSpec.Spec.Containers = make([]corev1.Container, 3)
725+
podTemplateSpec.Spec.Containers[0] = createAgentContainer(opts, mdb)
726+
podTemplateSpec.Spec.Containers[1] = createMongodBinaryHolderContainer(opts)
727+
podTemplateSpec.Spec.Containers[2] = createAgentUtilitiesHolderContainer()
731728

732-
mods := []podtemplatespec.Modification{
733-
sharedDatabaseConfiguration(opts, mdb),
734-
podtemplatespec.WithServiceAccount(util.MongoDBServiceAccount),
735-
podtemplatespec.WithServiceAccount(serviceAccountName),
736-
podtemplatespec.WithVolumes(volumes),
737-
podtemplatespec.WithContainerByIndex(0, databaseContainerModifications...),
738-
staticContainerMongodContainerModification,
729+
// Apply common configurations to all containers
730+
applyCommonStaticConfigurations(podTemplateSpec.Spec.Containers, opts)
731+
}
732+
733+
// buildNonStaticArchitectureContainers creates containers for non-static architecture
734+
func buildNonStaticArchitectureContainers(podTemplateSpec *corev1.PodTemplateSpec, opts DatabaseStatefulSetOptions, volumeMounts []corev1.VolumeMount) {
735+
podTemplateSpec.Spec.Containers = make([]corev1.Container, 1)
736+
podTemplateSpec.Spec.InitContainers = make([]corev1.Container, 1)
737+
738+
podTemplateSpec.Spec.InitContainers[0] = createDatabaseInitContainer(opts)
739+
podTemplateSpec.Spec.Containers[0] = createDatabaseContainer(opts, volumeMounts)
740+
}
741+
742+
// createAgentContainer creates the agent container for static architecture
743+
func createAgentContainer(opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource) corev1.Container {
744+
c := corev1.Container{
745+
Name: util.AgentContainerName,
746+
Image: opts.AgentImage,
747+
Args: []string{},
748+
Command: []string{"/opt/scripts/agent-launcher-shim.sh"},
749+
Env: databaseEnvVars(opts),
750+
}
751+
752+
// Add agent-specific environment variables
753+
c.Env = append(c.Env, startupParametersToAgentFlag(opts.AgentConfig.StartupParameters))
754+
c.Env = append(c.Env, staticContainersEnvVars(mdb)...)
755+
756+
return c
757+
}
758+
759+
// createMongodBinaryHolderContainer creates the mongod container for static architecture
760+
func createMongodBinaryHolderContainer(opts DatabaseStatefulSetOptions) corev1.Container {
761+
c := corev1.Container{
762+
Name: util.DatabaseContainerName,
763+
Image: opts.MongodbImage,
764+
Args: []string{"tail -F -n0 \"${MDB_LOG_FILE_MONGODB}\""},
765+
Command: []string{"bash", "-c", "tail -F -n0 ${MDB_LOG_FILE_MONGODB} mongodb_marker"},
766+
Env: databaseEnvVars(opts),
767+
}
768+
769+
// Add mongod-specific environment variables
770+
c.Env = append(c.Env, startupParametersToAgentFlag(opts.AgentConfig.StartupParameters))
771+
772+
return c
773+
}
774+
775+
// createAgentUtilitiesHolderContainer creates the agent utilities container for static architecture
776+
func createAgentUtilitiesHolderContainer() corev1.Container {
777+
return corev1.Container{
778+
Name: util.AgentContainerUtilitiesName,
779+
Args: []string{"tail -F -n0 /dev/null"},
780+
}
781+
}
782+
783+
// createInitContainer creates the init container for non-static architecture
784+
func createDatabaseInitContainer(opts DatabaseStatefulSetOptions) corev1.Container {
785+
c := corev1.Container{
786+
Name: InitDatabaseContainerName,
787+
Image: opts.InitDatabaseImage,
788+
VolumeMounts: []corev1.VolumeMount{
789+
databaseScriptsVolumeMount(false),
790+
},
739791
}
740792

741-
if len(initContainerModifications) > 0 {
742-
mods = append(mods, podtemplatespec.WithInitContainerByIndex(0, initContainerModifications...))
793+
// Apply security context
794+
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
795+
containerSecurityContext(&c)
796+
797+
return c
798+
}
799+
800+
// createDatabaseContainer creates the database container for non-static architecture
801+
func createDatabaseContainer(opts DatabaseStatefulSetOptions, volumeMounts []corev1.VolumeMount) corev1.Container {
802+
c := corev1.Container{
803+
Name: util.DatabaseContainerName,
804+
Image: opts.DatabaseNonStaticImage,
805+
Command: []string{"/opt/scripts/agent-launcher.sh"},
806+
Env: databaseEnvVars(opts),
807+
VolumeMounts: volumeMounts,
808+
Resources: buildRequirementsFromPodSpec(*opts.PodSpec),
809+
Ports: []corev1.ContainerPort{{ContainerPort: opts.ServicePort}},
810+
ImagePullPolicy: corev1.PullPolicy(env.ReadOrPanic(util.AutomationAgentImagePullPolicy)),
743811
}
744812

745-
return podtemplatespec.Apply(mods...)
813+
c.Env = append(c.Env, startupParametersToAgentFlag(opts.AgentConfig.StartupParameters))
814+
c.Env = append(c.Env, logConfigurationToEnvVars(opts.AgentConfig.StartupParameters, opts.AdditionalMongodConfig)...)
815+
c.Env = append(c.Env, readinessEnvironmentVariablesToEnvVars(opts.AgentConfig.ReadinessProbe.EnvironmentVariables)...)
816+
817+
c.LivenessProbe = &corev1.Probe{}
818+
DatabaseLivenessProbe()(c.LivenessProbe)
819+
820+
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
821+
containerSecurityContext(&c)
822+
823+
return c
824+
}
825+
826+
// applyCommonStaticConfigurations applies common configurations to all static architecture containers
827+
func applyCommonStaticConfigurations(containers []corev1.Container, opts DatabaseStatefulSetOptions) {
828+
for i := range containers {
829+
c := &containers[i]
830+
c.Resources = buildRequirementsFromPodSpec(*opts.PodSpec)
831+
c.Ports = []corev1.ContainerPort{{ContainerPort: opts.ServicePort}}
832+
// TODO: this should change
833+
c.ImagePullPolicy = corev1.PullPolicy(env.ReadOrPanic(util.AutomationAgentImagePullPolicy))
834+
835+
c.Env = append(c.Env, logConfigurationToEnvVars(opts.AgentConfig.StartupParameters, opts.AdditionalMongodConfig)...)
836+
837+
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
838+
containerSecurityContext(c)
839+
}
840+
}
841+
842+
// applyHostnameOverrideVolumeMounts applies hostname override volume mounts to all containers
843+
func applyHostnameOverrideVolumeMounts(podTemplateSpec *corev1.PodTemplateSpec, configmapName string) {
844+
volumeMount := corev1.VolumeMount{
845+
Name: configmapName,
846+
MountPath: "/opt/scripts/config",
847+
}
848+
849+
// Apply to init containers if they exist
850+
for i := range podTemplateSpec.Spec.InitContainers {
851+
podTemplateSpec.Spec.InitContainers[i].VolumeMounts = append(
852+
podTemplateSpec.Spec.InitContainers[i].VolumeMounts, volumeMount)
853+
}
854+
855+
// Apply to all containers
856+
for i := range podTemplateSpec.Spec.Containers {
857+
podTemplateSpec.Spec.Containers[i].VolumeMounts = append(
858+
podTemplateSpec.Spec.Containers[i].VolumeMounts, volumeMount)
859+
}
746860
}
747861

748862
// getServiceAccountName returns the serviceAccount to be used by the mongoDB pod,
@@ -760,83 +874,6 @@ func getServiceAccountName(opts DatabaseStatefulSetOptions) string {
760874
return util.MongoDBServiceAccount
761875
}
762876

763-
// sharedDatabaseConfiguration is a function which applies all the shared configuration
764-
// between the appDb and MongoDB resources
765-
func sharedDatabaseConfiguration(opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource) podtemplatespec.Modification {
766-
configurePodSpecSecurityContext, configureContainerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
767-
768-
pullSecretsConfigurationFunc := podtemplatespec.NOOP()
769-
if pullSecrets, ok := env.Read(util.ImagePullSecrets); ok { // nolint:forbidigo
770-
pullSecretsConfigurationFunc = podtemplatespec.WithImagePullSecrets(pullSecrets)
771-
}
772-
773-
agentModification := podtemplatespec.WithContainerByIndex(0,
774-
container.Apply(
775-
container.WithResourceRequirements(buildRequirementsFromPodSpec(*opts.PodSpec)),
776-
container.WithPorts([]corev1.ContainerPort{{ContainerPort: opts.ServicePort}}),
777-
container.WithImagePullPolicy(corev1.PullPolicy(env.ReadOrPanic(util.AutomationAgentImagePullPolicy))), // nolint:forbidigo
778-
container.WithLivenessProbe(DatabaseLivenessProbe()),
779-
container.WithEnvs(startupParametersToAgentFlag(opts.AgentConfig.StartupParameters)),
780-
container.WithEnvs(logConfigurationToEnvVars(opts.AgentConfig.StartupParameters, opts.AdditionalMongodConfig)...),
781-
container.WithEnvs(readinessEnvironmentVariablesToEnvVars(opts.AgentConfig.ReadinessProbe.EnvironmentVariables)...),
782-
configureContainerSecurityContext,
783-
),
784-
)
785-
786-
staticMongodModification := podtemplatespec.NOOP()
787-
agentUtilitiesModification := podtemplatespec.NOOP()
788-
if architectures.IsRunningStaticArchitecture(mdb.GetAnnotations()) {
789-
// agentUtilities
790-
agentUtilitiesModification = podtemplatespec.WithContainerByIndex(2,
791-
container.Apply(
792-
container.WithArgs([]string{"tail -F -n0 /dev/null"}),
793-
container.WithResourceRequirements(buildRequirementsFromPodSpec(*opts.PodSpec)),
794-
container.WithPorts([]corev1.ContainerPort{{ContainerPort: opts.ServicePort}}),
795-
container.WithImagePullPolicy(corev1.PullPolicy(env.ReadOrPanic(util.AutomationAgentImagePullPolicy))), // nolint:forbidigo
796-
container.WithEnvs(logConfigurationToEnvVars(opts.AgentConfig.StartupParameters, opts.AdditionalMongodConfig)...),
797-
configureContainerSecurityContext,
798-
),
799-
)
800-
// The mongod
801-
staticMongodModification = podtemplatespec.WithContainerByIndex(1,
802-
container.Apply(
803-
container.WithArgs([]string{"tail -F -n0 \"${MDB_LOG_FILE_MONGODB}\""}),
804-
container.WithResourceRequirements(buildRequirementsFromPodSpec(*opts.PodSpec)),
805-
container.WithPorts([]corev1.ContainerPort{{ContainerPort: opts.ServicePort}}),
806-
container.WithImagePullPolicy(corev1.PullPolicy(env.ReadOrPanic(util.AutomationAgentImagePullPolicy))), // nolint:forbidigo
807-
container.WithEnvs(startupParametersToAgentFlag(opts.AgentConfig.StartupParameters)),
808-
container.WithEnvs(logConfigurationToEnvVars(opts.AgentConfig.StartupParameters, opts.AdditionalMongodConfig)...),
809-
configureContainerSecurityContext,
810-
),
811-
)
812-
agentModification = podtemplatespec.WithContainerByIndex(0,
813-
container.Apply(
814-
container.WithImagePullPolicy(corev1.PullPolicy(env.ReadOrPanic(util.AutomationAgentImagePullPolicy))), // nolint:forbidigo
815-
container.WithLivenessProbe(DatabaseLivenessProbe()),
816-
container.WithEnvs(startupParametersToAgentFlag(opts.AgentConfig.StartupParameters)),
817-
container.WithEnvs(logConfigurationToEnvVars(opts.AgentConfig.StartupParameters, opts.AdditionalMongodConfig)...),
818-
container.WithEnvs(staticContainersEnvVars(mdb)...),
819-
container.WithEnvs(readinessEnvironmentVariablesToEnvVars(opts.AgentConfig.ReadinessProbe.EnvironmentVariables)...),
820-
container.WithArgs([]string{}),
821-
container.WithCommand([]string{"/opt/scripts/agent-launcher-shim.sh"}),
822-
configureContainerSecurityContext,
823-
),
824-
)
825-
}
826-
827-
return podtemplatespec.Apply(
828-
podtemplatespec.WithPodLabels(defaultPodLabels(opts.ServiceName, opts.Name)),
829-
podtemplatespec.WithTerminationGracePeriodSeconds(util.DefaultPodTerminationPeriodSeconds),
830-
pullSecretsConfigurationFunc,
831-
configurePodSpecSecurityContext,
832-
podtemplatespec.WithAffinity(opts.Name, PodAntiAffinityLabelKey, 100),
833-
podtemplatespec.WithTopologyKey(opts.PodSpec.GetTopologyKeyOrDefault(), 0),
834-
agentModification,
835-
staticMongodModification, // non static noop
836-
agentUtilitiesModification, // non static noop
837-
)
838-
}
839-
840877
// StartupParametersToAgentFlag takes a map representing key-value pairs
841878
// of startup parameters
842879
// and concatenates them into a single string that is then

scripts/dev/print_operator_env.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ MDB_SEARCH_COMMUNITY_REPO_URL=\"${MDB_SEARCH_COMMUNITY_REPO_URL}\"
9999
if [[ "${MDB_MAX_CONCURRENT_RECONCILES:-""}" != "" ]]; then
100100
echo "MDB_MAX_CONCURRENT_RECONCILES=${MDB_MAX_CONCURRENT_RECONCILES}"
101101
fi
102+
103+
if [[ "${OPERATOR_NAME:-""}" != "" ]]; then
104+
echo "OPERATOR_NAME=${OPERATOR_NAME}"
105+
fi
102106
}
103107

104108
print_operator_env

0 commit comments

Comments
 (0)