|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + configv1 "github.com/openshift/api/config/v1" |
| 10 | + "github.com/openshift/api/machine/v1beta1" |
| 11 | + configclient "github.com/openshift/client-go/config/clientset/versioned" |
| 12 | + machinesetclient "github.com/openshift/client-go/machine/clientset/versioned/typed/machine/v1beta1" |
| 13 | + "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/runtime" |
| 16 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 17 | + "k8s.io/apimachinery/pkg/util/wait" |
| 18 | + "k8s.io/client-go/discovery" |
| 19 | + "k8s.io/client-go/dynamic" |
| 20 | + coreclient "k8s.io/client-go/kubernetes/typed/core/v1" |
| 21 | + "k8s.io/client-go/rest" |
| 22 | + "k8s.io/client-go/restmapper" |
| 23 | + "k8s.io/client-go/scale" |
| 24 | + e2e "k8s.io/kubernetes/test/e2e/framework" |
| 25 | + e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + MachineAPINamespace = "openshift-machine-api" |
| 30 | + MachineAPIGroup = "machine.openshift.io" |
| 31 | + ScaleTimeout = time.Second * 5 |
| 32 | +) |
| 33 | + |
| 34 | +// SkipUnlessMachineAPIOperator is used to deterine if the Machine API is installed and running in a cluster. |
| 35 | +// It is expected to skip the test if it determines that the Machine API is not installed/running. |
| 36 | +// Use this early in a test that relies on Machine API functionality. |
| 37 | +// |
| 38 | +// It checks to see if the machine custom resource is installed in the cluster. |
| 39 | +// If machines are not installed, or there are no machines in the cluster, it skips the test case. |
| 40 | +// It then checks to see if the `openshift-machine-api` namespace is installed. |
| 41 | +// If the namespace is not present it skips the test case. |
| 42 | +func SkipUnlessMachineAPIOperator(dc dynamic.Interface, c coreclient.NamespaceInterface) { |
| 43 | + machineClient := dc.Resource(schema.GroupVersionResource{Group: "machine.openshift.io", Resource: "machines", Version: "v1beta1"}) |
| 44 | + |
| 45 | + err := wait.PollImmediate(time.Second, time.Minute, func() (bool, error) { |
| 46 | + // Listing the resource will return an IsNotFound error when the CRD has not been installed. |
| 47 | + // Otherwise it would return an empty list if no Machines are in use, which should not be |
| 48 | + // possible if the MachineAPI operator is in use. |
| 49 | + machines, err := machineClient.List(context.Background(), metav1.ListOptions{}) |
| 50 | + // If no error was returned and the list of Machines is populated, this cluster is using MachineAPI |
| 51 | + if err == nil { |
| 52 | + // If the Machine CRD exists but there are no Machine objects in the cluster we should |
| 53 | + // skip the test because any cluster that is using MachineAPI from the install will have |
| 54 | + // Machines for the control plane nodes at the minimum. |
| 55 | + if len(machines.Items) == 0 { |
| 56 | + e2eskipper.Skipf("The cluster supports the Machine CRD but has no Machines available") |
| 57 | + } |
| 58 | + |
| 59 | + return true, nil |
| 60 | + } |
| 61 | + |
| 62 | + // Not found error on the Machine CRD, cluster is not using MachineAPI |
| 63 | + if errors.IsNotFound(err) { |
| 64 | + e2eskipper.Skipf("The cluster does not support machine instances") |
| 65 | + } |
| 66 | + e2e.Logf("Unable to check for machine api operator: %v", err) |
| 67 | + return false, nil |
| 68 | + }) |
| 69 | + Expect(err).NotTo(HaveOccurred()) |
| 70 | + |
| 71 | + err = wait.PollImmediate(time.Second, time.Minute, func() (bool, error) { |
| 72 | + // Check if the openshift-machine-api namespace is present, if not then this |
| 73 | + // cluster is not using MachineAPI. |
| 74 | + _, err := c.Get(context.Background(), "openshift-machine-api", metav1.GetOptions{}) |
| 75 | + if err == nil { |
| 76 | + return true, nil |
| 77 | + } |
| 78 | + if errors.IsNotFound(err) { |
| 79 | + e2eskipper.Skipf("The cluster machines are not managed by machine api operator") |
| 80 | + } |
| 81 | + e2e.Logf("Unable to check for machine api operator: %v", err) |
| 82 | + return false, nil |
| 83 | + }) |
| 84 | + Expect(err).NotTo(HaveOccurred()) |
| 85 | +} |
| 86 | + |
| 87 | +func LoadInfra(cfg *rest.Config) *configv1.Infrastructure { |
| 88 | + configClient, err := configclient.NewForConfig(cfg) |
| 89 | + Expect(err).NotTo(HaveOccurred()) |
| 90 | + |
| 91 | + infra, err := configClient.ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{}) |
| 92 | + Expect(err).NotTo(HaveOccurred()) |
| 93 | + |
| 94 | + return infra |
| 95 | +} |
| 96 | + |
| 97 | +func GetMachineSets(cfg *rest.Config) (*v1beta1.MachineSetList, error) { |
| 98 | + ctx := context.Background() |
| 99 | + client, err := machinesetclient.NewForConfig(cfg) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + ms := client.MachineSets(MachineAPINamespace) |
| 105 | + return ms.List(ctx, metav1.ListOptions{}) |
| 106 | +} |
| 107 | + |
| 108 | +// ScaleMachineSet scales a machineSet with a given name to the given number of replicas. |
| 109 | +// This was borrowed from origin. Ideally we should make this a sharable method if possible. |
| 110 | +func ScaleMachineSet(cfg *rest.Config, name string, replicas int) error { |
| 111 | + scaleClient, err := GetScaleClient(cfg) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("error calling getScaleClient: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Depending on how long its been since machineset was create, we may hit an issue. This eventually will just try |
| 117 | + // again for a few seconds and then quit if unable to scale. |
| 118 | + Eventually(func() error { |
| 119 | + scale, err := scaleClient.Scales(MachineAPINamespace).Get(context.Background(), schema.GroupResource{Group: MachineAPIGroup, Resource: "MachineSet"}, name, metav1.GetOptions{}) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("error calling scaleClient.Scales get: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + scaleUpdate := scale.DeepCopy() |
| 125 | + scaleUpdate.Spec.Replicas = int32(replicas) |
| 126 | + _, err = scaleClient.Scales(MachineAPINamespace).Update(context.Background(), schema.GroupResource{Group: MachineAPIGroup, Resource: "MachineSet"}, scaleUpdate, metav1.UpdateOptions{}) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("error calling scaleClient.Scales update while setting replicas to %d: %v", err, replicas) |
| 129 | + } |
| 130 | + return nil |
| 131 | + }, ScaleTimeout).ShouldNot(HaveOccurred()) |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func GetScaleClient(cfg *rest.Config) (scale.ScalesGetter, error) { |
| 136 | + discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg) |
| 137 | + if err != nil { |
| 138 | + return nil, fmt.Errorf("error discovering client: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + groupResources, err := restmapper.GetAPIGroupResources(discoveryClient) |
| 142 | + if err != nil { |
| 143 | + return nil, fmt.Errorf("error getting API resources: %v", err) |
| 144 | + } |
| 145 | + restMapper := restmapper.NewDiscoveryRESTMapper(groupResources) |
| 146 | + scaleKindResolver := scale.NewDiscoveryScaleKindResolver(discoveryClient) |
| 147 | + |
| 148 | + scaleClient, err := scale.NewForConfig(cfg, restMapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver) |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("error creating scale client: %v", err) |
| 151 | + } |
| 152 | + return scaleClient, nil |
| 153 | +} |
| 154 | + |
| 155 | +func CreateMachine(ctx context.Context, cfg *rest.Config, mc *machinesetclient.MachineV1beta1Client, machineName, role string, provider *runtime.RawExtension) (*v1beta1.Machine, error) { |
| 156 | + // Get infra for configs |
| 157 | + infra := LoadInfra(cfg) |
| 158 | + |
| 159 | + machine := &v1beta1.Machine{ |
| 160 | + TypeMeta: metav1.TypeMeta{ |
| 161 | + Kind: "MachineSet", |
| 162 | + APIVersion: "machine.openshift.io/v1beta1", |
| 163 | + }, |
| 164 | + ObjectMeta: metav1.ObjectMeta{ |
| 165 | + Name: machineName, |
| 166 | + Namespace: MachineAPINamespace, |
| 167 | + Labels: map[string]string{ |
| 168 | + "machine.openshift.io/test": machineName, |
| 169 | + "machine.openshift.io/cluster-api-cluster": infra.Status.InfrastructureName, |
| 170 | + "machine.openshift.io/cluster-api-machine-role": role, |
| 171 | + "machine.openshift.io/cluster-api-machine-type": role, |
| 172 | + }, |
| 173 | + }, |
| 174 | + Spec: v1beta1.MachineSpec{ |
| 175 | + ObjectMeta: v1beta1.ObjectMeta{}, |
| 176 | + ProviderSpec: v1beta1.ProviderSpec{ |
| 177 | + Value: provider, |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + return mc.Machines(MachineAPINamespace).Create(ctx, machine, metav1.CreateOptions{}) |
| 183 | +} |
| 184 | + |
| 185 | +func CreateMachineSet(ctx context.Context, cfg *rest.Config, mc *machinesetclient.MachineV1beta1Client, name, role string, provider *runtime.RawExtension) (*v1beta1.MachineSet, error) { |
| 186 | + replicas := int32(0) |
| 187 | + |
| 188 | + // Get infra for configs |
| 189 | + infra := LoadInfra(cfg) |
| 190 | + |
| 191 | + machineset := &v1beta1.MachineSet{ |
| 192 | + TypeMeta: metav1.TypeMeta{ |
| 193 | + Kind: "MachineSet", |
| 194 | + APIVersion: "machine.openshift.io/v1beta1", |
| 195 | + }, |
| 196 | + ObjectMeta: metav1.ObjectMeta{ |
| 197 | + Name: name, |
| 198 | + Namespace: MachineAPINamespace, |
| 199 | + Labels: map[string]string{ |
| 200 | + "machine.openshift.io/test": name, |
| 201 | + }, |
| 202 | + }, |
| 203 | + Spec: v1beta1.MachineSetSpec{ |
| 204 | + Selector: metav1.LabelSelector{ |
| 205 | + MatchLabels: map[string]string{ |
| 206 | + "machine.openshift.io/cluster-api-cluster": infra.Status.InfrastructureName, |
| 207 | + "machine.openshift.io/cluster-api-machineset": name, |
| 208 | + }, |
| 209 | + }, |
| 210 | + Replicas: &replicas, |
| 211 | + Template: v1beta1.MachineTemplateSpec{ |
| 212 | + ObjectMeta: v1beta1.ObjectMeta{ |
| 213 | + Labels: map[string]string{ |
| 214 | + "machine.openshift.io/cluster-api-machineset": name, |
| 215 | + "machine.openshift.io/cluster-api-cluster": infra.Status.InfrastructureName, |
| 216 | + "machine.openshift.io/cluster-api-machine-role": role, |
| 217 | + "machine.openshift.io/cluster-api-machine-type": role, |
| 218 | + }, |
| 219 | + }, |
| 220 | + Spec: v1beta1.MachineSpec{ |
| 221 | + LifecycleHooks: v1beta1.LifecycleHooks{}, |
| 222 | + ObjectMeta: v1beta1.ObjectMeta{}, |
| 223 | + ProviderSpec: v1beta1.ProviderSpec{ |
| 224 | + Value: provider, |
| 225 | + }, |
| 226 | + }, |
| 227 | + }, |
| 228 | + }, |
| 229 | + } |
| 230 | + |
| 231 | + return mc.MachineSets(MachineAPINamespace).Create(ctx, machineset, metav1.CreateOptions{}) |
| 232 | +} |
0 commit comments