@@ -21,67 +21,129 @@ import (
2121
2222 . "github.com/onsi/ginkgo/v2"
2323 . "github.com/onsi/gomega"
24- "k8s.io/apimachinery/pkg/api/errors"
25- "k8s.io/apimachinery/pkg/types"
26- "sigs.k8s.io/controller-runtime/pkg/reconcile"
27-
24+ appsv1 "k8s.io/api/apps/v1"
25+ corev1 "k8s.io/api/core/v1"
2826 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+ "k8s.io/apimachinery/pkg/labels"
28+ "k8s.io/apimachinery/pkg/selection"
29+ "k8s.io/utils/ptr"
30+ ctrl "sigs.k8s.io/controller-runtime"
31+ runtime_client "sigs.k8s.io/controller-runtime/pkg/client"
2932
3033 clickhousecomv1alpha1 "github.com/clickhouse-operator/api/v1alpha1"
3134 "github.com/clickhouse-operator/internal/util"
3235)
3336
3437var _ = Describe ("KeeperCluster Controller" , func () {
35- Context ("When reconciling a resource" , func () {
36- const resourceName = "test-resource"
37-
38+ Context ("When reconciling standalone KeeperCluster resource" , Ordered , func () {
3839 ctx := context .Background ()
39-
40- typeNamespacedName := types.NamespacedName {
41- Name : resourceName ,
42- Namespace : "default" ,
40+ cr := & clickhousecomv1alpha1.KeeperCluster {
41+ ObjectMeta : metav1.ObjectMeta {
42+ Name : "standalone" ,
43+ Namespace : "default" ,
44+ },
45+ Spec : clickhousecomv1alpha1.KeeperClusterSpec {
46+ Replicas : ptr.To [int32 ](1 ),
47+ Labels : map [string ]string {
48+ "test-label" : "test-val" ,
49+ },
50+ Annotations : map [string ]string {
51+ "test-annotation" : "test-val" ,
52+ },
53+ },
4354 }
44- keepercluster := & clickhousecomv1alpha1.KeeperCluster {}
45-
46- BeforeEach (func () {
47- By ("creating the custom resource for the Kind KeeperCluster" )
48- err := k8sClient .Get (ctx , typeNamespacedName , keepercluster )
49- if err != nil && errors .IsNotFound (err ) {
50- resource := & clickhousecomv1alpha1.KeeperCluster {
51- ObjectMeta : metav1.ObjectMeta {
52- Name : resourceName ,
53- Namespace : "default" ,
54- },
55- }
56- Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
57- }
55+
56+ var services corev1.ServiceList
57+ var configs corev1.ConfigMapList
58+ var statefulsets appsv1.StatefulSetList
59+
60+ It ("should create standalone cluster" , func () {
61+ By ("by creating standalone resource CR" )
62+ Expect (k8sClient .Create (ctx , cr )).To (Succeed ())
63+ Expect (k8sClient .Get (ctx , cr .GetNamespacedName (), cr )).To (Succeed ())
5864 })
5965
60- AfterEach (func () {
61- var keepers clickhousecomv1alpha1.KeeperClusterList
62- Expect (k8sClient .List (ctx , & keepers )).To (Succeed ())
66+ It ("should successfully create all resources of the new cluster" , func () {
67+ By ("reconciling the created resource once" )
68+ _ , err := reconciler .Reconcile (ctx , ctrl.Request {NamespacedName : cr .GetNamespacedName ()})
69+ Expect (err ).NotTo (HaveOccurred ())
70+ Expect (k8sClient .Get (ctx , cr .GetNamespacedName (), cr )).To (Succeed ())
6371
64- for _ , keeper := range keepers .Items {
65- Expect (k8sClient .Delete (ctx , & keeper )).To (Succeed ())
72+ appReq , err := labels .NewRequirement (util .LabelAppKey , selection .Equals , []string {cr .SpecificName ()})
73+ Expect (err ).ToNot (HaveOccurred ())
74+ listOpts := & runtime_client.ListOptions {
75+ Namespace : cr .Namespace ,
76+ LabelSelector : labels .NewSelector ().Add (* appReq ),
6677 }
6778
68- By ("Cleanup all keeper clusters" )
79+ Expect (k8sClient .List (ctx , & services , listOpts )).To (Succeed ())
80+ Expect (services .Items ).To (HaveLen (1 ))
81+
82+ Expect (k8sClient .List (ctx , & configs , listOpts )).To (Succeed ())
83+ Expect (configs .Items ).To (HaveLen (2 ))
84+
85+ Expect (k8sClient .List (ctx , & statefulsets , listOpts )).To (Succeed ())
86+ Expect (statefulsets .Items ).To (HaveLen (1 ))
6987 })
7088
71- It ("should successfully reconcile the resource" , func () {
72- By ("Reconciling the created resource" )
73- controllerReconciler := & ClusterReconciler {
74- Client : k8sClient ,
75- Scheme : k8sClient .Scheme (),
89+ It ("should propagate meta attributes for every resource" , func () {
90+ expectedOwnerRef := metav1.OwnerReference {
91+ Kind : "KeeperCluster" ,
92+ APIVersion : "clickhouse.com/v1alpha1" ,
93+ UID : cr .UID ,
94+ Name : cr .Name ,
95+ Controller : ptr .To (true ),
96+ BlockOwnerDeletion : ptr .To (true ),
97+ }
7698
77- Reader : k8sClient ,
78- Logger : util .NewZapLogger (logger .Named ("keeper" )),
99+ By ("setting meta attributes for service" )
100+ for _ , service := range services .Items {
101+ Expect (service .ObjectMeta .OwnerReferences ).To (ContainElement (expectedOwnerRef ))
102+ for k , v := range cr .Spec .Labels {
103+ Expect (service .ObjectMeta .Labels ).To (HaveKeyWithValue (k , v ))
104+ }
105+ for k , v := range cr .Spec .Annotations {
106+ Expect (service .ObjectMeta .Annotations ).To (HaveKeyWithValue (k , v ))
107+ }
108+ }
109+
110+ By ("setting meta attributes for configs" )
111+ for _ , config := range configs .Items {
112+ Expect (config .ObjectMeta .OwnerReferences ).To (ContainElement (expectedOwnerRef ))
113+ for k , v := range cr .Spec .Labels {
114+ Expect (config .ObjectMeta .Labels ).To (HaveKeyWithValue (k , v ))
115+ }
116+ for k , v := range cr .Spec .Annotations {
117+ Expect (config .ObjectMeta .Annotations ).To (HaveKeyWithValue (k , v ))
118+ }
79119 }
80120
81- _ , err := controllerReconciler .Reconcile (ctx , reconcile.Request {
82- NamespacedName : typeNamespacedName ,
83- })
121+ By ("setting meta attributes for statefulsets" )
122+ for _ , sts := range statefulsets .Items {
123+ Expect (sts .ObjectMeta .OwnerReferences ).To (ContainElement (expectedOwnerRef ))
124+ for k , v := range cr .Spec .Labels {
125+ Expect (sts .ObjectMeta .Labels ).To (HaveKeyWithValue (k , v ))
126+ Expect (sts .Spec .Template .ObjectMeta .Labels ).To (HaveKeyWithValue (k , v ))
127+ }
128+ for k , v := range cr .Spec .Annotations {
129+ Expect (sts .ObjectMeta .Annotations ).To (HaveKeyWithValue (k , v ))
130+ Expect (sts .Spec .Template .ObjectMeta .Annotations ).To (HaveKeyWithValue (k , v ))
131+ }
132+ }
133+ })
134+
135+ It ("should reflect configuration changes in revisions" , func () {
136+ updatedCR := cr .DeepCopy ()
137+ updatedCR .Spec .LoggerConfig .LoggerLevel = "warning"
138+ Expect (k8sClient .Update (ctx , updatedCR )).To (Succeed ())
139+ _ , err := reconciler .Reconcile (ctx , ctrl.Request {NamespacedName : cr .GetNamespacedName ()})
84140 Expect (err ).NotTo (HaveOccurred ())
141+ Expect (k8sClient .Get (ctx , cr .GetNamespacedName (), updatedCR )).To (Succeed ())
142+
143+ Expect (updatedCR .Status .ObservedGeneration ).To (Equal (updatedCR .Generation ))
144+ Expect (updatedCR .Status .UpdateRevision ).NotTo (Equal (updatedCR .Status .CurrentRevision ))
145+ Expect (updatedCR .Status .ConfigurationRevision ).NotTo (Equal (cr .Status .ConfigurationRevision ))
146+ Expect (updatedCR .Status .StatefulSetRevision ).To (Equal (cr .Status .StatefulSetRevision ))
85147 })
86148 })
87149})
0 commit comments