-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkfilt.go
79 lines (65 loc) · 1.61 KB
/
kfilt.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
package main
import (
"github.com/ryane/kfilt/pkg/filter"
"github.com/ryane/kfilt/pkg/resource"
"sigs.k8s.io/kustomize/v3/pkg/ifc"
"sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/yaml"
)
//noinspection GoUnusedGlobalVariable
//nolint: golint
type plugin struct {
rf *resmap.Factory
Excludes []filter.Matcher `json:"excludes"`
Includes []filter.Matcher `json:"includes"`
}
//noinspection GoUnusedGlobalVariable
//nolint: golint
var KustomizePlugin plugin //nolint:deadcode
func (p *plugin) Config(ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
p.rf = rf
err := yaml.Unmarshal(c, p)
if err != nil {
return err
}
return nil
}
func (p *plugin) Generate() (resmap.ResMap, error) {
return nil, nil
}
func (p *plugin) Transform(m resmap.ResMap) error {
// setup kfilter
kfilt := filter.New()
for _, exclude := range p.Excludes {
kfilt.AddExclude(exclude)
}
for _, include := range p.Includes {
kfilt.AddInclude(include)
}
// copy kustomize resources into kfilt resources
resources := make([]resource.Resource, m.Size())
for i, res := range m.Resources() {
resources[i] = resource.New(res.Map())
}
// create new resmap with filtered resources
kresmap := resmap.New()
kresourceFactory := p.rf.RF()
filtered, err := kfilt.Filter(resources)
if err != nil {
return err
}
for _, res := range filtered {
kres := kresourceFactory.FromMap(res.Object)
if err := kresmap.Append(kres); err != nil {
return err
}
}
// reset the original resmap and add matching
m.Clear()
for _, res := range kresmap.Resources() {
if err := m.Append(res); err != nil {
return err
}
}
return nil
}