From 47297020d58c1a2426533c4b9fe9715eca7f9ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraci=20Paix=C3=A3o=20Kr=C3=B6hling?= Date: Thu, 21 Feb 2019 14:23:36 +0100 Subject: [PATCH 1/2] Added capability of detecting the platform MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Juraci Paixão Kröhling --- Makefile | 4 ---- README.adoc | 6 ++--- deploy/operator-openshift.yaml | 33 -------------------------- pkg/cmd/start/main.go | 43 ++++++++++++++++++++++++++++++++-- 4 files changed, 44 insertions(+), 42 deletions(-) delete mode 100644 deploy/operator-openshift.yaml diff --git a/Makefile b/Makefile index bca8b075b..5e7dff9df 100644 --- a/Makefile +++ b/Makefile @@ -82,10 +82,6 @@ e2e-tests: cassandra es crd build docker push run: crd @bash -c 'trap "exit 0" INT; OPERATOR_NAME=${OPERATOR_NAME} KUBERNETES_CONFIG=${KUBERNETES_CONFIG} WATCH_NAMESPACE=${WATCH_NAMESPACE} go run -ldflags ${LD_FLAGS} main.go start' -.PHONY: run-openshift -run-openshift: crd - @bash -c 'trap "exit 0" INT; OPERATOR_NAME=${OPERATOR_NAME} KUBERNETES_CONFIG=${KUBERNETES_CONFIG} WATCH_NAMESPACE=${WATCH_NAMESPACE} go run -ldflags ${LD_FLAGS} main.go start --platform=openshift' - .PHONY: es es: @kubectl create -f ./test/elasticsearch.yml 2>&1 | grep -v "already exists" || true diff --git a/README.adoc b/README.adoc index 48f9a84c5..0e6aabdcd 100644 --- a/README.adoc +++ b/README.adoc @@ -44,7 +44,7 @@ The operator is now ready to create Jaeger instances! === OpenShift -The instructions from the previous section also work on OpenShift given that the `operator-openshift.yaml` is used instead of `operator.yaml`. Make sure to install the RBAC rules, the CRD and the operator as a privileged user, such as `system:admin`. +The instructions from the previous section also work on OpenShift. Make sure to install the RBAC rules, the CRD and the operator as a privileged user, such as `system:admin`. [source,bash] ---- @@ -55,7 +55,7 @@ oc create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/mas oc create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/service_account.yaml oc create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/role.yaml oc create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/role_binding.yaml -oc create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/operator-openshift.yaml +oc create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/operator.yaml ---- <1> This creates the namespace used by default in the deployment files. @@ -252,7 +252,7 @@ NAME HOSTS ADDRESS PORTS AGE simplest-query * 192.168.122.34 80 3m ---- -IMPORTANT: an `Ingress` object is *not* created when the operator is started with the `--platform=openshift` flag, such as when using the resource `operator-openshift.yaml`. +IMPORTANT: an `Ingress` object is *not* created when the operator is running on OpenShift In this example, the Jaeger UI is available at http://192.168.122.34 diff --git a/deploy/operator-openshift.yaml b/deploy/operator-openshift.yaml deleted file mode 100644 index f5be7300a..000000000 --- a/deploy/operator-openshift.yaml +++ /dev/null @@ -1,33 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: jaeger-operator - namespace: observability -spec: - replicas: 1 - selector: - matchLabels: - name: jaeger-operator - template: - metadata: - labels: - name: jaeger-operator - spec: - serviceAccountName: jaeger-operator - containers: - - name: jaeger-operator - image: jaegertracing/jaeger-operator:1.9.2 - ports: - - containerPort: 60000 - name: metrics - args: ["start", "--platform=openshift"] - imagePullPolicy: Always - env: - - name: WATCH_NAMESPACE - value: "" - - name: POD_NAME - valueFrom: - fieldRef: - fieldPath: metadata.name - - name: OPERATOR_NAME - value: "jaeger-operator" diff --git a/pkg/cmd/start/main.go b/pkg/cmd/start/main.go index 4ea0fd35e..6865b1092 100644 --- a/pkg/cmd/start/main.go +++ b/pkg/cmd/start/main.go @@ -2,11 +2,14 @@ package start import ( "runtime" + "strings" "github.com/operator-framework/operator-sdk/pkg/k8sutil" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" + "k8s.io/client-go/discovery" + "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client/config" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/runtime/signals" @@ -57,7 +60,7 @@ func NewStartCommand() *cobra.Command { cmd.Flags().String("openshift-oauth-proxy-image", "openshift/oauth-proxy:latest", "The Docker image location definition for the OpenShift OAuth Proxy") viper.BindPFlag("openshift-oauth-proxy-image", cmd.Flags().Lookup("openshift-oauth-proxy-image")) - cmd.Flags().String("platform", "kubernetes", "The target platform the operator will run. Possible values: 'kubernetes' and 'openshift'") + cmd.Flags().String("platform", "auto-detect", "The target platform the operator will run. Possible values: 'kubernetes' and 'openshift'") viper.BindPFlag("platform", cmd.Flags().Lookup("platform")) cmd.Flags().String("log-level", "info", "The log-level for the operator. Possible values: trace, debug, info, warning, error, fatal, panic") @@ -99,7 +102,23 @@ func start(cmd *cobra.Command, args []string) { log.Fatal(err) } - log.Print("Registering Components.") + if strings.EqualFold(viper.GetString("platform"), "auto-detect") { + log.Debug("Attempting to auto-detect the platform") + os, err := detectOpenShift(mgr.GetConfig()) + if err != nil { + log.WithError(err).Info("failed to auto-detect the platform, falling back to 'kubernetes'") + } + + if os { + viper.Set("platform", "openshift") + } else { + viper.Set("platform", "kubernetes") + } + + log.WithField("platform", viper.GetString("platform")).Info("Auto-detected the platform") + } else { + log.WithField("platform", viper.GetString("platform")).Debug("The 'platform' option is set") + } // Setup Scheme for all resources if err := apis.AddToScheme(mgr.GetScheme()); err != nil { @@ -116,3 +135,23 @@ func start(cmd *cobra.Command, args []string) { // Start the Cmd log.Fatal(mgr.Start(signals.SetupSignalHandler())) } + +// copied from Snowdrop's Component Operator +// https://github.com/snowdrop/component-operator/blob/744a9501c58f60877aa2b3a7e6c75da669519e8e/pkg/util/kubernetes/config.go +func detectOpenShift(kubeconfig *rest.Config) (bool, error) { + discoveryClient, err := discovery.NewDiscoveryClientForConfig(kubeconfig) + if err != nil { + return false, err + } + apiList, err := discoveryClient.ServerGroups() + if err != nil { + return false, err + } + apiGroups := apiList.Groups + for i := 0; i < len(apiGroups); i++ { + if apiGroups[i].Name == "route.openshift.io" { + return true, nil + } + } + return false, nil +} From 29ff938cca9b82396203541c3e3f577b373122bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraci=20Paix=C3=A3o=20Kr=C3=B6hling?= Date: Thu, 21 Feb 2019 17:35:57 +0100 Subject: [PATCH 2/2] Added new flag constant for auto-detect (platform) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Juraci Paixão Kröhling --- pkg/apis/io/v1alpha1/jaeger_types.go | 3 +++ pkg/cmd/start/main.go | 9 +++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/apis/io/v1alpha1/jaeger_types.go b/pkg/apis/io/v1alpha1/jaeger_types.go index 5e20bfd37..44a7e1dcb 100644 --- a/pkg/apis/io/v1alpha1/jaeger_types.go +++ b/pkg/apis/io/v1alpha1/jaeger_types.go @@ -18,6 +18,9 @@ const ( // FlagPlatformOpenShift represents the value for the 'platform' flag for OpenShift FlagPlatformOpenShift = "openshift" + // FlagPlatformAutoDetect represents the "auto-detect" value for the platform flag + FlagPlatformAutoDetect = "auto-detect" + // IngressSecurityNone disables any form of security for ingress objects (default) IngressSecurityNone IngressSecurityType = "" diff --git a/pkg/cmd/start/main.go b/pkg/cmd/start/main.go index 6865b1092..d58592210 100644 --- a/pkg/cmd/start/main.go +++ b/pkg/cmd/start/main.go @@ -15,6 +15,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/runtime/signals" "github.com/jaegertracing/jaeger-operator/pkg/apis" + "github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1" "github.com/jaegertracing/jaeger-operator/pkg/controller" "github.com/jaegertracing/jaeger-operator/pkg/version" ) @@ -60,7 +61,7 @@ func NewStartCommand() *cobra.Command { cmd.Flags().String("openshift-oauth-proxy-image", "openshift/oauth-proxy:latest", "The Docker image location definition for the OpenShift OAuth Proxy") viper.BindPFlag("openshift-oauth-proxy-image", cmd.Flags().Lookup("openshift-oauth-proxy-image")) - cmd.Flags().String("platform", "auto-detect", "The target platform the operator will run. Possible values: 'kubernetes' and 'openshift'") + cmd.Flags().String("platform", "auto-detect", "The target platform the operator will run. Possible values: 'kubernetes', 'openshift', 'auto-detect'") viper.BindPFlag("platform", cmd.Flags().Lookup("platform")) cmd.Flags().String("log-level", "info", "The log-level for the operator. Possible values: trace, debug, info, warning, error, fatal, panic") @@ -102,7 +103,7 @@ func start(cmd *cobra.Command, args []string) { log.Fatal(err) } - if strings.EqualFold(viper.GetString("platform"), "auto-detect") { + if strings.EqualFold(viper.GetString("platform"), v1alpha1.FlagPlatformAutoDetect) { log.Debug("Attempting to auto-detect the platform") os, err := detectOpenShift(mgr.GetConfig()) if err != nil { @@ -110,9 +111,9 @@ func start(cmd *cobra.Command, args []string) { } if os { - viper.Set("platform", "openshift") + viper.Set("platform", v1alpha1.FlagPlatformOpenShift) } else { - viper.Set("platform", "kubernetes") + viper.Set("platform", v1alpha1.FlagPlatformKubernetes) } log.WithField("platform", viper.GetString("platform")).Info("Auto-detected the platform")