From f7ceae126061070db802742e95b41d8555698680 Mon Sep 17 00:00:00 2001 From: wenlin Date: Thu, 2 Sep 2021 11:43:00 +0100 Subject: [PATCH] Add new default config package Adds a new package to generate the default agent configuration Related #250 Signed-off-by: wenlin --- pkg/configs/default.go | 48 ++++++++++++ pkg/configs/default.yaml | 144 ++++++++++++++++++++++++++++++++++++ pkg/configs/default_test.go | 57 ++++++++++++++ 3 files changed, 249 insertions(+) create mode 100644 pkg/configs/default.go create mode 100644 pkg/configs/default.yaml create mode 100644 pkg/configs/default_test.go diff --git a/pkg/configs/default.go b/pkg/configs/default.go new file mode 100644 index 00000000..2193587d --- /dev/null +++ b/pkg/configs/default.go @@ -0,0 +1,48 @@ +package configs + +import ( + "fmt" + "io/ioutil" + "path/filepath" + + "github.com/jetstack/preflight/pkg/agent" + "gopkg.in/yaml.v2" +) + +func GetDefaultDataGatherers() ([]agent.DataGatherer, error) { + // This will read the default.yaml and call the getDataGatherers() + filename, err := filepath.Abs("./default.yaml") + if err != nil { + fmt.Print("fail to read the path") + } + + defaultYAML, err := ioutil.ReadFile(filename) + if err != nil { + fmt.Print("fail to read the file") + } + + var dataGatherer []agent.DataGatherer + err = yaml.Unmarshal(defaultYAML, &dataGatherer) + + return dataGatherer, err +} + +func getDataGatherers(yamlFile []byte) ([]agent.DataGatherer, error) { + // this will unmarshal the data gatherer + + // defaultYAML, err := ioutil.ReadFile(yamlFile) + // if err != nil { + // fmt.Print("fail to read the file") + // } + + type ConfigAgentRBACManifests struct { + ClusterRoles []rbac.ClusterRole + // ClusterRoleBindings is a list of crbs for resources which have no include/exclude ns configured + ClusterRoleBindings []rbac.ClusterRoleBinding + // RoleBindings is a list of namespaced bindings to grant permissions when include/exclude ns set + RoleBindings []rbac.RoleBinding + } + + var configAgentRBACManifests ConfigAgentRBACManifests + yaml.Unmarshal(yamlFile, configAgentRBACManifests) +} diff --git a/pkg/configs/default.yaml b/pkg/configs/default.yaml new file mode 100644 index 00000000..cb03e180 --- /dev/null +++ b/pkg/configs/default.yaml @@ -0,0 +1,144 @@ +# gather k8s apiserver version information +- kind: "k8s-discovery" + name: "k8s-discovery" +# pods data is used in the pods and application_versions packages +- kind: "k8s-dynamic" + name: "k8s/pods" + config: + resource-type: + resource: pods + version: v1 +# gather services for pod readiness probe rules +- kind: "k8s-dynamic" + name: "k8s/services" + config: + resource-type: + resource: services + version: v1 +# gather higher level resources to ensure data to determine ownership is present +- kind: "k8s-dynamic" + name: "k8s/deployments" + config: + resource-type: + version: v1 + resource: deployments + group: apps +- kind: "k8s-dynamic" + name: "k8s/replicasets" + config: + resource-type: + version: v1 + resource: replicasets + group: apps +- kind: "k8s-dynamic" + name: "k8s/statefulsets" + config: + resource-type: + version: v1 + resource: statefulsets + group: apps +- kind: "k8s-dynamic" + name: "k8s/daemonsets" + config: + resource-type: + version: v1 + resource: daemonsets + group: apps +- kind: "k8s-dynamic" + name: "k8s/jobs" + config: + resource-type: + version: v1 + resource: jobs + group: batch +- kind: "k8s-dynamic" + name: "k8s/cronjobs" + config: + resource-type: + version: v1beta1 + resource: cronjobs + group: batch +# gather resources for cert-manager package +- kind: "k8s-dynamic" + name: "k8s/secrets" + config: + resource-type: + version: v1 + resource: secrets +- kind: "k8s-dynamic" + name: "k8s/certificates" + config: + resource-type: + group: cert-manager.io + version: v1 + resource: certificates +- kind: "k8s-dynamic" + name: "k8s/ingresses" + config: + resource-type: + group: networking.k8s.io + version: v1 + resource: ingresses +- kind: "k8s-dynamic" + name: "k8s/certificaterequests" + config: + resource-type: + group: cert-manager.io + version: v1 + resource: certificaterequests +- kind: "k8s-dynamic" + name: "k8s/issuers" + config: + resource-type: + group: cert-manager.io + version: v1 + resource: issuers +- kind: "k8s-dynamic" + name: "k8s/clusterissuers" + config: + resource-type: + group: cert-manager.io + version: v1 + resource: clusterissuers +- kind: "k8s-dynamic" + name: "k8s/googlecasissuers" + config: + resource-type: + group: cas-issuer.jetstack.io + version: v1beta1 + resource: googlecasissuers +- kind: "k8s-dynamic" + name: "k8s/googlecasclusterissuers" + config: + resource-type: + group: cas-issuer.jetstack.io + version: v1beta1 + resource: googlecasclusterissuers +- kind: "k8s-dynamic" + name: "k8s/awspcaissuer" + config: + resource-type: + group: awspca.cert-manager.io + version: v1beta1 + resource: awspcaissuers +- kind: "k8s-dynamic" + name: "k8s/awspcaclusterissuers" + config: + resource-type: + group: awspca.cert-manager.io + version: v1beta1 + resource: awspcaclusterissuers +- kind: "k8s-dynamic" + name: "k8s/mutatingwebhookconfigurations" + config: + resource-type: + group: admissionregistration.k8s.io + version: v1 + resource: mutatingwebhookconfigurations +- kind: "k8s-dynamic" + name: "k8s/validatingwebhookconfigurations" + config: + resource-type: + group: admissionregistration.k8s.io + version: v1 + resource: validatingwebhookconfigurations diff --git a/pkg/configs/default_test.go b/pkg/configs/default_test.go new file mode 100644 index 00000000..22e9f088 --- /dev/null +++ b/pkg/configs/default_test.go @@ -0,0 +1,57 @@ +package configs + +import ( + "testing" + + "github.com/jetstack/preflight/pkg/agent" + "github.com/maxatome/go-testdeep/td" +) + +func TestParseDatagatherers(t *testing.T) { + testCases := []struct { + description string + expectedAgentDataGatherers []agent.DataGatherer + inputYaml string + }{ + { + description: "simple data gatherer unmarshal", + inputYaml: ` +- kind: "k8s-dynamic" + name: "k8s/pods" + config: + resource-type: + resource: pods + version: v1 +# gather services for pod readiness probe rules +- kind: "k8s-dynamic" + name: "k8s/services" + config: + resource-type: + resource: services + version: v1`, + expectedAgentDataGatherers: []agent.DataGatherer{ + { + Kind: "k8s-dynamic", + Name: "k8s/pods", + DataPath: "", + Config: nil, + }, + { + Kind: "k8s-dynamic", + Name: "k8s/services", + DataPath: "", + Config: nil, + }, + }, + }, + } + + for _, input := range testCases { + got, err := getDataGatherers(([]byte(input.inputYaml))) + if err != nil { + + } + + td.Cmp(t, input.expectedAgentDataGatherers, got) + } +}