Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SMI log collector #71

Merged
merged 17 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/aks-periscope/aks-periscope.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func main() {
systemPerfCollector := collector.NewSystemPerfCollector()
helmCollector := collector.NewHelmCollector()
osmCollector := collector.NewOsmCollector()
smiCollector := collector.NewSmiCollector()

collectors := []interfaces.Collector{
containerLogsCollector,
Expand All @@ -71,8 +72,12 @@ func main() {
collectors = append(collectors, systemPerfCollector)
}

// OSM and SMI flags are mutually exclusive
if contains(collectorList, "OSM") {
collectors = append(collectors, osmCollector)
collectors = append(collectors, smiCollector)
} else if contains(collectorList, "SMI") {
collectors = append(collectors, smiCollector)
}

collectorGrp := new(sync.WaitGroup)
Expand Down
3 changes: 3 additions & 0 deletions deployment/aks-periscope.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
johnsonshi marked this conversation as resolved.
Show resolved Hide resolved
arnaud-tincelin marked this conversation as resolved.
Show resolved Hide resolved
resources: ["*"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
92 changes: 92 additions & 0 deletions pkg/collector/smi_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package collector

import (
"errors"
"fmt"
"log"
"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"
}

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
allCrdsList, err := utils.GetResourceList([]string{"get", "crds", "-o", "jsonpath={..metadata.name}"}, " ")
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)
if len(smiCrdsList) == 0 {
return errors.New("cluster does not contain any SMI CRDs")
}

collectSmiCrds(collector, smiCrdsList)
return collectSmiCustomResourcesFromAllNamespaces(collector, smiCrdsList)
}

func collectSmiCrds(collector *SmiCollector, smiCrdsList []string) {
for _, smiCrd := range smiCrdsList {
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, smiCrdsList []string) error {
// Get all namespaces in the cluster
namespacesList, err := utils.GetResourceList([]string{"get", "namespaces", "-o", "jsonpath={..metadata.name}"}, " ")
if err != nil {
return fmt.Errorf("collect SMI custom resources: unable to list namespaces in the cluster: %w", err)
}

for _, namespace := range namespacesList {
collectSmiCustomResourcesFromNamespace(collector, smiCrdsList, namespace)
}

return nil
}

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}"}, " ")
if err != nil {
log.Printf("Skipping: unable to list custom resources of type %s in namespace %s: %+v", smiCrdType, namespace, err)
continue
}

for _, customResourceName := range customResourcesList {
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
}
}
}
11 changes: 11 additions & 0 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,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(slice []string, testPredicate func(string) bool) (ret []string) {
for _, s := range slice {
if testPredicate(s) {
ret = append(ret, s)
}
}
return
}
20 changes: 20 additions & 0 deletions tests/helper_test.go
Original file line number Diff line number Diff line change
@@ -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))
}