Skip to content

Commit

Permalink
use id as the key of the mapping and make kio.Pipeline behave the sam…
Browse files Browse the repository at this point in the history
…e as framework.Execute
  • Loading branch information
Mengqi Yu committed Nov 19, 2021
1 parent 4e7aebc commit c1ff9bc
Show file tree
Hide file tree
Showing 3 changed files with 513 additions and 64 deletions.
78 changes: 36 additions & 42 deletions kyaml/kio/kio.go
Expand Up @@ -136,7 +136,7 @@ func (p Pipeline) ExecuteWithCallback(callback PipelineExecuteCallbackFunc) erro

// If either the internal annotations for path, index, and id OR the legacy
// annotations for path, index, and id are changed, we have to update the other.
err = reconcileInternalAnnotations(result, nodeAnnos, false)
err = ReconcileInternalAnnotations(result, nodeAnnos)
if err != nil {
return err
}
Expand Down Expand Up @@ -164,22 +164,19 @@ func FilterAll(filter yaml.Filter) Filter {
})
}

// 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
// GetInternalAnnotationsFromResourceList returns a mapping from id to all
// internal annotations, so that we can use it to reconcile the annotations
// 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 GetInternalAnnotationsFromResourceList(result []*yaml.RNode) (map[nodeAnnotations]map[string]string, error) {
nodeAnnosMap := make(map[nodeAnnotations]map[string]string)

// reflected in the other if needed.
func GetInternalAnnotationsFromResourceList(result []*yaml.RNode) (map[string]map[string]string, error) {
nodeAnnosMap := make(map[string]map[string]string)
for i := range result {
id := kioutil.GetIdAnnotation(result[i])
path, index, _ := kioutil.GetFileAnnotations(result[i])
annoKey := nodeAnnotations{
path: path,
index: index,
id: id,
if id == "" {
continue
}
nodeAnnosMap[annoKey] = kioutil.GetInternalAnnotations(result[i])
nodeAnnosMap[id] = kioutil.GetInternalAnnotations(result[i])
if err := kioutil.CopyLegacyAnnotations(result[i]); err != nil {
return nil, err
}
Expand Down Expand Up @@ -221,21 +218,13 @@ type nodeAnnotations struct {
}

// ReconcileInternalAnnotations reconciles the annotation format for path, index and id annotations.
func ReconcileInternalAnnotations(result []*yaml.RNode, nodeAnnosMap map[nodeAnnotations]map[string]string) error {
return reconcileInternalAnnotations(result, nodeAnnosMap, true)
}

// 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 {
// It 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[string]map[string]string) error {
var useInternal, useLegacy bool
var err error
if formatAnnotations {
if useInternal, useLegacy, err = determineAnnotationsFormat(nodeAnnosMap); err != nil {
return err
}
if useInternal, useLegacy, err = determineAnnotationsFormat(nodeAnnosMap); err != nil {
return err
}

for i := range result {
Expand All @@ -251,14 +240,12 @@ func reconcileInternalAnnotations(result []*yaml.RNode, nodeAnnosMap map[nodeAnn
if err != nil {
return err
}
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
}
// 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
}
// if the annotations are still somehow out of sync, throw an error
err = checkMismatchedAnnos(result[i].GetAnnotations())
Expand All @@ -270,7 +257,7 @@ func reconcileInternalAnnotations(result []*yaml.RNode, nodeAnnosMap map[nodeAnn
}

// determineAnnotationsFormat determines if the resources are using one of the internal and legacy annotation format or both of them.
func determineAnnotationsFormat(nodeAnnosMap map[nodeAnnotations]map[string]string) (useInternal, useLegacy bool, err error) {
func determineAnnotationsFormat(nodeAnnosMap map[string]map[string]string) (useInternal, useLegacy bool, err error) {
if len(nodeAnnosMap) == 0 {
return true, true, nil
}
Expand All @@ -282,7 +269,8 @@ func determineAnnotationsFormat(nodeAnnosMap map[nodeAnnotations]map[string]stri
_, foundId := annos[kioutil.IdAnnotation]
foundOneOf := foundPath || foundIndex || foundId
if internal == nil {
internal = &foundOneOf
f := foundOneOf
internal = &f
}
if (foundOneOf && !*internal) || (!foundOneOf && *internal) {
err = fmt.Errorf("the formatting in the input resources is not consistent")
Expand All @@ -294,7 +282,8 @@ func determineAnnotationsFormat(nodeAnnosMap map[nodeAnnotations]map[string]stri
_, foundId = annos[kioutil.LegacyIdAnnotation]
foundOneOf = foundPath || foundIndex || foundId
if legacy == nil {
legacy = &foundOneOf
f := foundOneOf
legacy = &f
}
if (foundOneOf && !*legacy) || (!foundOneOf && *legacy) {
err = fmt.Errorf("the formatting in the input resources is not consistent")
Expand Down Expand Up @@ -346,7 +335,7 @@ func missingInternalOrLegacyAnnotation(rn *yaml.RNode, newKey string, legacyKey
return nil
}

func checkAnnotationsAltered(rn *yaml.RNode, nodeAnnosMap map[nodeAnnotations]map[string]string) error {
func checkAnnotationsAltered(rn *yaml.RNode, nodeAnnosMap map[string]map[string]string) error {
annotations := rn.GetAnnotations()
// get the resource's current path, index, and ids from the new annotations
internal := nodeAnnotations{
Expand All @@ -362,16 +351,19 @@ func checkAnnotationsAltered(rn *yaml.RNode, nodeAnnosMap map[nodeAnnotations]ma
id: annotations[kioutil.LegacyIdAnnotation],
}

originalAnnotations, found := nodeAnnosMap[internal]
id := kioutil.GetIdAnnotation(rn)
originalAnnotations, found := nodeAnnosMap[id]
if !found {
originalAnnotations, found = nodeAnnosMap[legacy]
return nil
}
originalPath, found := originalAnnotations[kioutil.PathAnnotation]
if !found {
originalPath = originalAnnotations[kioutil.LegacyPathAnnotation]
}
if originalPath != "" {
if originalPath != internal.path {
if originalPath != internal.path && originalPath != legacy.path && internal.path != legacy.path {
return fmt.Errorf("resource input to function has mismatched legacy and internal path annotations")
} else if originalPath != internal.path {
if _, err := rn.Pipe(yaml.SetAnnotation(kioutil.LegacyPathAnnotation, internal.path)); err != nil {
return err
}
Expand All @@ -387,7 +379,9 @@ func checkAnnotationsAltered(rn *yaml.RNode, nodeAnnosMap map[nodeAnnotations]ma
originalIndex = originalAnnotations[kioutil.LegacyIndexAnnotation]
}
if originalIndex != "" {
if originalIndex != internal.index {
if originalIndex != internal.index && originalIndex != legacy.index && internal.index != legacy.index {
return fmt.Errorf("resource input to function has mismatched legacy and internal index annotations")
} else if originalIndex != internal.index {
if _, err := rn.Pipe(yaml.SetAnnotation(kioutil.LegacyIndexAnnotation, internal.index)); err != nil {
return err
}
Expand Down

0 comments on commit c1ff9bc

Please sign in to comment.