Skip to content

Commit 389eec8

Browse files
Merge pull request #124 from rajathagasthya/apply-crds
Refactor ProcessCRDs API to take in variadic arguments
2 parents 45a3526 + b865d9b commit 389eec8

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

examples/apply-crds/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ func main() {
4848

4949
switch operation {
5050
case "apply":
51-
if err := crdutil.ProcessCRDs(ctx, []string{crdsPath}, crdutil.CRDOperationApply); err != nil {
51+
if err := crdutil.ProcessCRDs(ctx, crdutil.CRDOperationApply, crdsPath); err != nil {
5252
log.Fatalf("Failed to apply CRDs: %v", err)
5353
}
5454
case "delete":
55-
if err := crdutil.ProcessCRDs(ctx, []string{crdsPath}, crdutil.CRDOperationDelete); err != nil {
55+
if err := crdutil.ProcessCRDs(ctx, crdutil.CRDOperationDelete, crdsPath); err != nil {
5656
log.Fatalf("Failed to delete CRDs: %v", err)
5757
}
5858
default:

pkg/crdutil/crdutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const (
5353
// ProcessCRDs processes CRDs from the given paths based on the operation type.
5454
// It accepts both directories (walked recursively) and individual YAML files.
5555
// For each CRD found, it performs the specified operation (apply or delete).
56-
func ProcessCRDs(ctx context.Context, crdPaths []string, operation CRDOperation) error {
56+
func ProcessCRDs(ctx context.Context, operation CRDOperation, crdPaths ...string) error {
5757
if len(crdPaths) == 0 {
5858
return errors.New("at least one CRD path (file or directory) is required")
5959
}
@@ -63,13 +63,13 @@ func ProcessCRDs(ctx context.Context, crdPaths []string, operation CRDOperation)
6363
return fmt.Errorf("failed to get Kubernetes config: %w", err)
6464
}
6565

66-
return ProcessCRDsWithConfig(ctx, config, crdPaths, operation)
66+
return ProcessCRDsWithConfig(ctx, config, operation, crdPaths...)
6767
}
6868

6969
// ProcessCRDsWithConfig processes CRDs using a provided Kubernetes REST config.
7070
// It accepts both directories (walked recursively) and individual YAML files,
7171
// parses them, and either applies or deletes them from the cluster.
72-
func ProcessCRDsWithConfig(ctx context.Context, config *rest.Config, crdPaths []string, operation CRDOperation) error {
72+
func ProcessCRDsWithConfig(ctx context.Context, config *rest.Config, operation CRDOperation, crdPaths ...string) error {
7373
client, err := clientset.NewForConfig(config)
7474
if err != nil {
7575
return fmt.Errorf("failed to create API extensions client: %w", err)

pkg/crdutil/crdutil_test.go

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var _ = Describe("CRDUtil", func() {
6060
Describe("ProcessCRDsWithConfig", func() {
6161
It("should create CRDs from a directory", func() {
6262
// Apply CRDs from the crds directory
63-
err := ProcessCRDsWithConfig(ctx, cfg, []string{crdsDir}, CRDOperationApply)
63+
err := ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, crdsDir)
6464
Expect(err).NotTo(HaveOccurred())
6565

6666
// Verify CRDs were created
@@ -77,7 +77,7 @@ var _ = Describe("CRDUtil", func() {
7777

7878
It("should update existing CRDs when applying updated versions", func() {
7979
// First, create the initial CRDs
80-
err := ProcessCRDsWithConfig(ctx, cfg, []string{crdsDir}, CRDOperationApply)
80+
err := ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, crdsDir)
8181
Expect(err).NotTo(HaveOccurred())
8282

8383
// Get the initial resource versions
@@ -90,7 +90,7 @@ var _ = Describe("CRDUtil", func() {
9090
initialBarResourceVersion := initialBar.ResourceVersion
9191

9292
// Now apply the updated CRDs
93-
err = ProcessCRDsWithConfig(ctx, cfg, []string{updatedCRDsDir}, CRDOperationApply)
93+
err = ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, updatedCRDsDir)
9494
Expect(err).NotTo(HaveOccurred())
9595

9696
// Verify CRDs were updated by checking resource version changed
@@ -105,11 +105,11 @@ var _ = Describe("CRDUtil", func() {
105105

106106
It("should delete CRDs from a directory", func() {
107107
// Apply CRDs
108-
err := ProcessCRDsWithConfig(ctx, cfg, []string{crdsDir}, CRDOperationApply)
108+
err := ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, crdsDir)
109109
Expect(err).NotTo(HaveOccurred())
110110

111111
// Delete CRDs
112-
err = ProcessCRDsWithConfig(ctx, cfg, []string{crdsDir}, CRDOperationDelete)
112+
err = ProcessCRDsWithConfig(ctx, cfg, CRDOperationDelete, crdsDir)
113113
Expect(err).NotTo(HaveOccurred())
114114

115115
// Verify CRDs were deleted
@@ -126,21 +126,21 @@ var _ = Describe("CRDUtil", func() {
126126

127127
It("should handle apply and delete operations idempotently", func() {
128128
// Apply twice
129-
err := ProcessCRDsWithConfig(ctx, cfg, []string{crdsDir}, CRDOperationApply)
129+
err := ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, crdsDir)
130130
Expect(err).NotTo(HaveOccurred())
131131

132-
err = ProcessCRDsWithConfig(ctx, cfg, []string{crdsDir}, CRDOperationApply)
132+
err = ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, crdsDir)
133133
Expect(err).NotTo(HaveOccurred())
134134

135135
// Verify CRDs exist
136136
_, err = testCRDClient.Get(ctx, "foos.example.com", metav1.GetOptions{})
137137
Expect(err).NotTo(HaveOccurred())
138138

139139
// Delete twice
140-
err = ProcessCRDsWithConfig(ctx, cfg, []string{crdsDir}, CRDOperationDelete)
140+
err = ProcessCRDsWithConfig(ctx, cfg, CRDOperationDelete, crdsDir)
141141
Expect(err).NotTo(HaveOccurred())
142142

143-
err = ProcessCRDsWithConfig(ctx, cfg, []string{crdsDir}, CRDOperationDelete)
143+
err = ProcessCRDsWithConfig(ctx, cfg, CRDOperationDelete, crdsDir)
144144
Expect(err).NotTo(HaveOccurred())
145145

146146
// Verify CRDs are deleted
@@ -151,7 +151,7 @@ var _ = Describe("CRDUtil", func() {
151151
})
152152

153153
It("should recursively walk directories and apply CRDs from nested subdirectories", func() {
154-
err := ProcessCRDsWithConfig(ctx, cfg, []string{nestedDir}, CRDOperationApply)
154+
err := ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, nestedDir)
155155
Expect(err).NotTo(HaveOccurred())
156156

157157
// Verify CRDs were created
@@ -166,7 +166,7 @@ var _ = Describe("CRDUtil", func() {
166166
Expect(crd2.ResourceVersion).NotTo(BeEmpty())
167167

168168
// Clean up
169-
err = ProcessCRDsWithConfig(ctx, cfg, []string{nestedDir}, CRDOperationDelete)
169+
err = ProcessCRDsWithConfig(ctx, cfg, CRDOperationDelete, nestedDir)
170170
Expect(err).NotTo(HaveOccurred())
171171

172172
// Verify deletion
@@ -184,7 +184,7 @@ var _ = Describe("CRDUtil", func() {
184184
It("should accept individual file paths and apply CRDs", func() {
185185
// Apply a single CRD file
186186
singleFilePath := filepath.Join(crdsDir, "test-crds.yaml")
187-
err := ProcessCRDsWithConfig(ctx, cfg, []string{singleFilePath}, CRDOperationApply)
187+
err := ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, singleFilePath)
188188
Expect(err).NotTo(HaveOccurred())
189189

190190
// Verify CRDs were created
@@ -197,7 +197,7 @@ var _ = Describe("CRDUtil", func() {
197197
Expect(crd2.Name).To(Equal("bars.example.com"))
198198

199199
// Clean up using the same file path
200-
err = ProcessCRDsWithConfig(ctx, cfg, []string{singleFilePath}, CRDOperationDelete)
200+
err = ProcessCRDsWithConfig(ctx, cfg, CRDOperationDelete, singleFilePath)
201201
Expect(err).NotTo(HaveOccurred())
202202

203203
// Verify deletion
@@ -211,5 +211,54 @@ var _ = Describe("CRDUtil", func() {
211211
return apierrors.IsNotFound(err)
212212
}, eventuallyTimeout).Should(BeTrue())
213213
})
214+
215+
It("should accept multiple directories as variadic arguments", func() {
216+
// Apply CRDs from multiple directories at once
217+
err := ProcessCRDsWithConfig(ctx, cfg, CRDOperationApply, crdsDir, nestedDir)
218+
Expect(err).NotTo(HaveOccurred())
219+
220+
// Verify CRDs from first directory were created
221+
crd1, err := testCRDClient.Get(ctx, "foos.example.com", metav1.GetOptions{})
222+
Expect(err).NotTo(HaveOccurred())
223+
Expect(crd1.Name).To(Equal("foos.example.com"))
224+
225+
crd2, err := testCRDClient.Get(ctx, "bars.example.com", metav1.GetOptions{})
226+
Expect(err).NotTo(HaveOccurred())
227+
Expect(crd2.Name).To(Equal("bars.example.com"))
228+
229+
// Verify CRDs from nested directory were created
230+
crd3, err := testCRDClient.Get(ctx, "nestedfoos.example.com", metav1.GetOptions{})
231+
Expect(err).NotTo(HaveOccurred())
232+
Expect(crd3.Name).To(Equal("nestedfoos.example.com"))
233+
234+
crd4, err := testCRDClient.Get(ctx, "nestedbars.example.com", metav1.GetOptions{})
235+
Expect(err).NotTo(HaveOccurred())
236+
Expect(crd4.Name).To(Equal("nestedbars.example.com"))
237+
238+
// Clean up using the same multiple directories
239+
err = ProcessCRDsWithConfig(ctx, cfg, CRDOperationDelete, crdsDir, nestedDir)
240+
Expect(err).NotTo(HaveOccurred())
241+
242+
// Verify all CRDs were deleted
243+
Eventually(func() bool {
244+
_, err := testCRDClient.Get(ctx, "foos.example.com", metav1.GetOptions{})
245+
return apierrors.IsNotFound(err)
246+
}, eventuallyTimeout).Should(BeTrue())
247+
248+
Eventually(func() bool {
249+
_, err := testCRDClient.Get(ctx, "bars.example.com", metav1.GetOptions{})
250+
return apierrors.IsNotFound(err)
251+
}, eventuallyTimeout).Should(BeTrue())
252+
253+
Eventually(func() bool {
254+
_, err := testCRDClient.Get(ctx, "nestedfoos.example.com", metav1.GetOptions{})
255+
return apierrors.IsNotFound(err)
256+
}, eventuallyTimeout).Should(BeTrue())
257+
258+
Eventually(func() bool {
259+
_, err := testCRDClient.Get(ctx, "nestedbars.example.com", metav1.GetOptions{})
260+
return apierrors.IsNotFound(err)
261+
}, eventuallyTimeout).Should(BeTrue())
262+
})
214263
})
215264
})

0 commit comments

Comments
 (0)