-
Notifications
You must be signed in to change notification settings - Fork 53
/
manifest.go
155 lines (147 loc) · 4.02 KB
/
manifest.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package patch
import (
"bytes"
"encoding/hex"
"io"
"path/filepath"
"sync"
controlv1 "github.com/rancher/opni/pkg/apis/control/v1"
"github.com/rancher/opni/pkg/plugins"
"github.com/samber/lo"
"github.com/spf13/afero"
"go.uber.org/zap"
"golang.org/x/crypto/blake2b"
)
func LeftJoinOn(gateway, agent *controlv1.PluginManifest) *controlv1.PatchList {
res := &controlv1.PatchList{}
ourPlugins := map[string]*controlv1.PluginManifestEntry{}
// if a plugin gets renamed we need to look up hashes
ourDigests := map[string]struct{}{}
theirPlugins := map[string]*controlv1.PluginManifestEntry{}
theirDigests := map[string]struct{}{}
for _, v := range gateway.Items {
ourDigests[v.GetDigest()] = struct{}{}
ourPlugins[v.GetId()] = v
}
for _, v := range agent.Items {
theirPlugins[v.GetId()] = v
theirDigests[v.GetDigest()] = struct{}{}
}
for _, ours := range gateway.Items {
if theirs, ok := theirPlugins[ours.GetId()]; !ok {
// we have a plugin that they don't have
res.Items = append(res.Items, &controlv1.PatchSpec{
Module: ours.GetId(),
Op: controlv1.PatchOp_Create,
Filename: ours.GetFilename(),
OldDigest: "",
NewDigest: ours.GetDigest(),
})
} else {
// both sides have the plugin
if ours.GetDigest() != theirs.GetDigest() {
// the hashes are different
res.Items = append(res.Items, &controlv1.PatchSpec{
Module: ours.GetId(),
Op: controlv1.PatchOp_Update,
Filename: ours.GetFilename(),
OldDigest: theirs.GetDigest(),
NewDigest: ours.GetDigest(),
})
} else if ours.GetFilename() != theirs.GetFilename() {
// a plugin was renamed but the hash is the same
res.Items = append(res.Items, &controlv1.PatchSpec{
Module: ours.GetId(),
Op: controlv1.PatchOp_Rename,
Data: []byte(ours.GetFilename()),
Filename: theirs.GetFilename(),
OldDigest: theirs.GetDigest(),
NewDigest: ours.GetDigest(),
})
}
}
}
for _, theirs := range agent.Items {
if _, ok := ourPlugins[theirs.GetId()]; !ok {
if _, ok := ourDigests[theirs.GetDigest()]; !ok {
// they have a plugin that we don't have
res.Items = append(res.Items, &controlv1.PatchSpec{
Module: theirs.GetId(),
Op: controlv1.PatchOp_Remove,
Filename: theirs.GetFilename(),
OldDigest: theirs.GetDigest(),
NewDigest: "",
})
}
}
}
res.Sort()
return res
}
func GetFilesystemPlugins(dc plugins.DiscoveryConfig) (*controlv1.PluginArchive, error) {
if dc.Fs == nil {
dc.Fs = afero.NewOsFs()
}
matches := dc.Discover()
res := &controlv1.PluginArchive{
Items: make([]*controlv1.PluginArchiveEntry, len(matches)),
}
lg := dc.Logger
if lg == nil {
lg = zap.NewNop().Sugar()
}
lg.Debugf("found %d plugins", len(matches))
var wg sync.WaitGroup
for i, md := range matches {
i, md := i, md
wg.Add(1)
go func() {
defer wg.Done()
lg := lg.With("path", md.BinaryPath)
f, err := dc.Fs.Open(md.BinaryPath)
if err != nil {
lg.With(
zap.Error(err),
).Error("failed to read plugin, skipping")
return
}
defer f.Close()
fileInfo, _ := f.Stat()
fileSize := fileInfo.Size()
hash, _ := blake2b.New256(nil)
contents := bytes.NewBuffer(make([]byte, 0, fileSize))
if _, err := io.Copy(io.MultiWriter(hash, contents), f); err != nil {
lg.With(
zap.Error(err),
).Error("failed to read plugin, skipping")
return
}
sum := hex.EncodeToString(hash.Sum(nil))
res.Items[i] = &controlv1.PluginArchiveEntry{
Metadata: &controlv1.PluginManifestEntry{
Module: md.Module,
Filename: filepath.Base(md.BinaryPath),
Digest: sum,
},
Data: contents.Bytes(),
}
}()
}
wg.Wait()
// count and remove nil entries
var numFailed int
for i := 0; i < len(res.Items); i++ {
if res.Items[i] == nil {
numFailed++
}
}
if numFailed > 0 {
lg.Warnf("%d plugins failed to load", numFailed)
}
res.Items = lo.WithoutEmpty(res.Items)
res.Sort()
lg.With(
"plugins", len(res.Items),
).Debug("loaded plugin manifest")
return res, nil
}