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

Trigger context update when build finishes #236

Merged
merged 1 commit into from
Nov 23, 2018
Merged
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
18 changes: 9 additions & 9 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (b *defaultBuilder) Submit(request Request) Result {
go b.loop()
}

result, present := b.request.Load(request.Identifier)
result, present := b.request.Load(request.Meta.Name)
if !present || result == nil {
result = Result{
Request: request,
Expand All @@ -75,7 +75,7 @@ func (b *defaultBuilder) Submit(request Request) Result {

b.log.Infof("submitting request: %+v", request)

b.request.Store(request.Identifier, result)
b.request.Store(request.Meta.Name, result)
b.requests <- request
}

Expand All @@ -84,7 +84,7 @@ func (b *defaultBuilder) Submit(request Request) Result {

// Purge --
func (b *defaultBuilder) Purge(request Request) {
b.request.Delete(request.Identifier)
b.request.Delete(request.Meta.Name)
}

// ********************************
Expand Down Expand Up @@ -113,9 +113,9 @@ func (b *defaultBuilder) loop() {
}

func (b *defaultBuilder) submit(request Request) {
result, present := b.request.Load(request.Identifier)
result, present := b.request.Load(request.Meta.Name)
if !present || result == nil {
b.log.Panicf("no info found for: %+v", request.Identifier)
b.log.Panicf("no info found for: %+v", request.Meta.Name)
}

// update the status
Expand All @@ -138,7 +138,7 @@ func (b *defaultBuilder) submit(request Request) {
defer os.RemoveAll(builderPath)

// update the cache
b.request.Store(request.Identifier, r)
b.request.Store(request.Meta.Name, r)

c := Context{
C: b.ctx,
Expand All @@ -164,7 +164,7 @@ func (b *defaultBuilder) submit(request Request) {
l := b.log.WithFields(logrus.Fields{
"step": step.ID(),
"phase": step.Phase(),
"request": request.Identifier.String(),
"context": request.Meta.Name,
})

l.Infof("executing step")
Expand Down Expand Up @@ -195,7 +195,7 @@ func (b *defaultBuilder) submit(request Request) {
}

// update the cache
b.request.Store(request.Identifier, r)
b.request.Store(request.Meta.Name, r)

b.log.Infof("request %s:%s executed in %f seconds", r.Request.Identifier.Name, r.Request.Identifier.Qualifier, r.Task.Elapsed().Seconds())
b.log.Infof("request to build context %s executed in %f seconds", request.Meta.Name, r.Task.Elapsed().Seconds())
}
34 changes: 34 additions & 0 deletions pkg/builder/builder_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"path"
"strings"

"github.com/rs/xid"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/operator-framework/operator-sdk/pkg/sdk"

Expand Down Expand Up @@ -294,3 +296,35 @@ func FindBestImage(images []PublishedImage, entries []v1alpha1.Artifact) (*Publi

return &bestImage, bestImageCommonLibs
}

// Notify --
func Notify(ctx *Context) error {
c := v1alpha1.IntegrationContext{
TypeMeta: metav1.TypeMeta{
Kind: v1alpha1.IntegrationContextKind,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: ctx.Namespace,
Name: ctx.Request.Meta.Name,
},
}

if err := sdk.Get(&c); err != nil {
return err
}

t := c.DeepCopy()
if t.Annotations == nil {
t.Annotations = make(map[string]string)
}

// Add a random ID to trigger update
t.Annotations["camel.apache.org/build.id"] = xid.New().String()

if err := sdk.Update(t); err != nil {
return err
}

return nil
}
41 changes: 14 additions & 27 deletions pkg/builder/builder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ package builder
import (
"context"
"fmt"
"math"
"time"

"k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/apache/camel-k/pkg/util/maven"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
)

const (
// ProjectGenerationPhase --
ProjectGenerationPhase int = 10
ProjectGenerationPhase int32 = 10
// ProjectBuildPhase --
ProjectBuildPhase int = 20
ProjectBuildPhase int32 = 20
// ApplicationPackagePhase --
ApplicationPackagePhase int = 30
ApplicationPackagePhase int32 = 30
// ApplicationPublishPhase --
ApplicationPublishPhase int = 40
ApplicationPublishPhase int32 = 40
// NotifyPhase --
NotifyPhase int32 = math.MaxInt32
)

// Builder --
Expand All @@ -47,13 +52,13 @@ type Builder interface {
// Step --
type Step interface {
ID() string
Phase() int
Phase() int32
Execute(*Context) error
}

type stepWrapper struct {
id string
phase int
phase int32
task func(*Context) error
}

Expand All @@ -65,7 +70,7 @@ func (s *stepWrapper) ID() string {
return s.id
}

func (s *stepWrapper) Phase() int {
func (s *stepWrapper) Phase() int32 {
return s.phase
}

Expand All @@ -74,7 +79,7 @@ func (s *stepWrapper) Execute(ctx *Context) error {
}

// NewStep --
func NewStep(ID string, phase int, task func(*Context) error) Step {
func NewStep(ID string, phase int32, task func(*Context) error) Step {
s := stepWrapper{
id: ID,
phase: phase,
Expand All @@ -84,27 +89,9 @@ func NewStep(ID string, phase int, task func(*Context) error) Step {
return &s
}

// NewIdentifierForContext --
func NewIdentifierForContext(context *v1alpha1.IntegrationContext) Identifier {
return Identifier{
Name: "context-" + context.Name,
Qualifier: context.ResourceVersion,
}
}

// Identifier --
type Identifier struct {
Name string
Qualifier string
}

func (r *Identifier) String() string {
return r.Name + ":" + r.Qualifier
}

// Request --
type Request struct {
Identifier Identifier
Meta v1.ObjectMeta
Platform v1alpha1.IntegrationPlatformSpec
Code v1alpha1.SourceSpec
Dependencies []string
Expand Down
1 change: 1 addition & 0 deletions pkg/builder/kaniko/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var DefaultSteps = []builder.Step{
builder.NewStep("build/compute-dependencies", builder.ProjectBuildPhase, builder.ComputeDependencies),
builder.NewStep("packager", builder.ApplicationPackagePhase, builder.StandardPackager),
builder.NewStep("publisher/kaniko", builder.ApplicationPublishPhase, Publisher),
builder.NewStep("notify", builder.NotifyPhase, builder.Notify),
}

// BuildDir is the directory where to build artifacts (shared with the Kaniko pod)
Expand Down
12 changes: 6 additions & 6 deletions pkg/builder/kaniko/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Publisher(ctx *builder.Context) error {
if organization == "" {
organization = ctx.Namespace
}
image := ctx.Request.Platform.Build.Registry + "/" + organization + "/camel-k-" + ctx.Request.Identifier.Name + ":" + ctx.Request.Identifier.Qualifier
image := ctx.Request.Platform.Build.Registry + "/" + organization + "/camel-k-" + ctx.Request.Meta.Name + ":" + ctx.Request.Meta.ResourceVersion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice solution

baseDir, _ := path.Split(ctx.Archive)
contextDir := path.Join(baseDir, "context")
if err := tar.Extract(ctx.Archive, contextDir); err != nil {
Expand Down Expand Up @@ -88,7 +88,7 @@ func Publisher(ctx *builder.Context) error {
},
})
volumeMounts = append(volumeMounts, v1.VolumeMount{
Name: "kaniko-secret",
Name: "kaniko-secret",
MountPath: "/secret",
})
envs = append(envs, v1.EnvVar{
Expand All @@ -105,14 +105,14 @@ func Publisher(ctx *builder.Context) error {
},
ObjectMeta: metav1.ObjectMeta{
Namespace: ctx.Namespace,
Name: "camel-k-" + ctx.Request.Identifier.Name,
Name: "camel-k-" + ctx.Request.Meta.Name,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "kaniko",
Image: "gcr.io/kaniko-project/executor@sha256:f29393d9c8d40296e1692417089aa2023494bce9afd632acac7dd0aea763e5bc",
Args: args,
Name: "kaniko",
Image: "gcr.io/kaniko-project/executor@sha256:f29393d9c8d40296e1692417089aa2023494bce9afd632acac7dd0aea763e5bc",
Args: args,
Env: envs,
VolumeMounts: volumeMounts,
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/builder/s2i/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Publisher(ctx *builder.Context) error {
Kind: "BuildConfig",
},
ObjectMeta: metav1.ObjectMeta{
Name: "camel-k-" + ctx.Request.Identifier.Name,
Name: "camel-k-" + ctx.Request.Meta.Name,
Namespace: ctx.Namespace,
},
Spec: buildv1.BuildConfigSpec{
Expand All @@ -64,7 +64,7 @@ func Publisher(ctx *builder.Context) error {
Output: buildv1.BuildOutput{
To: &v1.ObjectReference{
Kind: "ImageStreamTag",
Name: "camel-k-" + ctx.Request.Identifier.Name + ":" + ctx.Request.Identifier.Qualifier,
Name: "camel-k-" + ctx.Request.Meta.Name + ":" + ctx.Request.Meta.ResourceVersion,
},
},
},
Expand All @@ -83,7 +83,7 @@ func Publisher(ctx *builder.Context) error {
Kind: "ImageStream",
},
ObjectMeta: metav1.ObjectMeta{
Name: "camel-k-" + ctx.Request.Identifier.Name,
Name: "camel-k-" + ctx.Request.Meta.Name,
Namespace: ctx.Namespace,
},
Spec: imagev1.ImageStreamSpec{
Expand Down Expand Up @@ -113,7 +113,7 @@ func Publisher(ctx *builder.Context) error {
Namespace(ctx.Namespace).
Body(resource).
Resource("buildconfigs").
Name("camel-k-" + ctx.Request.Identifier.Name).
Name("camel-k-" + ctx.Request.Meta.Name).
SubResource("instantiatebinary").
Do()

Expand Down Expand Up @@ -159,7 +159,7 @@ func Publisher(ctx *builder.Context) error {
return errors.New("dockerImageRepository not available in ImageStream")
}

ctx.Image = is.Status.DockerImageRepository + ":" + ctx.Request.Identifier.Qualifier
ctx.Image = is.Status.DockerImageRepository + ":" + ctx.Request.Meta.ResourceVersion

return nil
}
1 change: 1 addition & 0 deletions pkg/builder/s2i/s2i.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ var DefaultSteps = []builder.Step{
builder.NewStep("build/compute-dependencies", builder.ProjectBuildPhase, builder.ComputeDependencies),
builder.NewStep("packager/incremental", builder.ApplicationPackagePhase, builder.IncrementalPackager),
builder.NewStep("publisher/s2i", builder.ApplicationPublishPhase, Publisher),
builder.NewStep("notify", builder.NotifyPhase, builder.Notify),
}
2 changes: 1 addition & 1 deletion pkg/stub/action/context/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (action *buildAction) Handle(context *v1alpha1.IntegrationContext) error {
}

r := builder.Request{
Identifier: builder.NewIdentifierForContext(context),
Meta: context.ObjectMeta,
Dependencies: context.Spec.Dependencies,
Steps: env.Steps,
Platform: env.Platform.Spec,
Expand Down
4 changes: 2 additions & 2 deletions pkg/trait/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestS2IBuilderTrait(t *testing.T) {
assert.NotEmpty(t, env.ExecutedTraits)
assert.Contains(t, env.ExecutedTraits, ID("builder"))
assert.NotEmpty(t, env.Steps)
assert.Len(t, env.Steps, 4)
assert.Len(t, env.Steps, 5)
assert.Condition(t, func() bool {
for _, s := range env.Steps {
if s.ID() == "publisher/s2i" && s.Phase() == builder.ApplicationPublishPhase {
Expand All @@ -97,7 +97,7 @@ func TestKanikoBuilderTrait(t *testing.T) {
assert.NotEmpty(t, env.ExecutedTraits)
assert.Contains(t, env.ExecutedTraits, ID("builder"))
assert.NotEmpty(t, env.Steps)
assert.Len(t, env.Steps, 4)
assert.Len(t, env.Steps, 5)
assert.Condition(t, func() bool {
for _, s := range env.Steps {
if s.ID() == "publisher/kaniko" && s.Phase() == builder.ApplicationPublishPhase {
Expand Down
22 changes: 12 additions & 10 deletions test/build_manager_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
"testing"
"time"

"k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/builder"
"github.com/apache/camel-k/pkg/builder/s2i"
"github.com/apache/camel-k/pkg/util/digest"

"github.com/stretchr/testify/assert"
)

Expand All @@ -40,9 +40,9 @@ func TestBuildManagerBuild(t *testing.T) {
b := builder.New(ctx, namespace)

r := builder.Request{
Identifier: builder.Identifier{
Name: "man-test",
Qualifier: digest.Random(),
Meta: v1.ObjectMeta{
Name: "man-test",
ResourceVersion: "1",
},
Code: v1alpha1.SourceSpec{
Content: createTimerToLogIntegrationCode(),
Expand All @@ -51,7 +51,8 @@ func TestBuildManagerBuild(t *testing.T) {
"mvn:org.apache.camel/camel-core",
"camel:telegram",
},
Steps: s2i.DefaultSteps,
// to not include notify step
Steps: s2i.DefaultSteps[:len(s2i.DefaultSteps)-1],
}

b.Submit(r)
Expand All @@ -78,17 +79,18 @@ func TestBuildManagerFailedBuild(t *testing.T) {
b := builder.New(ctx, namespace)

r := builder.Request{
Identifier: builder.Identifier{
Name: "man-test",
Qualifier: digest.Random(),
Meta: v1.ObjectMeta{
Name: "man-test",
ResourceVersion: "1",
},
Code: v1alpha1.SourceSpec{
Content: createTimerToLogIntegrationCode(),
},
Dependencies: []string{
"mvn:org.apache.camel/camel-cippalippa",
},
Steps: s2i.DefaultSteps,
// to not include notify step
Steps: s2i.DefaultSteps[:len(s2i.DefaultSteps)-1],
}

b.Submit(r)
Expand Down