Skip to content

Commit

Permalink
Merge pull request #92 from reactiveops/rs/better-error-handling
Browse files Browse the repository at this point in the history
Improving error handling
  • Loading branch information
robscott authored May 16, 2019
2 parents 5921341 + 40bb07b commit 1194c8b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,18 @@ func main() {
if *webhook {
startWebhookServer(c, *disableWebhookConfigInstaller, *webhookPort)
} else if *dashboard {
k, _ := kube.CreateResourceProvider(*auditPath)
k, err := kube.CreateResourceProvider(*auditPath)
if err != nil {
logrus.Errorf("Error fetching Kubernetes resources %v", err)
os.Exit(1)
}
startDashboardServer(c, k, *dashboardPort)
} else if *audit {
k, _ := kube.CreateResourceProvider(*auditPath)
k, err := kube.CreateResourceProvider(*auditPath)
if err != nil {
logrus.Errorf("Error fetching Kubernetes resources %v", err)
os.Exit(1)
}
runAudit(c, k, *auditOutputFile, *auditOutputURL)
}
}
Expand Down
23 changes: 15 additions & 8 deletions pkg/kube/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ func CreateResourceProviderFromPath(directory string) (*ResourceProvider, error)
dep := appsv1.Deployment{}
err = decoder.Decode(&dep)
if err != nil {
logrus.Errorf("Error parsing deployment %v", err)
return err
}
resources.Deployments = append(resources.Deployments, dep)
} else if resource.Kind == "Namespace" {
ns := corev1.Namespace{}
err = decoder.Decode(&ns)
if err != nil {
logrus.Errorf("Error parsing namespace %v", err)
return err
}
resources.Namespaces = append(resources.Namespaces, ns)
} else if resource.Kind == "Pod" {
pod := corev1.Pod{}
err = decoder.Decode(&pod)
if err != nil {
logrus.Errorf("Error parsing pod %v", err)
return err
}
resources.Pods = append(resources.Pods, pod)
Expand All @@ -91,12 +94,14 @@ func CreateResourceProviderFromPath(directory string) (*ResourceProvider, error)
}
contents, err := ioutil.ReadFile(path)
if err != nil {
logrus.Errorf("Error reading file %v", path)
return err
}
specs := regexp.MustCompile("\n-+\n").Split(string(contents), -1)
for _, spec := range specs {
err = addYaml(spec)
if err != nil {
logrus.Errorf("Error parsing YAML %v", err)
return err
}
}
Expand All @@ -115,6 +120,7 @@ func CreateResourceProviderFromCluster() (*ResourceProvider, error) {
kubeConf := config.GetConfigOrDie()
api, err := kubernetes.NewForConfig(kubeConf)
if err != nil {
logrus.Errorf("Error creating Kubernetes client %v", err)
return nil, err
}
return CreateResourceProviderFromAPI(api)
Expand All @@ -125,34 +131,35 @@ func CreateResourceProviderFromAPI(kube kubernetes.Interface) (*ResourceProvider
listOpts := metav1.ListOptions{}
serverVersion, err := kube.Discovery().ServerVersion()
if err != nil {
logrus.Errorf("Error fetching Kubernetes API version %v", err)
return nil, err
}
deploys, err := kube.AppsV1().Deployments("").List(listOpts)
if err != nil {
logrus.Errorf("Error fetching Kubernetes Deployments %v", err)
return nil, err
}
nodes, err := kube.CoreV1().Nodes().List(listOpts)
if err != nil {
logrus.Errorf("Error fetching Kubernetes Nodes %v", err)
return nil, err
}
namespaces, err := kube.CoreV1().Namespaces().List(listOpts)
if err != nil {
logrus.Errorf("Error fetching Kubernetes Namespaces %v", err)
return nil, err
}
allPods := []corev1.Pod{}
for _, ns := range namespaces.Items {
pods, err := kube.CoreV1().Pods(ns.Name).List(listOpts)
if err != nil {
return nil, err
}
allPods = append(allPods, pods.Items...)
pods, err := kube.CoreV1().Pods("").List(listOpts)
if err != nil {
logrus.Errorf("Error fetching Kubernetes Pods %v", err)
return nil, err
}
api := ResourceProvider{
ServerVersion: serverVersion.Major + "." + serverVersion.Minor,
Deployments: deploys.Items,
Nodes: nodes.Items,
Namespaces: namespaces.Items,
Pods: allPods,
Pods: pods.Items,
}
return &api, nil
}

0 comments on commit 1194c8b

Please sign in to comment.