address comments

This commit is contained in:
Mengqi Yu
2021-11-18 14:58:04 -08:00
parent 5caed5b90a
commit 4e7aebc20c
2 changed files with 17 additions and 9 deletions

View File

@@ -117,7 +117,7 @@ func Execute(p ResourceListProcessor, rlSource *kio.ByteReadWriter) error {
rl.FunctionConfig = rlSource.FunctionConfig
// We store the original
nodeAnnos, err := kio.StoreInternalAnnotations(rl.Items)
nodeAnnos, err := kio.GetInternalAnnotationsFromResourceList(rl.Items)
if err != nil {
return err
}

View File

@@ -7,6 +7,7 @@ package kio
import (
"fmt"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
@@ -116,7 +117,7 @@ func (p Pipeline) ExecuteWithCallback(callback PipelineExecuteCallbackFunc) erro
for i := range p.Filters {
// Not all RNodes passed through kio.Pipeline have metadata nor should
// they all be required to.
nodeAnnos, err := StoreInternalAnnotations(result)
nodeAnnos, err := GetInternalAnnotationsFromResourceList(result)
if err != nil {
return err
}
@@ -163,11 +164,11 @@ func FilterAll(filter yaml.Filter) Filter {
})
}
// StoreInternalAnnotations stores the original path, index, and id annotations so that we can reconcile
// GetInternalAnnotationsFromResourceList stores the original path, index, and id annotations so that we can reconcile
// it later. This is necessary because currently both internal-prefixed annotations
// and legacy annotations are currently supported, and a change to one must be
// reflected in the other.
func StoreInternalAnnotations(result []*yaml.RNode) (map[nodeAnnotations]map[string]string, error) {
func GetInternalAnnotationsFromResourceList(result []*yaml.RNode) (map[nodeAnnotations]map[string]string, error) {
nodeAnnosMap := make(map[nodeAnnotations]map[string]string)
for i := range result {
@@ -224,10 +225,14 @@ func ReconcileInternalAnnotations(result []*yaml.RNode, nodeAnnosMap map[nodeAnn
return reconcileInternalAnnotations(result, nodeAnnosMap, true)
}
func reconcileInternalAnnotations(result []*yaml.RNode, nodeAnnosMap map[nodeAnnotations]map[string]string, enforceAnnotationsFormat bool) error {
// reconcileInternalAnnotations reconciles the annotation format for path, index and id annotations.
// If formatAnnotations is true, we will ensure the output annotation format matches the format
// in the input. e.g. if the input format uses the legacy format and the output will be converted to
// the legacy format if it's not.
func reconcileInternalAnnotations(result []*yaml.RNode, nodeAnnosMap map[nodeAnnotations]map[string]string, formatAnnotations bool) error {
var useInternal, useLegacy bool
var err error
if enforceAnnotationsFormat {
if formatAnnotations {
if useInternal, useLegacy, err = determineAnnotationsFormat(nodeAnnosMap); err != nil {
return err
}
@@ -246,8 +251,11 @@ func reconcileInternalAnnotations(result []*yaml.RNode, nodeAnnosMap map[nodeAnn
if err != nil {
return err
}
if enforceAnnotationsFormat {
err = enforceConsistentAnnotationFormat(result[i], useInternal, useLegacy)
if formatAnnotations {
// We invoke determineAnnotationsFormat to find out if the original annotations
// use the internal or (and) the legacy format. We format the resources to
// make them consistent with original format.
err = formatInternalAnnotations(result[i], useInternal, useLegacy)
if err != nil {
return err
}
@@ -392,7 +400,7 @@ func checkAnnotationsAltered(rn *yaml.RNode, nodeAnnosMap map[nodeAnnotations]ma
return nil
}
func enforceConsistentAnnotationFormat(rn *yaml.RNode, useInternal, useLegacy bool) error {
func formatInternalAnnotations(rn *yaml.RNode, useInternal, useLegacy bool) error {
if !useInternal {
if err := rn.PipeE(yaml.ClearAnnotation(kioutil.IdAnnotation)); err != nil {
return err