Fix error while checking if running in openshift
Use KV implementation directly instead of copying it.
@@ -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"
)
@@ -18,7 +18,6 @@ type ClusterInfo interface {
}
type ClusterInfoImp struct {
- firstTime bool
runningInOpenshift bool
runningLocally bool
}
@@ -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"
}
- 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
}
@@ -126,7 +106,6 @@ func (c ClusterInfoImp) findApi(apis []*metav1.APIResourceList, resourceName str
func init() {
clusterInfo = &ClusterInfoImp{
- firstTime: true,
runningInOpenshift: false,
}
}
