Skip to content

Commit

Permalink
fix: invalidate dependency replacement in preview (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Sep 8, 2023
1 parent 93ac0e7 commit 78a88dc
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions pkg/engine/operation/graph/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ func (rn *ResourceNode) PreExecute(o *opsmodels.Operation) status.Status {
if len(o.PriorStateResourceIndex) == 0 {
_, replaced, s = ReplaceSecretRef(value, o.SecretStores)
} else {
// fixme what if the last apply is failed
_, replaced, s = ReplaceRef(value, o.CtxResourceIndex, ImplicitReplaceFun, o.SecretStores, vals.ParseSecretRef)
_, replaced, s = ReplaceRef(value, o.CtxResourceIndex, OptionalImplicitReplaceFun, o.SecretStores, vals.ParseSecretRef)
}
case opsmodels.Apply:
// replace secret ref and implicit ref
_, replaced, s = ReplaceRef(value, o.CtxResourceIndex, ImplicitReplaceFun, o.SecretStores, vals.ParseSecretRef)
_, replaced, s = ReplaceRef(value, o.CtxResourceIndex, MustImplicitReplaceFun, o.SecretStores, vals.ParseSecretRef)
default:
return nil
}
Expand Down Expand Up @@ -301,7 +300,20 @@ func ReplaceSecretRef(v reflect.Value, ss *vals.SecretStores) ([]string, reflect
return ReplaceRef(v, nil, nil, ss, vals.ParseSecretRef)
}

var ImplicitReplaceFun = func(resourceIndex map[string]*models.Resource, refPath string) (reflect.Value, status.Status) {
var MustImplicitReplaceFun = func(resourceIndex map[string]*models.Resource, refPath string) (reflect.Value, status.Status) {
return implicitReplaceFun(true, resourceIndex, refPath)
}

var OptionalImplicitReplaceFun = func(resourceIndex map[string]*models.Resource, refPath string) (reflect.Value, status.Status) {
return implicitReplaceFun(false, resourceIndex, refPath)
}

// implicitReplaceFun will replace implicit dependency references. If force is true, this function will return an error when replace references failed
var implicitReplaceFun = func(
force bool,
resourceIndex map[string]*models.Resource,
refPath string,
) (reflect.Value, status.Status) {
const Sep = "."
split := strings.Split(refPath, Sep)
key := split[0]
Expand All @@ -321,8 +333,13 @@ var ImplicitReplaceFun = func(resourceIndex map[string]*models.Resource, refPath
split := split[1:]
for _, k := range split {
if valueMap.(map[string]interface{})[k] == nil {
msg := fmt.Sprintf("can't find specified value in resource:%s by ref:%s", key, refPath)
return reflect.Value{}, status.NewErrorStatusWithMsg(status.IllegalManifest, msg)
if force {
// only throw errors when force replacing operations like apply
msg := fmt.Sprintf("can't find specified value in resource:%s by ref:%s", key, refPath)
return reflect.Value{}, status.NewErrorStatusWithMsg(status.IllegalManifest, msg)
} else {
break
}
}
valueMap = valueMap.(map[string]interface{})[k]
}
Expand Down

0 comments on commit 78a88dc

Please sign in to comment.