Skip to content

Commit e53bce4

Browse files
authored
Merge pull request #410 from openshift-cherrypick-robot/cherry-pick-391-to-release-1.0
[release-1.0] Use SecurityContextConstraints resource to identify OCP environment
2 parents 787538b + a14c653 commit e53bce4

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

controllers/common/utils/kubernetes/common.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
9+
"sync"
810

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

71+
var onceIsOpenshift sync.Once
72+
var isOpenshift bool
73+
6974
func IsOpenShift(client client.Client) bool {
70-
_, err := client.RESTMapper().ResourceFor(schema.GroupVersionResource{
71-
Group: "image.openshift.io",
72-
Version: "v1",
73-
Resource: "image",
75+
// atomic
76+
onceIsOpenshift.Do(func() {
77+
log := ctrllog.Log.WithName("IsOpenshift")
78+
_, err := client.RESTMapper().ResourceFor(schema.GroupVersionResource{
79+
Group: "security.openshift.io",
80+
Resource: "SecurityContextConstraints",
81+
})
82+
if err != nil {
83+
// continue with non-ocp standard
84+
log.Info("no")
85+
log.V(1).Info(err.Error())
86+
isOpenshift = false
87+
return
88+
}
89+
isOpenshift = true
90+
log.Info("yes")
7491
})
75-
if err != nil {
76-
// continue with non-ocp standard
77-
return false
78-
}
79-
return true
92+
return isOpenshift
8093
}
8194

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

0 commit comments

Comments
 (0)