Skip to content

Commit

Permalink
Support for properties apache#62
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Sep 13, 2018
1 parent 3ba8d5b commit ac38b6f
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 57 deletions.
35 changes: 28 additions & 7 deletions pkg/apis/camel/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// IntegrationList --
type IntegrationList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Expand All @@ -31,43 +32,57 @@ type IntegrationList struct {

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Integration --
type Integration struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
Spec IntegrationSpec `json:"spec"`
Status IntegrationStatus `json:"status,omitempty"`
}

// IntegrationSpec --
type IntegrationSpec struct {
Replicas *int32 `json:"replicas,omitempty"`
Source SourceSpec `json:"source,omitempty"`
Context string `json:"context,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Replicas *int32 `json:"replicas,omitempty"`
Source SourceSpec `json:"source,omitempty"`
Context string `json:"context,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Properties []PropertySpec `json:"properties,omitempty"`
}

// SourceSpec --
type SourceSpec struct {
Name string `json:"name,omitempty"`
Content string `json:"content,omitempty"`
Language string `json:"language,omitempty"`
}

// IntegrationStatus --
type IntegrationStatus struct {
Phase IntegrationPhase `json:"phase,omitempty"`
Digest string `json:"digest,omitempty"`
Image string `json:"image,omitempty"`
}

// IntegrationPhase --
type IntegrationPhase string

