Skip to content

Commit

Permalink
Allow to customize contexts create at installation #318
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli authored and nicolaferraro committed Jan 8, 2019
1 parent 5ad33f6 commit 2cb5754
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 37 deletions.
9 changes: 6 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions pkg/apis/camel/v1alpha1/integrationplatform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import (

// IntegrationPlatformSpec defines the desired state of IntegrationPlatform
type IntegrationPlatformSpec struct {
Cluster IntegrationPlatformCluster `json:"cluster,omitempty"`
Profile TraitProfile `json:"profile,omitempty"`
Build IntegrationPlatformBuildSpec `json:"build,omitempty"`
Cluster IntegrationPlatformCluster `json:"cluster,omitempty"`
Profile TraitProfile `json:"profile,omitempty"`
Build IntegrationPlatformBuildSpec `json:"build,omitempty"`
Resources IntegrationPlatformResourcesSpec `json:"resources,omitempty"`
}

// IntegrationPlatformResourcesSpec contains platform related resources
type IntegrationPlatformResourcesSpec struct {
Contexts []string `json:"contexts,omitempty"`
}

// IntegrationPlatformStatus defines the observed state of IntegrationPlatform
Expand Down
22 changes: 22 additions & 0 deletions pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkg/cmd/completion_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"os"
"strings"

"github.com/apache/camel-k/pkg/platform"

"github.com/apache/camel-k/pkg/trait"

"github.com/apache/camel-k/pkg/util/camel"
Expand Down Expand Up @@ -141,6 +143,12 @@ __kamel_kubectl_get_user_integrationcontexts() {
fi
}
__kamel_kubectl_get_known_integrationcontexts() {
local type_list="` + strings.Join(platform.GetContextsNames(), " ") + `"
COMPREPLY=( $( compgen -W "${type_list}" -- "$cur") )
compopt -o nospace
}
__custom_func() {
case ${last_command} in
kamel_delete)
Expand Down
37 changes: 25 additions & 12 deletions pkg/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,36 @@ import (
)

func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command {
options := installCmdOptions{
impl := installCmdOptions{
RootCmdOptions: rootCmdOptions,
}
cmd := cobra.Command{
Use: "install",
Short: "Install Camel K on a Kubernetes cluster",
Long: `Installs Camel K on a Kubernetes or OpenShift cluster.`,
RunE: options.install,
RunE: impl.install,
}

cmd.Flags().BoolVar(&options.clusterSetupOnly, "cluster-setup", false, "Execute cluster-wide operations only (may require admin rights)")
cmd.Flags().BoolVar(&options.skipClusterSetup, "skip-cluster-setup", false, "Skip the cluster-setup phase")
cmd.Flags().BoolVar(&options.exampleSetup, "example", false, "Install example integration")
cmd.Flags().StringVar(&options.registry, "registry", "", "A Docker registry that can be used to publish images")
cmd.Flags().StringVarP(&options.outputFormat, "output", "o", "", "Output format. One of: json|yaml")
cmd.Flags().StringVar(&options.organization, "organization", "", "A organization on the Docker registry that can be used to publish images")
cmd.Flags().StringVar(&options.pushSecret, "push-secret", "", "A secret used to push images to the Docker registry")
cmd.Flags().StringSliceVar(&options.repositories, "repository", nil, "Add a maven repository")
cmd.Flags().StringSliceVarP(&options.properties, "property", "p", nil, "Add a camel property")
cmd.Flags().StringVar(&options.camelVersion, "camel-version", "", "Set the camel version")
cmd.Flags().BoolVar(&impl.clusterSetupOnly, "cluster-setup", false, "Execute cluster-wide operations only (may require admin rights)")
cmd.Flags().BoolVar(&impl.skipClusterSetup, "skip-cluster-setup", false, "Skip the cluster-setup phase")
cmd.Flags().BoolVar(&impl.exampleSetup, "example", false, "Install example integration")
cmd.Flags().StringVar(&impl.registry, "registry", "", "A Docker registry that can be used to publish images")
cmd.Flags().StringVarP(&impl.outputFormat, "output", "o", "", "Output format. One of: json|yaml")
cmd.Flags().StringVar(&impl.organization, "organization", "", "A organization on the Docker registry that can be used to publish images")
cmd.Flags().StringVar(&impl.pushSecret, "push-secret", "", "A secret used to push images to the Docker registry")
cmd.Flags().StringSliceVar(&impl.repositories, "repository", nil, "Add a maven repository")
cmd.Flags().StringSliceVarP(&impl.properties, "property", "p", nil, "Add a camel property")
cmd.Flags().StringVar(&impl.camelVersion, "camel-version", "", "Set the camel version")
cmd.Flags().StringSliceVar(&impl.contexts, "context", nil, "Add a camel context to build at startup, by default all known contexts are built")

// completion support
configureBashAnnotationForFlag(
&cmd,
"context",
map[string][]string{
cobra.BashCompCustom: {"__kamel_kubectl_get_known_integrationcontexts"},
},
)

return &cmd
}
Expand All @@ -66,6 +76,7 @@ type installCmdOptions struct {
camelVersion string
repositories []string
properties []string
contexts []string
}

func (o *installCmdOptions) install(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -129,6 +140,8 @@ func (o *installCmdOptions) install(cmd *cobra.Command, args []string) error {
platform.Spec.Build.CamelVersion = o.camelVersion
}

platform.Spec.Resources.Contexts = o.contexts

err = install.RuntimeObjectOrCollect(o.Context, c, namespace, collection, platform)
if err != nil {
return err
Expand Down
53 changes: 34 additions & 19 deletions pkg/controller/integrationplatform/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,16 @@ package integrationplatform

import (
"context"
"fmt"
"strings"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/install"
p "github.com/apache/camel-k/pkg/platform"

"github.com/sirupsen/logrus"
)

var resources = []string{
"platform-integration-context-jvm.yaml",
"platform-integration-context-groovy.yaml",
"platform-integration-context-kotlin.yaml",
"platform-integration-context-spring-boot.yaml",
}

var knativeResources = []string{
"platform-integration-context-knative.yaml",
}

// NewCreateAction returns a action that creates resources needed by the platform
func NewCreateAction() Action {
return &createAction{}
Expand All @@ -54,18 +47,40 @@ func (action *createAction) CanHandle(platform *v1alpha1.IntegrationPlatform) bo
}

func (action *createAction) Handle(ctx context.Context, platform *v1alpha1.IntegrationPlatform) error {
logrus.Info("Installing platform resources")
err := install.Resources(ctx, action.client, platform.Namespace, resources...)
if err != nil {
return err
}
if l := len(platform.Spec.Resources.Contexts); l > 0 {
res := make([]string, 0, l)

for _, c := range platform.Spec.Resources.Contexts {
//
// Assuming that if the resource ends with a yaml extension, the full
// resource name is provided
//
if !strings.HasSuffix(c, ".yaml") && !strings.HasSuffix(c, ".yml") {
c = fmt.Sprintf("platform-integration-context-%s.yaml", c)
}

if platform.Spec.Profile == v1alpha1.TraitProfileKnative {
logrus.Info("Installing knative resources")
err := install.Resources(ctx, action.client, platform.Namespace, knativeResources...)
res = append(res, c)
}

logrus.Info("Installing custom platform resources")
err := install.Resources(ctx, action.client, platform.Namespace, res...)
if err != nil {
return err
}
} else {
logrus.Info("Installing default platform resources")
err := install.Resources(ctx, action.client, platform.Namespace, p.DefaultContexts...)
if err != nil {
return err
}

if platform.Spec.Profile == v1alpha1.TraitProfileKnative {
logrus.Info("Installing knative resources")
err := install.Resources(ctx, action.client, platform.Namespace, p.KnativeContexts...)
if err != nil {
return err
}
}
}

target := platform.DeepCopy()
Expand Down
53 changes: 53 additions & 0 deletions pkg/platform/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
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 platform

import "strings"

// DefaultContexts --
var DefaultContexts = []string{
"platform-integration-context-jvm.yaml",
"platform-integration-context-groovy.yaml",
"platform-integration-context-kotlin.yaml",
"platform-integration-context-spring-boot.yaml",
}

// KnativeContexts --
var KnativeContexts = []string{
"platform-integration-context-knative.yaml",
}

// GetContexts --
func GetContexts() []string {
return append(DefaultContexts, KnativeContexts...)
}

// GetContextsNames --
func GetContextsNames() []string {
ctxs := GetContexts()
names := make([]string, 0, len(ctxs))

for _, r := range ctxs {
r = strings.TrimPrefix(r, "platform-integration-context-")
r = strings.TrimSuffix(r, ".yaml")

names = append(names, r)
}

return names
}

0 comments on commit 2cb5754

Please sign in to comment.