Skip to content

Commit

Permalink
[fix] close #189
Browse files Browse the repository at this point in the history
  • Loading branch information
gmeghnag committed Jan 5, 2025
1 parent 1a326d3 commit 91bfe3c
Showing 1 changed file with 52 additions and 48 deletions.
100 changes: 52 additions & 48 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var GetCmd = &cobra.Command{
}
err := validateArgs(args)
if err != nil {
fmt.Println(err)
fmt.Fprintf(os.Stderr, "%s", err.Error())
os.Exit(1)
}
for resource := range vars.GetArgs {
Expand Down Expand Up @@ -129,7 +129,7 @@ func init() {
vars.JsonPathList = types.JsonPathList{Kind: "List", ApiVersion: "v1"}
err := goyaml.Unmarshal(yamlData, vars.KnownResources)
if err != nil {
fmt.Println(err)
fmt.Fprintf(os.Stderr, "%s", err.Error())
}
vars.Schema = runtime.NewScheme()
schemeBuilder := runtime.SchemeBuilder{
Expand Down Expand Up @@ -193,7 +193,7 @@ func getNamespacedResources(resourceNamePlural string, resourceGroup string, res
if vars.AllNamespaceBoolVar {
vars.Namespace = ""
vars.ShowNamespace = true
_namespaces, _ := ioutil.ReadDir(vars.MustGatherRootPath + "/namespaces/")
_namespaces, _ := ReadDirForResources(vars.MustGatherRootPath + "/namespaces/")
for _, f := range _namespaces {
namespaces = append(namespaces, f.Name())
}
Expand All @@ -202,9 +202,54 @@ func getNamespacedResources(resourceNamePlural string, resourceGroup string, res
}
for _, namespace := range namespaces {
UnstructuredItems := types.UnstructuredList{ApiVersion: "v1", Kind: "List"}
resourcePath := fmt.Sprintf("%s/namespaces/%s/%s/%s.yaml", vars.MustGatherRootPath, namespace, resourceGroup, resourceNamePlural)
_file, err := ioutil.ReadFile(resourcePath)
if err != nil && resourceNamePlural != "pods" {
resourcesItemsPath := fmt.Sprintf("%s/namespaces/%s/%s/%s.yaml", vars.MustGatherRootPath, namespace, resourceGroup, resourceNamePlural)
_file, err := os.ReadFile(resourcesItemsPath)
if err == nil { // able to read <resourceplural>.yaml, which contains list of items, i.e. /namespaces/<NAMESPACE>/core/pods.yaml
err := yaml.Unmarshal(_file, &UnstructuredItems)
if err != nil { // unable to unmarshal the file, it may be empty or corrupted
// We handle this situation by looking for the pod in the pods directory
fStat, _ := os.Stat(resourcesItemsPath)
fSize := fStat.Size()
if resourceNamePlural == "pods" && fSize == 0 {
// tranverse the pods directory and fill in UnstructuredItems.Items
podsDir := fmt.Sprintf("%s/namespaces/%s/pods", vars.MustGatherRootPath, namespace)
pods, rErr := ReadDirForResources(podsDir)
if rErr != nil {
klog.V(3).ErrorS(err, "Failed to read resources:")
}
for _, pod := range pods {
podName := pod.Name()
podPath := fmt.Sprintf("%s/%s/%s.yaml", podsDir, podName, podName)
_file, err := os.ReadFile(podPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading %s: %s\n", podPath, err)
os.Exit(1)
}
var podItem unstructured.Unstructured
if err := yaml.Unmarshal(_file, &podItem); err != nil {
fmt.Fprintln(os.Stderr, "Error when trying to unmarshal file "+podPath)
os.Exit(1)
}
if podItem.Object != nil {
UnstructuredItems.Items = append(UnstructuredItems.Items, podItem)
}
}
} else {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
for _, item := range UnstructuredItems.Items {
if len(resources) > 0 {
_, ok := resources[item.GetName()]
if ok {
handleObject(item)
}
} else {
handleObject(item)
}
}
} else { // the resources are customresources so, stored in a single file per resource
resourceDir := fmt.Sprintf("%s/namespaces/%s/%s/%s", vars.MustGatherRootPath, namespace, resourceGroup, resourceNamePlural)
_, err = os.Stat(resourceDir)
if err == nil {
Expand All @@ -214,7 +259,7 @@ func getNamespacedResources(resourceNamePlural string, resourceGroup string, res
}
for _, f := range resourcesFiles {
resourceYamlPath := resourceDir + "/" + f.Name()
_file, _ := ioutil.ReadFile(resourceYamlPath)
_file, _ := os.ReadFile(resourceYamlPath)
item := unstructured.Unstructured{}
if err := yaml.Unmarshal(_file, &item); err != nil {
fmt.Fprintln(os.Stderr, "Error when trying to unmarshal file: "+resourceYamlPath)
Expand All @@ -230,47 +275,6 @@ func getNamespacedResources(resourceNamePlural string, resourceGroup string, res
}
}
}
} else {
// Sometimes the core/pods.yaml might be empty due to unknown reasons when MG is collected
// We handle this situation by looking for the pod in the pods directory
if resourceNamePlural == "pods" {
// tranverse the pods directory and fill in UnstructuredItems.Items
podsDir := fmt.Sprintf("%s/namespaces/%s/pods", vars.MustGatherRootPath, namespace)
pods, rErr := ReadDirForResources(podsDir)
if rErr != nil {
klog.V(3).ErrorS(err, "Failed to read resources:")
}
for _, pod := range pods {
podName := pod.Name()
podPath := fmt.Sprintf("%s/%s/%s.yaml", podsDir, podName, podName)
_file, err := ioutil.ReadFile(podPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading %s: %s\n", podPath, err)
os.Exit(1)
}
var podItem unstructured.Unstructured
if err := yaml.Unmarshal(_file, &podItem); err != nil {
fmt.Fprintln(os.Stderr, "Error when trying to unmarshal file "+podPath)
os.Exit(1)
}
if podItem.Object != nil {
UnstructuredItems.Items = append(UnstructuredItems.Items, podItem)
}
}
} else if err := yaml.Unmarshal(_file, &UnstructuredItems); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
for _, item := range UnstructuredItems.Items {
if len(resources) > 0 {
_, ok := resources[item.GetName()]
if ok {
handleObject(item)
}
} else {
handleObject(item)
}
}
}
}
}
Expand Down

0 comments on commit 91bfe3c

Please sign in to comment.