You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2018/09/19 09:27:27 UTC

[GitHub] nicolaferraro closed pull request #99: chore: refactor pkg/build to a more idiomatic structure

nicolaferraro closed pull request #99: chore: refactor pkg/build to a more idiomatic structure
URL: https://github.com/apache/camel-k/pull/99
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pkg/build/build_manager.go b/pkg/build/build_manager.go
index 49f04f1..325e995 100644
--- a/pkg/build/build_manager.go
+++ b/pkg/build/build_manager.go
@@ -20,32 +20,37 @@ package build
 import (
 	"context"
 	"sync"
-
-	"github.com/apache/camel-k/pkg/build/api"
-	"github.com/apache/camel-k/pkg/build/local"
 )
 
-// main facade to the image build system
+// Manager represent the main facade to the image build system
 type Manager struct {
-	builds  sync.Map
-	builder api.Builder
+	builds    sync.Map
+	ctx       context.Context
+	namespace string
+	builder   Builder
 }
 
-func NewManager(ctx context.Context, namespace string) *Manager {
+// NewManager creates an instance of the build manager using the given builder
+func NewManager(ctx context.Context, namespace string, builder Builder) *Manager {
 	return &Manager{
-		builder: local.NewLocalBuilder(ctx, namespace),
+		ctx:       ctx,
+		namespace: namespace,
+		builder:   builder,
 	}
 }
 
-func (m *Manager) Get(identifier api.BuildIdentifier) api.BuildResult {
-	if info, present := m.builds.Load(identifier); !present || info == nil {
+// Get retrieve the build result associated to the given build identifier
+func (m *Manager) Get(identifier Identifier) Result {
+	info, present := m.builds.Load(identifier)
+	if !present || info == nil {
 		return noBuildInfo()
-	} else {
-		return *info.(*api.BuildResult)
 	}
+
+	return *info.(*Result)
 }
 
-func (m *Manager) Start(source api.BuildSource) {
+// Start starts a new build
+func (m *Manager) Start(source Request) {
 	initialBuildInfo := initialBuildInfo(&source)
 	m.builds.Store(source.Identifier, &initialBuildInfo)
 
@@ -56,15 +61,15 @@ func (m *Manager) Start(source api.BuildSource) {
 	}()
 }
 
-func noBuildInfo() api.BuildResult {
-	return api.BuildResult{
-		Status: api.BuildStatusNotRequested,
+func noBuildInfo() Result {
+	return Result{
+		Status: StatusNotRequested,
 	}
 }
 
-func initialBuildInfo(source *api.BuildSource) api.BuildResult {
-	return api.BuildResult{
+func initialBuildInfo(source *Request) Result {
+	return Result{
 		Source: source,
-		Status: api.BuildStatusStarted,
+		Status: StatusStarted,
 	}
 }
diff --git a/pkg/build/api/types.go b/pkg/build/build_types.go
similarity index 60%
rename from pkg/build/api/types.go
rename to pkg/build/build_types.go
index b6c178e..da1ffed 100644
--- a/pkg/build/api/types.go
+++ b/pkg/build/build_types.go
@@ -15,44 +15,54 @@ See the License for the specific language governing permissions and
 limitations under the License.
 */
 
-package api
+package build
 
-// a request to build a specific code
-type BuildSource struct {
-	Identifier   BuildIdentifier
-	Code         Code
+// Request represent a request to build a specific code
+type Request struct {
+	Identifier   Identifier
+	Code         Source
 	Dependencies []string
 }
 
-type BuildIdentifier struct {
+// Identifier identifies a build
+type Identifier struct {
 	Name      string
 	Qualifier string
 }
 
-type Code struct {
+// Source represent the integration code
+type Source struct {
 	Name     string
 	Content  string
 	Language string
 }
 
-// represents the result of a build
-type BuildResult struct {
-	Source *BuildSource
-	Status BuildStatus
+// Result represents the result of a build
+type Result struct {
+	Source *Request
+	Status Status
 	Image  string
 	Error  error
 }
 
-// supertype of all builders
+// Builder is supertype of all builders
 type Builder interface {
-	Build(BuildSource) <-chan BuildResult
+	Build(Request) <-chan Result
 }
 
-type BuildStatus int
+// Status --
+type Status int
 
 const (
-	BuildStatusNotRequested BuildStatus = iota
-	BuildStatusStarted
-	BuildStatusCompleted
-	BuildStatusError
+	// StatusNotRequested --
+	StatusNotRequested Status = iota
+
+	// StatusStarted --
+	StatusStarted
+
+	// StatusCompleted --
+	StatusCompleted
+
+	// StatusError --
+	StatusError
 )
diff --git a/pkg/build/local/local_builder.go b/pkg/build/local/local_builder.go
index 127657d..0274c12 100644
--- a/pkg/build/local/local_builder.go
+++ b/pkg/build/local/local_builder.go
@@ -35,10 +35,12 @@ import (
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 
-	build "github.com/apache/camel-k/pkg/build/api"
+	"github.com/apache/camel-k/pkg/build"
 	"github.com/apache/camel-k/pkg/util/kubernetes"
 	"github.com/apache/camel-k/pkg/util/kubernetes/customclient"
 	"github.com/apache/camel-k/pkg/util/maven"
+
+	// import openshift utilities
 	_ "github.com/apache/camel-k/pkg/util/openshift"
 	"github.com/apache/camel-k/version"
 )
@@ -49,10 +51,11 @@ type localBuilder struct {
 }
 
 type buildOperation struct {
-	source build.BuildSource
-	output chan build.BuildResult
+	source build.Request
+	output chan build.Result
 }
 
+// NewLocalBuilder create a new builder
 func NewLocalBuilder(ctx context.Context, namespace string) build.Builder {
 	builder := localBuilder{
 		buffer:    make(chan buildOperation, 100),
@@ -62,8 +65,8 @@ func NewLocalBuilder(ctx context.Context, namespace string) build.Builder {
 	return &builder
 }
 
-func (b *localBuilder) Build(source build.BuildSource) <-chan build.BuildResult {
-	res := make(chan build.BuildResult, 1)
+func (b *localBuilder) Build(source build.Request) <-chan build.Result {
+	res := make(chan build.Result, 1)
 	op := buildOperation{
 		source: source,
 		output: res,
@@ -90,15 +93,15 @@ func (b *localBuilder) buildCycle(ctx context.Context) {
 			}
 
 			if err != nil {
-				op.output <- build.BuildResult{
+				op.output <- build.Result{
 					Source: &op.source,
-					Status: build.BuildStatusError,
+					Status: build.StatusError,
 					Error:  err,
 				}
 			} else {
-				op.output <- build.BuildResult{
+				op.output <- build.Result{
 					Source: &op.source,
-					Status: build.BuildStatusCompleted,
+					Status: build.StatusCompleted,
 					Image:  image,
 				}
 			}
@@ -107,7 +110,7 @@ func (b *localBuilder) buildCycle(ctx context.Context) {
 	}
 }
 
-func (b *localBuilder) execute(source build.BuildSource) (string, error) {
+func (b *localBuilder) execute(source build.Request) (string, error) {
 	project, err := generateProjectDefinition(source)
 	if err != nil {
 		return "", err
@@ -127,7 +130,7 @@ func (b *localBuilder) execute(source build.BuildSource) (string, error) {
 	return image, nil
 }
 
-func (b *localBuilder) publish(tarFile string, source build.BuildSource) (string, error) {
+func (b *localBuilder) publish(tarFile string, source build.Request) (string, error) {
 
 	bc := buildv1.BuildConfig{
 		TypeMeta: metav1.TypeMeta{
@@ -251,7 +254,7 @@ func (b *localBuilder) publish(tarFile string, source build.BuildSource) (string
 	return is.Status.DockerImageRepository + ":" + source.Identifier.Qualifier, nil
 }
 
-func generateProjectDefinition(source build.BuildSource) (maven.ProjectDefinition, error) {
+func generateProjectDefinition(source build.Request) (maven.ProjectDefinition, error) {
 	project := maven.ProjectDefinition{
 		Project: maven.Project{
 			XMLName:           xml.Name{Local: "project"},
@@ -293,13 +296,13 @@ func generateProjectDefinition(source build.BuildSource) (maven.ProjectDefinitio
 
 	for _, d := range source.Dependencies {
 		if strings.HasPrefix(d, "camel:") {
-			artifactId := strings.TrimPrefix(d, "camel:")
+			artifactID := strings.TrimPrefix(d, "camel:")
 
-			if !strings.HasPrefix(artifactId, "camel-") {
-				artifactId = "camel-" + artifactId
+			if !strings.HasPrefix(artifactID, "camel-") {
+				artifactID = "camel-" + artifactID
 			}
 
-			deps.AddGAV("org.apache.camel", artifactId, "")
+			deps.AddGAV("org.apache.camel", artifactID, "")
 		} else if strings.HasPrefix(d, "mvn:") {
 			mid := strings.TrimPrefix(d, "mvn:")
 			gav := strings.Replace(mid, "/", ":", -1)
diff --git a/pkg/build/local/local_builder_test.go b/pkg/build/local/local_builder_test.go
index b88567e..790c124 100644
--- a/pkg/build/local/local_builder_test.go
+++ b/pkg/build/local/local_builder_test.go
@@ -20,17 +20,17 @@ package local
 import (
 	"testing"
 
-	"github.com/apache/camel-k/pkg/build/api"
+	"github.com/apache/camel-k/pkg/build"
 	"github.com/stretchr/testify/assert"
 )
 
 func TestProjectGeneration(t *testing.T) {
-	source := api.BuildSource{
-		Identifier: api.BuildIdentifier{
+	source := build.Request{
+		Identifier: build.Identifier{
 			Name:      "my-integration",
 			Qualifier: "",
 		},
-		Code: api.Code{
+		Code: build.Source{
 			Name:     "my-code.js",
 			Content:  `from("timer:start").to("log:end")`,
 			Language: "",
@@ -57,12 +57,12 @@ func TestProjectGeneration(t *testing.T) {
 }
 
 func TestProjectGenerationWithFailure(t *testing.T) {
-	source := api.BuildSource{
-		Identifier: api.BuildIdentifier{
+	source := build.Request{
+		Identifier: build.Identifier{
 			Name:      "my-integration",
 			Qualifier: "",
 		},
-		Code: api.Code{
+		Code: build.Source{
 			Name:     "my-code.js",
 			Content:  `from("timer:start").to("log:end")`,
 			Language: "",
diff --git a/pkg/stub/action/context/build.go b/pkg/stub/action/context/build.go
index 249cf71..e8d26e4 100644
--- a/pkg/stub/action/context/build.go
+++ b/pkg/stub/action/context/build.go
@@ -19,21 +19,24 @@ package action
 
 import (
 	"context"
+
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 
 	"github.com/operator-framework/operator-sdk/pkg/sdk"
 	"github.com/sirupsen/logrus"
 
-	"github.com/apache/camel-k/pkg/build/api"
-
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/build"
+	"github.com/apache/camel-k/pkg/build/local"
 )
 
 // NewIntegrationContextBuildAction creates a new build handling action for the context
 func NewIntegrationContextBuildAction(ctx context.Context, namespace string) IntegrationContextAction {
+	builder := local.NewLocalBuilder(ctx, namespace)
+	manager := build.NewManager(ctx, namespace, builder)
+
 	return &integrationContextBuildAction{
-		buildManager: build.NewManager(ctx, namespace),
+		buildManager: manager,
 	}
 }
 
@@ -50,23 +53,23 @@ func (action *integrationContextBuildAction) CanHandle(context *v1alpha1.Integra
 }
 
 func (action *integrationContextBuildAction) Handle(context *v1alpha1.IntegrationContext) error {
-	buildIdentifier := api.BuildIdentifier{
+	buildIdentifier := build.Identifier{
 		Name:      "context-" + context.Name,
 		Qualifier: context.ResourceVersion,
 	}
 
 	buildResult := action.buildManager.Get(buildIdentifier)
-	if buildResult.Status == api.BuildStatusNotRequested {
-		action.buildManager.Start(api.BuildSource{
+	if buildResult.Status == build.StatusNotRequested {
+		action.buildManager.Start(build.Request{
 			Identifier:   buildIdentifier,
 			Dependencies: context.Spec.Dependencies,
 		})
 		logrus.Info("Build started")
-	} else if buildResult.Status == api.BuildStatusError {
+	} else if buildResult.Status == build.StatusError {
 		target := context.DeepCopy()
 		target.Status.Phase = v1alpha1.IntegrationContextPhaseError
 		return sdk.Update(target)
-	} else if buildResult.Status == api.BuildStatusCompleted {
+	} else if buildResult.Status == build.StatusCompleted {
 		target := context.DeepCopy()
 		target.Status.Image = buildResult.Image
 		target.Status.Phase = v1alpha1.IntegrationContextPhaseReady


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services