Skip to content

Commit

Permalink
Fix error while checking if running in openshift
Browse files Browse the repository at this point in the history
Use KV implementation directly instead of copying it.

Signed-off-by: Nahshon Unna-Tsameret <[email protected]>
  • Loading branch information
nunnatsa committed Sep 17, 2020
1 parent 5c67dff commit d382928
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions pkg/util/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/go-logr/logr"
secv1 "github.com/openshift/api/security/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/pkg/util/cluster"
"os"
"strings"
)
Expand All @@ -18,7 +18,6 @@ type ClusterInfo interface {
}

type ClusterInfoImp struct {
firstTime bool
runningInOpenshift bool
runningLocally bool
}
Expand All @@ -30,43 +29,24 @@ func GetClusterInfo() ClusterInfo {
}

func (c *ClusterInfoImp) CheckRunningInOpenshift(logger logr.Logger, runningLocally bool) error {

if !c.firstTime {
return nil
}

c.runningLocally = runningLocally

virtCli, err := c.getKubevirtClient(logger, runningLocally)
virtClient, err := c.getKubevirtClient(logger, runningLocally)
if err != nil {
return err
}

c.runningInOpenshift = false
clusterType := "kubernetes"

_, apis, err := virtCli.DiscoveryClient().ServerGroupsAndResources()
isOpenShift, err := cluster.IsOnOpenShift(virtClient)
if err != nil {
if !discovery.IsGroupDiscoveryFailedError(err) {
logger.Error(err, "failed to get ServerGroupsAndResources")
return err
} else if discovery.IsGroupDiscoveryFailedError(err) {
// In case of an error, check if security.openshift.io is the reason (unlikely).
// If it is, we are obviously on an openshift cluster.
// Otherwise we can do a positive check.
e := err.(*discovery.ErrGroupDiscoveryFailed)
if _, exists := e.Groups[secv1.GroupVersion]; exists {
c.runningInOpenshift = true
clusterType = "openshift"
}
}
} else if c.findApi(apis, "securitycontextconstraints") {
c.runningInOpenshift = true
clusterType = "openshift"
return err
}

logger.Info("Cluster type = " + clusterType)
c.firstTime = false
c.runningInOpenshift = isOpenShift
if isOpenShift {
logger.Info("Cluster type = openshift")
} else {
logger.Info("Cluster type = kubernetes")
}

return nil
}
Expand Down Expand Up @@ -126,7 +106,6 @@ func (c ClusterInfoImp) findApi(apis []*metav1.APIResourceList, resourceName str

func init() {
clusterInfo = &ClusterInfoImp{
firstTime: true,
runningInOpenshift: false,
}
}

0 comments on commit d382928

Please sign in to comment.