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://github.com/raw/jaegertracing/jaeger-operator/mas oc create -f https://github.com/raw/jaegertracing/jaeger-operator/master/deploy/service_account.yaml oc create -f https://github.com/raw/jaegertracing/jaeger-operator/master/deploy/role.yaml oc create -f https://github.com/raw/jaegertracing/jaeger-operator/master/deploy/role_binding.yaml -oc create -f https://github.com/raw/jaegertracing/jaeger-operator/master/deploy/operator-openshift.yaml +oc create -f https://github.com/raw/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/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 4ea0fd35e..d58592210 100644 --- a/pkg/cmd/start/main.go +++ b/pkg/cmd/start/main.go @@ -2,16 +2,20 @@ 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" "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" ) @@ -57,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", "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', '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") @@ -99,7 +103,23 @@ func start(cmd *cobra.Command, args []string) { log.Fatal(err) } - log.Print("Registering Components.") + if strings.EqualFold(viper.GetString("platform"), v1alpha1.FlagPlatformAutoDetect) { + 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", v1alpha1.FlagPlatformOpenShift) + } else { + viper.Set("platform", v1alpha1.FlagPlatformKubernetes) + } + + 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 +136,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 +}