|
| 1 | +package manifests |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + lokiv1 "github.com/grafana/loki/operator/api/loki/v1" |
| 7 | + "github.com/grafana/loki/operator/internal/manifests/storage" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + appsv1 "k8s.io/api/apps/v1" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + policyv1 "k8s.io/api/policy/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | +) |
| 14 | + |
| 15 | +func TestNewPatternIngesterDeployment_HasTemplateConfigHashAnnotation(t *testing.T) { |
| 16 | + d := NewPatternIngesterDeployment(Options{ |
| 17 | + Name: "abcd", |
| 18 | + Namespace: "efgh", |
| 19 | + ConfigSHA1: "deadbeef", |
| 20 | + Stack: lokiv1.LokiStackSpec{ |
| 21 | + StorageClassName: "standard", |
| 22 | + Template: &lokiv1.LokiTemplateSpec{ |
| 23 | + PatternIngester: &lokiv1.LokiComponentSpec{ |
| 24 | + Replicas: 1, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }) |
| 29 | + |
| 30 | + annotations := d.Spec.Template.Annotations |
| 31 | + require.Contains(t, annotations, AnnotationLokiConfigHash) |
| 32 | + require.Equal(t, annotations[AnnotationLokiConfigHash], "deadbeef") |
| 33 | +} |
| 34 | + |
| 35 | +func TestNewPatternIngesterDeployment_HasTemplateObjectStoreHashAnnotation(t *testing.T) { |
| 36 | + d := NewPatternIngesterDeployment(Options{ |
| 37 | + Name: "abcd", |
| 38 | + Namespace: "efgh", |
| 39 | + ObjectStorage: storage.Options{ |
| 40 | + SecretSHA1: "deadbeef", |
| 41 | + }, |
| 42 | + Stack: lokiv1.LokiStackSpec{ |
| 43 | + StorageClassName: "standard", |
| 44 | + Template: &lokiv1.LokiTemplateSpec{ |
| 45 | + PatternIngester: &lokiv1.LokiComponentSpec{ |
| 46 | + Replicas: 1, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }) |
| 51 | + |
| 52 | + annotations := d.Spec.Template.Annotations |
| 53 | + require.Contains(t, annotations, AnnotationLokiObjectStoreHash) |
| 54 | + require.Equal(t, annotations[AnnotationLokiObjectStoreHash], "deadbeef") |
| 55 | +} |
| 56 | + |
| 57 | +func TestNewPatternIngesterDeployment_HasTemplateCertRotationRequiredAtAnnotation(t *testing.T) { |
| 58 | + ss := NewPatternIngesterDeployment(Options{ |
| 59 | + Name: "abcd", |
| 60 | + Namespace: "efgh", |
| 61 | + CertRotationRequiredAt: "deadbeef", |
| 62 | + Stack: lokiv1.LokiStackSpec{ |
| 63 | + Template: &lokiv1.LokiTemplateSpec{ |
| 64 | + PatternIngester: &lokiv1.LokiComponentSpec{ |
| 65 | + Replicas: 1, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }) |
| 70 | + |
| 71 | + annotations := ss.Spec.Template.Annotations |
| 72 | + require.Contains(t, annotations, AnnotationCertRotationRequiredAt) |
| 73 | + require.Equal(t, annotations[AnnotationCertRotationRequiredAt], "deadbeef") |
| 74 | +} |
| 75 | + |
| 76 | +func TestNewPatternIngesterDeployment_SelectorMatchesLabels(t *testing.T) { |
| 77 | + // You must set the .spec.selector field of a Deployment to match the labels of |
| 78 | + // its .spec.template.metadata.labels. Prior to Kubernetes 1.8, the |
| 79 | + // .spec.selector field was defaulted when omitted. In 1.8 and later versions, |
| 80 | + // failing to specify a matching Pod Selector will result in a validation error |
| 81 | + // during Deployment creation. |
| 82 | + // See https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-selector |
| 83 | + ss := NewPatternIngesterDeployment(Options{ |
| 84 | + Name: "abcd", |
| 85 | + Namespace: "efgh", |
| 86 | + Stack: lokiv1.LokiStackSpec{ |
| 87 | + StorageClassName: "standard", |
| 88 | + Template: &lokiv1.LokiTemplateSpec{ |
| 89 | + PatternIngester: &lokiv1.LokiComponentSpec{ |
| 90 | + Replicas: 1, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }) |
| 95 | + |
| 96 | + l := ss.Spec.Template.GetObjectMeta().GetLabels() |
| 97 | + for key, value := range ss.Spec.Selector.MatchLabels { |
| 98 | + require.Contains(t, l, key) |
| 99 | + require.Equal(t, l[key], value) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestBuildPatternIngester_PodDisruptionBudget(t *testing.T) { |
| 104 | + opts := Options{ |
| 105 | + Name: "abcd", |
| 106 | + Namespace: "efgh", |
| 107 | + Stack: lokiv1.LokiStackSpec{ |
| 108 | + Template: &lokiv1.LokiTemplateSpec{ |
| 109 | + PatternIngester: &lokiv1.LokiComponentSpec{ |
| 110 | + Replicas: 1, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + objs, err := BuildPatternIngester(opts) |
| 117 | + require.NoError(t, err) |
| 118 | + require.Len(t, objs, 4) |
| 119 | + |
| 120 | + pdb := objs[3].(*policyv1.PodDisruptionBudget) |
| 121 | + require.Equal(t, "abcd-pattern-ingester", pdb.Name) |
| 122 | + require.Equal(t, "efgh", pdb.Namespace) |
| 123 | + require.NotNil(t, pdb.Spec.MinAvailable.IntVal) |
| 124 | + require.Equal(t, int32(1), pdb.Spec.MinAvailable.IntVal) |
| 125 | + require.EqualValues(t, ComponentLabels(LabelPatternIngesterComponent, opts.Name), pdb.Spec.Selector.MatchLabels) |
| 126 | +} |
| 127 | + |
| 128 | +func TestNewPatternIngesterDeployment_TopologySpreadConstraints(t *testing.T) { |
| 129 | + obj, _ := BuildPatternIngester(Options{ |
| 130 | + Name: "abcd", |
| 131 | + Namespace: "efgh", |
| 132 | + Stack: lokiv1.LokiStackSpec{ |
| 133 | + Template: &lokiv1.LokiTemplateSpec{ |
| 134 | + PatternIngester: &lokiv1.LokiComponentSpec{ |
| 135 | + Replicas: 1, |
| 136 | + }, |
| 137 | + }, |
| 138 | + Replication: &lokiv1.ReplicationSpec{ |
| 139 | + Zones: []lokiv1.ZoneSpec{ |
| 140 | + { |
| 141 | + TopologyKey: "zone", |
| 142 | + MaxSkew: 2, |
| 143 | + }, |
| 144 | + { |
| 145 | + TopologyKey: "region", |
| 146 | + MaxSkew: 1, |
| 147 | + }, |
| 148 | + }, |
| 149 | + Factor: 1, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }) |
| 153 | + |
| 154 | + d := obj[0].(*appsv1.Deployment) |
| 155 | + require.Equal(t, []corev1.TopologySpreadConstraint{ |
| 156 | + { |
| 157 | + MaxSkew: 2, |
| 158 | + TopologyKey: "zone", |
| 159 | + WhenUnsatisfiable: "DoNotSchedule", |
| 160 | + LabelSelector: &metav1.LabelSelector{ |
| 161 | + MatchLabels: map[string]string{ |
| 162 | + "app.kubernetes.io/component": "pattern-ingester", |
| 163 | + "app.kubernetes.io/instance": "abcd", |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + { |
| 168 | + MaxSkew: 1, |
| 169 | + TopologyKey: "region", |
| 170 | + WhenUnsatisfiable: "DoNotSchedule", |
| 171 | + LabelSelector: &metav1.LabelSelector{ |
| 172 | + MatchLabels: map[string]string{ |
| 173 | + "app.kubernetes.io/component": "pattern-ingester", |
| 174 | + "app.kubernetes.io/instance": "abcd", |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, d.Spec.Template.Spec.TopologySpreadConstraints) |
| 179 | +} |
0 commit comments