Skip to content
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

Annotations will fail on computed values #826

Open
lblackstone opened this issue Oct 4, 2019 · 0 comments
Open

Annotations will fail on computed values #826

lblackstone opened this issue Oct 4, 2019 · 0 comments
Labels
kind/bug Some behavior is incorrect or out of spec

Comments

@lblackstone
Copy link
Member

Problem description

  • Other: The provider won't handle computed values properly for annotations during preview. Labels have proper error handling for this case, so it should be straightforward to copy that logic and return an error if appropriate.

func SetAnnotation(obj *unstructured.Unstructured, key, value string) {
// Note: Cannot use obj.GetAnnotations() here because it doesn't properly handle computed values from preview.
// During preview, don't set annotations if the metadata or annotation contains a computed value since there's
// no way to insert data into the computed object.
metadataRaw := obj.Object["metadata"]
if isComputedValue(metadataRaw) {
return
}
metadata := metadataRaw.(map[string]interface{})
annotationsRaw, ok := metadata["annotations"]
if isComputedValue(annotationsRaw) {
return
}
var annotations map[string]interface{}
if !ok {
annotations = make(map[string]interface{})
} else {
annotations = annotationsRaw.(map[string]interface{})
}
annotations[key] = value
metadata["annotations"] = annotations
}
can possibly error

func TrySetLabel(obj *unstructured.Unstructured, key, value string) (succeeded bool, err error) {
// Note: Cannot use obj.GetLabels() here because it doesn't properly handle computed values from preview.
// During preview, don't set labels if the metadata or label contains a computed value since there's
// no way to insert data into the computed object.
metadataRaw, ok := obj.Object["metadata"]
if isComputedValue(metadataRaw) {
return false, nil
}
var isMap bool
var metadata map[string]interface{}
if !ok {
metadata = map[string]interface{}{}
obj.Object["metadata"] = metadata
} else {
metadata, isMap = metadataRaw.(map[string]interface{})
if !isMap {
return false, fmt.Errorf("expected .metadata to be a map[string]interface{}, got %q",
reflect.TypeOf(metadata))
}
}
labelsRaw, ok := metadata["labels"]
if isComputedValue(labelsRaw) {
return false, nil
}
var labels map[string]interface{}
if !ok {
labels = make(map[string]interface{})
} else {
labels, isMap = labelsRaw.(map[string]interface{})
if !isMap {
return false, fmt.Errorf("expected .metadata.labels to be a map[string]interface{}, got %q",
reflect.TypeOf(labels))
}
}
labels[key] = value
metadata["labels"] = labels
return true, nil
}
is an example of proper handling

@lblackstone lblackstone added the kind/bug Some behavior is incorrect or out of spec label Oct 4, 2019
@lblackstone lblackstone self-assigned this Oct 4, 2019
@lblackstone lblackstone removed their assignment Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Some behavior is incorrect or out of spec
Projects
None yet
Development

No branches or pull requests

1 participant