Skip to content

Commit

Permalink
fix #662: mark kit traits to avoid rebuilding when not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro authored and lburgazzoli committed Nov 7, 2019
1 parent 742f1d0 commit 34963b0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/controller/integration/build_kit.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (action *buildKitAction) Handle(ctx context.Context, integration *v1alpha1.
platformKit.Spec = v1alpha1.IntegrationKitSpec{
Dependencies: integration.Status.Dependencies,
Repositories: integration.Spec.Repositories,
Traits: integration.Spec.Traits,
Traits: action.filterKitTraits(ctx, integration.Spec.Traits),
}

if err := action.client.Create(ctx, &platformKit); err != nil {
Expand All @@ -134,3 +134,20 @@ func (action *buildKitAction) Handle(ctx context.Context, integration *v1alpha1.

return integration, nil
}

func (action *buildKitAction) filterKitTraits(ctx context.Context, in map[string]v1alpha1.TraitSpec) map[string]v1alpha1.TraitSpec {
if len(in) == 0 {
return in
}
catalog := trait.NewCatalog(ctx, action.client)
out := make(map[string]v1alpha1.TraitSpec)
for name, conf := range in {
t := catalog.GetTrait(name)
if t != nil && !t.InfluencesKit() {
// We don't store the trait configuration if the trait cannot influence the kit behavior
continue
}
out[name] = conf
}
return out
}
5 changes: 5 additions & 0 deletions pkg/trait/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ func (t *builderTrait) Apply(e *Environment) error {

return nil
}

// InfluencesKit overrides base class method
func (t *builderTrait) InfluencesKit() bool {
return true
}
13 changes: 13 additions & 0 deletions pkg/trait/trait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,19 @@ func TestConfigureVolumesAndMounts(t *testing.T) {
assert.Equal(t, "/foo/bar", m.MountPath)
}

func TestOnlySomeKitsInfluenceBuild(t *testing.T) {
c := NewTraitTestCatalog()
buildTraits := []string{"builder"}

for _, trait := range c.allTraits() {
if trait.InfluencesKit() {
assert.Contains(t, buildTraits, string(trait.ID()))
} else {
assert.NotContains(t, buildTraits, trait.ID())
}
}
}

func findVolume(vols []corev1.Volume, condition func(corev1.Volume) bool) *corev1.Volume {
for _, v := range vols {
v := v
Expand Down
8 changes: 8 additions & 0 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Trait interface {

// Apply executes a customization of the Environment
Apply(environment *Environment) error

// InfluencesKit determines if the trait has any influence on Integration Kits
InfluencesKit() bool
}

/* Base trait */
Expand Down Expand Up @@ -101,6 +104,11 @@ func (trait *BaseTrait) InjectContext(ctx context.Context) {
trait.ctx = ctx
}

// InfluencesKit determines if the trait has any influence on Integration Kits
func (trait *BaseTrait) InfluencesKit() bool {
return false
}

/* Environment */

// A Environment provides the context where the trait is executed
Expand Down

0 comments on commit 34963b0

Please sign in to comment.