Skip to content

Commit 3959b31

Browse files
authored
Update OpenShift detection logic
This looks for the presence of SecurityContextConstraints as the delimiter for if this is running in an OpenShift cluster. With more Kubernetes APIs having an OpenShift suffix that can run in other environments, this gives a stronger check of actually running in an OpenShift cluster. Issue: [sc-12837] Issue: #2778
1 parent 5b4808f commit 3959b31

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

internal/config/pgoconfig.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ import (
3939
)
4040

4141
const (
42-
CustomConfigMapName = "pgo-config"
43-
defaultConfigPath = "/default-pgo-config/"
44-
openShiftAPIGroupSuffix = ".openshift.io"
42+
CustomConfigMapName = "pgo-config"
43+
defaultConfigPath = "/default-pgo-config/"
44+
openShiftSCCGroup = "security.openshift.io"
45+
openShiftSCCKind = "SecurityContextConstraints"
4546
)
4647

4748
var PgoDefaultServiceAccountTemplate *template.Template
@@ -892,18 +893,24 @@ func (c *PgoConfig) DisableFSGroup() bool {
892893

893894
// isOpenShift returns true if we've detected that we're in an OpenShift cluster
894895
func isOpenShift(clientset kubernetes.Interface) bool {
895-
groups, _, err := clientset.Discovery().ServerGroupsAndResources()
896+
_, resourceLists, err := clientset.Discovery().ServerGroupsAndResources()
896897

897898
if err != nil {
898899
log.Errorf("could not get server api groups: %s", err.Error())
899900
return false
900901
}
901902

902-
// ff we detect that any API group name ends with "openshift.io", we'll return
903-
// that this is an OpenShift environment
904-
for _, g := range groups {
905-
if strings.HasSuffix(g.Name, openShiftAPIGroupSuffix) {
906-
return true
903+
// If we detect that the "SecurityContextConstraints" Kind is present in the
904+
// "security.openshift.io" Group, we'll return that this is an OpenShift
905+
// environment
906+
for _, rl := range resourceLists {
907+
if strings.HasPrefix(rl.GroupVersion, openShiftSCCGroup+"/") {
908+
for _, r := range rl.APIResources {
909+
if r.Kind == openShiftSCCKind {
910+
log.Info("detected OpenShift environment")
911+
return true
912+
}
913+
}
907914
}
908915
}
909916

0 commit comments

Comments
 (0)