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 6, 2023
1 parent ab33947 commit 94650f4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
22 changes: 14 additions & 8 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:
case part.Regex != nil:
if part.Regex.Regex == nil {
panic("Regex should be non nil")
}
matchedKeys, err := t.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
7 changes: 6 additions & 1 deletion pkg/kapp/resources/mod_field_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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,7 +97,10 @@ 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:
case part.Regex != nil:
if part.Regex.Regex == nil {
panic("Regex should be non nil")
}
matchedKeys, err := t.obtainMatchingRegexKeys(obj, *part.Regex.Regex)
if err != nil {
return err
Expand All @@ -108,6 +112,7 @@ func (t FieldRemoveMod) apply(obj interface{}, path Path) error {
return err
}
}

return nil

default:
Expand Down
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 94650f4

Please sign in to comment.