@@ -17,8 +17,6 @@ limitations under the License.
1717package controllers_test
1818
1919import (
20- "time"
21-
2220 . "github.com/onsi/ginkgo/v2"
2321 . "github.com/onsi/gomega"
2422 helmrelease "helm.sh/helm/v3/pkg/release"
@@ -32,51 +30,62 @@ import (
3230 "sigs.k8s.io/controller-runtime/pkg/client"
3331)
3432
35- var (
36- testNamespace = "test-namespace"
37- newVersion = "new-version"
33+ const (
34+ testNamespace1 = "test-namespace1"
35+ testNamespace2 = "test-namespace2"
36+ newVersion = "new-version"
37+ releaseFailedMessage = "unable to remove helm release"
38+ )
3839
39- defaultProxy = & addonsv1alpha1.HelmChartProxy {
40- TypeMeta : metav1.TypeMeta {
41- APIVersion : addonsv1alpha1 .GroupVersion .String (),
42- Kind : "HelmChartProxy" ,
43- },
44- ObjectMeta : metav1.ObjectMeta {
45- Name : "test-hcp" ,
46- Namespace : testNamespace ,
47- },
48- Spec : addonsv1alpha1.HelmChartProxySpec {
49- ClusterSelector : metav1.LabelSelector {
50- MatchLabels : map [string ]string {
51- "test-label" : "test-value" ,
40+ var (
41+ namespaces = []string {testNamespace1 , testNamespace2 }
42+ failedHelmUninstall bool
43+
44+ newProxy = func (namespace string ) * addonsv1alpha1.HelmChartProxy {
45+ return & addonsv1alpha1.HelmChartProxy {
46+ TypeMeta : metav1.TypeMeta {
47+ APIVersion : addonsv1alpha1 .GroupVersion .String (),
48+ Kind : "HelmChartProxy" ,
49+ },
50+ ObjectMeta : metav1.ObjectMeta {
51+ Name : "test-hcp" ,
52+ Namespace : namespace ,
53+ },
54+ Spec : addonsv1alpha1.HelmChartProxySpec {
55+ ClusterSelector : metav1.LabelSelector {
56+ MatchLabels : map [string ]string {
57+ "test-label" : "test-value" ,
58+ },
5259 },
60+ ReleaseName : "test-release-name" ,
61+ ChartName : "test-chart-name" ,
62+ RepoURL : "https://test-repo-url" ,
63+ ReleaseNamespace : "test-release-namespace" ,
64+ Version : "test-version" ,
65+ ValuesTemplate : "apiServerPort: {{ .Cluster.spec.clusterNetwork.apiServerPort }}" ,
5366 },
54- ReleaseName : "test-release-name" ,
55- ChartName : "test-chart-name" ,
56- RepoURL : "https://test-repo-url" ,
57- ReleaseNamespace : "test-release-namespace" ,
58- Version : "test-version" ,
59- ValuesTemplate : "apiServerPort: {{ .Cluster.spec.clusterNetwork.apiServerPort }}" ,
60- },
67+ }
6168 }
6269
63- cluster1 = & clusterv1.Cluster {
64- TypeMeta : metav1.TypeMeta {
65- APIVersion : clusterv1 .GroupVersion .String (),
66- Kind : "Cluster" ,
67- },
68- ObjectMeta : metav1.ObjectMeta {
69- Name : "test-cluster-1" ,
70- Namespace : testNamespace ,
71- Labels : map [string ]string {
72- "test-label" : "test-value" ,
70+ newCluster = func (namespace string ) * clusterv1.Cluster {
71+ return & clusterv1.Cluster {
72+ TypeMeta : metav1.TypeMeta {
73+ APIVersion : clusterv1 .GroupVersion .String (),
74+ Kind : "Cluster" ,
7375 },
74- },
75- Spec : clusterv1.ClusterSpec {
76- ClusterNetwork : & clusterv1.ClusterNetwork {
77- APIServerPort : ptr .To (int32 (1234 )),
76+ ObjectMeta : metav1.ObjectMeta {
77+ Name : "test-cluster-1" ,
78+ Namespace : namespace ,
79+ Labels : map [string ]string {
80+ "test-label" : "test-value" ,
81+ },
7882 },
79- },
83+ Spec : clusterv1.ClusterSpec {
84+ ClusterNetwork : & clusterv1.ClusterNetwork {
85+ APIServerPort : ptr .To (int32 (1234 )),
86+ },
87+ },
88+ }
8089 }
8190
8291 helmReleaseDeployed = & helmrelease.Release {
@@ -135,62 +144,91 @@ var _ = Describe("Testing HelmChartProxy and HelmReleaseProxy reconcile", func()
135144 return condition != nil && condition (hrpList .Items )
136145 }, timeout , interval ).Should (BeTrue ())
137146 }
138- )
139147
140- It ("HelmChartProxy and HelmReleaseProxy lifecycle test" , func () {
141- cluster := cluster1 .DeepCopy ()
142- err := k8sClient .Create (ctx , cluster )
143- Expect (err ).ToNot (HaveOccurred ())
144- err = k8sClient .Create (ctx , newKubeconfigSecretForCluster (cluster ))
145- Expect (err ).ToNot (HaveOccurred ())
148+ install = func (cluster * clusterv1.Cluster , proxy * addonsv1alpha1.HelmChartProxy ) {
149+ err := k8sClient .Create (ctx , cluster )
150+ Expect (err ).ToNot (HaveOccurred ())
151+ err = k8sClient .Create (ctx , newKubeconfigSecretForCluster (cluster ))
152+ Expect (err ).ToNot (HaveOccurred ())
146153
147- patch := client .MergeFrom (cluster .DeepCopy ())
148- cluster .Status .Conditions = clusterv1.Conditions {
149- {
150- Type : clusterv1 .ControlPlaneInitializedCondition ,
151- Status : corev1 .ConditionTrue ,
152- LastTransitionTime : metav1 .NewTime (time .Now ()),
153- },
154+ patch := client .MergeFrom (cluster .DeepCopy ())
155+ conditions .MarkTrue (cluster , clusterv1 .ControlPlaneInitializedCondition )
156+ err = k8sClient .Status ().Patch (ctx , cluster , patch )
157+ Expect (err ).ToNot (HaveOccurred ())
158+
159+ err = k8sClient .Create (ctx , proxy )
160+ Expect (err ).ToNot (HaveOccurred ())
161+
162+ waitForHelmChartProxyCondition (client .ObjectKeyFromObject (proxy ), func (helmChartProxy * addonsv1alpha1.HelmChartProxy ) bool {
163+ return conditions .IsTrue (helmChartProxy , clusterv1 .ReadyCondition )
164+ })
165+
166+ waitForHelmReleaseProxyCondition (client .ObjectKeyFromObject (proxy ), func (helmReleaseProxyList []addonsv1alpha1.HelmReleaseProxy ) bool {
167+ return len (helmReleaseProxyList ) == 1 && conditions .IsTrue (& helmReleaseProxyList [0 ], clusterv1 .ReadyCondition )
168+ })
154169 }
155- err = k8sClient .Status ().Patch (ctx , cluster , patch )
156- Expect (err ).ToNot (HaveOccurred ())
157170
158- err = k8sClient .Create (ctx , defaultProxy )
159- Expect (err ).ToNot (HaveOccurred ())
171+ deleteAndWaitHelmChartProxy = func (proxy * addonsv1alpha1.HelmChartProxy ) {
172+ err := k8sClient .Delete (ctx , proxy )
173+ Expect (err ).ToNot (HaveOccurred ())
160174
161- waitForHelmChartProxyCondition (client .ObjectKeyFromObject (defaultProxy ), func (helmChartProxy * addonsv1alpha1.HelmChartProxy ) bool {
162- return conditions .IsTrue (helmChartProxy , clusterv1 .ReadyCondition )
163- })
175+ Eventually (func () bool {
176+ if err := k8sClient .Get (ctx , client .ObjectKeyFromObject (proxy ), & addonsv1alpha1.HelmChartProxy {}); client .IgnoreNotFound (err ) != nil {
177+ return false
178+ }
164179
165- waitForHelmReleaseProxyCondition (client .ObjectKeyFromObject (defaultProxy ), func (helmReleaseProxyList []addonsv1alpha1.HelmReleaseProxy ) bool {
166- return len (helmReleaseProxyList ) == 1 && conditions .IsTrue (& helmReleaseProxyList [0 ], clusterv1 .ReadyCondition )
167- })
180+ return true
181+ }, timeout , interval ).Should (BeTrue ())
182+
183+ waitForHelmReleaseProxyCondition (client .ObjectKeyFromObject (proxy ), func (helmReleaseProxyList []addonsv1alpha1.HelmReleaseProxy ) bool {
184+ return len (helmReleaseProxyList ) == 0
185+ })
186+ }
187+ )
188+
189+ It ("HelmChartProxy and HelmReleaseProxy lifecycle happy path test" , func () {
190+ cluster := newCluster (testNamespace1 )
191+ helmChartProxy := newProxy (testNamespace1 )
192+ install (cluster , helmChartProxy )
168193
169- hcp := & addonsv1alpha1.HelmChartProxy {}
170- err = k8sClient .Get (ctx , client .ObjectKeyFromObject (defaultProxy ), hcp )
194+ err := k8sClient .Get (ctx , client .ObjectKeyFromObject (helmChartProxy ), helmChartProxy )
171195 Expect (err ).ToNot (HaveOccurred ())
172- patch = client .MergeFrom (hcp .DeepCopy ())
173- hcp .Spec .Version = newVersion
174- err = k8sClient .Patch (ctx , hcp , patch )
196+ patch : = client .MergeFrom (helmChartProxy .DeepCopy ())
197+ helmChartProxy .Spec .Version = newVersion
198+ err = k8sClient .Patch (ctx , helmChartProxy , patch )
175199 Expect (err ).ToNot (HaveOccurred ())
176200
177- waitForHelmReleaseProxyCondition (client .ObjectKeyFromObject (defaultProxy ), func (helmReleaseProxyList []addonsv1alpha1.HelmReleaseProxy ) bool {
201+ waitForHelmReleaseProxyCondition (client .ObjectKeyFromObject (helmChartProxy ), func (helmReleaseProxyList []addonsv1alpha1.HelmReleaseProxy ) bool {
178202 return len (helmReleaseProxyList ) == 1 && conditions .IsTrue (& helmReleaseProxyList [0 ], clusterv1 .ReadyCondition ) && helmReleaseProxyList [0 ].Spec .Version == "new-version"
179203 })
180204
181- err = k8sClient .Delete (ctx , hcp )
205+ deleteAndWaitHelmChartProxy (helmChartProxy )
206+ })
207+
208+ It ("HelmChartProxy and HelmReleaseProxy test with failed Release uninstall" , func () {
209+ cluster := newCluster (testNamespace2 )
210+ helmChartProxy := newProxy (testNamespace2 )
211+ failedHelmUninstall = true
212+ install (cluster , helmChartProxy )
213+
214+ err := k8sClient .Delete (ctx , helmChartProxy )
182215 Expect (err ).ToNot (HaveOccurred ())
183216
184- Eventually (func () bool {
185- if err := k8sClient .Get (ctx , client .ObjectKeyFromObject (hcp ), hcp ); client . IgnoreNotFound ( err ) != nil {
217+ Consistently (func () bool {
218+ if err := k8sClient .Get (ctx , client .ObjectKeyFromObject (helmChartProxy ), helmChartProxy ); err != nil {
186219 return false
187220 }
188221
189222 return true
190223 }, timeout , interval ).Should (BeTrue ())
191224
192- waitForHelmReleaseProxyCondition (client .ObjectKeyFromObject (defaultProxy ), func (helmReleaseProxyList []addonsv1alpha1.HelmReleaseProxy ) bool {
193- return len (helmReleaseProxyList ) == 0
194- })
225+ readyCondition := conditions .Get (helmChartProxy , clusterv1 .ReadyCondition )
226+ Expect (readyCondition ).NotTo (BeNil ())
227+ Expect (readyCondition .Status ).To (Equal (corev1 .ConditionFalse ))
228+ Expect (readyCondition .Message ).To (Equal (releaseFailedMessage ))
229+
230+ By ("Making HelmChartProxy uninstallable" )
231+ failedHelmUninstall = false
232+ deleteAndWaitHelmChartProxy (helmChartProxy )
195233 })
196234})
0 commit comments