|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha" |
| 9 | + "github.com/arangodb/kube-arangodb/pkg/client" |
| 10 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 11 | + "github.com/arangodb/kube-arangodb/pkg/util/retry" |
| 12 | + "github.com/dchest/uniuri" |
| 13 | + v1beta1 "k8s.io/api/scheduling/v1beta1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/client-go/kubernetes" |
| 16 | + |
| 17 | + "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" |
| 18 | +) |
| 19 | + |
| 20 | +func waitForPriorityOfServerGroup(kube kubernetes.Interface, c versioned.Interface, depl, ns string, group api.ServerGroup, priority int32) error { |
| 21 | + return retry.Retry(func() error { |
| 22 | + |
| 23 | + apiObject, err := c.DatabaseV1alpha().ArangoDeployments(ns).Get(depl, metav1.GetOptions{}) |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + |
| 28 | + for _, m := range apiObject.Status.Members.MembersOfGroup(group) { |
| 29 | + pod, err := kube.CoreV1().Pods(apiObject.Namespace).Get(m.PodName, metav1.GetOptions{}) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + if pod.Spec.Priority == nil { |
| 35 | + return fmt.Errorf("No pod priority set") |
| 36 | + } |
| 37 | + |
| 38 | + if *pod.Spec.Priority != priority { |
| 39 | + return fmt.Errorf("Wrong pod priority, expected %d, found %d", priority, pod.Spec.Priority) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return nil |
| 44 | + }, 5*time.Minute) |
| 45 | +} |
| 46 | + |
| 47 | +// TestPriorityClasses creates a PriorityClass and associates coordinators with that class. |
| 48 | +// Then check if the pods have the desired priority. Then change the class and check that the pods are rotated. |
| 49 | +func TestPriorityClasses(t *testing.T) { |
| 50 | + longOrSkip(t) |
| 51 | + c := client.MustNewInCluster() |
| 52 | + kubecli := mustNewKubeClient(t) |
| 53 | + ns := getNamespace(t) |
| 54 | + |
| 55 | + lowClassName := "test-low-class" |
| 56 | + lowClassValue := int32(1000) |
| 57 | + |
| 58 | + highClassName := "test-high-class" |
| 59 | + highClassValue := int32(2000) |
| 60 | + |
| 61 | + // Create two priority classes |
| 62 | + if _, err := kubecli.SchedulingV1beta1().PriorityClasses().Create(&v1beta1.PriorityClass{ |
| 63 | + Value: lowClassValue, |
| 64 | + GlobalDefault: false, |
| 65 | + Description: "Low priority test class", |
| 66 | + ObjectMeta: metav1.ObjectMeta{ |
| 67 | + Name: lowClassName, |
| 68 | + }, |
| 69 | + }); err != nil { |
| 70 | + t.Fatalf("Could not create PC: %v", err) |
| 71 | + } |
| 72 | + defer kubecli.SchedulingV1beta1().PriorityClasses().Delete(lowClassName, &metav1.DeleteOptions{}) |
| 73 | + |
| 74 | + if _, err := kubecli.SchedulingV1beta1().PriorityClasses().Create(&v1beta1.PriorityClass{ |
| 75 | + Value: highClassValue, |
| 76 | + GlobalDefault: false, |
| 77 | + Description: "Low priority test class", |
| 78 | + ObjectMeta: metav1.ObjectMeta{ |
| 79 | + Name: highClassName, |
| 80 | + }, |
| 81 | + }); err != nil { |
| 82 | + t.Fatalf("Could not create PC: %v", err) |
| 83 | + } |
| 84 | + defer kubecli.SchedulingV1beta1().PriorityClasses().Delete(highClassName, &metav1.DeleteOptions{}) |
| 85 | + |
| 86 | + // Prepare deployment config |
| 87 | + depl := newDeployment("test-pc-" + uniuri.NewLen(4)) |
| 88 | + depl.Spec.Mode = api.NewMode(api.DeploymentModeCluster) |
| 89 | + depl.Spec.TLS = api.TLSSpec{CASecretName: util.NewString("None")} |
| 90 | + depl.Spec.DBServers.Count = util.NewInt(2) |
| 91 | + depl.Spec.Coordinators.Count = util.NewInt(2) |
| 92 | + depl.Spec.Coordinators.PriorityClassName = lowClassName |
| 93 | + depl.Spec.SetDefaults(depl.GetName()) // this must be last |
| 94 | + defer deferedCleanupDeployment(c, depl.GetName(), ns) |
| 95 | + |
| 96 | + // Create deployment |
| 97 | + _, err := c.DatabaseV1alpha().ArangoDeployments(ns).Create(depl) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Create deployment failed: %v", err) |
| 100 | + } |
| 101 | + defer deferedCleanupDeployment(c, depl.GetName(), ns) |
| 102 | + |
| 103 | + // Wait for deployment to be ready |
| 104 | + _, err = waitUntilDeployment(c, depl.GetName(), ns, deploymentIsReady()) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("Deployment not running in time: %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + if err := waitForPriorityOfServerGroup(kubecli, c, depl.GetName(), ns, api.ServerGroupCoordinators, lowClassValue); err != nil { |
| 110 | + t.Errorf("PDBs not as expected: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + _, err = updateDeployment(c, depl.GetName(), ns, func(spec *api.DeploymentSpec) { |
| 114 | + spec.Coordinators.PriorityClassName = highClassName |
| 115 | + }) |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("Failed to update deployment: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + // Check if priority class is updated |
| 121 | + if err := waitForPriorityOfServerGroup(kubecli, c, depl.GetName(), ns, api.ServerGroupCoordinators, highClassValue); err != nil { |
| 122 | + t.Errorf("Priority not as expected: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + removeDeployment(c, depl.GetName(), ns) |
| 126 | +} |
0 commit comments