From fc965f2734a12a1cce15593a99d03269236e0ac4 Mon Sep 17 00:00:00 2001 From: David Kydd Date: Tue, 22 Jun 2021 12:43:00 +1200 Subject: [PATCH 01/17] Add text=auto in .gitattributes (#70) add default .gitattributes file and git add --renormalize --- builder/Dockerfile | 2 +- pkg/utils/helper.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/builder/Dockerfile b/builder/Dockerfile index 105fe25f..f0f6d788 100644 --- a/builder/Dockerfile +++ b/builder/Dockerfile @@ -30,4 +30,4 @@ RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master COPY --from=builder /build/aks-periscope / -ENTRYPOINT ["/aks-periscope"] +ENTRYPOINT ["/aks-periscope"] \ No newline at end of file diff --git a/pkg/utils/helper.go b/pkg/utils/helper.go index bf7c1385..f2954c93 100644 --- a/pkg/utils/helper.go +++ b/pkg/utils/helper.go @@ -58,6 +58,7 @@ func CopyFileFromHost(source, destination string) error { if err != nil { return fmt.Errorf("unable to retrieve source content: %w", err) } +<<<<<<< HEAD if err := os.MkdirAll(filepath.Dir(destination), os.ModePerm); err != nil { return fmt.Errorf("create path directories for file %s: %w", destination, err) @@ -73,6 +74,10 @@ func CopyFileFromHost(source, destination string) error { _, err = f.Write([]byte(sourceFile)) if err != nil { return fmt.Errorf("write data to file %s: %w", destination, err) +======= + if err = WriteToFile(destination, sourceFile); err != nil { + return fmt.Errorf("unable to write source file to destination: %w", err) +>>>>>>> bdb3847 (Add text=auto in .gitattributes (#70)) } return nil } From 45af0a3db387ee5896818af028f0d990c9acef56 Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Tue, 22 Jun 2021 09:29:31 -0700 Subject: [PATCH 02/17] feat: add SMI log collector Add log collector for Service Mesh Interface logs, CustomResourceDefinitions, and custom resources. Related to PR #48. Resolves issue #67. Signed-off-by: Johnson Shi --- cmd/aks-periscope/aks-periscope.go | 5 ++ deployment/aks-periscope.yaml | 7 +- pkg/collector/smi_collector.go | 107 +++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 pkg/collector/smi_collector.go diff --git a/cmd/aks-periscope/aks-periscope.go b/cmd/aks-periscope/aks-periscope.go index 376adafe..d13a005f 100644 --- a/cmd/aks-periscope/aks-periscope.go +++ b/cmd/aks-periscope/aks-periscope.go @@ -53,6 +53,7 @@ func main() { systemPerfCollector := collector.NewSystemPerfCollector() helmCollector := collector.NewHelmCollector() osmCollector := collector.NewOsmCollector() + smiCollector := collector.NewSmiCollector(exporter) collectors := []interfaces.Collector{ containerLogsCollector, @@ -75,6 +76,10 @@ func main() { collectors = append(collectors, osmCollector) } + if contains(collectorList, "SMI") { + collectors = append(collectors, smiCollector) + } + collectorGrp := new(sync.WaitGroup) for _, c := range collectors { diff --git a/deployment/aks-periscope.yaml b/deployment/aks-periscope.yaml index 31ce837b..0f61aa42 100644 --- a/deployment/aks-periscope.yaml +++ b/deployment/aks-periscope.yaml @@ -28,6 +28,9 @@ rules: - apiGroups: ["apiextensions.k8s.io"] resources: ["customresourcedefinitions"] verbs: ["get", "list", "watch"] +- apiGroups: ["access.smi-spec.io", "specs.smi-spec.io", "split.smi-spec.io"] + resources: ["*"] + verbs: ["get", "list", "watch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding @@ -77,7 +80,7 @@ spec: beta.kubernetes.io/os: linux containers: - name: aks-periscope - image: aksrepos.azurecr.io/staging/aks-periscope:v0.3 + image: johshmsft/aks-periscope:latest securityContext: privileged: true imagePullPolicy: Always @@ -148,7 +151,7 @@ metadata: name: collectors-config namespace: aks-periscope data: - COLLECTOR_LIST: managedCluster # + COLLECTOR_LIST: managedCluster, OSM, SMI # --- apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go new file mode 100644 index 00000000..8d6c972b --- /dev/null +++ b/pkg/collector/smi_collector.go @@ -0,0 +1,107 @@ +package collector + +import ( + "log" + "path/filepath" + "strings" + + "github.com/Azure/aks-periscope/pkg/utils" +) + +// SmiCollector defines an Smi Collector struct +type SmiCollector struct { + data map[string]string +} + +// NewSmiCollector is a constructor +func NewSmiCollector() *SmiCollector { + return &SmiCollector{ + data: make(map[string]string), + } +} + +func (collector *SmiCollector) GetName() string { + return "smi" +} + +// Collect implements the interface method +func (collector *SmiCollector) Collect() error { + // Get all CustomResourceDefinitions in the cluster + allCrdsList, err := utils.GetResourceList([]string{"get", "crds", "-o", "jsonpath={..metadata.name}"}, " ") + if err != nil { + return err + } + + // Directory where logs will be written to + rootPath, err := utils.CreateCollectorDir(collector.GetName()) + if err != nil { + return err + } + + // Filter to obtain a list of Smi CustomResourceDefinitions in the cluster + crdNameContainsSmiPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } + smiCrdsList := filter(allCrdsList, crdNameContainsSmiPredicate) + if len(smiCrdsList) == 0 { + // TODO should this return an error? + log.Printf("Cluster does not contain any SMI CustomResourceDefinitions\n") + return nil + } + + collectSmiCrdDefinitions(collector, filepath.Join(rootPath, "smi_crd_definitions"), smiCrdsList) + collectSmiCustomResourcesFromAllNamespaces(collector, filepath.Join(rootPath, "smi_custom_resources"), smiCrdsList) + + return nil +} + +func collectSmiCrdDefinitions(collector *SmiCollector, rootPath string, smiCrdsList []string) { + for _, smiCrd := range smiCrdsList { + fileName := smiCrd + "_definition.yaml" + kubeCmd := []string{"get", "crd", smiCrd, "-o", "yaml"} + if err := collector.CollectKubectlOutputToCollectorFiles(rootPath, fileName, kubeCmd); err != nil { + log.Printf("Failed to collect %s: %+v", fileName, err) + } + } +} + +func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, rootPath string, smiCrdsList []string) { + // Get all namespaces in the cluster + namespacesList, err := utils.GetResourceList([]string{"get", "namespaces", "-o", "jsonpath={..metadata.name}"}, " ") + if err != nil { + log.Printf("Failed to list namespaces in the cluster: %+v", err) + return + } + + for _, namespace := range namespacesList { + namespaceRootPath := filepath.Join(rootPath, "namespace_"+namespace) + collectSmiCustomResourcesFromSingleNamespace(collector, namespaceRootPath, smiCrdsList, namespace) + } +} + +func collectSmiCustomResourcesFromSingleNamespace(collector *SmiCollector, rootPath string, smiCrdsList []string, namespace string) { + for _, smiCrdType := range smiCrdsList { + // get all custom resources of this smi crd type + customResourcesList, err := utils.GetResourceList([]string{"get", smiCrdType, "-n", namespace, "-o", "jsonpath={..metadata.name}"}, " ") + if err != nil { + log.Printf("Failed to list custom resources of type %s in namespace %s: %+v", smiCrdType, namespace, err) + continue + } + + customResourcesRootPath := filepath.Join(rootPath, smiCrdType+"_custom_resources") + for _, customResourceName := range customResourcesList { + fileName := smiCrdType + "_" + customResourceName + ".yaml" + kubeCmd := []string{"get", smiCrdType, customResourceName, "-n", namespace, "-o", "yaml"} + if err := collector.CollectKubectlOutputToCollectorFiles(customResourcesRootPath, fileName, kubeCmd); err != nil { + log.Printf("Failed to collect %s: %+v", fileName, err) + } + } + } +} + +func filter(ss []string, test func(string) bool) (ret []string) { + for _, s := range ss { + if test(s) { + ret = append(ret, s) + } + } + return +} From 136602433b312e80a3fbaff5dcbe6eb1fa5a08c0 Mon Sep 17 00:00:00 2001 From: Shalier Xia Date: Thu, 24 Jun 2021 13:56:19 -0400 Subject: [PATCH 03/17] Add logic to make flags mutually exclusive --- cmd/aks-periscope/aks-periscope.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/aks-periscope/aks-periscope.go b/cmd/aks-periscope/aks-periscope.go index d13a005f..5281c23c 100644 --- a/cmd/aks-periscope/aks-periscope.go +++ b/cmd/aks-periscope/aks-periscope.go @@ -72,11 +72,11 @@ func main() { collectors = append(collectors, systemPerfCollector) } + // OSM and SMI flags are mutually exclusive if contains(collectorList, "OSM") { collectors = append(collectors, osmCollector) - } - - if contains(collectorList, "SMI") { + collectors = append(collectors, smiCollector) + } else if contains(collectorList, "SMI") { collectors = append(collectors, smiCollector) } From 5053bcb52f93af9dd52f38229a4e515516d3324a Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Sun, 27 Jun 2021 18:06:17 -0700 Subject: [PATCH 04/17] Reverse deployment/aks-periscope.yaml changes. Signed-off-by: Johnson Shi --- deployment/aks-periscope.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deployment/aks-periscope.yaml b/deployment/aks-periscope.yaml index 0f61aa42..29ee8ac8 100644 --- a/deployment/aks-periscope.yaml +++ b/deployment/aks-periscope.yaml @@ -80,7 +80,7 @@ spec: beta.kubernetes.io/os: linux containers: - name: aks-periscope - image: johshmsft/aks-periscope:latest + image: aksrepos.azurecr.io/staging/aks-periscope:v0.3 securityContext: privileged: true imagePullPolicy: Always @@ -151,7 +151,7 @@ metadata: name: collectors-config namespace: aks-periscope data: - COLLECTOR_LIST: managedCluster, OSM, SMI # + COLLECTOR_LIST: managedCluster # --- apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition From 5590524466226ffa4199554e2455b1e9afebd6aa Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Sun, 27 Jun 2021 18:09:59 -0700 Subject: [PATCH 05/17] Rename func to collectSmiCrds Signed-off-by: Johnson Shi --- pkg/collector/smi_collector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go index 8d6c972b..e3edb146 100644 --- a/pkg/collector/smi_collector.go +++ b/pkg/collector/smi_collector.go @@ -47,13 +47,13 @@ func (collector *SmiCollector) Collect() error { return nil } - collectSmiCrdDefinitions(collector, filepath.Join(rootPath, "smi_crd_definitions"), smiCrdsList) + collectSmiCrds(collector, filepath.Join(rootPath, "smi_crd_definitions"), smiCrdsList) collectSmiCustomResourcesFromAllNamespaces(collector, filepath.Join(rootPath, "smi_custom_resources"), smiCrdsList) return nil } -func collectSmiCrdDefinitions(collector *SmiCollector, rootPath string, smiCrdsList []string) { +func collectSmiCrds(collector *SmiCollector, rootPath string, smiCrdsList []string) { for _, smiCrd := range smiCrdsList { fileName := smiCrd + "_definition.yaml" kubeCmd := []string{"get", "crd", smiCrd, "-o", "yaml"} From b06f18760103c8a24ca085956f8e0c1d06d8e1aa Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Sun, 27 Jun 2021 18:30:57 -0700 Subject: [PATCH 06/17] Clarify log messages and error returns Signed-off-by: Johnson Shi --- pkg/collector/smi_collector.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go index e3edb146..d20cf874 100644 --- a/pkg/collector/smi_collector.go +++ b/pkg/collector/smi_collector.go @@ -1,6 +1,7 @@ package collector import ( + "fmt" "log" "path/filepath" "strings" @@ -42,15 +43,11 @@ func (collector *SmiCollector) Collect() error { crdNameContainsSmiPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } smiCrdsList := filter(allCrdsList, crdNameContainsSmiPredicate) if len(smiCrdsList) == 0 { - // TODO should this return an error? - log.Printf("Cluster does not contain any SMI CustomResourceDefinitions\n") - return nil + return fmt.Errorf("Cluster does not contain any SMI CRDs") } collectSmiCrds(collector, filepath.Join(rootPath, "smi_crd_definitions"), smiCrdsList) - collectSmiCustomResourcesFromAllNamespaces(collector, filepath.Join(rootPath, "smi_custom_resources"), smiCrdsList) - - return nil + return collectSmiCustomResourcesFromAllNamespaces(collector, filepath.Join(rootPath, "smi_custom_resources"), smiCrdsList) } func collectSmiCrds(collector *SmiCollector, rootPath string, smiCrdsList []string) { @@ -58,23 +55,24 @@ func collectSmiCrds(collector *SmiCollector, rootPath string, smiCrdsList []stri fileName := smiCrd + "_definition.yaml" kubeCmd := []string{"get", "crd", smiCrd, "-o", "yaml"} if err := collector.CollectKubectlOutputToCollectorFiles(rootPath, fileName, kubeCmd); err != nil { - log.Printf("Failed to collect %s: %+v", fileName, err) + log.Printf("Skipping: unable to collect yaml definition of %s to %s: %+v", smiCrd, fileName, err) } } } -func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, rootPath string, smiCrdsList []string) { +func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, rootPath string, smiCrdsList []string) error { // Get all namespaces in the cluster namespacesList, err := utils.GetResourceList([]string{"get", "namespaces", "-o", "jsonpath={..metadata.name}"}, " ") if err != nil { - log.Printf("Failed to list namespaces in the cluster: %+v", err) - return + return fmt.Errorf("Failed to collect SMI custom resources: unable to list namespaces in the cluster: %+v", err) } for _, namespace := range namespacesList { namespaceRootPath := filepath.Join(rootPath, "namespace_"+namespace) collectSmiCustomResourcesFromSingleNamespace(collector, namespaceRootPath, smiCrdsList, namespace) } + + return nil } func collectSmiCustomResourcesFromSingleNamespace(collector *SmiCollector, rootPath string, smiCrdsList []string, namespace string) { @@ -82,7 +80,7 @@ func collectSmiCustomResourcesFromSingleNamespace(collector *SmiCollector, rootP // get all custom resources of this smi crd type customResourcesList, err := utils.GetResourceList([]string{"get", smiCrdType, "-n", namespace, "-o", "jsonpath={..metadata.name}"}, " ") if err != nil { - log.Printf("Failed to list custom resources of type %s in namespace %s: %+v", smiCrdType, namespace, err) + log.Printf("Skipping: unable to list custom resources of type %s in namespace %s: %+v", smiCrdType, namespace, err) continue } @@ -91,7 +89,7 @@ func collectSmiCustomResourcesFromSingleNamespace(collector *SmiCollector, rootP fileName := smiCrdType + "_" + customResourceName + ".yaml" kubeCmd := []string{"get", smiCrdType, customResourceName, "-n", namespace, "-o", "yaml"} if err := collector.CollectKubectlOutputToCollectorFiles(customResourcesRootPath, fileName, kubeCmd); err != nil { - log.Printf("Failed to collect %s: %+v", fileName, err) + log.Printf("Skipping: unable to collect yaml definition of %s (custom resource type: %s) to %s: %+v", customResourceName, smiCrdType, fileName, err) } } } From a9a8ebe92661459e93e0bab48f3ea2e12eabe963 Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Sun, 27 Jun 2021 18:46:34 -0700 Subject: [PATCH 07/17] Rename func collectSmiCustomResourcesFromNamespace Signed-off-by: Johnson Shi --- pkg/collector/smi_collector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go index d20cf874..7d26ffa7 100644 --- a/pkg/collector/smi_collector.go +++ b/pkg/collector/smi_collector.go @@ -69,13 +69,13 @@ func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, rootPat for _, namespace := range namespacesList { namespaceRootPath := filepath.Join(rootPath, "namespace_"+namespace) - collectSmiCustomResourcesFromSingleNamespace(collector, namespaceRootPath, smiCrdsList, namespace) + collectSmiCustomResourcesFromNamespace(collector, namespaceRootPath, smiCrdsList, namespace) } return nil } -func collectSmiCustomResourcesFromSingleNamespace(collector *SmiCollector, rootPath string, smiCrdsList []string, namespace string) { +func collectSmiCustomResourcesFromNamespace(collector *SmiCollector, rootPath string, smiCrdsList []string, namespace string) { for _, smiCrdType := range smiCrdsList { // get all custom resources of this smi crd type customResourcesList, err := utils.GetResourceList([]string{"get", smiCrdType, "-n", namespace, "-o", "jsonpath={..metadata.name}"}, " ") From 28466ef1ecda2cbdadd5f897a65a0952a253d3dc Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Sun, 27 Jun 2021 18:51:10 -0700 Subject: [PATCH 08/17] Add comment about directories with namespace names Signed-off-by: Johnson Shi --- pkg/collector/smi_collector.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go index 7d26ffa7..fcaa5f30 100644 --- a/pkg/collector/smi_collector.go +++ b/pkg/collector/smi_collector.go @@ -68,6 +68,7 @@ func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, rootPat } for _, namespace := range namespacesList { + // all SMI custom resources in the namespace will be collected to the directory "namespace_X" where X is the namespace name namespaceRootPath := filepath.Join(rootPath, "namespace_"+namespace) collectSmiCustomResourcesFromNamespace(collector, namespaceRootPath, smiCrdsList, namespace) } From 848229a7ea779deccbfccb178ae0163a6cde7b71 Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Sun, 27 Jun 2021 19:09:55 -0700 Subject: [PATCH 09/17] Add test for FilterSliceElemsWithTestPredicate Signed-off-by: Johnson Shi --- pkg/collector/smi_collector.go | 13 ++----------- pkg/utils/helper.go | 11 +++++++++++ tests/aksperiscopeintegration_test.go | 11 +++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go index fcaa5f30..e97d5405 100644 --- a/pkg/collector/smi_collector.go +++ b/pkg/collector/smi_collector.go @@ -40,8 +40,8 @@ func (collector *SmiCollector) Collect() error { } // Filter to obtain a list of Smi CustomResourceDefinitions in the cluster - crdNameContainsSmiPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } - smiCrdsList := filter(allCrdsList, crdNameContainsSmiPredicate) + crdNameContainsSmiTestPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } + smiCrdsList := utils.FilterSliceElemsWithTestPredicate(allCrdsList, crdNameContainsSmiTestPredicate) if len(smiCrdsList) == 0 { return fmt.Errorf("Cluster does not contain any SMI CRDs") } @@ -95,12 +95,3 @@ func collectSmiCustomResourcesFromNamespace(collector *SmiCollector, rootPath st } } } - -func filter(ss []string, test func(string) bool) (ret []string) { - for _, s := range ss { - if test(s) { - ret = append(ret, s) - } - } - return -} diff --git a/pkg/utils/helper.go b/pkg/utils/helper.go index f2954c93..ad581c55 100644 --- a/pkg/utils/helper.go +++ b/pkg/utils/helper.go @@ -366,3 +366,14 @@ func writeDiagnosticCRD(crdName string) error { return nil } + +// FilterSliceElemsWithTestPredicate filters a slice using the test predicate function +// and returns a slice that only contains elements that pass the test predicate function. +func FilterSliceElemsWithTestPredicate(ss []string, testPredicate func(string) bool) (ret []string) { + for _, s := range ss { + if testPredicate(s) { + ret = append(ret, s) + } + } + return +} diff --git a/tests/aksperiscopeintegration_test.go b/tests/aksperiscopeintegration_test.go index 18bf84b7..66d3ca75 100644 --- a/tests/aksperiscopeintegration_test.go +++ b/tests/aksperiscopeintegration_test.go @@ -76,3 +76,14 @@ func checkifpodsrunning(t *testing.T) bool { return false } + +func TestFilterSliceElemsWithTest(t *testing.T) { + g := NewGomegaWithT(t) + + crdNameContainsSmiTestPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } + testSlice := []string{"abc", "httproutegroups.specs.smi-spec.io", "def", "tcproutes.specs.smi-spec.io", "ghi"} + expectedSlice := []string{"httproutegroups.specs.smi-spec.io", "tcproutes.specs.smi-spec.io"} + actualSlice := utils.FilterSliceElemsWithTestPredicate(testSlice, crdNameContainsSmiTestPredicate) + + g.Expect(actualSlice).To(Equal(expectedSlice)) +} From e92aa86cfb8473a7b56d0f8bb606edbf65bcb8bf Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Wed, 7 Jul 2021 13:49:34 -0700 Subject: [PATCH 10/17] Merge smi collector Signed-off-by: Johnson Shi --- cmd/aks-periscope/aks-periscope.go | 2 +- pkg/collector/smi_collector.go | 42 +++++++++++++----------------- pkg/utils/helper.go | 5 ---- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/cmd/aks-periscope/aks-periscope.go b/cmd/aks-periscope/aks-periscope.go index 5281c23c..85d1fc80 100644 --- a/cmd/aks-periscope/aks-periscope.go +++ b/cmd/aks-periscope/aks-periscope.go @@ -53,7 +53,7 @@ func main() { systemPerfCollector := collector.NewSystemPerfCollector() helmCollector := collector.NewHelmCollector() osmCollector := collector.NewOsmCollector() - smiCollector := collector.NewSmiCollector(exporter) + smiCollector := collector.NewSmiCollector() collectors := []interfaces.Collector{ containerLogsCollector, diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go index e97d5405..e8a3d80a 100644 --- a/pkg/collector/smi_collector.go +++ b/pkg/collector/smi_collector.go @@ -3,7 +3,6 @@ package collector import ( "fmt" "log" - "path/filepath" "strings" "github.com/Azure/aks-periscope/pkg/utils" @@ -25,6 +24,10 @@ func (collector *SmiCollector) GetName() string { return "smi" } +func (collector *SmiCollector) GetData() map[string]string { + return collector.data +} + // Collect implements the interface method func (collector *SmiCollector) Collect() error { // Get all CustomResourceDefinitions in the cluster @@ -33,12 +36,6 @@ func (collector *SmiCollector) Collect() error { return err } - // Directory where logs will be written to - rootPath, err := utils.CreateCollectorDir(collector.GetName()) - if err != nil { - return err - } - // Filter to obtain a list of Smi CustomResourceDefinitions in the cluster crdNameContainsSmiTestPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } smiCrdsList := utils.FilterSliceElemsWithTestPredicate(allCrdsList, crdNameContainsSmiTestPredicate) @@ -46,21 +43,21 @@ func (collector *SmiCollector) Collect() error { return fmt.Errorf("Cluster does not contain any SMI CRDs") } - collectSmiCrds(collector, filepath.Join(rootPath, "smi_crd_definitions"), smiCrdsList) - return collectSmiCustomResourcesFromAllNamespaces(collector, filepath.Join(rootPath, "smi_custom_resources"), smiCrdsList) + collectSmiCrds(collector, smiCrdsList) + return collectSmiCustomResourcesFromAllNamespaces(collector, smiCrdsList) } -func collectSmiCrds(collector *SmiCollector, rootPath string, smiCrdsList []string) { +func collectSmiCrds(collector *SmiCollector, smiCrdsList []string) { for _, smiCrd := range smiCrdsList { - fileName := smiCrd + "_definition.yaml" - kubeCmd := []string{"get", "crd", smiCrd, "-o", "yaml"} - if err := collector.CollectKubectlOutputToCollectorFiles(rootPath, fileName, kubeCmd); err != nil { - log.Printf("Skipping: unable to collect yaml definition of %s to %s: %+v", smiCrd, fileName, err) + yamlDefinition, err := utils.RunCommandOnContainer("kubectl", "get", "crd", smiCrd, "-o", "yaml") + if err != nil { + log.Printf("Skipping: unable to collect yaml definition of %s: %+v", smiCrd, err) } + collector.data["smi_crd_definition_"+smiCrd] = yamlDefinition } } -func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, rootPath string, smiCrdsList []string) error { +func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, smiCrdsList []string) error { // Get all namespaces in the cluster namespacesList, err := utils.GetResourceList([]string{"get", "namespaces", "-o", "jsonpath={..metadata.name}"}, " ") if err != nil { @@ -68,15 +65,13 @@ func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, rootPat } for _, namespace := range namespacesList { - // all SMI custom resources in the namespace will be collected to the directory "namespace_X" where X is the namespace name - namespaceRootPath := filepath.Join(rootPath, "namespace_"+namespace) - collectSmiCustomResourcesFromNamespace(collector, namespaceRootPath, smiCrdsList, namespace) + collectSmiCustomResourcesFromNamespace(collector, smiCrdsList, namespace) } return nil } -func collectSmiCustomResourcesFromNamespace(collector *SmiCollector, rootPath string, smiCrdsList []string, namespace string) { +func collectSmiCustomResourcesFromNamespace(collector *SmiCollector, smiCrdsList []string, namespace string) { for _, smiCrdType := range smiCrdsList { // get all custom resources of this smi crd type customResourcesList, err := utils.GetResourceList([]string{"get", smiCrdType, "-n", namespace, "-o", "jsonpath={..metadata.name}"}, " ") @@ -85,13 +80,12 @@ func collectSmiCustomResourcesFromNamespace(collector *SmiCollector, rootPath st continue } - customResourcesRootPath := filepath.Join(rootPath, smiCrdType+"_custom_resources") for _, customResourceName := range customResourcesList { - fileName := smiCrdType + "_" + customResourceName + ".yaml" - kubeCmd := []string{"get", smiCrdType, customResourceName, "-n", namespace, "-o", "yaml"} - if err := collector.CollectKubectlOutputToCollectorFiles(customResourcesRootPath, fileName, kubeCmd); err != nil { - log.Printf("Skipping: unable to collect yaml definition of %s (custom resource type: %s) to %s: %+v", customResourceName, smiCrdType, fileName, err) + yamlDefinition, err := utils.RunCommandOnContainer("kubectl", "get", smiCrdType, customResourceName, "-n", namespace, "-o", "yaml") + if err != nil { + log.Printf("Skipping: unable to collect yaml definition of %s (custom resource type: %s): %+v", customResourceName, smiCrdType, err) } + collector.data["namespace_"+namespace+"_"+smiCrdType+"_custom_resource_"+customResourceName] = yamlDefinition } } } diff --git a/pkg/utils/helper.go b/pkg/utils/helper.go index ad581c55..0fc583d3 100644 --- a/pkg/utils/helper.go +++ b/pkg/utils/helper.go @@ -58,7 +58,6 @@ func CopyFileFromHost(source, destination string) error { if err != nil { return fmt.Errorf("unable to retrieve source content: %w", err) } -<<<<<<< HEAD if err := os.MkdirAll(filepath.Dir(destination), os.ModePerm); err != nil { return fmt.Errorf("create path directories for file %s: %w", destination, err) @@ -74,10 +73,6 @@ func CopyFileFromHost(source, destination string) error { _, err = f.Write([]byte(sourceFile)) if err != nil { return fmt.Errorf("write data to file %s: %w", destination, err) -======= - if err = WriteToFile(destination, sourceFile); err != nil { - return fmt.Errorf("unable to write source file to destination: %w", err) ->>>>>>> bdb3847 (Add text=auto in .gitattributes (#70)) } return nil } From 8b0e991859f9ee02d1dbcd0a6d4fbb5dd6f31795 Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Wed, 7 Jul 2021 13:50:31 -0700 Subject: [PATCH 11/17] Merge Dockerfile Signed-off-by: Johnson Shi --- builder/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/Dockerfile b/builder/Dockerfile index f0f6d788..105fe25f 100644 --- a/builder/Dockerfile +++ b/builder/Dockerfile @@ -30,4 +30,4 @@ RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master COPY --from=builder /build/aks-periscope / -ENTRYPOINT ["/aks-periscope"] \ No newline at end of file +ENTRYPOINT ["/aks-periscope"] From 376c3272af8a16ba11e01c79f1fb2af235a483fb Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Thu, 8 Jul 2021 01:40:20 -0700 Subject: [PATCH 12/17] Address errors comments Signed-off-by: Johnson Shi --- deployment/aks-periscope.yaml | 6 +++--- pkg/collector/smi_collector.go | 5 +++-- run.sh | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100755 run.sh diff --git a/deployment/aks-periscope.yaml b/deployment/aks-periscope.yaml index 29ee8ac8..8971b308 100644 --- a/deployment/aks-periscope.yaml +++ b/deployment/aks-periscope.yaml @@ -80,7 +80,7 @@ spec: beta.kubernetes.io/os: linux containers: - name: aks-periscope - image: aksrepos.azurecr.io/staging/aks-periscope:v0.3 + image: johshmsft/aks-periscope:latest # johshmsft/aks-periscope:latest aksrepos.azurecr.io/staging/aks-periscope:v0.3 securityContext: privileged: true imagePullPolicy: Always @@ -118,8 +118,8 @@ metadata: namespace: aks-periscope type: Opaque data: - AZURE_BLOB_ACCOUNT_NAME: # - AZURE_BLOB_SAS_KEY: # + AZURE_BLOB_ACCOUNT_NAME: YXpzdG9yYWdlYWNjam9oc2g= + AZURE_BLOB_SAS_KEY: P3N2PTIwMjAtMDgtMDQmc3M9YmZxdCZzcnQ9c2NvJnNwPXJ3ZGxhY3VwdGZ4JnNlPTIwMjItMDEtMDFUMTc6MTg6MTZaJnN0PTIwMjEtMDctMDhUMDg6MTg6MTZaJnNwcj1odHRwcyZzaWc9bGhMZ0h4Yjl5QWRQZ05XTU1zR2VRZFRnT2F0NXpiJTJCZGgzS3RBbmd1SmE4JTNE --- apiVersion: v1 kind: ConfigMap diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go index e8a3d80a..bb29b7a1 100644 --- a/pkg/collector/smi_collector.go +++ b/pkg/collector/smi_collector.go @@ -1,6 +1,7 @@ package collector import ( + "errors" "fmt" "log" "strings" @@ -40,7 +41,7 @@ func (collector *SmiCollector) Collect() error { crdNameContainsSmiTestPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } smiCrdsList := utils.FilterSliceElemsWithTestPredicate(allCrdsList, crdNameContainsSmiTestPredicate) if len(smiCrdsList) == 0 { - return fmt.Errorf("Cluster does not contain any SMI CRDs") + return errors.New("cluster does not contain any SMI CRDs") } collectSmiCrds(collector, smiCrdsList) @@ -61,7 +62,7 @@ func collectSmiCustomResourcesFromAllNamespaces(collector *SmiCollector, smiCrds // Get all namespaces in the cluster namespacesList, err := utils.GetResourceList([]string{"get", "namespaces", "-o", "jsonpath={..metadata.name}"}, " ") if err != nil { - return fmt.Errorf("Failed to collect SMI custom resources: unable to list namespaces in the cluster: %+v", err) + return fmt.Errorf("collect SMI custom resources: unable to list namespaces in the cluster: %w", err) } for _, namespace := range namespacesList { diff --git a/run.sh b/run.sh new file mode 100755 index 00000000..df50cff6 --- /dev/null +++ b/run.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -e + +DOCKER_ACCOUNT="johshmsft" +DOCKER_REPO="aks-periscope" + +echo "[****************] DOCKER BUILD AND PUSH [****************]" +docker build -f ./builder/Dockerfile -t "$DOCKER_ACCOUNT/$DOCKER_REPO" . +docker push "$DOCKER_ACCOUNT/$DOCKER_REPO" +echo "" + +AKS_PERISCOPE_NS="aks-periscope" +AKS_PERISCOPE_DEPL_YAML_PATH="deployment/aks-periscope.yaml" + +echo "[****************] KUBECTL DELETE [****************]" +kubectl delete --force -f "$AKS_PERISCOPE_DEPL_YAML_PATH" || true +echo "" + +sleep 7 + +echo "[****************] KUBECTL APPLY [****************]" +kubectl apply --force -f "$AKS_PERISCOPE_DEPL_YAML_PATH" +echo "" + +pods=$(kubectl get pods -n aks-periscope | grep --color=none aks-periscope | awk '{print $1}') +echo "[****************] AKS PERISCOPE PODS [****************]" +echo "${pods[@]}" +echo "" + +for p in ${pods[@]}; do + echo "[****************] LOGS FOR POD $p [****************]" + kubectl wait --for=condition=ready pod "$p" -n "$AKS_PERISCOPE_NS" + sleep 5 + kubectl logs "$p" -n "$AKS_PERISCOPE_NS" + echo "" +done + From 3a4202a3c975013f5a7e68eb02bd61a6488f8258 Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Mon, 12 Jul 2021 16:33:28 -0700 Subject: [PATCH 13/17] Remove dev-test changes and run scripts Signed-off-by: Johnson Shi --- deployment/aks-periscope.yaml | 6 +++--- run.sh | 38 ----------------------------------- 2 files changed, 3 insertions(+), 41 deletions(-) delete mode 100755 run.sh diff --git a/deployment/aks-periscope.yaml b/deployment/aks-periscope.yaml index 8971b308..29ee8ac8 100644 --- a/deployment/aks-periscope.yaml +++ b/deployment/aks-periscope.yaml @@ -80,7 +80,7 @@ spec: beta.kubernetes.io/os: linux containers: - name: aks-periscope - image: johshmsft/aks-periscope:latest # johshmsft/aks-periscope:latest aksrepos.azurecr.io/staging/aks-periscope:v0.3 + image: aksrepos.azurecr.io/staging/aks-periscope:v0.3 securityContext: privileged: true imagePullPolicy: Always @@ -118,8 +118,8 @@ metadata: namespace: aks-periscope type: Opaque data: - AZURE_BLOB_ACCOUNT_NAME: YXpzdG9yYWdlYWNjam9oc2g= - AZURE_BLOB_SAS_KEY: P3N2PTIwMjAtMDgtMDQmc3M9YmZxdCZzcnQ9c2NvJnNwPXJ3ZGxhY3VwdGZ4JnNlPTIwMjItMDEtMDFUMTc6MTg6MTZaJnN0PTIwMjEtMDctMDhUMDg6MTg6MTZaJnNwcj1odHRwcyZzaWc9bGhMZ0h4Yjl5QWRQZ05XTU1zR2VRZFRnT2F0NXpiJTJCZGgzS3RBbmd1SmE4JTNE + AZURE_BLOB_ACCOUNT_NAME: # + AZURE_BLOB_SAS_KEY: # --- apiVersion: v1 kind: ConfigMap diff --git a/run.sh b/run.sh deleted file mode 100755 index df50cff6..00000000 --- a/run.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -set -e - -DOCKER_ACCOUNT="johshmsft" -DOCKER_REPO="aks-periscope" - -echo "[****************] DOCKER BUILD AND PUSH [****************]" -docker build -f ./builder/Dockerfile -t "$DOCKER_ACCOUNT/$DOCKER_REPO" . -docker push "$DOCKER_ACCOUNT/$DOCKER_REPO" -echo "" - -AKS_PERISCOPE_NS="aks-periscope" -AKS_PERISCOPE_DEPL_YAML_PATH="deployment/aks-periscope.yaml" - -echo "[****************] KUBECTL DELETE [****************]" -kubectl delete --force -f "$AKS_PERISCOPE_DEPL_YAML_PATH" || true -echo "" - -sleep 7 - -echo "[****************] KUBECTL APPLY [****************]" -kubectl apply --force -f "$AKS_PERISCOPE_DEPL_YAML_PATH" -echo "" - -pods=$(kubectl get pods -n aks-periscope | grep --color=none aks-periscope | awk '{print $1}') -echo "[****************] AKS PERISCOPE PODS [****************]" -echo "${pods[@]}" -echo "" - -for p in ${pods[@]}; do - echo "[****************] LOGS FOR POD $p [****************]" - kubectl wait --for=condition=ready pod "$p" -n "$AKS_PERISCOPE_NS" - sleep 5 - kubectl logs "$p" -n "$AKS_PERISCOPE_NS" - echo "" -done - From 6cb89395a0905264c5c4aac66d48373ef4b991dc Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Mon, 12 Jul 2021 16:34:12 -0700 Subject: [PATCH 14/17] Rename filter slice util helper param Signed-off-by: Johnson Shi --- pkg/utils/helper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/utils/helper.go b/pkg/utils/helper.go index 0fc583d3..b914014f 100644 --- a/pkg/utils/helper.go +++ b/pkg/utils/helper.go @@ -364,8 +364,8 @@ func writeDiagnosticCRD(crdName string) error { // FilterSliceElemsWithTestPredicate filters a slice using the test predicate function // and returns a slice that only contains elements that pass the test predicate function. -func FilterSliceElemsWithTestPredicate(ss []string, testPredicate func(string) bool) (ret []string) { - for _, s := range ss { +func FilterSliceElemsWithTestPredicate(slice []string, testPredicate func(string) bool) (ret []string) { + for _, s := range slice { if testPredicate(s) { ret = append(ret, s) } From 4be506bf9c6617095cc2a03f6ddf98f51ac04f89 Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Mon, 12 Jul 2021 16:47:21 -0700 Subject: [PATCH 15/17] Create helper test file Signed-off-by: Johnson Shi --- tests/aksperiscopeintegration_test.go | 11 ----------- tests/helper_test.go | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 tests/helper_test.go diff --git a/tests/aksperiscopeintegration_test.go b/tests/aksperiscopeintegration_test.go index 66d3ca75..18bf84b7 100644 --- a/tests/aksperiscopeintegration_test.go +++ b/tests/aksperiscopeintegration_test.go @@ -76,14 +76,3 @@ func checkifpodsrunning(t *testing.T) bool { return false } - -func TestFilterSliceElemsWithTest(t *testing.T) { - g := NewGomegaWithT(t) - - crdNameContainsSmiTestPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } - testSlice := []string{"abc", "httproutegroups.specs.smi-spec.io", "def", "tcproutes.specs.smi-spec.io", "ghi"} - expectedSlice := []string{"httproutegroups.specs.smi-spec.io", "tcproutes.specs.smi-spec.io"} - actualSlice := utils.FilterSliceElemsWithTestPredicate(testSlice, crdNameContainsSmiTestPredicate) - - g.Expect(actualSlice).To(Equal(expectedSlice)) -} diff --git a/tests/helper_test.go b/tests/helper_test.go new file mode 100644 index 00000000..43c8e0a6 --- /dev/null +++ b/tests/helper_test.go @@ -0,0 +1,20 @@ +package main + +import ( + "strings" + "testing" + + "github.com/Azure/aks-periscope/pkg/utils" + . "github.com/onsi/gomega" +) + +func TestFilterSliceElemsWithTest(t *testing.T) { + g := NewGomegaWithT(t) + + crdNameContainsSmiTestPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } + testSlice := []string{"abc", "httproutegroups.specs.smi-spec.io", "def", "tcproutes.specs.smi-spec.io", "ghi"} + expectedSlice := []string{"httproutegroups.specs.smi-spec.io", "tcproutes.specs.smi-spec.io"} + actualSlice := utils.FilterSliceElemsWithTestPredicate(testSlice, crdNameContainsSmiTestPredicate) + + g.Expect(actualSlice).To(Equal(expectedSlice)) +} From 4b1b7723e3758461b891118d90d37c76a8cb2924 Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Fri, 16 Jul 2021 14:21:55 -0700 Subject: [PATCH 16/17] Add permissions to kustomize yaml Signed-off-by: Johnson Shi --- deployment/aks-periscope.yaml | 3 --- deployment/cluster-role.yaml | 6 ++++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/deployment/aks-periscope.yaml b/deployment/aks-periscope.yaml index 29ee8ac8..31ce837b 100644 --- a/deployment/aks-periscope.yaml +++ b/deployment/aks-periscope.yaml @@ -28,9 +28,6 @@ rules: - apiGroups: ["apiextensions.k8s.io"] resources: ["customresourcedefinitions"] verbs: ["get", "list", "watch"] -- apiGroups: ["access.smi-spec.io", "specs.smi-spec.io", "split.smi-spec.io"] - resources: ["*"] - verbs: ["get", "list", "watch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding diff --git a/deployment/cluster-role.yaml b/deployment/cluster-role.yaml index cd58e155..d817103d 100644 --- a/deployment/cluster-role.yaml +++ b/deployment/cluster-role.yaml @@ -9,3 +9,9 @@ rules: - apiGroups: ["aks-periscope.azure.github.com"] resources: ["diagnostics"] verbs: ["get", "watch", "list", "create", "patch"] +- apiGroups: ["apiextensions.k8s.io"] + resources: ["customresourcedefinitions"] + verbs: ["get", "list", "watch"] +- apiGroups: ["access.smi-spec.io", "specs.smi-spec.io", "split.smi-spec.io"] + resources: ["*"] + verbs: ["get", "list", "watch"] From 3b6b9731d2aba888e314235ed2e4418498b3201a Mon Sep 17 00:00:00 2001 From: Johnson Shi Date: Fri, 16 Jul 2021 14:46:50 -0700 Subject: [PATCH 17/17] Filter smi crds directly Signed-off-by: Johnson Shi --- pkg/collector/smi_collector.go | 8 ++++++-- pkg/utils/helper.go | 11 ----------- tests/helper_test.go | 20 -------------------- 3 files changed, 6 insertions(+), 33 deletions(-) delete mode 100644 tests/helper_test.go diff --git a/pkg/collector/smi_collector.go b/pkg/collector/smi_collector.go index bb29b7a1..712c0f93 100644 --- a/pkg/collector/smi_collector.go +++ b/pkg/collector/smi_collector.go @@ -38,8 +38,12 @@ func (collector *SmiCollector) Collect() error { } // Filter to obtain a list of Smi CustomResourceDefinitions in the cluster - crdNameContainsSmiTestPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } - smiCrdsList := utils.FilterSliceElemsWithTestPredicate(allCrdsList, crdNameContainsSmiTestPredicate) + var smiCrdsList []string + for _, s := range allCrdsList { + if strings.Contains(s, "smi-spec.io") { + smiCrdsList = append(smiCrdsList, s) + } + } if len(smiCrdsList) == 0 { return errors.New("cluster does not contain any SMI CRDs") } diff --git a/pkg/utils/helper.go b/pkg/utils/helper.go index b914014f..bf7c1385 100644 --- a/pkg/utils/helper.go +++ b/pkg/utils/helper.go @@ -361,14 +361,3 @@ func writeDiagnosticCRD(crdName string) error { return nil } - -// FilterSliceElemsWithTestPredicate filters a slice using the test predicate function -// and returns a slice that only contains elements that pass the test predicate function. -func FilterSliceElemsWithTestPredicate(slice []string, testPredicate func(string) bool) (ret []string) { - for _, s := range slice { - if testPredicate(s) { - ret = append(ret, s) - } - } - return -} diff --git a/tests/helper_test.go b/tests/helper_test.go deleted file mode 100644 index 43c8e0a6..00000000 --- a/tests/helper_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "strings" - "testing" - - "github.com/Azure/aks-periscope/pkg/utils" - . "github.com/onsi/gomega" -) - -func TestFilterSliceElemsWithTest(t *testing.T) { - g := NewGomegaWithT(t) - - crdNameContainsSmiTestPredicate := func(s string) bool { return strings.Contains(s, "smi-spec.io") } - testSlice := []string{"abc", "httproutegroups.specs.smi-spec.io", "def", "tcproutes.specs.smi-spec.io", "ghi"} - expectedSlice := []string{"httproutegroups.specs.smi-spec.io", "tcproutes.specs.smi-spec.io"} - actualSlice := utils.FilterSliceElemsWithTestPredicate(testSlice, crdNameContainsSmiTestPredicate) - - g.Expect(actualSlice).To(Equal(expectedSlice)) -}