From c796ef9a4ae09c11afb603c5eff74a7e55e928ab Mon Sep 17 00:00:00 2001 From: anushkamittal2001 <138426011+anushkamittal2001@users.noreply.github.com> Date: Thu, 7 Sep 2023 21:15:57 +0530 Subject: [PATCH] fix: Remove os.exit calls in apply command (#8269) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Remove os.exit calls in apply command Signed-off-by: anushkamittal2001 * Resolve linter issues Signed-off-by: anushkamittal2001 * Merged main Signed-off-by: anushkamittal2001 * Remove duplicate code Signed-off-by: anushkamittal2001 * Removed comments for osExit var Signed-off-by: anushkamittal2001 * Minor corrections Signed-off-by: anushkamittal2001 --------- Signed-off-by: anushkamittal2001 Co-authored-by: Charles-Edouard Brétéché --- .../kubectl-kyverno/commands/apply/command.go | 26 +++++++++---------- .../commands/apply/command_test.go | 5 ---- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/cmd/cli/kubectl-kyverno/commands/apply/command.go b/cmd/cli/kubectl-kyverno/commands/apply/command.go index d320c6abc5..49e6513e56 100644 --- a/cmd/cli/kubectl-kyverno/commands/apply/command.go +++ b/cmd/cli/kubectl-kyverno/commands/apply/command.go @@ -67,8 +67,6 @@ type ApplyCommandConfig struct { } // allow os.exit to be overwritten during unit tests -var osExit = os.Exit - func Command() *cobra.Command { var cmd *cobra.Command var removeColor, detailedResults, table bool @@ -101,8 +99,7 @@ func Command() *cobra.Command { } else { printViolations(rc) } - exit(rc, applyCommandConfig.warnExitCode, applyCommandConfig.warnNoPassed) - return nil + return exit(rc, applyCommandConfig.warnExitCode, applyCommandConfig.warnNoPassed) }, } cmd.Flags().StringSliceVarP(&applyCommandConfig.ResourcePaths, "resource", "r", []string{}, "Path to resource files") @@ -167,7 +164,10 @@ func (c *ApplyCommandConfig) applyCommandHelper() (*processor.ResultCounts, []*u if err != nil { return rc, uu, skipInvalidPolicies, er, err } - resources := c.loadResources(policies, validatingAdmissionPolicies, dClient) + resources, err := c.loadResources(policies, validatingAdmissionPolicies, dClient) + if err != nil { + return rc, uu, skipInvalidPolicies, er, err + } rc, uu, skipInvalidPolicies, er, err = c.applyPolicytoResource(variables, policies, validatingAdmissionPolicies, resources, openApiManager, skipInvalidPolicies, dClient, userInfo, mutateLogPathIsDir) if err != nil { return rc, uu, skipInvalidPolicies, er, err @@ -298,13 +298,12 @@ func (c *ApplyCommandConfig) applyPolicytoResource( return &rc, resources, skipInvalidPolicies, responses, nil } -func (c *ApplyCommandConfig) loadResources(policies []kyvernov1.PolicyInterface, validatingAdmissionPolicies []v1alpha1.ValidatingAdmissionPolicy, dClient dclient.Interface) []*unstructured.Unstructured { +func (c *ApplyCommandConfig) loadResources(policies []kyvernov1.PolicyInterface, validatingAdmissionPolicies []v1alpha1.ValidatingAdmissionPolicy, dClient dclient.Interface) ([]*unstructured.Unstructured, error) { resources, err := common.GetResourceAccordingToResourcePath(nil, c.ResourcePaths, c.Cluster, policies, validatingAdmissionPolicies, dClient, c.Namespace, c.PolicyReport, "") if err != nil { - fmt.Printf("Error: failed to load resources\nCause: %s\n", err) - osExit(1) + return resources, fmt.Errorf("Error: failed to load resources\nCause: %s\n", err) } - return resources + return resources, nil } func (c *ApplyCommandConfig) loadPolicies(skipInvalidPolicies SkippedInvalidPolicies) (*processor.ResultCounts, []*unstructured.Unstructured, SkippedInvalidPolicies, []engineapi.EngineResponse, error, []kyvernov1.PolicyInterface, []v1alpha1.ValidatingAdmissionPolicy) { @@ -461,14 +460,15 @@ func printViolations(rc *processor.ResultCounts) { fmt.Printf("\npass: %d, fail: %d, warn: %d, error: %d, skip: %d \n", rc.Pass(), rc.Fail(), rc.Warn(), rc.Error(), rc.Skip()) } -func exit(rc *processor.ResultCounts, warnExitCode int, warnNoPassed bool) { +func exit(rc *processor.ResultCounts, warnExitCode int, warnNoPassed bool) error { if rc.Fail() > 0 || rc.Error() > 0 { - osExit(1) + return fmt.Errorf("exit as fail or error count > 0") } else if rc.Warn() > 0 && warnExitCode != 0 { - osExit(warnExitCode) + return fmt.Errorf("exit as warnExitCode is %d", warnExitCode) } else if rc.Pass() == 0 && warnNoPassed { - osExit(warnExitCode) + return fmt.Errorf("exit as warnExitCode is %d", warnExitCode) } + return nil } func processSkipEngineResponses(responses []engineapi.EngineResponse) []engineapi.EngineResponse { diff --git a/cmd/cli/kubectl-kyverno/commands/apply/command_test.go b/cmd/cli/kubectl-kyverno/commands/apply/command_test.go index 444daf0ff9..86732ca6e8 100644 --- a/cmd/cli/kubectl-kyverno/commands/apply/command_test.go +++ b/cmd/cli/kubectl-kyverno/commands/apply/command_test.go @@ -313,12 +313,7 @@ func Test_Apply(t *testing.T) { }() } desc := fmt.Sprintf("Policies: [%s], / Resources: [%s]", strings.Join(tc.config.PolicyPaths, ","), strings.Join(tc.config.ResourcePaths, ",")) - // prevent os.Exit from being called - osExit = func(code int) { - assert.Check(t, false, "os.Exit(%d) should not be called: %s", code, desc) - } - defer func() { osExit = os.Exit }() _, _, _, responses, err := tc.config.applyCommandHelper() assert.NilError(t, err, desc)