const (
IntegrationPhaseBuilding IntegrationPhase = "Building"
// IntegrationKind --
IntegrationKind string = "Integration"

// IntegrationPhaseBuilding --
IntegrationPhaseBuilding IntegrationPhase = "Building"
// IntegrationPhaseDeploying --
IntegrationPhaseDeploying IntegrationPhase = "Deploying"
IntegrationPhaseRunning IntegrationPhase = "Running"
IntegrationPhaseError IntegrationPhase = "Error"
// IntegrationPhaseRunning --
IntegrationPhaseRunning IntegrationPhase = "Running"
// IntegrationPhaseError --
IntegrationPhaseError IntegrationPhase = "Error"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// IntegrationContextList --
type IntegrationContextList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Expand All @@ -76,35 +91,41 @@ type IntegrationContextList struct {

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// IntegrationContext --
type IntegrationContext struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
Spec IntegrationContextSpec `json:"spec"`
Status IntegrationContextStatus `json:"status,omitempty"`
}

// IntegrationContextSpec --
type IntegrationContextSpec struct {
Dependencies []string `json:"dependencies,omitempty"`
Properties []PropertySpec `json:"properties,omitempty"`
Environment []EnvironmentSpec `json:"environment,omitempty"`
}

// PropertySpec --
type PropertySpec struct {
Name string
Value string
}

// EnvironmentSpec --
type EnvironmentSpec struct {
Name string
Value string
}

// IntegrationContextStatus --
type IntegrationContextStatus struct {
Phase IntegrationContextPhase `json:"phase,omitempty"`
Image string `json:"image,omitempty"`
Digest string `json:"digest,omitempty"`
}

// IntegrationContextPhase --
type IntegrationContextPhase string

const (
Expand Down
43 changes: 27 additions & 16 deletions pkg/client/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,9 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1"
)

type RunCmdOptions struct {
*RootCmdOptions
IntegrationContext string
Language string
IntegrationName string
Dependencies []string
Wait bool
}

// NewCmdRun --
func NewCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
options := RunCmdOptions{
options := runCmdOptions{
RootCmdOptions: rootCmdOptions,
}

Expand All @@ -61,11 +53,22 @@ func NewCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
cmd.Flags().StringSliceVarP(&options.Dependencies, "dependency", "d", nil, "The integration dependency")
cmd.Flags().BoolVarP(&options.Wait, "wait", "w", false, "Waits for the integration to be running")
cmd.Flags().StringVarP(&options.IntegrationContext, "context", "x", "", "The contex used to run the integration")
cmd.Flags().StringSliceVarP(&options.Properties, "property", "p", nil, "Add a system property")

return &cmd
}

func (*RunCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
type runCmdOptions struct {
*RootCmdOptions
IntegrationContext string
Language string
IntegrationName string
Dependencies []string
Properties []string
Wait bool
}

func (*runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("accepts 1 arg, received " + strconv.Itoa(len(args)))
}
Expand All @@ -78,7 +81,7 @@ func (*RunCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
return nil
}

func (o *RunCmdOptions) run(cmd *cobra.Command, args []string) error {
func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error {
integration, err := o.createIntegration(cmd, args)
if err != nil {
return err
Expand All @@ -92,7 +95,7 @@ func (o *RunCmdOptions) run(cmd *cobra.Command, args []string) error {
return nil
}

func (o *RunCmdOptions) waitForIntegrationReady(integration *v1alpha1.Integration) error {
func (o *runCmdOptions) waitForIntegrationReady(integration *v1alpha1.Integration) error {
// Block this goroutine until the integration is in a final status
changes, err := watch.WatchStateChanges(o.Context, integration)
if err != nil {
Expand Down Expand Up @@ -130,7 +133,7 @@ watcher:
return nil
}

func (o *RunCmdOptions) createIntegration(cmd *cobra.Command, args []string) (*v1alpha1.Integration, error) {
func (o *runCmdOptions) createIntegration(cmd *cobra.Command, args []string) (*v1alpha1.Integration, error) {
code, err := o.loadCode(args[0])
if err != nil {
return nil, err
Expand All @@ -157,7 +160,7 @@ func (o *RunCmdOptions) createIntegration(cmd *cobra.Command, args []string) (*v

integration := v1alpha1.Integration{
TypeMeta: v1.TypeMeta{
Kind: "Integration",
Kind: v1alpha1.IntegrationKind,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: v1.ObjectMeta{
Expand All @@ -175,6 +178,14 @@ func (o *RunCmdOptions) createIntegration(cmd *cobra.Command, args []string) (*v
},
}

integration.Spec.Properties = make([]v1alpha1.PropertySpec, 0)
for _, item := range o.Properties {
pair := strings.Split(item, "=")
if len(pair) == 2 {
integration.Spec.Properties = append(integration.Spec.Properties, v1alpha1.PropertySpec{Name: pair[0], Value: pair[1]})
}
}

existed := false
err = sdk.Create(&integration)
if err != nil && k8serrors.IsAlreadyExists(err) {
Expand All @@ -200,7 +211,7 @@ func (o *RunCmdOptions) createIntegration(cmd *cobra.Command, args []string) (*v
return &integration, nil
}

func (*RunCmdOptions) loadCode(fileName string) (string, error) {
func (*runCmdOptions) loadCode(fileName string) (string, error) {
content, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
Expand Down
35 changes: 17 additions & 18 deletions pkg/stub/action/integration/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,37 @@ import (
"github.com/apache/camel-k/pkg/build"
"github.com/apache/camel-k/pkg/build/api"
"github.com/operator-framework/operator-sdk/pkg/sdk"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

type BuildAction struct {
buildManager *build.Manager
}

// NewBuildAction create an action that handles integration build
func NewBuildAction(ctx context.Context, namespace string) IntegrationAction {
return &BuildAction{
return &buildAction{
buildManager: build.NewManager(ctx, namespace),
}
}

func (b *BuildAction) Name() string {
type buildAction struct {
buildManager *build.Manager
}

func (action *buildAction) Name() string {
return "build"
}

func (b *BuildAction) CanHandle(integration *v1alpha1.Integration) bool {
func (action *buildAction) CanHandle(integration *v1alpha1.Integration) bool {
return integration.Status.Phase == v1alpha1.IntegrationPhaseBuilding
}

func (b *BuildAction) Handle(integration *v1alpha1.Integration) error {
if integration.Spec.Context != "" {
name := integration.Spec.Context
ctx := v1alpha1.NewIntegrationContext(integration.Namespace, name)
func (action *buildAction) Handle(integration *v1alpha1.Integration) error {
ctx, err := LookupContextForIntegration(integration)
if err != nil {
//TODO: we may need to add a wait strategy, i.e give up after some time
return err
}

if err := sdk.Get(&ctx); err != nil {
//TODO: we may need to add a wait strategy, i.e give up after some time
return errors.Wrapf(err, "unable to find integration context %s, %s", ctx.Name, err)
}

if ctx != nil {
if ctx.Status.Phase == v1alpha1.IntegrationContextPhaseReady {
target := integration.DeepCopy()
target.Status.Image = ctx.Status.Image
Expand All @@ -70,9 +69,9 @@ func (b *BuildAction) Handle(integration *v1alpha1.Integration) error {
Name: integration.Name,
Qualifier: integration.Status.Digest,
}
buildResult := b.buildManager.Get(buildIdentifier)
buildResult := action.buildManager.Get(buildIdentifier)
if buildResult.Status == api.BuildStatusNotRequested {
b.buildManager.Start(api.BuildSource{
action.buildManager.Start(api.BuildSource{
Identifier: buildIdentifier,
Code: api.Code{
Name: integration.Spec.Source.Name,
Expand Down
Loading

0 comments on commit ac38b6f

Please sign in to comment.