|
| 1 | +/* |
| 2 | +Copyright 2019 The Knative Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package testing |
| 18 | + |
| 19 | +import ( |
| 20 | + appsv1 "k8s.io/api/apps/v1" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | +) |
| 24 | + |
| 25 | +// StatefulSetOption enables further configuration of a StatefulSet. |
| 26 | +type StatefulSetOption func(*appsv1.StatefulSet) |
| 27 | + |
| 28 | +// NewStatefulSet creates a StatefulSet with StatefulSetOptions. |
| 29 | +func NewStatefulSet(name, namespace string, so ...StatefulSetOption) *appsv1.StatefulSet { |
| 30 | + s := &appsv1.StatefulSet{ |
| 31 | + ObjectMeta: metav1.ObjectMeta{ |
| 32 | + Namespace: namespace, |
| 33 | + Name: name, |
| 34 | + }, |
| 35 | + Spec: appsv1.StatefulSetSpec{ |
| 36 | + Template: corev1.PodTemplateSpec{ |
| 37 | + Spec: corev1.PodSpec{ |
| 38 | + Containers: []corev1.Container{{}}, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + for _, opt := range so { |
| 44 | + opt(s) |
| 45 | + } |
| 46 | + return s |
| 47 | +} |
| 48 | + |
| 49 | +func WithStatefulSetLabels(labels map[string]string) StatefulSetOption { |
| 50 | + return func(s *appsv1.StatefulSet) { |
| 51 | + s.ObjectMeta.Labels = labels |
| 52 | + s.Spec.Selector = &metav1.LabelSelector{ |
| 53 | + MatchLabels: labels, |
| 54 | + } |
| 55 | + s.Spec.Template.Labels = labels |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func WithStatefulSetOwnerReferences(ownerReferences []metav1.OwnerReference) StatefulSetOption { |
| 60 | + return func(s *appsv1.StatefulSet) { |
| 61 | + s.OwnerReferences = ownerReferences |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func WithStatefulSetAnnotations(annotations map[string]string) StatefulSetOption { |
| 66 | + return func(s *appsv1.StatefulSet) { |
| 67 | + s.Spec.Template.Annotations = annotations |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func WithStatefulSetServiceAccount(serviceAccountName string) StatefulSetOption { |
| 72 | + return func(s *appsv1.StatefulSet) { |
| 73 | + s.Spec.Template.Spec.ServiceAccountName = serviceAccountName |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func WithStatefulSetContainer(name, image string, liveness *corev1.Probe, readiness *corev1.Probe, envVars []corev1.EnvVar, containerPorts []corev1.ContainerPort) StatefulSetOption { |
| 78 | + return func(s *appsv1.StatefulSet) { |
| 79 | + s.Spec.Template.Spec.Containers[0].Name = name |
| 80 | + s.Spec.Template.Spec.Containers[0].Image = image |
| 81 | + s.Spec.Template.Spec.Containers[0].LivenessProbe = liveness |
| 82 | + s.Spec.Template.Spec.Containers[0].ReadinessProbe = readiness |
| 83 | + s.Spec.Template.Spec.Containers[0].Env = envVars |
| 84 | + s.Spec.Template.Spec.Containers[0].Ports = containerPorts |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// WithStatefulSetReady marks the StatefulSet as ready. |
| 89 | +func WithStatefulSetReady() StatefulSetOption { |
| 90 | + return func(s *appsv1.StatefulSet) { |
| 91 | + s.Status.ReadyReplicas = 1 |
| 92 | + s.Status.Replicas = 1 |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func WithStatefulSetReplicas(replicas int32) StatefulSetOption { |
| 97 | + return func(s *appsv1.StatefulSet) { |
| 98 | + s.Spec.Replicas = &replicas |
| 99 | + s.Status.Replicas = replicas |
| 100 | + s.Status.ReadyReplicas = replicas |
| 101 | + } |
| 102 | +} |
0 commit comments