-
Notifications
You must be signed in to change notification settings - Fork 39
/
deployer.go
70 lines (58 loc) · 1.53 KB
/
deployer.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
package deploy
import (
"context"
v1 "github.com/solo-io/wasme/pkg/operator/api/wasme.io/v1"
"github.com/pkg/errors"
"github.com/solo-io/wasme/pkg/pull"
)
//go:generate mockgen -destination ./mocks/deployer.go github.com/solo-io/wasme/pkg/deploy Provider
// mesh-provider specific implementation that adds/removes filters
type Provider interface {
ApplyFilter(filter *v1.FilterSpec) error
RemoveFilter(filter *v1.FilterSpec) error
}
type Deployer struct {
Ctx context.Context
Puller pull.ImagePuller
Provider Provider
}
func (d *Deployer) ApplyFilter(filter *v1.FilterSpec) error {
if err := d.setRootID(filter); err != nil {
return err
}
return d.Provider.ApplyFilter(filter)
}
func (d *Deployer) RemoveFilter(filter *v1.FilterSpec) error {
return d.Provider.RemoveFilter(filter)
}
// gets the root ID of the filter.
// the first time it must pull the image and inspect it
// second time it will cache it locally
// if the user provides
func (d *Deployer) setRootID(f *v1.FilterSpec) error {
if f.RootID != "" {
return nil
}
rootId, err := d.getRootId(f.Image)
if err != nil {
return err
}
f.RootID = rootId
return nil
}
// get the root id by pulling the image
func (d *Deployer) getRootId(ref string) (string, error) {
image, err := d.Puller.Pull(d.Ctx, ref)
if err != nil {
return "", err
}
cfg, err := image.FetchConfig(d.Ctx)
if err != nil {
return "", err
}
rootIds := cfg.GetConfig().GetRootIds()
if len(rootIds) < 1 {
return "", errors.Errorf("no roots found in config")
}
return rootIds[0], nil
}