Skip to content

Commit

Permalink
Remove --default-profile in favor of automatic platform detection (#…
Browse files Browse the repository at this point in the history
…434)

* Remove `--default-profile` in favor of automatic platform detection

When the operator detects that it's running on OpenShift, it sets the default profile to "openshift".

Signed-off-by: Marko Lukša <[email protected]>

* Fix detection on Kind

Signed-off-by: Marko Lukša <[email protected]>

---------

Signed-off-by: Marko Lukša <[email protected]>
  • Loading branch information
luksa authored Oct 17, 2024
1 parent 4af5c27 commit fae1cef
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
3 changes: 1 addition & 2 deletions bundle/manifests/sailoperator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ metadata:
capabilities: Seamless Upgrades
categories: OpenShift Optional, Integration & Delivery, Networking, Security
containerImage: quay.io/maistra-dev/sail-operator:0.2-latest
createdAt: "2024-10-17T09:56:08Z"
createdAt: "2024-10-17T16:01:13Z"
description: Experimental operator for installing Istio service mesh
features.operators.openshift.io/cnf: "false"
features.operators.openshift.io/cni: "true"
Expand Down Expand Up @@ -665,7 +665,6 @@ spec:
- --health-probe-bind-address=:8081
- --metrics-bind-address=127.0.0.1:8080
- --zap-log-level=info
- --default-profile=openshift
command:
- /sail-operator
image: quay.io/maistra-dev/sail-operator:0.2-latest
Expand Down
3 changes: 0 additions & 3 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ spec:
- --health-probe-bind-address=:8081
- --metrics-bind-address=127.0.0.1:8080
- --zap-log-level={{ .Values.operatorLogLevel }}
{{- if eq .Values.platform "openshift" }}
- --default-profile=openshift
{{- end }}
command:
- /sail-operator
image: {{ .Values.image }}
Expand Down
15 changes: 13 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ func main() {
var probeAddr string
var configFile string
var resourceDirectory string
var defaultProfile string
var logAPIRequests bool
var printVersion bool
var leaderElectionEnabled bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&configFile, "config-file", "/etc/sail-operator/config.properties", "Location of the config file, propagated by k8s downward APIs")
flag.StringVar(&resourceDirectory, "resource-directory", "/var/lib/sail-operator/resources", "Where to find resources (e.g. charts)")
flag.StringVar(&defaultProfile, "default-profile", "default", "The name of the profile to apply to Istio and IstioRevision resources by default")
flag.BoolVar(&logAPIRequests, "log-api-requests", false, "Whether to log each request sent to the Kubernetes API server")
flag.BoolVar(&printVersion, "version", printVersion, "Prints version information and exits")
flag.BoolVar(&leaderElectionEnabled, "leader-elect", true,
Expand Down Expand Up @@ -126,6 +124,19 @@ func main() {

chartManager := helm.NewChartManager(mgr.GetConfig(), os.Getenv("HELM_DRIVER"))

platform, err := config.DetectPlatform(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "unable to detect platform")
os.Exit(1)
}

var defaultProfile string
if platform == config.PlatformOpenShift {
defaultProfile = "openshift"
} else {
defaultProfile = "default"
}

err = istio.NewReconciler(mgr.GetClient(), mgr.GetScheme(), resourceDirectory, defaultProfile).
SetupWithManager(mgr)
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions pkg/config/platform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright Istio Authors
//
// Licensed 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 config

import (
"fmt"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
)

type Platform string

const (
PlatformOpenShift Platform = "openshift"
PlatformKubernetes Platform = "kubernetes"
)

const (
openshiftKind = "OpenShiftAPIServer"
openshiftResourceGroup = "operator.openshift.io"
openshiftResourceVersion = "v1"
)

func DetectPlatform(cfg *rest.Config) (Platform, error) {
dc, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return "", fmt.Errorf("failed to create discoveryClient: %w", err)
}

resources, err := dc.ServerResourcesForGroupVersion(openshiftResourceGroup + "/" + openshiftResourceVersion)
if errors.IsNotFound(err) {
return PlatformKubernetes, nil
} else if err != nil {
return "", err
}

for _, apiResource := range resources.APIResources {
if apiResource.Kind == openshiftKind {
return PlatformOpenShift, nil
}
}
return PlatformKubernetes, nil
}

0 comments on commit fae1cef

Please sign in to comment.