...
1 package envctl
2
3 import (
4 "context"
5 "fmt"
6 "slices"
7 "strings"
8
9 "maps"
10
11 corev1 "k8s.io/api/core/v1"
12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13 "k8s.io/apimachinery/pkg/types"
14 "sigs.k8s.io/controller-runtime/pkg/client"
15 )
16
17 func (s *Suite) TestConfigMapReplication() {
18 s.NoError(confiMapsExists(s.ctx, s.Client))
19
20 keys := maps.Keys(configMapMapping)
21 configMapKeys := strings.Join(slices.Collect(keys), ",")
22 ns := &corev1.Namespace{
23 TypeMeta: metav1.TypeMeta{
24 APIVersion: "v1",
25 Kind: "Namespace",
26 },
27 ObjectMeta: metav1.ObjectMeta{
28 Name: fmt.Sprintf("cm-%s", s.UniqueName),
29 Annotations: map[string]string{
30 CMNamespaceAnnotation: configMapKeys,
31 },
32 },
33 }
34 s.NoError(s.Client.Create(s.ctx, ns))
35 for nn := range maps.Values(configMapMapping) {
36 var err error
37 s.Eventually(func() bool {
38 err = s.assertConfigMapEqual(nn, types.NamespacedName{Namespace: ns.Name, Name: nn.Name})
39 return err == nil
40 }, s.timeout, s.tick, "configmaps not equal: %v", err)
41 }
42 }
43
44 func (s *Suite) assertConfigMapEqual(expected, actual types.NamespacedName) error {
45 expectedCM := &corev1.ConfigMap{}
46 actualCM := &corev1.ConfigMap{}
47
48 err := s.Client.Get(s.ctx, expected, expectedCM)
49 if err != nil {
50 return err
51 }
52
53 err = s.Client.Get(s.ctx, actual, actualCM)
54 if err != nil {
55 return err
56 }
57
58 if !s.Equal(expectedCM.Data, actualCM.Data) {
59 return fmt.Errorf("replicated configmap does not equal original configmap: %v", actual)
60 }
61
62 if actualCM.Annotations[CMReplicatedAnnotation] == "" {
63 return fmt.Errorf("replicated configmap does have generated annotation: %v", actual)
64 }
65
66 return nil
67 }
68
69 func confiMapsExists(ctx context.Context, cl client.Client) error {
70 for _, nn := range configMapMapping {
71 cm := &corev1.ConfigMap{
72 TypeMeta: metav1.TypeMeta{
73 APIVersion: "v1",
74 Kind: "ConfigMap",
75 },
76 ObjectMeta: metav1.ObjectMeta{
77 Name: nn.Name,
78 Namespace: nn.Namespace,
79 },
80 Data: map[string]string{
81 "foo": "bar",
82 },
83 }
84 err := client.IgnoreAlreadyExists(cl.Create(ctx, cm))
85 if err != nil {
86 return err
87 }
88 }
89 return nil
90 }
91
View as plain text