This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b79970  fix context.Context misuse
6b79970 is described below

commit 6b799702bfcccb7e28d68703311d183a84e4302a
Author: lburgazzoli <[email protected]>
AuthorDate: Mon Feb 4 11:44:36 2019 +0100

    fix context.Context misuse
---
 cmd/util/publisher/publisher.go            |  5 +--
 pkg/builder/builder.go                     | 12 +++----
 pkg/builder/builder_steps.go               |  2 +-
 pkg/builder/builder_types.go               |  6 ++--
 pkg/builder/kaniko/publisher.go            |  6 ++--
 pkg/builder/s2i/publisher.go               | 12 +++----
 pkg/controller/integration/build_image.go  |  4 ++-
 pkg/controller/integrationcontext/build.go |  8 +++--
 pkg/platform/platform.go                   |  2 +-
 pkg/util/cancellable/cancellable.go        | 51 ++++++++++++++++++++++++++++++
 test/build_manager_integration_test.go     | 11 ++++---
 11 files changed, 88 insertions(+), 31 deletions(-)

diff --git a/cmd/util/publisher/publisher.go b/cmd/util/publisher/publisher.go
index 9d21e68..20baf50 100644
--- a/cmd/util/publisher/publisher.go
+++ b/cmd/util/publisher/publisher.go
@@ -18,7 +18,6 @@ limitations under the License.
 package main
 
 import (
-       "context"
        "fmt"
        "io/ioutil"
        "os"
@@ -29,6 +28,8 @@ import (
        "strings"
        "time"
 
+       "github.com/apache/camel-k/pkg/util/cancellable"
+
        "github.com/apache/camel-k/deploy"
        "github.com/apache/camel-k/pkg/apis"
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
@@ -135,9 +136,9 @@ func (options *PublisherOptions) build(component string, 
camelVersion string) er
        dependencies = append(dependencies, "camel:"+component)
 
        ctx := builder.Context{
-               C:    context.TODO(),
                Path: dir,
                Request: builder.Request{
+                       C: cancellable.NewContext(),
                        Platform: v1alpha1.IntegrationPlatformSpec{
                                Build: v1alpha1.IntegrationPlatformBuildSpec{
                                        CamelVersion: camelVersion,
diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go
index 25d18d6..07ca213 100644
--- a/pkg/builder/builder.go
+++ b/pkg/builder/builder.go
@@ -18,7 +18,6 @@ limitations under the License.
 package builder
 
 import (
-       "context"
        "errors"
        "fmt"
        "io/ioutil"
@@ -28,6 +27,8 @@ import (
        "sync/atomic"
        "time"
 
+       "github.com/apache/camel-k/pkg/util/cancellable"
+
        "k8s.io/apimachinery/pkg/apis/meta/v1"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
@@ -48,7 +49,7 @@ type buildTask struct {
 
 type defaultBuilder struct {
        log       log.Logger
-       ctx       context.Context
+       ctx       cancellable.Context
        client    client.Client
        tasks     chan buildTask
        interrupt chan bool
@@ -58,10 +59,10 @@ type defaultBuilder struct {
 }
 
 // New --
-func New(ctx context.Context, c client.Client, namespace string) Builder {
+func New(c client.Client, namespace string) Builder {
        m := defaultBuilder{
                log:       log.WithName("builder"),
-               ctx:       ctx,
+               ctx:       cancellable.NewContext(),
                client:    c,
                tasks:     make(chan buildTask),
                interrupt: make(chan bool, 1),
@@ -104,7 +105,7 @@ func (b *defaultBuilder) Submit(request Request, handler 
func(*Result)) {
 }
 
 func (b *defaultBuilder) Close() {
-       b.ctx.Done()
+       b.ctx.Cancel()
 }
 
 // ********************************
@@ -175,7 +176,6 @@ func (b *defaultBuilder) process(request Request, handler 
func(*Result)) {
        defer b.request.Delete(request.Meta.Name)
 
        c := Context{
-               C:         b.ctx,
                Client:    b.client,
                Path:      builderPath,
                Namespace: b.namespace,
diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go
index fe90b78..d588422 100644
--- a/pkg/builder/builder_steps.go
+++ b/pkg/builder/builder_steps.go
@@ -241,7 +241,7 @@ func packager(ctx *Context, selector ArtifactsSelector) 
error {
 func ListPublishedImages(context *Context) ([]PublishedImage, error) {
        list := v1alpha1.NewIntegrationContextList()
 
-       err := context.Client.List(context.C, &k8sclient.ListOptions{Namespace: 
context.Namespace}, &list)
+       err := context.Client.List(context.Request.C, 
&k8sclient.ListOptions{Namespace: context.Namespace}, &list)
        if err != nil {
                return nil, err
        }
diff --git a/pkg/builder/builder_types.go b/pkg/builder/builder_types.go
index 27cca9c..ec2fd60 100644
--- a/pkg/builder/builder_types.go
+++ b/pkg/builder/builder_types.go
@@ -18,11 +18,12 @@ limitations under the License.
 package builder
 
 import (
-       "context"
        "fmt"
        "math"
        "time"
 
+       "github.com/apache/camel-k/pkg/util/cancellable"
+
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
        "github.com/apache/camel-k/pkg/client"
        "github.com/apache/camel-k/pkg/util/maven"
@@ -102,7 +103,7 @@ type Resource struct {
 
 // Request --
 type Request struct {
-       C            context.Context
+       C            cancellable.Context
        Meta         v1.ObjectMeta
        Platform     v1alpha1.IntegrationPlatformSpec
        Dependencies []string
@@ -139,7 +140,6 @@ type Result struct {
 
 // Context --
 type Context struct {
-       C context.Context
        client.Client
        Request           Request
        BaseImage         string
diff --git a/pkg/builder/kaniko/publisher.go b/pkg/builder/kaniko/publisher.go
index 4710b4b..460cfdc 100644
--- a/pkg/builder/kaniko/publisher.go
+++ b/pkg/builder/kaniko/publisher.go
@@ -138,17 +138,17 @@ func Publisher(ctx *builder.Context) error {
                },
        }
 
-       err = ctx.Client.Delete(ctx.C, &pod)
+       err = ctx.Client.Delete(ctx.Request.C, &pod)
        if err != nil && !apierrors.IsNotFound(err) {
                return errors.Wrap(err, "cannot delete kaniko builder pod")
        }
 
-       err = ctx.Client.Create(ctx.C, &pod)
+       err = ctx.Client.Create(ctx.Request.C, &pod)
        if err != nil {
                return errors.Wrap(err, "cannot create kaniko builder pod")
        }
 
-       err = kubernetes.WaitCondition(ctx.C, ctx.Client, &pod, func(obj 
interface{}) (bool, error) {
+       err = kubernetes.WaitCondition(ctx.Request.C, ctx.Client, &pod, 
func(obj interface{}) (bool, error) {
                if val, ok := obj.(*v1.Pod); ok {
                        if val.Status.Phase == v1.PodSucceeded {
                                return true, nil
diff --git a/pkg/builder/s2i/publisher.go b/pkg/builder/s2i/publisher.go
index b02a7ef..24f8719 100644
--- a/pkg/builder/s2i/publisher.go
+++ b/pkg/builder/s2i/publisher.go
@@ -71,12 +71,12 @@ func Publisher(ctx *builder.Context) error {
                },
        }
 
-       err := ctx.Client.Delete(ctx.C, &bc)
+       err := ctx.Client.Delete(ctx.Request.C, &bc)
        if err != nil && !apierrors.IsNotFound(err) {
                return errors.Wrap(err, "cannot delete build config")
        }
 
-       err = ctx.Client.Create(ctx.C, &bc)
+       err = ctx.Client.Create(ctx.Request.C, &bc)
        if err != nil {
                return errors.Wrap(err, "cannot create build config")
        }
@@ -97,12 +97,12 @@ func Publisher(ctx *builder.Context) error {
                },
        }
 
-       err = ctx.Client.Delete(ctx.C, &is)
+       err = ctx.Client.Delete(ctx.Request.C, &is)
        if err != nil && !apierrors.IsNotFound(err) {
                return errors.Wrap(err, "cannot delete image stream")
        }
 
-       err = ctx.Client.Create(ctx.C, &is)
+       err = ctx.Client.Create(ctx.Request.C, &is)
        if err != nil {
                return errors.Wrap(err, "cannot create image stream")
        }
@@ -140,7 +140,7 @@ func Publisher(ctx *builder.Context) error {
                return errors.Wrap(err, "cannot unmarshal instantiated binary 
response")
        }
 
-       err = kubernetes.WaitCondition(ctx.C, ctx.Client, &ocbuild, func(obj 
interface{}) (bool, error) {
+       err = kubernetes.WaitCondition(ctx.Request.C, ctx.Client, &ocbuild, 
func(obj interface{}) (bool, error) {
                if val, ok := obj.(*buildv1.Build); ok {
                        if val.Status.Phase == buildv1.BuildPhaseComplete {
                                return true, nil
@@ -160,7 +160,7 @@ func Publisher(ctx *builder.Context) error {
        if err != nil {
                return err
        }
-       err = ctx.Client.Get(ctx.C, key, &is)
+       err = ctx.Client.Get(ctx.Request.C, key, &is)
        if err != nil {
                return err
        }
diff --git a/pkg/controller/integration/build_image.go 
b/pkg/controller/integration/build_image.go
index 8ae8fa4..e94792a 100644
--- a/pkg/controller/integration/build_image.go
+++ b/pkg/controller/integration/build_image.go
@@ -23,6 +23,8 @@ import (
        "path"
        "time"
 
+       "github.com/apache/camel-k/pkg/util/cancellable"
+
        "github.com/apache/camel-k/pkg/util/source"
 
        "github.com/pkg/errors"
@@ -116,7 +118,7 @@ func (action *buildImageAction) 
handleBuildImageSubmitted(ctx context.Context, i
                // happens asynchronously, a new context has to be created. the 
new context
                // can be used also to stop the build.
                r := builder.Request{
-                       C:        context.TODO(),
+                       C:        cancellable.NewContext(),
                        Meta:     integration.ObjectMeta,
                        Steps:    env.Steps,
                        BuildDir: env.BuildDir,
diff --git a/pkg/controller/integrationcontext/build.go 
b/pkg/controller/integrationcontext/build.go
index 91e1762..1036985 100644
--- a/pkg/controller/integrationcontext/build.go
+++ b/pkg/controller/integrationcontext/build.go
@@ -22,6 +22,8 @@ import (
        "fmt"
        "time"
 
+       "github.com/apache/camel-k/pkg/util/cancellable"
+
        "github.com/apache/camel-k/pkg/util/kubernetes"
 
        "github.com/apache/camel-k/pkg/trait"
@@ -106,7 +108,7 @@ func (action *buildAction) handleBuildSubmitted(ctx 
context.Context, ictx *v1alp
                // happens asynchronously, a new context has to be created. the 
new context
                // can be used also to stop the build.
                r := builder.Request{
-                       C:            context.TODO(),
+                       C:            cancellable.NewContext(),
                        Meta:         ictx.ObjectMeta,
                        Dependencies: ictx.Spec.Dependencies,
                        Repositories: repositories,
@@ -155,7 +157,7 @@ func (action *buildAction) handleBuildStateChange(ctx 
context.Context, res *buil
                if target.Status.Phase != 
v1alpha1.IntegrationContextPhaseBuildRunning {
 
                        // terminate the build
-                       res.Request.C.Done()
+                       res.Request.C.Cancel()
 
                        return fmt.Errorf("found context %s not the an expected 
phase (expectd=%s, found=%s)",
                                res.Request.Meta.Name,
@@ -186,7 +188,7 @@ func (action *buildAction) handleBuildStateChange(ctx 
context.Context, res *buil
                // by the user
                if target.Status.Phase != 
v1alpha1.IntegrationContextPhaseBuildRunning {
                        // terminate the build
-                       res.Request.C.Done()
+                       res.Request.C.Cancel()
 
                        return fmt.Errorf("found context %s not in the expected 
phase (expectd=%s, found=%s)",
                                res.Request.Meta.Name,
diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go
index 39b1438..30741ff 100644
--- a/pkg/platform/platform.go
+++ b/pkg/platform/platform.go
@@ -37,7 +37,7 @@ func GetPlatformBuilder(c client.Client, namespace string) 
(builder.Builder, err
                return gBuilder, nil
        }
 
-       gBuilder = builder.New(context.TODO(), c, namespace)
+       gBuilder = builder.New(c, namespace)
 
        return gBuilder, nil
 }
diff --git a/pkg/util/cancellable/cancellable.go 
b/pkg/util/cancellable/cancellable.go
new file mode 100644
index 0000000..3e90d31
--- /dev/null
+++ b/pkg/util/cancellable/cancellable.go
@@ -0,0 +1,51 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You 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 cancellable
+
+import "context"
+
+// Context --
+type Context interface {
+       context.Context
+
+       Cancel()
+}
+
+// NewContext --
+func NewContext() Context {
+       return NewContextWithParent(context.TODO())
+}
+
+// NewContextWithParent --
+func NewContextWithParent(parent context.Context) Context {
+       c, cc := context.WithCancel(parent)
+
+       return &cancellableContext{
+               Context: c,
+               cancel:  cc,
+       }
+}
+
+type cancellableContext struct {
+       context.Context
+       cancel func()
+}
+
+func (c *cancellableContext) Cancel() {
+       c.cancel()
+}
diff --git a/test/build_manager_integration_test.go 
b/test/build_manager_integration_test.go
index d4a7c06..fc80416 100644
--- a/test/build_manager_integration_test.go
+++ b/test/build_manager_integration_test.go
@@ -22,11 +22,12 @@ limitations under the License.
 package test
 
 import (
-       "context"
        "fmt"
        "testing"
        "time"
 
+       "github.com/apache/camel-k/pkg/util/cancellable"
+
        "k8s.io/apimachinery/pkg/apis/meta/v1"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
@@ -53,10 +54,10 @@ func handler(in chan builder.Result, out chan 
builder.Result) {
 
 func TestBuildManagerBuild(t *testing.T) {
        namespace := getTargetNamespace()
-       b := builder.New(testContext, testClient, namespace)
+       b := builder.New(testClient, namespace)
 
        r := builder.Request{
-               C: context.TODO(),
+               C: cancellable.NewContext(),
                Meta: v1.ObjectMeta{
                        Name:            "man-test",
                        ResourceVersion: "1",
@@ -95,10 +96,10 @@ func TestBuildManagerBuild(t *testing.T) {
 
 func TestBuildManagerFailedBuild(t *testing.T) {
        namespace := getTargetNamespace()
-       b := builder.New(testContext, testClient, namespace)
+       b := builder.New(testClient, namespace)
 
        r := builder.Request{
-               C: context.TODO(),
+               C: cancellable.NewContext(),
                Meta: v1.ObjectMeta{
                        Name:            "man-test",
                        ResourceVersion: "1",

Reply via email to