Skip to content

fix: replace path with filepath for Windows compatibility #34

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

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions pkg/jobs/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
"os"
"path"
"path/filepath"
"time"
)

Expand Down Expand Up @@ -60,14 +60,14 @@ func (j Job) Collect(dc *data_collector.DataCollector) error {
}

for fileName, fileValue := range jobResults.Files {
err := os.MkdirAll(path.Dir(fileName), os.ModePerm)
err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
if err != nil {
return err
return fmt.Errorf("MkdirAll failed: %v", err)
}
file, _ := os.Create(fileName)
_, err = file.Write(fileValue)
if err != nil {
return err
return fmt.Errorf("Write failed: %v", err)
}
_ = file.Close()
dc.Logger.Printf("\tJob %s wrote %d bytes to %s\n", j.Name, len(fileValue), fileName)
Expand Down
54 changes: 27 additions & 27 deletions pkg/jobs/nic_job_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"io"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"path"
"path/filepath"
"strings"
"time"
)
Expand All @@ -46,7 +46,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve pod list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "pods.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "pods.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -64,7 +64,7 @@ func NICJobList() []Job {
}
for _, pod := range pods.Items {
for _, container := range pod.Spec.Containers {
logFileName := path.Join(dc.BaseDir, "logs", namespace, fmt.Sprintf("%s__%s.txt", pod.Name, container.Name))
logFileName := filepath.Join(dc.BaseDir, "logs", namespace, fmt.Sprintf("%s__%s.txt", pod.Name, container.Name))
bufferedLogs := dc.K8sCoreClientSet.CoreV1().Pods(namespace).GetLogs(pod.Name, &corev1.PodLogOptions{Container: container.Name})
podLogs, err := bufferedLogs.Stream(context.TODO())
if err != nil {
Expand Down Expand Up @@ -96,7 +96,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve events list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "events.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "events.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -113,7 +113,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve configmap list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "configmaps.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "configmaps.json")] = jsonResult
}
}

Expand All @@ -131,7 +131,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve services list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "services.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "services.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -148,7 +148,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve deployments list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "deployments.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "deployments.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -165,7 +165,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve statefulsets list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "statefulsets.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "statefulsets.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -182,7 +182,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve daemonsets list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "daemonsets.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "daemonsets.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -199,7 +199,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve replicasets list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "replicasets.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "replicasets.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -216,7 +216,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve leases list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "resources", namespace, "leases.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "resources", namespace, "leases.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -233,7 +233,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve roles list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "k8s", "rbac", namespace, "roles.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "k8s", "rbac", namespace, "roles.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -250,7 +250,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve serviceaccounts list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "k8s", "rbac", namespace, "serviceaccounts.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "k8s", "rbac", namespace, "serviceaccounts.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -267,7 +267,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve role bindings list for namespace %s: %v\n", namespace, err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "k8s", "rbac", namespace, "rolebindings.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "k8s", "rbac", namespace, "rolebindings.json")] = jsonResult
}
}
ch <- jobResult
Expand All @@ -283,7 +283,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve server version: %v\n", err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "k8s", "version.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "k8s", "version.json")] = jsonResult
}
ch <- jobResult
},
Expand All @@ -298,7 +298,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve crd data: %v\n", err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "k8s", "crd.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "k8s", "crd.json")] = jsonResult
}
ch <- jobResult
},
Expand All @@ -313,7 +313,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve clusterroles data: %v\n", err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "k8s", "rbac", "clusterroles.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "k8s", "rbac", "clusterroles.json")] = jsonResult
}
ch <- jobResult
},
Expand All @@ -328,7 +328,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve clusterroles binding data: %v\n", err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "k8s", "rbac", "clusterrolesbindings.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "k8s", "rbac", "clusterrolesbindings.json")] = jsonResult
}
ch <- jobResult
},
Expand All @@ -343,7 +343,7 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve nodes information: %v\n", err)
} else {
jsonResult, _ := json.MarshalIndent(result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "k8s", "nodes.json")] = jsonResult
jobResult.Files[filepath.Join(dc.BaseDir, "k8s", "nodes.json")] = jsonResult
}
ch <- jobResult
},
Expand All @@ -358,15 +358,15 @@ func NICJobList() []Job {
dc.Logger.Printf("\tCould not retrieve nodes metrics: %v\n", err)
} else {
jsonNodeMetrics, _ := json.MarshalIndent(nodeMetrics, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "metrics", "node-resource-list.json")] = jsonNodeMetrics
jobResult.Files[filepath.Join(dc.BaseDir, "metrics", "node-resource-list.json")] = jsonNodeMetrics
}
for _, namespace := range dc.Namespaces {
podMetrics, _ := dc.K8sMetricsClientSet.MetricsV1beta1().PodMetricses(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
dc.Logger.Printf("\tCould not retrieve pods metrics for namespace %s: %v\n", namespace, err)
} else {
jsonPodMetrics, _ := json.MarshalIndent(podMetrics, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "metrics", namespace, "pod-resource-list.json")] = jsonPodMetrics
jobResult.Files[filepath.Join(dc.BaseDir, "metrics", namespace, "pod-resource-list.json")] = jsonPodMetrics
}
}
ch <- jobResult
Expand All @@ -382,7 +382,7 @@ func NICJobList() []Job {
if err != nil {
dc.Logger.Printf("\tCould not retrieve helm information: %v\n", err)
} else {
jobResult.Files[path.Join(dc.BaseDir, "helm", "settings.json")] = jsonSettings
jobResult.Files[filepath.Join(dc.BaseDir, "helm", "settings.json")] = jsonSettings
}
ch <- jobResult
},
Expand All @@ -399,8 +399,8 @@ func NICJobList() []Job {
} else {
for _, release := range releases {
jsonRelease, _ := json.MarshalIndent(release, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "helm", namespace, release.Name+"_release.json")] = jsonRelease
jobResult.Files[path.Join(dc.BaseDir, "helm", namespace, release.Name+"_manifest.txt")] = []byte(release.Manifest)
jobResult.Files[filepath.Join(dc.BaseDir, "helm", namespace, release.Name+"_release.json")] = jsonRelease
jobResult.Files[filepath.Join(dc.BaseDir, "helm", namespace, release.Name+"_manifest.txt")] = []byte(release.Manifest)
}
}
}
Expand All @@ -425,7 +425,7 @@ func NICJobList() []Job {
if err != nil {
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
} else {
jobResult.Files[path.Join(dc.BaseDir, "exec", namespace, pod.Name+"__nginx-t.txt")] = res
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, pod.Name+"__nginx-t.txt")] = res
}
}
}
Expand All @@ -451,7 +451,7 @@ func NICJobList() []Job {
if err != nil {
dc.Logger.Printf("\tCommand execution %s failed for pod %s in namespace %s: %v\n", command, pod.Name, namespace, err)
} else {
jobResult.Files[path.Join(dc.BaseDir, "exec", namespace, pod.Name+"__nginx-ingress-version.txt")] = res
jobResult.Files[filepath.Join(dc.BaseDir, "exec", namespace, pod.Name+"__nginx-ingress-version.txt")] = res
}
}
}
Expand All @@ -473,7 +473,7 @@ func NICJobList() []Job {
} else {
var jsonResult bytes.Buffer
_ = json.Indent(&jsonResult, result, "", " ")
jobResult.Files[path.Join(dc.BaseDir, "crds", namespace, crd.Resource+".json")] = jsonResult.Bytes()
jobResult.Files[filepath.Join(dc.BaseDir, "crds", namespace, crd.Resource+".json")] = jsonResult.Bytes()
}
}
}
Expand Down