Skip to content

Commit

Permalink
multiple params to struct of params
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed Oct 9, 2024
1 parent 31932a7 commit c4adc17
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
11 changes: 6 additions & 5 deletions controller/dataplane/konnect_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, datap
d.WithVolume(kongInKonnectClusterCertVolume(konnectExt.Spec.AuthConfiguration.ClusterCertificateSecretRef.Name))
d.WithVolumeMount(kongInKonnectClusterVolumeMount(), consts.DataPlaneProxyContainerName)

// Only KonnectID currently supported. It's existence is enforced via CEL.
envSet := dputils.KongInKonnectDefaults(
*konnectExt.Spec.ControlPlaneRef.KonnectID,
konnectExt.Spec.ControlPlaneRegion,
konnectExt.Spec.ServerHostname)
// KonnectID is the only supported type for now, and its presence is guaranteed by a proper CEL rule.
envSet := dputils.KongInKonnectDefaults(dputils.KongInKonnectParams{
ControlPlane: *konnectExt.Spec.ControlPlaneRef.KonnectID,
Region: konnectExt.Spec.ControlPlaneRegion,
Server: konnectExt.Spec.ServerHostname,
})

dputils.FillDataPlaneProxyContainerEnvs(nil, &d.Spec.Template, envSet)
dataplane.Spec.Deployment.PodTemplateSpec = &d.Spec.Template
Expand Down
11 changes: 5 additions & 6 deletions controller/dataplane/konnect_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,11 @@ func TestApplyDataPlaneKonnectExtension(t *testing.T) {

func getKongInKonnectEnvVars(konnectExt operatorv1alpha1.DataPlaneKonnectExtension) []corev1.EnvVar {
envSet := []corev1.EnvVar{}
envMap := dputils.KongInKonnectDefaults(
*konnectExt.Spec.ControlPlaneRef.KonnectID,
konnectExt.Spec.ControlPlaneRegion,
konnectExt.Spec.ServerHostname,
)
for k, v := range envMap {
for k, v := range dputils.KongInKonnectDefaults(dputils.KongInKonnectParams{
ControlPlane: *konnectExt.Spec.ControlPlaneRef.KonnectID,
Region: konnectExt.Spec.ControlPlaneRegion,
Server: konnectExt.Spec.ServerHostname,
}) {
envSet = append(envSet, corev1.EnvVar{
Name: k,
Value: v,
Expand Down
20 changes: 12 additions & 8 deletions internal/utils/dataplane/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,21 @@ var kongInKonnectDefaultsTemplate = map[string]string{
"KONG_VITALS": "off",
}

// KongInKonnectParams is a struct that holds the parameters needed to customize
// the KongInKonnectDefaultsTemplate map.
type KongInKonnectParams struct {
ControlPlane string
Region string
Server string
}

// KongInKonnectDefaults returnes the map of Konnect-related env vars properly configured.
func KongInKonnectDefaults(
controlPlane,
region,
server string,
) map[string]string {
func KongInKonnectDefaults(params KongInKonnectParams) map[string]string {
newEnvSet := make(map[string]string, len(kongInKonnectDefaultsTemplate))
for k, v := range kongInKonnectDefaultsTemplate {
v = strings.ReplaceAll(v, "<CP-ID>", controlPlane)
v = strings.ReplaceAll(v, "<REGION>", region)
v = strings.ReplaceAll(v, "<SERVER>", server)
v = strings.ReplaceAll(v, "<CP-ID>", params.ControlPlane)
v = strings.ReplaceAll(v, "<REGION>", params.Region)
v = strings.ReplaceAll(v, "<SERVER>", params.Server)
newEnvSet[k] = v
}
return newEnvSet
Expand Down

0 comments on commit c4adc17

Please sign in to comment.