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

[release-1.0] Use SecurityContextConstraints resource to identify OCP environment #410

Merged
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
31 changes: 22 additions & 9 deletions controllers/common/utils/kubernetes/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"
"path/filepath"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sync"

v13 "github.com/openshift/api/operator/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -66,17 +68,28 @@ func ContainerMode() (bool, error) {
return false, nil
}

var onceIsOpenshift sync.Once
var isOpenshift bool

func IsOpenShift(client client.Client) bool {
_, err := client.RESTMapper().ResourceFor(schema.GroupVersionResource{
Group: "image.openshift.io",
Version: "v1",
Resource: "image",
// atomic
onceIsOpenshift.Do(func() {
log := ctrllog.Log.WithName("IsOpenshift")
_, err := client.RESTMapper().ResourceFor(schema.GroupVersionResource{
Group: "security.openshift.io",
Resource: "SecurityContextConstraints",
})
if err != nil {
// continue with non-ocp standard
log.Info("no")
log.V(1).Info(err.Error())
isOpenshift = false
return
}
isOpenshift = true
log.Info("yes")
})
if err != nil {
// continue with non-ocp standard
return false
}
return true
return isOpenshift
}

func CalculateHostname(ctx context.Context, client client.Client, svcName, ns string) (string, error) {
Expand Down
Loading