-
Notifications
You must be signed in to change notification settings - Fork 245
fix(live): validate annotation values to prevent silent data loss #4371
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
base: main
Are you sure you want to change the base?
Changes from all commits
b6a9ace
a0b112d
3798dcb
f9e9cf1
fd2e2da
86a8a35
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Copyright 2026 The kpt Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| parallel: true | ||
|
|
||
| kptArgs: | ||
| - "live" | ||
| - "apply" | ||
|
|
||
| # Expected exit code is non-zero (failure) | ||
| exitCode: 1 | ||
|
|
||
| # Error should be in stderr with clear message about which field failed | ||
| stdErr: | | ||
| error: annotation "example.com/enabled" must be a string, got boolean |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: invalid-annotation-type |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: bad-config | ||
| annotations: | ||
| example.com/enabled: true | ||
| data: | ||
| key: value |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| apiVersion: kpt.dev/v1alpha1 | ||
| kind: ResourceGroup | ||
| metadata: | ||
| name: inventory-obj | ||
| namespace: invalid-annotation-type | ||
| labels: | ||
| cli-utils.sigs.k8s.io/inventory-id: test-invalid-annotation |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Copyright 2026 The kpt Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| parallel: true | ||
|
|
||
| kptArgs: | ||
| - "live" | ||
| - "apply" | ||
|
|
||
| # Expected exit code is non-zero (failure) | ||
| exitCode: 1 | ||
|
|
||
| # Error should be in stderr with clear message about which field failed | ||
| stdErr: | | ||
| error: label "app.kubernetes.io/enabled" must be a string, got boolean |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: invalid-label-type |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: bad-label-config | ||
| labels: | ||
| app.kubernetes.io/enabled: true | ||
| data: | ||
| key: value |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| apiVersion: kpt.dev/v1alpha1 | ||
| kind: ResourceGroup | ||
| metadata: | ||
| name: inventory-obj | ||
| namespace: invalid-label-type | ||
| labels: | ||
| cli-utils.sigs.k8s.io/inventory-id: test-invalid-label |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ package live | |||||||||||||||
|
|
||||||||||||||||
| import ( | ||||||||||||||||
| "encoding/json" | ||||||||||||||||
| "fmt" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/kptdev/kpt/internal/util/pathutil" | ||||||||||||||||
| rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" | ||||||||||||||||
|
|
@@ -93,6 +94,9 @@ func removeAnnotations(n *yaml.RNode, annotations ...kioutil.AnnotationKey) erro | |||||||||||||||
| // | ||||||||||||||||
| //nolint:interfacer | ||||||||||||||||
| func kyamlNodeToUnstructured(n *yaml.RNode) (*unstructured.Unstructured, error) { | ||||||||||||||||
| if err := validateMetadataStringMaps(n); err != nil { | ||||||||||||||||
| return nil, err | ||||||||||||||||
| } | ||||||||||||||||
| b, err := n.MarshalJSON() | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return nil, err | ||||||||||||||||
|
|
@@ -109,6 +113,109 @@ func kyamlNodeToUnstructured(n *yaml.RNode) (*unstructured.Unstructured, error) | |||||||||||||||
| }, nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
||||||||||||||||
| // validateAnnotationTypes verifies that the metadata.annotations field on the | |
| // provided RNode, if present and non-null, is a mapping node whose values are | |
| // all string scalars. It returns nil when metadata or annotations are missing | |
| // or explicitly null. It returns an error if metadata.annotations is not a | |
| // mapping node or if any annotation value is not a 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.
I think we don't need to convert the type to more human readable, but curious about the others' opinion.
Maybe separate the checks to kind != scalar and tag != !!str.
Also, is "!!str" not available as a const from somewhere?
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.
Hi @mozesl-nokia , indeed !!str is available as a constant in yaml.NodeTagString, I will shortly replace the other ones as well and push it
Copilot
AI
Feb 2, 2026
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.
This helper function is missing a documentation comment. According to Go conventions and best practices visible throughout the codebase, functions should have documentation comments that explain their purpose. Add a comment explaining that this function converts YAML type tags to human-readable type names for error messages.
| // yamlTagToType converts YAML type tags to human-readable type names for error messages. |
|
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. Should there be a test for labels here too? |
|
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. Should there be a test for labels here too? |
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.
Should there be a test for labels here too?
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.
Hi @mozesl-nokia , i will add the test for labels and commit it soon