Fix filter allowed env variables in Knative
@@ -207,7 +207,7 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) *serving.Service {
envvar.SetVal(environment, "CAMEL_K_CONF_D", "/etc/camel/conf.d")
// add env vars from traits
- for _, envVar := range e.EnvVars {
+ for _, envVar := range t.getAllowedEnvVars(e) {
envvar.SetVar(&svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.Env, envVar)
}
@@ -218,3 +218,27 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) *serving.Service {
return &svc
}
+
+func (t *knativeServiceTrait) getAllowedEnvVars(e *Environment) []corev1.EnvVar {
+ res := make([]corev1.EnvVar, 0, len(e.EnvVars))
+ for _, env := range e.EnvVars {
+ if env.ValueFrom == nil {
+ // Standard env vars are supported
+ res = append(res, env)
+ } else if env.ValueFrom.FieldRef != nil && env.ValueFrom.FieldRef.FieldPath == "metadata.namespace" {
+ // Namespace is known to the operator
+ res = append(res, corev1.EnvVar{
+ Name: env.Name,
+ Value: e.Integration.Namespace,
+ })
+ } else if env.ValueFrom.FieldRef != nil {
+ t.L.Infof("Environment variable %s uses fieldRef and cannot be set on a Knative service", env.Name)
+ } else if env.ValueFrom.ResourceFieldRef != nil {
+ t.L.Infof("Environment variable %s uses resourceFieldRef and cannot be set on a Knative service", env.Name)
+ } else {
+ // Other downward APIs should be supported
+ res = append(res, env)
+ }
+ }
+ return res
+}
