diff --git a/engine/cmp.go b/engine/cmp.go index 31e2b6a6..72724634 100644 --- a/engine/cmp.go +++ b/engine/cmp.go @@ -24,7 +24,8 @@ import ( ) // ResCmp compares two resources by checking multiple aspects. This is the main -// entry point for running all the compare steps on two resource. +// entry point for running all the compare steps on two resources. This code is +// very similar to AdaptCmp. func ResCmp(r1, r2 Res) error { if r1.Kind() != r2.Kind() { return fmt.Errorf("kind differs") @@ -37,6 +38,30 @@ func ResCmp(r1, r2 Res) error { return err } + // TODO: do we need to compare other traits/metaparams? + + m1 := r1.MetaParams() + m2 := r2.MetaParams() + if (m1 == nil) != (m2 == nil) { // xor + return fmt.Errorf("meta params differ") + } + if m1 != nil && m2 != nil { + if err := m1.Cmp(m2); err != nil { + return err + } + } + + r1x, ok1 := r1.(RefreshableRes) + r2x, ok2 := r2.(RefreshableRes) + if ok1 != ok2 { + return fmt.Errorf("refreshable differs") // they must be different (optional) + } + if ok1 && ok2 { + if r1x.Refresh() != r2x.Refresh() { + return fmt.Errorf("refresh differs") + } + } + // compare meta params for resources with auto edges r1e, ok1 := r1.(EdgeableRes) r2e, ok2 := r2.(EdgeableRes) @@ -87,6 +112,174 @@ func ResCmp(r1, r2 Res) error { } } + r1r, ok1 := r1.(RecvableRes) + r2r, ok2 := r2.(RecvableRes) + if ok1 != ok2 { + return fmt.Errorf("recvable differs") // they must be different (optional) + } + if ok1 && ok2 { + v1 := r1r.Recv() + v2 := r2r.Recv() + + if (v1 == nil) != (v2 == nil) { // xor + return fmt.Errorf("recv params differ") + } + if v1 != nil && v2 != nil { + // TODO: until we hit this code path, don't allow + // comparing anything that has this set to non-zero + if len(v1) != 0 || len(v2) != 0 { + return fmt.Errorf("recv params exist") + } + } + } + + r1s, ok1 := r1.(SendableRes) + r2s, ok2 := r2.(SendableRes) + if ok1 != ok2 { + return fmt.Errorf("sendable differs") // they must be different (optional) + } + if ok1 && ok2 { + s1 := r1s.Sent() + s2 := r2s.Sent() + + if (s1 == nil) != (s2 == nil) { // xor + return fmt.Errorf("send params differ") + } + if s1 != nil && s2 != nil { + // TODO: until we hit this code path, don't allow + // adapting anything that has this set to non-nil + return fmt.Errorf("send params exist") + } + } + + return nil +} + +// AdaptCmp compares two resources by checking multiple aspects. This is the +// main entry point for running all the compatible compare steps on two +// resources. This code is very similar to ResCmp. +func AdaptCmp(r1, r2 CompatibleRes) error { + if r1.Kind() != r2.Kind() { + return fmt.Errorf("kind differs") + } + if r1.Name() != r2.Name() { + return fmt.Errorf("name differs") + } + + // run `Adapts` instead of `Cmp` + if err := r1.Adapts(r2); err != nil { + return err + } + + // TODO: do we need to compare other traits/metaparams? + + m1 := r1.MetaParams() + m2 := r2.MetaParams() + if (m1 == nil) != (m2 == nil) { // xor + return fmt.Errorf("meta params differ") + } + if m1 != nil && m2 != nil { + if err := m1.Cmp(m2); err != nil { + return err + } + } + + // we don't need to compare refresh, since those can always be merged... + + // compare meta params for resources with auto edges + r1e, ok1 := r1.(EdgeableRes) + r2e, ok2 := r2.(EdgeableRes) + if ok1 != ok2 { + return fmt.Errorf("edgeable differs") // they must be different (optional) + } + if ok1 && ok2 { + if r1e.AutoEdgeMeta().Cmp(r2e.AutoEdgeMeta()) != nil { + return fmt.Errorf("autoedge differs") + } + } + + // compare meta params for resources with auto grouping + r1g, ok1 := r1.(GroupableRes) + r2g, ok2 := r2.(GroupableRes) + if ok1 != ok2 { + return fmt.Errorf("groupable differs") // they must be different (optional) + } + if ok1 && ok2 { + if r1g.AutoGroupMeta().Cmp(r2g.AutoGroupMeta()) != nil { + return fmt.Errorf("autogroup differs") + } + + // if resources are grouped, are the groups the same? + if i, j := r1g.GetGroup(), r2g.GetGroup(); len(i) != len(j) { + return fmt.Errorf("autogroup groups differ") + } else if len(i) > 0 { // trick the golinter + + // Sort works with Res, so convert the lists to that + iRes := []Res{} + for _, r := range i { + res := r.(Res) + iRes = append(iRes, res) + } + jRes := []Res{} + for _, r := range j { + res := r.(Res) + jRes = append(jRes, res) + } + + ix, jx := Sort(iRes), Sort(jRes) // now sort :) + for k := range ix { + // compare sub resources + // TODO: should we use AdaptCmp here? + // TODO: how would they run `Merge` ? (we don't) + // this code path will probably not run, because + // it is called in the lang before autogrouping! + if err := ResCmp(ix[k], jx[k]); err != nil { + return err + } + } + } + } + + r1r, ok1 := r1.(RecvableRes) + r2r, ok2 := r2.(RecvableRes) + if ok1 != ok2 { + return fmt.Errorf("recvable differs") // they must be different (optional) + } + if ok1 && ok2 { + v1 := r1r.Recv() + v2 := r2r.Recv() + + if (v1 == nil) != (v2 == nil) { // xor + return fmt.Errorf("recv params differ") + } + if v1 != nil && v2 != nil { + // TODO: until we hit this code path, don't allow + // adapting anything that has this set to non-zero + if len(v1) != 0 || len(v2) != 0 { + return fmt.Errorf("recv params exist") + } + } + } + + r1s, ok1 := r1.(SendableRes) + r2s, ok2 := r2.(SendableRes) + if ok1 != ok2 { + return fmt.Errorf("sendable differs") // they must be different (optional) + } + if ok1 && ok2 { + s1 := r1s.Sent() + s2 := r2s.Sent() + + if (s1 == nil) != (s2 == nil) { // xor + return fmt.Errorf("send params differ") + } + if s1 != nil && s2 != nil { + // TODO: until we hit this code path, don't allow + // adapting anything that has this set to non-nil + return fmt.Errorf("send params exist") + } + } + return nil } diff --git a/engine/copy.go b/engine/copy.go index 330707a6..2eea98f5 100644 --- a/engine/copy.go +++ b/engine/copy.go @@ -108,3 +108,53 @@ func ResCopy(r CopyableRes) (CopyableRes, error) { return res, nil } + +// ResMerge merges a set of resources that are compatible with each other. This +// is the main entry point for the merging. They must each successfully be able +// to run AdaptCmp without error. +func ResMerge(r ...CompatibleRes) (CompatibleRes, error) { + if len(r) == 0 { + return nil, fmt.Errorf("zero resources given") + } + if len(r) == 1 { + return r[0], nil + } + if len(r) > 2 { + r0 := r[0] + r1, err := ResMerge(r[1:]...) + if err != nil { + return nil, err + } + return ResMerge(r0, r1) + } + // now we have r[0] and r[1] to merge here... + r0 := r[0] + r1 := r[1] + if err := AdaptCmp(r0, r1); err != nil { + return nil, err + } + + res, err := r0.Merge(r1) // resource method of this interface + if err != nil { + return nil, err + } + + // meta should have come over in the copy + + if x, ok := res.(RefreshableRes); ok { + x0, ok0 := r0.(RefreshableRes) + x1, ok1 := r1.(RefreshableRes) + if !ok0 || !ok1 { + // programming error + panic("refresh interfaces are illogical") + } + + x.SetRefresh(x0.Refresh() || x1.Refresh()) // true if either is! + } + + // the other traits and metaparams can't be merged easily... so we don't + // merge them, and if they were present and differed, and weren't copied + // in the ResCopy method, then we should have errored above in AdaptCmp! + + return res, nil +} diff --git a/engine/resources.go b/engine/resources.go index 8e0966c1..c66fb262 100644 --- a/engine/resources.go +++ b/engine/resources.go @@ -197,7 +197,9 @@ type Res interface { CheckApply(apply bool) (checkOK bool, err error) // Cmp compares itself to another resource and returns an error if they - // are not equivalent. + // are not equivalent. This is more strict than the Equiv method of the + // CompatibleRes interface which allows for equivalent differences if + // the have a compatible result in CheckApply. Cmp(Res) error } @@ -266,6 +268,30 @@ type CopyableRes interface { Copy() CopyableRes } +// CompatibleRes is an interface that a resource can implement to express if a +// similar variant of itself is functionally equivalent. For example, two `pkg` +// resources that install `cowsay` could be equivalent if one requests a state +// of `installed` and the other requests `newest`, since they'll finish with a +// compatible result. This doesn't need to be behind a metaparam flag or trait, +// because it is never beneficial to turn it off, unless there is a bug to fix. +type CompatibleRes interface { + //Res // causes "duplicate method" error + CopyableRes // we'll need to use the Copy method in the Merge function! + + // Adapts compares itself to another resource and returns an error if + // they are not compatibly equivalent. This is less strict than the + // default `Cmp` method which should be used for most cases. Don't call + // this directly, use engine.AdaptCmp instead. + Adapts(CompatibleRes) error + + // Merge returns the combined resource to use when two are equivalent. + // This might get called multiple times for N different resources that + // need to get merged, and so it should produce a consistent result no + // matter which order it is called in. Don't call this directly, use + // engine.ResMerge instead. + Merge(CompatibleRes) (CompatibleRes, error) +} + // CollectableRes is an interface for resources that support collection. It is // currently temporary until a proper API for all resources is invented. type CollectableRes interface {