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

Fix #698: 2nd attempt #704

Merged
merged 1 commit into from
May 29, 2019
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
6 changes: 6 additions & 0 deletions assets/json-schema/Integration.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@
"camelVersion": {
"type": "string"
},
"configuration": {
"items": {
"$ref": "#/definitions/ConfigurationSpec"
},
"type": "array"
},
"context": {
"type": "string"
},
Expand Down
5 changes: 5 additions & 0 deletions deploy/operator-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.name
# NAMESPACE is always the operator namespace, independently from WATCH_NAMESPACE
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
27 changes: 5 additions & 22 deletions deploy/resources.go

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

26 changes: 10 additions & 16 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.

4 changes: 3 additions & 1 deletion pkg/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command {
cmd.Flags().StringVar(&impl.camelVersion, "camel-version", "", "Set the camel version")
cmd.Flags().StringVar(&impl.runtimeVersion, "runtime-version", "", "Set the camel-k runtime version")
cmd.Flags().StringVar(&impl.baseImage, "base-image", "", "Set the base image used to run integrations")
cmd.Flags().StringVar(&impl.operatorImage, "operator-image", "", "Set the operator image used for the operator deployment")
cmd.Flags().StringSliceVar(&impl.contexts, "context", nil, "Add a camel context to build at startup, by default all known contexts are built")
cmd.Flags().StringVar(&impl.buildStrategy, "build-strategy", "", "Set the build strategy")
cmd.Flags().StringVar(&impl.buildTimeout, "build-timeout", "", "Set how long the build process can last")
Expand Down Expand Up @@ -96,6 +97,7 @@ type installCmdOptions struct {
camelVersion string
runtimeVersion string
baseImage string
operatorImage string
localRepository string
buildStrategy string
buildTimeout string
Expand Down Expand Up @@ -139,7 +141,7 @@ func (o *installCmdOptions) install(_ *cobra.Command, _ []string) error {

namespace := o.Namespace

err = install.OperatorOrCollect(o.Context, c, namespace, collection)
err = install.OperatorOrCollect(o.Context, c, namespace, o.operatorImage, collection)
if err != nil {
return err
}
Expand Down
21 changes: 17 additions & 4 deletions pkg/controller/build/schedule_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"context"
"sync"

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

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -31,7 +34,6 @@ import (
"github.com/pkg/errors"

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

// NewSchedulePodAction creates a new schedule action
Expand Down Expand Up @@ -88,10 +90,16 @@ func (action *schedulePodAction) Handle(ctx context.Context, build *v1alpha1.Bui
return nil
}

// Try to get operator image name before starting the build
operatorImage, err := platform.GetCurrentOperatorImage(ctx, action.client)
if err != nil {
return err
}

// Otherwise, let's create the build pod
// We may want to explicitly manage build priority as opposed to relying on
// the reconcile loop to handle the queuing
pod := newBuildPod(build)
pod := newBuildPod(build, operatorImage)

// Set the Build instance as the owner and controller
if err := controllerutil.SetControllerReference(build, pod, action.client.GetScheme()); err != nil {
Expand All @@ -115,7 +123,11 @@ func (action *schedulePodAction) Handle(ctx context.Context, build *v1alpha1.Bui
return action.client.Status().Update(ctx, target)
}

func newBuildPod(build *v1alpha1.Build) *corev1.Pod {
func newBuildPod(build *v1alpha1.Build, operatorImage string) *corev1.Pod {
builderImage := operatorImage
if builderImage == "" {
builderImage = defaults.ImageName + ":" + defaults.Version
}
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Expand All @@ -130,7 +142,8 @@ func newBuildPod(build *v1alpha1.Build) *corev1.Pod {
Containers: []corev1.Container{
{
Name: "builder",
Image: defaults.ImageName + ":" + defaults.Version,
Image: builderImage,
ImagePullPolicy: "IfNotPresent",
Args: []string{
"camel-k-builder",
build.Namespace,
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/integration/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (action *initializeAction) CanHandle(integration *v1alpha1.Integration) boo
// Handle handles the integrations
func (action *initializeAction) Handle(ctx context.Context, integration *v1alpha1.Integration) error {
pl, err := platform.GetCurrentPlatform(ctx, action.client, integration.Namespace)
if err != nil {
return err
}

// The integration platform needs to be ready before starting to create integrations
if err != nil || pl.Status.Phase != v1alpha1.IntegrationPlatformPhaseReady {
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/integrationplatform/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (action *createAction) Handle(ctx context.Context, platform *v1alpha1.Integ
for k := range deploy.Resources {
if strings.HasPrefix(k, "camel-catalog-") {
action.L.Infof("Installing camel catalog: %s", k)
err := install.Resources(ctx, action.client, platform.Namespace, k)
err := install.Resources(ctx, action.client, platform.Namespace, install.IdentityResourceCustomizer, k)
if err != nil {
return err
}
Expand Down Expand Up @@ -79,21 +79,21 @@ func (action *createAction) Handle(ctx context.Context, platform *v1alpha1.Integ

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

if platform.Spec.Profile == v1alpha1.TraitProfileKnative {
action.L.Info("Installing knative resources")
err := install.Resources(ctx, action.client, platform.Namespace, p.KnativeContexts...)
err := install.Resources(ctx, action.client, platform.Namespace, install.IdentityResourceCustomizer, p.KnativeContexts...)
if err != nil {
return err
}
Expand Down
24 changes: 16 additions & 8 deletions pkg/install/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,42 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

// ResourceCustomizer can be used to inject code that changes the objects before they are created
type ResourceCustomizer func(object runtime.Object)runtime.Object
// IdentityResourceCustomizer is a ResourceCustomizer that does nothing
var IdentityResourceCustomizer = func(object runtime.Object)runtime.Object {
return object
}


// Resources installs named resources from the project resource directory
func Resources(ctx context.Context, c client.Client, namespace string, names ...string) error {
return ResourcesOrCollect(ctx, c, namespace, nil, names...)
func Resources(ctx context.Context, c client.Client, namespace string, customizer ResourceCustomizer, names ...string) error {
return ResourcesOrCollect(ctx, c, namespace, nil, customizer, names...)
}

// ResourcesOrCollect --
func ResourcesOrCollect(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection, names ...string) error {
func ResourcesOrCollect(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection, customizer ResourceCustomizer, names ...string) error {
for _, name := range names {
if err := ResourceOrCollect(ctx, c, namespace, collection, name); err != nil {
if err := ResourceOrCollect(ctx, c, namespace, collection, customizer, name); err != nil {
return err
}
}
return nil
}

// Resource installs a single named resource from the project resource directory
func Resource(ctx context.Context, c client.Client, namespace string, name string) error {
return ResourceOrCollect(ctx, c, namespace, nil, name)
func Resource(ctx context.Context, c client.Client, namespace string, customizer ResourceCustomizer, name string) error {
return ResourceOrCollect(ctx, c, namespace, nil, customizer, name)
}

// ResourceOrCollect --
func ResourceOrCollect(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection, name string) error {
func ResourceOrCollect(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection, customizer ResourceCustomizer, name string) error {
obj, err := kubernetes.LoadResourceFromYaml(c.GetScheme(), deploy.Resources[name])
if err != nil {
return err
}

return RuntimeObjectOrCollect(ctx, c, namespace, collection, obj)
return RuntimeObjectOrCollect(ctx, c, namespace, collection, customizer(obj))
}

// RuntimeObject installs a single runtime object
Expand Down
36 changes: 25 additions & 11 deletions pkg/install/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"context"
"errors"

v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"

"github.com/apache/camel-k/deploy"
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/client"
Expand All @@ -31,22 +34,33 @@ import (
)

// Operator installs the operator resources in the given namespace
func Operator(ctx context.Context, c client.Client, namespace string) error {
return OperatorOrCollect(ctx, c, namespace, nil)
func Operator(ctx context.Context, c client.Client, customImage string, namespace string) error {
return OperatorOrCollect(ctx, c, namespace, customImage, nil)
}

// OperatorOrCollect installs the operator resources or adds them to the collector if present
func OperatorOrCollect(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection) error {
func OperatorOrCollect(ctx context.Context, c client.Client, namespace string, customImage string, collection *kubernetes.Collection) error {
customizer := IdentityResourceCustomizer
if customImage != "" {
customizer = func(o runtime.Object) runtime.Object {
if d, ok := o.(*v1.Deployment); ok {
if d.Labels["camel.apache.org/component"] == "operator" {
d.Spec.Template.Spec.Containers[0].Image = customImage
}
}
return o
}
}
isOpenshift, err := openshift.IsOpenShift(c)
if err != nil {
return err
}
if isOpenshift {
if err := installOpenshift(ctx, c, namespace, collection); err != nil {
if err := installOpenshift(ctx, c, namespace, customizer, collection); err != nil {
return err
}
} else {
if err := installKubernetes(ctx, c, namespace, collection); err != nil {
if err := installKubernetes(ctx, c, namespace, customizer, collection); err != nil {
return err
}
}
Expand All @@ -61,17 +75,17 @@ func OperatorOrCollect(ctx context.Context, c client.Client, namespace string, c
return nil
}

func installOpenshift(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection) error {
return ResourcesOrCollect(ctx, c, namespace, collection,
func installOpenshift(ctx context.Context, c client.Client, namespace string, customizer ResourceCustomizer, collection *kubernetes.Collection) error {
return ResourcesOrCollect(ctx, c, namespace, collection, customizer,
"operator-service-account.yaml",
"operator-role-openshift.yaml",
"operator-role-binding.yaml",
"operator-deployment.yaml",
)
}

func installKubernetes(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection) error {
return ResourcesOrCollect(ctx, c, namespace, collection,
func installKubernetes(ctx context.Context, c client.Client, namespace string, customizer ResourceCustomizer, collection *kubernetes.Collection) error {
return ResourcesOrCollect(ctx, c, namespace, collection, customizer,
"operator-service-account.yaml",
"operator-role-kubernetes.yaml",
"operator-role-binding.yaml",
Expand All @@ -80,7 +94,7 @@ func installKubernetes(ctx context.Context, c client.Client, namespace string, c
}

func installKnative(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection) error {
return ResourcesOrCollect(ctx, c, namespace, collection,
return ResourcesOrCollect(ctx, c, namespace, collection, IdentityResourceCustomizer,
"operator-role-knative.yaml",
"operator-role-binding-knative.yaml",
)
Expand Down Expand Up @@ -143,7 +157,7 @@ func Example(ctx context.Context, c client.Client, namespace string) error {

// ExampleOrCollect --
func ExampleOrCollect(ctx context.Context, c client.Client, namespace string, collection *kubernetes.Collection) error {
return ResourcesOrCollect(ctx, c, namespace, collection,
return ResourcesOrCollect(ctx, c, namespace, collection, IdentityResourceCustomizer,
"cr-example.yaml",
)
}
Loading