Skip to content
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

Added cancellable context #309

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ delete-test-cluster:

GOPATH := $(shell go env GOPATH)
HAS_GOLANGCI := $(shell $(CHECK) golangci-lint)
GOLANGCI_VERSION := v1.51.2
GOLANGCI_VERSION := v1.63.4
HAS_KIND := $(shell $(CHECK) kind)
HAS_KUBECTL := $(shell $(CHECK) kubectl)
HAS_GOCOV_XML := $(shell $(CHECK) gocov-xml;)
Expand Down
15 changes: 11 additions & 4 deletions action/action.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action

import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -53,7 +54,7 @@ func New(d driver.Driver) Action {
// caller is responsible for persisting the claim records and outputs using the
// SaveOperationResult function. An error is only returned when the operation could not
// be executed, otherwise any error is returned in the OperationResult.
func (a Action) Run(c claim.Claim, creds valuesource.Set, opCfgs ...OperationConfigFunc) (driver.OperationResult, claim.Result, error) {
func (a Action) Run(ctx context.Context, c claim.Claim, creds valuesource.Set, opCfgs ...OperationConfigFunc) (driver.OperationResult, claim.Result, error) {
if a.Driver == nil {
return driver.OperationResult{}, claim.Result{}, errors.New("the action driver is not set")
}
Expand Down Expand Up @@ -84,7 +85,7 @@ func (a Action) Run(c claim.Claim, creds valuesource.Set, opCfgs ...OperationCon
}

var opErr *multierror.Error
opResult, err := a.Driver.Run(op)
opResult, err := a.Driver.Run(ctx, op)
if err != nil {
opErr = multierror.Append(opErr, err)
}
Expand Down Expand Up @@ -279,7 +280,10 @@ func setOutputsOnClaimResult(c claim.Claim, result *claim.Result, opResult drive

for outputName, outputValue := range opResult.Outputs {
outputDef, isDefined := c.Bundle.Outputs[outputName]
result.OutputMetadata.SetGeneratedByBundle(outputName, isDefined)
err := result.OutputMetadata.SetGeneratedByBundle(outputName, isDefined)
if err != nil {
outputErrors = append(outputErrors, err)
}
if isDefined {
err := validateOutputType(c.Bundle, outputName, outputDef, outputValue)
if err != nil {
Expand All @@ -288,7 +292,10 @@ func setOutputsOnClaimResult(c claim.Claim, result *claim.Result, opResult drive
}

if outputValue != "" {
result.OutputMetadata.SetContentDigest(outputName, buildOutputContentDigest(outputValue))
err := result.OutputMetadata.SetContentDigest(outputName, buildOutputContentDigest(outputValue))
if err != nil {
outputErrors = append(outputErrors, err)
}
}
}

Expand Down
23 changes: 12 additions & 11 deletions action/action_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -31,7 +32,7 @@ type mockDriver struct {
func (d *mockDriver) Handles(imageType string) bool {
return d.shouldHandle
}
func (d *mockDriver) Run(op *driver.Operation) (driver.OperationResult, error) {
func (d *mockDriver) Run(ctx context.Context, op *driver.Operation) (driver.OperationResult, error) {
d.Operation = op
fmt.Fprintln(op.Out, "mocked running the bundle")
return d.Result, d.Error
Expand Down Expand Up @@ -720,7 +721,7 @@ func TestAction_RunAction(t *testing.T) {
inst := New(d)
inst.SaveLogs = true

opResult, claimResult, err := inst.Run(c, mockSet, out)
opResult, claimResult, err := inst.Run(context.Background(), c, mockSet, out)
require.NoError(t, err)
require.NoError(t, opResult.Error)
assert.Equal(t, claim.ActionInstall, c.Action)
Expand Down Expand Up @@ -748,7 +749,7 @@ func TestAction_RunAction(t *testing.T) {
inst := New(d)
inst.SaveLogs = false

opResult, _, err := inst.Run(c, mockSet, out)
opResult, _, err := inst.Run(context.Background(), c, mockSet, out)
require.NoError(t, err)
require.NoError(t, opResult.Error)

Expand All @@ -772,7 +773,7 @@ func TestAction_RunAction(t *testing.T) {
op.Files["/tmp/another/path"] = "ANOTHER FILE"
return nil
}
_, _, err := inst.Run(c, mockSet, out, addFile)
_, _, err := inst.Run(context.Background(), c, mockSet, out, addFile)
require.NoError(t, err)
assert.Contains(t, d.Operation.Files, "/tmp/another/path")
})
Expand All @@ -792,7 +793,7 @@ func TestAction_RunAction(t *testing.T) {
sabotage := func(op *driver.Operation) error {
return errors.New("oops")
}
_, _, err := inst.Run(c, mockSet, out, sabotage)
_, _, err := inst.Run(context.Background(), c, mockSet, out, sabotage)
require.EqualError(t, err, "oops")
})

Expand All @@ -805,7 +806,7 @@ func TestAction_RunAction(t *testing.T) {
Error: nil,
}
inst := New(d)
_, claimResult, err := inst.Run(c, mockSet, out)
_, claimResult, err := inst.Run(context.Background(), c, mockSet, out)
require.NoError(t, err)
assert.Equal(t, claim.ActionInstall, c.Action)
assert.Equal(t, claim.StatusSucceeded, claimResult.Status)
Expand Down Expand Up @@ -848,7 +849,7 @@ func TestAction_RunAction(t *testing.T) {
Error: nil,
}
inst := New(d)
opResult, _, err := inst.Run(c, mockSet, out)
opResult, _, err := inst.Run(context.Background(), c, mockSet, out)
require.NoError(t, err)

assert.Contains(t, opResult.Outputs, "hasDefault1", "the output always applies so an output value should have been set")
Expand All @@ -873,7 +874,7 @@ func TestAction_RunAction(t *testing.T) {
Error: nil,
}
inst := New(d)
opResult, _, err := inst.Run(c, mockSet, out)
opResult, _, err := inst.Run(context.Background(), c, mockSet, out)
require.NoError(t, err)
require.Contains(t, opResult.Error.Error(), "required output noDefault is missing and has no default")
})
Expand All @@ -885,7 +886,7 @@ func TestAction_RunAction(t *testing.T) {
Error: errors.New("I always fail"),
}
inst := New(d)
_, _, err := inst.Run(c, mockSet, out)
_, _, err := inst.Run(context.Background(), c, mockSet, out)
require.Error(t, err)
})

Expand All @@ -901,7 +902,7 @@ func TestAction_RunAction(t *testing.T) {
Error: errors.New("I always fail"),
}
inst := New(d)
opResult, claimResult, err := inst.Run(c, mockSet, out)
opResult, claimResult, err := inst.Run(context.Background(), c, mockSet, out)
require.NoError(t, err)
require.Contains(t, opResult.Error.Error(), "I always fail")
assert.Equal(t, claim.ActionInstall, c.Action)
Expand All @@ -922,7 +923,7 @@ func TestAction_RunAction(t *testing.T) {
Error: errors.New("I always fail"),
}
inst := New(d)
opResult, claimResult, err := inst.Run(c, mockSet, out)
opResult, claimResult, err := inst.Run(context.Background(), c, mockSet, out)
require.Error(t, err, "Unknown action should fail")
require.NoError(t, opResult.Error)
assert.Empty(t, claimResult)
Expand Down
3 changes: 2 additions & 1 deletion action/example_install_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action_test

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -61,7 +62,7 @@ func Example_install() {
// Pass an empty set of credentials
var creds valuesource.Set

opResult, claimResult, err := a.Run(c, creds)
opResult, claimResult, err := a.Run(context.Background(), c, creds)
if err != nil {
// Something terrible has occurred and we could not even run the bundle
panic(err)
Expand Down
6 changes: 2 additions & 4 deletions action/example_invoke_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action_test

import (
"context"
"fmt"
"time"

Expand All @@ -23,9 +24,6 @@ func Example_invoke() {

// Load the previous claim for the installation
existingClaim := createInstallClaim()
if err != nil {
panic(err)
}

// Create a claim for running the custom logs action based on the previous claim
c, err := existingClaim.NewClaim("logs", existingClaim.Bundle, parameters)
Expand All @@ -51,7 +49,7 @@ func Example_invoke() {
// Pass an empty set of credentials
var creds valuesource.Set

opResult, claimResult, err := a.Run(c, creds)
opResult, claimResult, err := a.Run(context.Background(), c, creds)
if err != nil {
// Something terrible has occurred and we could not even run the bundle
panic(err)
Expand Down
3 changes: 2 additions & 1 deletion action/example_running_status_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action_test

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -56,7 +57,7 @@ func Example_runningStatus() {
// Save the upgrade claim in the Running Status
saveResult(c, claim.StatusRunning)

opResult, claimResult, err := a.Run(c, creds)
opResult, claimResult, err := a.Run(context.Background(), c, creds)
if err != nil {
// If the bundle isn't run due to an error preparing,
// record a failure so we aren't left stuck in running
Expand Down
3 changes: 2 additions & 1 deletion action/example_upgrade_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action_test

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -44,7 +45,7 @@ func Example_upgrade() {
// Pass an empty set of credentials
var creds valuesource.Set

opResult, claimResult, err := a.Run(c, creds)
opResult, claimResult, err := a.Run(context.Background(), c, creds)
if err != nil {
// Something terrible has occurred and we could not even run the bundle
panic(err)
Expand Down
3 changes: 2 additions & 1 deletion driver/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -26,7 +27,7 @@ type Driver struct {
}

// Run executes the command
func (d *Driver) Run(op *driver.Operation) (driver.OperationResult, error) {
func (d *Driver) Run(_ context.Context, op *driver.Operation) (driver.OperationResult, error) {
return d.exec(op)
}

Expand Down
9 changes: 5 additions & 4 deletions driver/command/command_nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package command

import (
"context"
"os"
"testing"

Expand Down Expand Up @@ -64,7 +65,7 @@ func TestCommandDriverOutputs(t *testing.T) {
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name)
}
op := buildOp()
opResult, err := cmddriver.Run(op)
opResult, err := cmddriver.Run(context.Background(), op)
if err != nil {
t.Fatalf("Driver Run failed %v", err)
}
Expand All @@ -89,7 +90,7 @@ func TestCommandDriverOutputs(t *testing.T) {
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name)
}
op := buildOp()
opResult, err := cmddriver.Run(op)
opResult, err := cmddriver.Run(context.Background(), op)
if err != nil {
t.Fatalf("Driver Run failed %v", err)
}
Expand All @@ -114,7 +115,7 @@ func TestCommandDriverOutputs(t *testing.T) {
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name)
}
op := buildOp()
_, err := cmddriver.Run(op)
_, err := cmddriver.Run(context.Background(), op)
assert.NoError(t, err)
}
CreateAndRunTestCommandDriver(t, name, false, content, testfunc)
Expand All @@ -133,7 +134,7 @@ func TestCommandDriverOutputs(t *testing.T) {
}
op := buildOp()
op.Bundle.Definitions["output2"].Default = "DEFAULT OUTPUT 2"
opResult, err := cmddriver.Run(op)
opResult, err := cmddriver.Run(context.Background(), op)
if err != nil {
t.Fatalf("Driver Run failed %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion driver/debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package debug

import (
"context"
"encoding/json"
"fmt"

Expand All @@ -15,7 +16,7 @@ type Driver struct {
}

// Run executes the operation on the Debug driver
func (d *Driver) Run(op *driver.Operation) (driver.OperationResult, error) {
func (d *Driver) Run(_ context.Context, op *driver.Operation) (driver.OperationResult, error) {
data, err := json.MarshalIndent(op, "", " ")
if err != nil {
return driver.OperationResult{}, err
Expand Down
3 changes: 2 additions & 1 deletion driver/debug/debug_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package debug

import (
"context"
"io/ioutil"
"testing"

Expand Down Expand Up @@ -36,6 +37,6 @@ func TestDebugDriver_Run(t *testing.T) {
Out: ioutil.Discard,
}

_, err := d.Run(op)
_, err := d.Run(context.Background(), op)
is.NoError(err)
}
7 changes: 3 additions & 4 deletions driver/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ type Driver struct {
}

// Run executes the Docker driver
func (d *Driver) Run(op *driver.Operation) (driver.OperationResult, error) {
return d.exec(op)
func (d *Driver) Run(ctx context.Context, op *driver.Operation) (driver.OperationResult, error) {
return d.exec(ctx, op)
}

// Handles indicates that the Docker driver supports "docker" and "oci"
Expand Down Expand Up @@ -181,8 +181,7 @@ func (d *Driver) initializeDockerCli() (command.Cli, error) {
return cli, nil
}

func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
ctx := context.Background()
func (d *Driver) exec(ctx context.Context, op *driver.Operation) (driver.OperationResult, error) {

cli, err := d.initializeDockerCli()
if err != nil {
Expand Down
10 changes: 4 additions & 6 deletions driver/docker/docker_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//go:build integration
// +build integration

package docker

import (
"bytes"
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -92,7 +90,7 @@ func runDriverTest(t *testing.T, image bundle.InvocationImage, skipValidations b
}

docker := &Driver{}
opResult, err := docker.Run(op)
opResult, err := docker.Run(context.Background(), op)

if skipValidations {
return
Expand Down Expand Up @@ -155,7 +153,7 @@ func TestDriver_Run_CaptureOutput(t *testing.T) {
}

docker := &Driver{}
_, err := docker.Run(op)
_, err := docker.Run(context.Background(), op)

assert.NoError(t, err)
assert.Equal(t, "installing bundle...\n", stdout.String())
Expand Down Expand Up @@ -190,7 +188,7 @@ func TestDriver_ValidateImageDigestFail(t *testing.T) {

docker := &Driver{}

_, err := docker.Run(op)
_, err := docker.Run(context.Background(), op)
require.Error(t, err, "expected an error")
// Not asserting actual image digests to support arbitrary integration test images
assert.Contains(t, err.Error(),
Expand Down
Loading