|
| 1 | +package cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/pkg/errors" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 10 | + "k8s.io/client-go/discovery" |
| 11 | + "k8s.io/client-go/rest" |
| 12 | + |
| 13 | + v1 "github.com/openshift/api/config/v1" |
| 14 | + configv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" |
| 15 | +) |
| 16 | + |
| 17 | +type Capabilities struct { |
| 18 | + IsOnOpenshift, HasMachineAPI bool |
| 19 | +} |
| 20 | + |
| 21 | +func NewCapabilities(config *rest.Config) (Capabilities, error) { |
| 22 | + var err error |
| 23 | + var onOpenshift, machineAPI bool |
| 24 | + |
| 25 | + if onOpenshift, err = isOnOpenshift(config); err != nil { |
| 26 | + return Capabilities{}, errors.Wrap(err, "failed to check if cluster is on OpenShift") |
| 27 | + } else if onOpenshift { |
| 28 | + if machineAPI, err = hasMachineAPICapability(config); err != nil { |
| 29 | + return Capabilities{}, errors.Wrap(err, "failed to check machine API capability") |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return Capabilities{ |
| 34 | + IsOnOpenshift: onOpenshift, |
| 35 | + HasMachineAPI: machineAPI, |
| 36 | + }, nil |
| 37 | +} |
| 38 | + |
| 39 | +// IsOnOpenshift returns true if the cluster has the openshift config group |
| 40 | +func isOnOpenshift(config *rest.Config) (bool, error) { |
| 41 | + dc, err := discovery.NewDiscoveryClientForConfig(config) |
| 42 | + if err != nil { |
| 43 | + return false, err |
| 44 | + } |
| 45 | + apiGroups, err := dc.ServerGroups() |
| 46 | + kind := schema.GroupVersionKind{Group: "config.openshift.io", Version: "v1", Kind: "ClusterVersion"} |
| 47 | + for _, apiGroup := range apiGroups.Groups { |
| 48 | + for _, supportedVersion := range apiGroup.Versions { |
| 49 | + if supportedVersion.GroupVersion == kind.GroupVersion().String() { |
| 50 | + return true, nil |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + return false, nil |
| 55 | +} |
| 56 | + |
| 57 | +// HasMachineAPICapability returns true if the cluster has the MachineAPI Enabled |
| 58 | +func hasMachineAPICapability(config *rest.Config) (bool, error) { |
| 59 | + configV1Client, err := configv1.NewForConfig(config) |
| 60 | + if err != nil { |
| 61 | + return false, errors.Wrapf(err, "failed to create cluster version client") |
| 62 | + } |
| 63 | + cvs, err := configV1Client.ClusterVersions().List(context.Background(), metav1.ListOptions{}) |
| 64 | + if err != nil { |
| 65 | + return false, errors.Wrapf(err, "failed to get ClusterVersion") |
| 66 | + } |
| 67 | + |
| 68 | + for _, cv := range cvs.Items { |
| 69 | + if cv.Status.Capabilities.EnabledCapabilities == nil { |
| 70 | + return false, nil |
| 71 | + } |
| 72 | + var MachineAPI v1.ClusterVersionCapability = "MachineAPI" |
| 73 | + for _, capability := range cv.Status.Capabilities.EnabledCapabilities { |
| 74 | + if capability == MachineAPI { |
| 75 | + return true, nil |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return false, nil |
| 80 | +} |
0 commit comments