How to change and delete values in a struct

I need to change the name of the keys in a struct through go.

I have this function

func (p *DbConfig) myfunction(prefix string, namespace string, dbName string, userSecret string) []corev1.EnvVar {
	   prefix = Underscore(prefix)
	   namespace = Underscore(namespace)
        

        envs := filter(p.myfunction(prefix), func(item corev1.EnvVar) bool {
		if item.Name == "USER" {
			return false
		}
		if item.Name == "PASSWORD" {
			return false
		}
		return true
	 })


	return append(envs, []corev1.EnvVar{
		{
			Name:  prefix + "DB_NAME",
			Value: namespace + dbName,
		},
		{
			Name: prefix + "DB_USER",
			ValueFrom: &corev1.EnvVarSource{
				SecretKeyRef: &corev1.SecretKeySelector{
					LocalObjectReference: corev1.LocalObjectReference{
						Name: userSecret,
					},
					Key: "POSTGRES_USER",
				},
			},
		},
		{
			Name: prefix + "DB_PASSWORD",
			ValueFrom: &corev1.EnvVarSource{
				SecretKeyRef: &corev1.SecretKeySelector{
					LocalObjectReference: corev1.LocalObjectReference{
						Name: userSecret,
					},
					Key: "POSTGRES_PASSWORD",
				},
			},
		},
	}...)
}

Where EnVar struct looks like

type EnvVar struct {
    Name string `json:"name" protobuf:"bytes,1,opt,name=name"`

	Value string `json:"value,omitempty" protobuf:"bytes,2,opt,name=value"`

	ValueFrom *EnvVarSource `json:"valueFrom,omitempty" protobuf:"bytes,3,opt,name=valueFrom"`
}

type EnvVarSource struct {


	FieldRef *ObjectFieldSelector `json:"fieldRef,omitempty" protobuf:"bytes,1,opt,name=fieldRef"`

	ResourceFieldRef *ResourceFieldSelector `json:"resourceFieldRef,omitempty" protobuf:"bytes,2,opt,name=resourceFieldRef"`

	ConfigMapKeyRef *ConfigMapKeySelector `json:"configMapKeyRef,omitempty" protobuf:"bytes,3,opt,name=configMapKeyRef"`

	SecretKeyRef *SecretKeySelector `json:"secretKeyRef,omitempty" protobuf:"bytes,4,opt,name=secretKeyRef"`
}

and here I need to change the values containing “CONTAINER” with the correct env vars used in the container.

func containerEnvs(hub *immv1alpha1.ImmHub, instance *immv1alpha1.Container) []corev1.EnvVar {
	postgresService := hub.Spec.DbConfig.myfunction("CONTAINER_", hub.Namespace, instance.Spec.dbName, instance.Spec.userSecret)
    
    //Here I want to modify the keys of the struct that I need so that correct environment variables are passed

	postgresService[0].Name = "DATABASE_NAME"
	postgresService[1].Name = "DATABASE_USER"

	service := []corev1.EnvVar{
		{
			Name:  "DATABASE_NAME",
			Value: instance.Spec.dbName,
		}
	}
	return append(service, append(postgresService)...)

}

The output of the postgresService variable looks like

[{CONTAINER_DB_NAME testdb nil} {CONTAINER_DB_USER  &EnvVarSource{FieldRef:nil,ResourceFieldRef:nil,ConfigMapKeyRef:nil,SecretKeyRef:&SecretKeySelector{LocalObjectReference:LocalObjectReference{Name:secret,},Key:POSTGRES_USER,Optional:nil,},}} {CONTAINER_DB_PASSWORD  &EnvVarSource{FieldRef:nil,ResourceFieldRef:nil,ConfigMapKeyRef:nil,SecretKeyRef:&SecretKeySelector{LocalObjectReference:LocalObjectReference{Name:secret,},Key:POSTGRES_PASSWORD,Optional:nil,},}}]

I have two problems here:

1- Is this the best way to change the default key names in my case?

2- If the variable postgresService contains more values but I just need the DATABASE_NAME and DATABASE_USER how can I delete the rest of the values from postgresService?

That’s defined at compile time. Maybe you want a map instead.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.