Skip to content

Commit

Permalink
feat: Add a --global install option
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Jun 18, 2019
1 parent 58aae0c commit 56b6197
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
9 changes: 8 additions & 1 deletion pkg/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command {
cmd.Flags().BoolVar(&impl.skipOperatorSetup, "skip-operator-setup", false, "Do not install the operator in the namespace (in case there's a global one)")
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().BoolVar(&impl.global, "global", false, "Configure the operator to watch all namespaces")

cmd.Flags().StringVarP(&impl.outputFormat, "output", "o", "", "Output format. One of: json|yaml")
cmd.Flags().StringVar(&impl.registry.Organization, "organization", "", "A organization on the Docker registry that can be used to publish images")
Expand Down Expand Up @@ -102,6 +103,7 @@ type installCmdOptions struct {
skipOperatorSetup bool
skipClusterSetup bool
exampleSetup bool
global bool
outputFormat string
camelVersion string
runtimeVersion string
Expand Down Expand Up @@ -152,7 +154,12 @@ func (o *installCmdOptions) install(_ *cobra.Command, _ []string) error {
namespace := o.Namespace

if !o.skipOperatorSetup {
err = install.OperatorOrCollect(o.Context, c, namespace, o.operatorImage, collection)
cfg := install.OperatorConfiguration{
CustomImage: o.operatorImage,
Namespace: namespace,
Global: o.global,
}
err = install.OperatorOrCollect(o.Context, c, cfg, collection)
if err != nil {
return err
}
Expand Down
71 changes: 60 additions & 11 deletions pkg/install/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,94 @@ import (
"errors"

v1 "k8s.io/api/apps/v1"
v1beta1 "k8s.io/api/rbac/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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"
"github.com/apache/camel-k/pkg/util/envvar"
"github.com/apache/camel-k/pkg/util/knative"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/minishift"
"github.com/apache/camel-k/pkg/util/openshift"
)

// OperatorConfiguration --
type OperatorConfiguration struct {
CustomImage string
Namespace string
Global bool
}

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

// OperatorOrCollect installs the operator resources or adds them to the collector if present
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 {
func OperatorOrCollect(ctx context.Context, c client.Client, cfg OperatorConfiguration, collection *kubernetes.Collection) error {
customizer := func(o runtime.Object) runtime.Object {
if cfg.CustomImage != "" {
if d, ok := o.(*v1.Deployment); ok {
if d.Labels["camel.apache.org/component"] == "operator" {
d.Spec.Template.Spec.Containers[0].Image = customImage
d.Spec.Template.Spec.Containers[0].Image = cfg.CustomImage
}
}
return o
}

if cfg.Global {
if d, ok := o.(*v1.Deployment); ok {
if d.Labels["camel.apache.org/component"] == "operator" {
// Make the operator watch all namespaces
envvar.SetVal(&d.Spec.Template.Spec.Containers[0].Env, "WATCH_NAMESPACE", "")
}
}

// Turn Role & RoleBinding into their equivalent cluster types
if r, ok := o.(*v1beta1.Role); ok {
target := r.DeepCopy()
o = &v1beta1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Namespace: cfg.Namespace,
Name: target.Name,
},
Rules: target.Rules,
}
}

if rb, ok := o.(*v1beta1.RoleBinding); ok {
target := rb.DeepCopy()
target.Subjects[0].Namespace = cfg.Namespace

o = &v1beta1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Namespace: cfg.Namespace,
Name: target.Name,
},
Subjects: target.Subjects,
RoleRef: v1beta1.RoleRef{
APIGroup: target.RoleRef.APIGroup,
Kind: "ClusterRole",
Name: target.RoleRef.Name,
},
}
}
}
return o
}

isOpenshift, err := openshift.IsOpenShift(c)
if err != nil {
return err
}
if isOpenshift {
if err := installOpenshift(ctx, c, namespace, customizer, collection); err != nil {
if err := installOpenshift(ctx, c, cfg.Namespace, customizer, collection); err != nil {
return err
}
} else {
if err := installKubernetes(ctx, c, namespace, customizer, collection); err != nil {
if err := installKubernetes(ctx, c, cfg.Namespace, customizer, collection); err != nil {
return err
}
}
Expand All @@ -70,7 +119,7 @@ func OperatorOrCollect(ctx context.Context, c client.Client, namespace string, c
return err
}
if isKnative {
return installKnative(ctx, c, namespace, collection)
return installKnative(ctx, c, cfg.Namespace, collection)
}
return nil
}
Expand Down
11 changes: 8 additions & 3 deletions test/testing_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ package test

import (
"context"
"k8s.io/apimachinery/pkg/labels"
"time"

"k8s.io/apimachinery/pkg/labels"

"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/install"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -55,7 +56,11 @@ func init() {
panic(err)
}

err = install.Operator(testContext, testClient, "", getTargetNamespace())
cfg := install.OperatorConfiguration{
Namespace: getTargetNamespace(),
}

err = install.Operator(testContext, testClient, cfg)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 56b6197

Please sign in to comment.