Skip to content

Commit ebdfe63

Browse files
jkatzJonathan S. Katz
authored andcommitted
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 aebcae2 commit ebdfe63

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
@@ -875,18 +876,24 @@ func (c *PgoConfig) DisableFSGroup() bool {
875876

876877
// isOpenShift returns true if we've detected that we're in an OpenShift cluster
877878
func isOpenShift(clientset kubernetes.Interface) bool {
878-
groups, _, err := clientset.Discovery().ServerGroupsAndResources()
879+
_, resourceLists, err := clientset.Discovery().ServerGroupsAndResources()
879880

880881
if err != nil {
881882
log.Errorf("could not get server api groups: %s", err.Error())
882883
return false
883884
}
884885

885-
// ff we detect that any API group name ends with "openshift.io", we'll return
886-
// that this is an OpenShift environment
887-
for _, g := range groups {
888-
if strings.HasSuffix(g.Name, openShiftAPIGroupSuffix) {
889-
return true
886+
// If we detect that the "SecurityContextConstraints" Kind is present in the
887+
// "security.openshift.io" Group, we'll return that this is an OpenShift
888+
// environment
889+
for _, rl := range resourceLists {
890+
if strings.HasPrefix(rl.GroupVersion, openShiftSCCGroup+"/") {
891+
for _, r := range rl.APIResources {
892+
if r.Kind == openShiftSCCKind {
893+
log.Info("detected OpenShift environment")
894+
return true
895+
}
896+
}
890897
}
891898
}
892899

0 commit comments

Comments
 (0)