/
unifier.go
90 lines (76 loc) · 2.52 KB
/
unifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2020 Evan Cordell
package unifier
import (
"fmt"
"strings"
"sync"
"cuelang.org/go/cue"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/util/workqueue"
"github.com/cuebernetes/cuebectl/pkg/cache"
"github.com/cuebernetes/cuebectl/pkg/identity"
)
// ClusterUnifier takes an initial cue.Instance and can return a new cue.Instance where initial has been unified
// with the current state of the cluster.
type ClusterUnifier struct {
runtime *cue.Runtime
instance *cue.Instance
informerSet cache.Interface
// protects access to the build.Instance being unified
sync.RWMutex
}
func NewClusterUnifier(runtime *cue.Runtime, instance *cue.Instance, informerSet cache.Interface) *ClusterUnifier {
return &ClusterUnifier{
runtime: runtime,
instance: instance,
informerSet: informerSet,
}
}
// unify takes the initial instance and updates the unified representation with current cluster state.
// the cluster state is constructed from the informer cache
func (u *ClusterUnifier) unify(fromCluster map[*identity.Locator]*unstructured.Unstructured) (instance *cue.Instance, err error) {
u.Lock()
defer u.Unlock()
i := *u.instance
instance = &i
for l, o := range fromCluster {
if instance, err = instance.Fill(o, l.Path...); err != nil {
return
}
}
return
}
func (u *ClusterUnifier) Fill(queue workqueue.RateLimitingInterface) (total int, err error) {
instance := *u.instance
itr, err := instance.Value().Fields()
if err != nil {
return
}
for itr.Next() {
total++
queue.Add(itr.Label())
}
return
}
// Lookup first unifies the instance with the cluster state, and then does a lookup of path in the unified instance
// if the value is concrete, the unstructured representation will be returned.
func (u *ClusterUnifier) Lookup(fromCluster map[*identity.Locator]*unstructured.Unstructured, path ...string) (*unstructured.Unstructured, error) {
instance, err := u.unify(fromCluster)
if err != nil {
return nil, err
}
u.RLock()
defer u.RUnlock()
cueValue := instance.Lookup(path...)
if err := cueValue.Validate(cue.Concrete(true)); err != nil {
// note: err is not safe to return over the error chan because it holds references to the instance internals.
// this takes the error string only and returns it
return nil, fmt.Errorf("%s not yet concrete: %s", strings.Join(path, "/"), err.Error())
}
obj := &unstructured.Unstructured{}
if err := cueValue.Decode(obj); err != nil {
return nil, err
}
return obj, nil
}