-
Notifications
You must be signed in to change notification settings - Fork 28
/
harvester.go
145 lines (123 loc) · 4.65 KB
/
harvester.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
package collectors
import (
"fmt"
"path/filepath"
"github.com/Jeffail/gabs/v2"
"github.com/rancher/wrangler/pkg/slice"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
var ignoreHarvesterSettingsList = []string{"cluster-registration-url", "containerd-registry", "additional-ca", "ssl-certificates"}
type harvesterModule struct {
c *common
name string
}
func NewHarvesterModule(common *common, name string) *harvesterModule {
return &harvesterModule{
c: common,
name: name,
}
}
func (module harvesterModule) generateYAMLs() {
logrus.Infof("[%s] generate YAMLs, yamlsDir: %s", module.name, module.c.yamlsDir)
extraResources := getHarvesterExtraResource()
for namespace, resourceLists := range extraResources {
dir := filepath.Join(module.c.yamlsDir, "namespaced", namespace)
objs, err := module.c.discovery.SpecificResourcesForNamespace(module.toObj, module.name, namespace, resourceLists, module.c.errorLog)
if err != nil {
logrus.WithError(err).Error("Unable to fetch namespaced resources")
fmt.Fprintf(module.c.errorLog, "Unable to fetch namespaced resources: %v\n", err)
return
}
for name, obj := range objs {
file := filepath.Join(dir, name+".yaml")
logrus.Debugf("Prepare to encode to yaml file path: %s", file)
module.c.encodeFunc(obj, file, module.c.errorLog)
}
}
dir := filepath.Join(module.c.yamlsDir, "cluster")
objs, err := module.c.discovery.ResourcesForCluster(module.toClusterObj, module.skipClusterObjects, module.c.errorLog)
if err != nil {
logrus.WithError(err).Error("Unable to fetch cluster resources")
fmt.Fprintf(module.c.errorLog, "Unable to fetch cluster resources: %v\n", err)
return
}
for name, obj := range objs {
file := filepath.Join(dir, name+".yaml")
logrus.Debugf("Prepare to encode to yaml file path: %s", file)
module.c.encodeFunc(obj, file, module.c.errorLog)
}
}
func (module harvesterModule) toObj(b []byte, groupVersion, kind string, resources ...string) (interface{}, error) {
jsonParsed, err := module.c.toObjCommon(b, groupVersion, kind)
if err != nil {
return nil, err
}
/* Checking all resource and log the unknown one */
for _, resource := range resources {
switch resource {
case "secrets":
currentItems, _ := jsonParsed.S("items").Data().([]interface{})
logrus.Debugf("Whole items: %v", currentItems)
var newItems []interface{}
for _, item := range currentItems {
gItem := gabs.Wrap(item)
if find := gItem.S("type").Data().(string) == "rke.cattle.io/machine-plan"; find {
logrus.Debugf("Prepare to append item: %v", gItem.Data().(map[string]interface{}))
newItems = append(newItems, item)
}
}
if _, err := jsonParsed.Set(newItems, "items"); err != nil {
return nil, err
}
default:
// undefined resource, just logged it.
logrus.Warnf("Could not handle unknown resource %s", resource)
}
}
return jsonParsed.Data(), nil
}
/* return map{<namespace>: [resource]} */
func getHarvesterExtraResource() map[string][]string {
extraResources := make(map[string][]string)
extraResources["fleet-local"] = []string{"secrets"}
return extraResources
}
// skipClusterObjects implements the ExcludeFilter and skips all cluster resources
// other than settings.harvesterhci.io
func (module harvesterModule) skipClusterObjects(gv schema.GroupVersion, resource v1.APIResource) bool {
if gv.Group == "harvesterhci.io" && gv.Version == "v1beta1" && resource.Kind == "Setting" {
logrus.Debugf("processing object %s with gv %s\n", resource.String(), gv.String())
return false
}
logrus.Debugf("skipping object %s with gv %s\n", resource.String(), gv.String())
return true
}
func (module harvesterModule) toClusterObj(b []byte, groupVersion, kind string, resources ...string) (interface{}, error) {
jsonParsed, err := module.c.toObjCommon(b, groupVersion, kind)
if err != nil {
return nil, err
}
switch kind {
case "Setting":
currentItems, _ := jsonParsed.S("items").Data().([]interface{})
logrus.Debugf("Whole items in cluster: %v", currentItems)
var newItems []interface{}
for _, item := range currentItems {
gItem := gabs.Wrap(item)
logrus.Debugf("processing setting %v", gItem.S("metadata", "name").Data().(string))
if !slice.ContainsString(ignoreHarvesterSettingsList, gItem.S("metadata", "name").Data().(string)) {
logrus.Debugf("Prepare to append item: %v", gItem.Data().(map[string]interface{}))
newItems = append(newItems, item)
}
}
if _, err := jsonParsed.Set(newItems, "items"); err != nil {
return nil, err
}
default:
// undefined resource, just logged it.
logrus.Warnf("Could not handle kind %s", kind)
}
return jsonParsed.Data(), nil
}