Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 56 additions & 28 deletions dynamic/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,45 +162,73 @@ func merge(fieldPath string, observedAsDest, lastApplied, desired interface{}) (

switch observedDestVal := observedAsDest.(type) {
case map[string]interface{}:

var lastAppliedVal, desiredVal map[string]interface{}
var ok bool
// In this case, observed is a map.
// Make sure the others are maps too.
// Nil desired &/ nil last applied are OK.
lastAppliedVal, ok := lastApplied.(map[string]interface{})
if !ok && lastAppliedVal != nil {
return nil,
errors.Errorf(
"%s: Expecting last applied as map[string]interface{}, got %T",
fieldPath, lastApplied,
)

// NOTE: There are chances of having lastApplied field as nil
// if observedAsDest fields are autogenerated. If lastApplied is
// nil nothing we can do by type asserting.
if lastApplied != nil {
lastAppliedVal, ok = lastApplied.(map[string]interface{})
if !ok {
return nil,
errors.Errorf(
"%s: Expecting last applied as map[string]interface{}, got %T",
fieldPath, lastApplied,
)
}
}
desiredVal, ok := desired.(map[string]interface{})
if !ok && desiredVal != nil {
return nil,
errors.Errorf(
"%s: Expecting desired as map[string]interface{}, got %T",
fieldPath, desired,
)

// NOTE: There are chances of having desired field as nil if a caller
// doesn't need to update the fields in that path no need for any update
if desired != nil {
desiredVal, ok = desired.(map[string]interface{})
if !ok {
return nil,
errors.Errorf(
"%s: Expecting desired as map[string]interface{}, got %T",
fieldPath, desired,
)
}
}
return mergeMap(fieldPath, observedDestVal, lastAppliedVal, desiredVal)
case []interface{}:
var lastAppliedVal, desiredVal []interface{}
var ok bool

// In this case observed is an array.
// Make sure desired & last applied are arrays too.
// Nil desired &/ last applied are OK.
lastAppliedVal, ok := lastApplied.([]interface{})
if !ok && lastAppliedVal != nil {
return nil,
errors.Errorf(
"%s: Expecting last applied as []interface{}, got %T",
fieldPath, lastApplied,
)

// NOTE: There are chances of having lastApplied field as nil
// if observedAsDest fields are autogenerated. If lastApplied is
// nil nothing we can do by type asserting.
if lastApplied != nil {
lastAppliedVal, ok = lastApplied.([]interface{})
if !ok {
return nil,
errors.Errorf(
"%s: Expecting last applied as []interface{}, got %T",
fieldPath, lastApplied,
)
}
}
desiredVal, ok := desired.([]interface{})
if !ok && desiredVal != nil {
return nil,
fmt.Errorf(
"%s: Expecting desired as []interface{}, got %T",
fieldPath, desired,
)

// NOTE: There are chances of having desired field as nil if a caller
// doesn't need to update the fields in that path no need for any update
if desired != nil {
desiredVal, ok = desired.([]interface{})
if !ok {
return nil,
fmt.Errorf(
"%s: Expecting desired as []interface{}, got %T",
fieldPath, desired,
)
}
}
return mergeArray(fieldPath, observedDestVal, lastAppliedVal, desiredVal)
default:
Expand Down
Loading