forked from Azure/aks-periscope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add log collector for Service Mesh Interface logs, CustomResourceDefinitions, and custom resources. Related to PR Azure#48. Resolves issue Azure#67. Signed-off-by: Johnson Shi <[email protected]>
- Loading branch information
1 parent
fc965f2
commit 45af0a3
Showing
3 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |