From b2484eb11f58bc250dd3e4abe12aace09fef7716 Mon Sep 17 00:00:00 2001 From: Brandt Keller <43887158+brandtkeller@users.noreply.github.com> Date: Fri, 12 Jan 2024 18:26:26 -0800 Subject: [PATCH] fix: add detection/error for inability to connect to cluster (#211) --- src/cmd/validate/validate.go | 2 +- src/pkg/common/kubernetes/resource.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cmd/validate/validate.go b/src/cmd/validate/validate.go index ac1891da..2a21eb42 100644 --- a/src/cmd/validate/validate.go +++ b/src/cmd/validate/validate.go @@ -48,7 +48,7 @@ var validateCmd = &cobra.Command{ // Primary expected path for validation of OSCAL documents findings, observations, err := ValidateOnPath(opts.InputFile) if err != nil { - message.Fatalf(err, "Validation error") + message.Fatalf(err, "Validation error: %s", err) } report, err := oscal.GenerateAssessmentResults(findings, observations) diff --git a/src/pkg/common/kubernetes/resource.go b/src/pkg/common/kubernetes/resource.go index e2e61261..52dfe233 100644 --- a/src/pkg/common/kubernetes/resource.go +++ b/src/pkg/common/kubernetes/resource.go @@ -65,7 +65,10 @@ func GetResourcesDynamically(ctx context.Context, group string, version string, resource string, namespace string) ( []unstructured.Unstructured, error) { - config := ctrl.GetConfigOrDie() + config, err := ctrl.GetConfig() + if err != nil { + return nil, fmt.Errorf("Error with connection to the Cluster") + } dynamic := dynamic.NewForConfigOrDie(config) resourceId := schema.GroupVersionResource{ @@ -85,7 +88,10 @@ func GetResourcesDynamically(ctx context.Context, } func getGroupVersionResource(kind string) (gvr *schema.GroupVersionResource, err error) { - config := ctrl.GetConfigOrDie() + config, err := ctrl.GetConfig() + if err != nil { + return nil, fmt.Errorf("Error with connection to the Cluster") + } name := strings.Split(kind, "/")[0] discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)