kyaml/fn/framework ensures the annotation output format matches the input

If the input only contains legacy format anntations (path, index, id), the
output will be the same.
This commit is contained in:
Mengqi Yu
2021-11-17 14:06:20 -08:00
parent 2f115223cc
commit 5caed5b90a
5 changed files with 186 additions and 218 deletions

View File

@@ -44,18 +44,27 @@ const (
)
func GetFileAnnotations(rn *yaml.RNode) (string, string, error) {
if err := CopyLegacyAnnotations(rn); err != nil {
return "", "", err
annotations := rn.GetAnnotations()
path, found := annotations[PathAnnotation]
if !found {
path = annotations[LegacyPathAnnotation]
}
meta, err := rn.GetMeta()
if err != nil {
return "", "", err
index, found := annotations[IndexAnnotation]
if !found {
index = annotations[LegacyIndexAnnotation]
}
path := meta.Annotations[PathAnnotation]
index := meta.Annotations[IndexAnnotation]
return path, index, nil
}
func GetIdAnnotation(rn *yaml.RNode) string {
annotations := rn.GetAnnotations()
id, found := annotations[IdAnnotation]
if !found {
id = annotations[LegacyIdAnnotation]
}
return id
}
func CopyLegacyAnnotations(rn *yaml.RNode) error {
meta, err := rn.GetMeta()
if err != nil {
@@ -377,13 +386,15 @@ func ConfirmInternalAnnotationUnchanged(r1 *yaml.RNode, r2 *yaml.RNode, exclusio
return nil
}
// GetInternalAnnotations returns a map of all the annotations of the provided RNode that begin
// with the prefix `internal.config.kubernetes.io`
// GetInternalAnnotations returns a map of all the annotations of the provided
// RNode that satisfies one of the following: 1) begin with the prefix
// `internal.config.kubernetes.io` 2) is one of `config.kubernetes.io/path`,
// `config.kubernetes.io/index` and `config.k8s.io/id`.
func GetInternalAnnotations(rn *yaml.RNode) map[string]string {
annotations := rn.GetAnnotations()
result := make(map[string]string)
for k, v := range annotations {
if strings.HasPrefix(k, internalPrefix) {
if strings.HasPrefix(k, internalPrefix) || k == LegacyPathAnnotation || k == LegacyIndexAnnotation || k == LegacyIdAnnotation {
result[k] = v
}
}