-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding sidecarEnvJson annotation #726
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package inject | |
|
||
import ( | ||
"fmt" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
"strconv" | ||
"strings" | ||
|
||
|
@@ -92,6 +93,15 @@ func (m *envoyMutator) mutate(pod *corev1.Pod) error { | |
return err | ||
} | ||
|
||
customEnvJson, err := m.getCustomEnvJson(pod) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k, v := range customEnvJson { | ||
customEnv[k] = v | ||
} | ||
|
||
container, err := buildEnvoySidecar(variables, customEnv) | ||
if err != nil { | ||
return err | ||
|
@@ -249,6 +259,45 @@ func (m *envoyMutator) getCustomEnv(pod *corev1.Pod) (map[string]string, error) | |
return customEnv, nil | ||
} | ||
|
||
type customEnvJsonType []map[string]string | ||
|
||
func (c *customEnvJsonType) UnmarshalJSON(data []byte) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need we make a valid check for bytes before unmarshal if the input is empty or nil? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will throw malformed error. so, I didn't make a check for that condition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Good to know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sort of a sidenote, but a nil array is functionally identical to a empty array in Go, so you generally never need to check for nil arrays. (the same is not always true for maps) |
||
var temp []map[string]interface{} | ||
if err := json.Unmarshal(data, &temp); err != nil { | ||
return err | ||
} | ||
*c = make(customEnvJsonType, 1) | ||
(*c)[0] = make(map[string]string) | ||
for _, item := range temp { | ||
for key, value := range item { | ||
if strValue, isString := value.(string); isString { | ||
(*c)[0][key] = strValue | ||
} else { | ||
return errors.Errorf("nested json isn't supported with this annotation %s, expected format: %s", AppMeshEnvJsonAnnotation, `[{"DD_ENV":"prod","TEST_ENV":"env_val"}]`) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (m *envoyMutator) getCustomEnvJson(pod *corev1.Pod) (map[string]string, error) { | ||
var customEnvJson customEnvJsonType | ||
if v, ok := pod.ObjectMeta.Annotations[AppMeshEnvJsonAnnotation]; ok { | ||
err := json.Unmarshal([]byte(v), &customEnvJson) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "nested json") { | ||
return nil, err | ||
} | ||
err = errors.Errorf("malformed annotation %s, expected format: %s", AppMeshEnvJsonAnnotation, `[{"DD_ENV":"prod","TEST_ENV":"env_val"}]`) | ||
return nil, err | ||
} | ||
} | ||
if len(customEnvJson) > 0 { | ||
return customEnvJson[0], nil | ||
} | ||
return map[string]string{}, nil | ||
} | ||
|
||
func (m *envoyMutator) mutateVolumeMounts(pod *corev1.Pod, envoyContainer *corev1.Container, volumeMounts map[string]string) { | ||
for volumeName, mountPath := range volumeMounts { | ||
volumeMount := corev1.VolumeMount{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you not just using a map[string]string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially, I thought of returning Array of maps, then I changed to returning only one map. I will change the logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.