From 4f6814afb51f25f6cc3ccf7d5fa81abc456f141a Mon Sep 17 00:00:00 2001 From: Jacob LeGrone Date: Wed, 7 Aug 2019 14:33:02 -0400 Subject: [PATCH] refactor(hooks): replace hook execution Successful bool with HookPhase Signed-off-by: Jacob LeGrone --- cmd/helm/status_test.go | 24 +++++++++-- .../output/status-with-test-suite.txt | 9 +++- pkg/action/hooks.go | 18 ++++---- pkg/action/printer.go | 2 +- pkg/release/hook.go | 21 ++++++++-- pkg/release/responses.go | 6 --- pkg/release/test_run.go | 41 ------------------- 7 files changed, 58 insertions(+), 63 deletions(-) delete mode 100644 pkg/release/test_run.go diff --git a/cmd/helm/status_test.go b/cmd/helm/status_test.go index 8aca8aefb..167c1ed38 100644 --- a/cmd/helm/status_test.go +++ b/cmd/helm/status_test.go @@ -84,16 +84,34 @@ func TestStatusCmd(t *testing.T) { Status: release.StatusDeployed, }, &release.Hook{ - Name: "foo", + Name: "never-run-test", Events: []release.HookEvent{release.HookTest}, }, &release.Hook{ - Name: "bar", + Name: "passing-test", Events: []release.HookEvent{release.HookTest}, LastRun: release.HookExecution{ StartedAt: mustParseTime("2006-01-02T15:04:05Z"), CompletedAt: mustParseTime("2006-01-02T15:04:07Z"), - Successful: true, + Phase: release.HookPhaseSucceeded, + }, + }, + &release.Hook{ + Name: "failing-test", + Events: []release.HookEvent{release.HookTest}, + LastRun: release.HookExecution{ + StartedAt: mustParseTime("2006-01-02T15:10:05Z"), + CompletedAt: mustParseTime("2006-01-02T15:10:07Z"), + Phase: release.HookPhaseFailed, + }, + }, + &release.Hook{ + Name: "passing-pre-install", + Events: []release.HookEvent{release.HookPreInstall}, + LastRun: release.HookExecution{ + StartedAt: mustParseTime("2006-01-02T15:00:05Z"), + CompletedAt: mustParseTime("2006-01-02T15:00:07Z"), + Phase: release.HookPhaseSucceeded, }, }, ), diff --git a/cmd/helm/testdata/output/status-with-test-suite.txt b/cmd/helm/testdata/output/status-with-test-suite.txt index 6790ea5ea..69c9cf425 100644 --- a/cmd/helm/testdata/output/status-with-test-suite.txt +++ b/cmd/helm/testdata/output/status-with-test-suite.txt @@ -3,8 +3,13 @@ LAST DEPLOYED: 2016-01-16 00:00:00 +0000 UTC NAMESPACE: default STATUS: deployed -TEST SUITE: bar +TEST SUITE: passing-test Last Started: 2006-01-02 15:04:05 +0000 UTC Last Completed: 2006-01-02 15:04:07 +0000 UTC -Successful: true +Phase: Succeeded + +TEST SUITE: failing-test +Last Started: 2006-01-02 15:10:05 +0000 UTC +Last Completed: 2006-01-02 15:10:07 +0000 UTC +Phase: Failed diff --git a/pkg/action/hooks.go b/pkg/action/hooks.go index 84cedead3..8338b599a 100644 --- a/pkg/action/hooks.go +++ b/pkg/action/hooks.go @@ -53,24 +53,28 @@ func (cfg *Configuration) execHook(rl *release.Release, hook release.HookEvent, } // Get the time at which the hook was applied to the cluster - start := time.Now() - err = cfg.KubeClient.WatchUntilReady(resources, timeout) h.LastRun = release.HookExecution{ - StartedAt: start, - CompletedAt: time.Now(), - Successful: err == nil, + StartedAt: time.Now(), + Phase: release.HookPhaseUnknown, } + // Execute the hook + err = cfg.KubeClient.WatchUntilReady(resources, timeout) + // Note the time of success/failure + h.LastRun.CompletedAt = time.Now() + // Mark hook as succeeded or failed if err != nil { - // If a hook is failed, checkout the annotation of the hook to determine whether the hook should be deleted + h.LastRun.Phase = release.HookPhaseFailed + // If a hook is failed, check the annotation of the hook to determine whether the hook should be deleted // under failed condition. If so, then clear the corresponding resource object in the hook if err := cfg.deleteHookByPolicy(h, release.HookFailed); err != nil { return err } return err } + h.LastRun.Phase = release.HookPhaseSucceeded } - // If all hooks are succeeded, checkout the annotation of each hook to determine whether the hook should be deleted + // If all hooks are successful, check the annotation of each hook to determine whether the hook should be deleted // under succeeded condition. If so, then clear the corresponding resource object in each hook for _, h := range executingHooks { if err := cfg.deleteHookByPolicy(h, release.HookSucceeded); err != nil { diff --git a/pkg/action/printer.go b/pkg/action/printer.go index 6fe3c6385..128e9fb65 100644 --- a/pkg/action/printer.go +++ b/pkg/action/printer.go @@ -57,7 +57,7 @@ func PrintRelease(out io.Writer, rel *release.Release) { h.Name, fmt.Sprintf("Last Started: %s", h.LastRun.StartedAt), fmt.Sprintf("Last Completed: %s", h.LastRun.CompletedAt), - fmt.Sprintf("Successful: %t", h.LastRun.Successful), + fmt.Sprintf("Phase: %s", h.LastRun.Phase), ) } } diff --git a/pkg/release/hook.go b/pkg/release/hook.go index f29da4a72..5cec89e3a 100644 --- a/pkg/release/hook.go +++ b/pkg/release/hook.go @@ -82,8 +82,23 @@ type Hook struct { type HookExecution struct { // StartedAt indicates the date/time this hook was started StartedAt time.Time `json:"started_at,omitempty"` - // CompletedAt indicates the date/time this hook was completed + // CompletedAt indicates the date/time this hook was completed. CompletedAt time.Time `json:"completed_at,omitempty"` - // Successful indicates whether the hook completed successfully - Successful bool `json:"successful"` + // Phase indicates whether the hook completed successfully + Phase HookPhase `json:"phase"` } + +// A HookPhase indicates the state of a hook execution +type HookPhase string + +const ( + // HookPhaseUnknown indicates that a hook is in an unknown state + HookPhaseUnknown HookPhase = "Unknown" + // HookPhaseSucceeded indicates that hook execution succeeded + HookPhaseSucceeded HookPhase = "Succeeded" + // HookPhaseFailed indicates that hook execution failed + HookPhaseFailed HookPhase = "Failed" +) + +// Strng converts a hook phase to a printable string +func (x HookPhase) String() string { return string(x) } diff --git a/pkg/release/responses.go b/pkg/release/responses.go index 6eb9cbb5a..10b7f2054 100644 --- a/pkg/release/responses.go +++ b/pkg/release/responses.go @@ -32,9 +32,3 @@ type UninstallReleaseResponse struct { // Info is an uninstall message Info string `json:"info,omitempty"` } - -// TestReleaseResponse represents a message from executing a test -type TestReleaseResponse struct { - Msg string `json:"msg,omitempty"` - Status TestRunStatus `json:"status,omitempty"` -} diff --git a/pkg/release/test_run.go b/pkg/release/test_run.go deleted file mode 100644 index ff55301ab..000000000 --- a/pkg/release/test_run.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright The Helm Authors. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package release - -import "time" - -// TestRunStatus is the status of a test run -type TestRunStatus string - -// Indicates the results of a test run -const ( - TestRunUnknown TestRunStatus = "unknown" - TestRunSuccess TestRunStatus = "success" - TestRunFailure TestRunStatus = "failure" - TestRunRunning TestRunStatus = "running" -) - -// Strng converts a test run status to a printable string -func (x TestRunStatus) String() string { return string(x) } - -// TestRun describes the run of a test -type TestRun struct { - Name string `json:"name,omitempty"` - Status TestRunStatus `json:"status,omitempty"` - Info string `json:"info,omitempty"` - StartedAt time.Time `json:"started_at,omitempty"` - CompletedAt time.Time `json:"completed_at,omitempty"` -}