Skip to content

Commit

Permalink
adopted some nits
Browse files Browse the repository at this point in the history
Signed-off-by: sethiyash <[email protected]>
  • Loading branch information
sethiyash committed Jun 7, 2023
1 parent ab33947 commit 566b3f9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
26 changes: 16 additions & 10 deletions pkg/kapp/resources/mod_field_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (t FieldCopyMod) IsResourceMatching(res Resource) bool {

func (t FieldCopyMod) ApplyFromMultiple(res Resource, srcs map[FieldCopyModSource]Resource) error {
for _, src := range t.Sources {
// Make a copy of resource, to avoid modifications
// that may be done even in case when there is nothing to copy
updatedRes := res.DeepCopy()
source, found := srcs[src]
if !found {
continue
}
// Make a copy of resource, to avoid modifications
// that may be done even in case when there is nothing to copy
updatedRes := res.DeepCopy()
updated, err := t.apply(updatedRes.unstructured().Object, source.unstructured().Object, t.Path, Path{}, srcs)
if err != nil {
return fmt.Errorf("FieldCopyMod for path '%s' on resource '%s': %s", t.Path.AsString(), res.Description(), err)
Expand All @@ -47,6 +47,7 @@ func (t FieldCopyMod) ApplyFromMultiple(res Resource, srcs map[FieldCopyModSourc
res.setUnstructured(updatedRes.unstructured())
}
}

return nil
}

Expand Down Expand Up @@ -154,24 +155,29 @@ func (t FieldCopyMod) apply(obj interface{}, srcObj interface{}, path Path, full
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}

case part.Regex != nil && part.Regex.Regex != nil:
matchedKeys, err := t.matchRegexWithSrcObj(*part.Regex.Regex, srcObj)
case part.Regex != nil:
if part.Regex.Regex == nil {
panic("Regex should be non nil")
}
matchedKeys, err := matchRegexWithSrcObj(*part.Regex.Regex, srcObj)
if err != nil {
return false, err
}
allUpdated := true
var anyUpdated bool
for _, key := range matchedKeys {
newPath := append(Path{&PathPart{MapKey: &key}}, path[i+1:]...)
newFullPath := fullPath[:len(fullPath)-1]
updated, err := t.apply(obj, srcObj, newPath, newFullPath, srcs)
if err != nil {
return false, err
}
if !updated {
allUpdated = false
if updated {
anyUpdated = true
}
}
return allUpdated, nil

return anyUpdated, nil

default:
panic(fmt.Sprintf("Unexpected path part: %#v", part))
}
Expand Down Expand Up @@ -253,7 +259,7 @@ func (t FieldCopyMod) obtainValue(obj interface{}, path Path) (interface{}, bool
return obj, true, nil
}

func (t FieldCopyMod) matchRegexWithSrcObj(regexString string, srcObj interface{}) ([]string, error) {
func matchRegexWithSrcObj(regexString string, srcObj interface{}) ([]string, error) {
var matchedKeys []string
regex, err := regexp.Compile(regexString)
if err != nil {
Expand Down
28 changes: 7 additions & 21 deletions pkg/kapp/resources/mod_field_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package resources

import (
"fmt"
"regexp"
)

type FieldRemoveMod struct {
Expand Down Expand Up @@ -79,6 +78,7 @@ func (t FieldRemoveMod) apply(obj interface{}, path Path) error {
return err
}
}

return nil // dealt with children, get out

case part.ArrayIndex.Index != nil:
Expand All @@ -96,8 +96,11 @@ func (t FieldRemoveMod) apply(obj interface{}, path Path) error {
default:
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}
case part.Regex != nil && part.Regex.Regex != nil:
matchedKeys, err := t.obtainMatchingRegexKeys(obj, *part.Regex.Regex)
case part.Regex != nil:
if part.Regex.Regex == nil {
panic("Regex should be non nil")
}
matchedKeys, err := matchRegexWithSrcObj(*part.Regex.Regex, obj)
if err != nil {
return err
}
Expand All @@ -108,6 +111,7 @@ func (t FieldRemoveMod) apply(obj interface{}, path Path) error {
return err
}
}

return nil

default:
Expand All @@ -117,21 +121,3 @@ func (t FieldRemoveMod) apply(obj interface{}, path Path) error {

panic("unreachable")
}

func (t FieldRemoveMod) obtainMatchingRegexKeys(obj interface{}, regexString string) ([]string, error) {
var matchedKeys []string
regex, err := regexp.Compile(regexString)
if err != nil {
return matchedKeys, err
}
typedObj, ok := obj.(map[string]interface{})
if !ok && typedObj != nil {
return matchedKeys, fmt.Errorf("Unexpected non-map found: %T", obj)
}
for key := range typedObj {
if regex.MatchString(key) {
matchedKeys = append(matchedKeys, key)
}
}
return matchedKeys, nil
}
4 changes: 2 additions & 2 deletions pkg/kapp/resources/mod_object_ref_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (t ObjectRefSetMod) apply(obj interface{}, path Path) error {
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}

case part.Regex != nil && part.Regex.Regex != nil:
panic(fmt.Sprintf("Regex in path part: %#v is only supported for rebase rules.", part))
case part.Regex != nil:
panic("Regex in path part is only supported for rebaseRules.")

default:
panic(fmt.Sprintf("Unexpected path part: %#v", part))
Expand Down
4 changes: 2 additions & 2 deletions pkg/kapp/resources/mod_string_map_append.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (t StringMapAppendMod) apply(obj interface{}, path Path) error {
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}

case part.Regex != nil && part.Regex.Regex != nil:
panic(fmt.Sprintf("Regex in path part: %#v is only supported for rebase rules.", part))
case part.Regex != nil:
panic("Regex in path part is only supported for rebaseRules.")

default:
panic(fmt.Sprintf("Unexpected path part: %#v", part))
Expand Down

0 comments on commit 566b3f9

Please sign in to comment.