Skip to content

Commit 56fdeb6

Browse files
committed
crdutil: Add feature to also apply also single files
Signed-off-by: Tobias Giese <[email protected]>
1 parent 1ad8938 commit 56fdeb6

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

pkg/crdutil/crdutil.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,29 @@ func (s *StringList) Set(value string) error {
5050

5151
var (
5252
crdsDir StringList
53+
crds StringList
5354
)
5455

5556
func initFlags() {
5657
flag.Var(&crdsDir, "crds-dir", "Path to the directory containing the CRD manifests")
58+
flag.Var(&crds, "crds-file", "Single CRDs file with CRD manifests to apply")
5759
flag.Parse()
5860

59-
if len(crdsDir) == 0 {
60-
log.Fatalf("CRDs directory is required")
61+
if len(crdsDir) == 0 && len(crds) == 0 {
62+
log.Fatalf("CRDs directory or single CRDs are required")
6163
}
6264

6365
for _, crdDir := range crdsDir {
6466
if _, err := os.Stat(crdDir); os.IsNotExist(err) {
6567
log.Fatalf("CRDs directory %s does not exist", crdsDir)
6668
}
6769
}
70+
71+
for _, crd := range crds {
72+
if _, err := os.Stat(crd); os.IsNotExist(err) {
73+
log.Fatalf("CRD file %s does not exist", crd)
74+
}
75+
}
6876
}
6977

7078
// EnsureCRDsCmd reads each YAML file in the directory, splits it into documents, and applies each CRD to the cluster.
@@ -87,6 +95,10 @@ func EnsureCRDsCmd() {
8795
if err := walkCrdsDir(ctx, client.ApiextensionsV1().CustomResourceDefinitions()); err != nil {
8896
log.Fatalf("Failed to apply CRDs: %v", err)
8997
}
98+
99+
if err := applyCrdFiles(ctx, client.ApiextensionsV1().CustomResourceDefinitions()); err != nil {
100+
log.Fatalf("Failed to apply CRDs: %v", err)
101+
}
90102
}
91103

92104
// walkCrdsDir walks the CRDs directory and applies each YAML file.
@@ -114,6 +126,16 @@ func walkCrdsDir(ctx context.Context, crdClient v1.CustomResourceDefinitionInter
114126
return nil
115127
}
116128

129+
func applyCrdFiles(ctx context.Context, crdClient v1.CustomResourceDefinitionInterface) error {
130+
for _, crdFile := range crds {
131+
log.Printf("Apply CRDs from file: %s", crdFile)
132+
if err := applyCRDsFromFile(ctx, crdClient, crdFile); err != nil {
133+
return fmt.Errorf("apply CRD %s: %w", crdFile, err)
134+
}
135+
}
136+
return nil
137+
}
138+
117139
// applyCRDsFromFile reads a YAML file, splits it into documents, and applies each CRD to the cluster.
118140
func applyCRDsFromFile(ctx context.Context, crdClient v1.CustomResourceDefinitionInterface, filePath string) error {
119141
file, err := os.Open(filePath)

pkg/crdutil/crdutil_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ var _ = Describe("CRD Application", func() {
5252
Expect(crds.Items).To(HaveLen(2))
5353
})
5454

55+
It("should apply CRDs from a valid YAML file", func() {
56+
By("applying CRDs")
57+
Expect(applyCRDsFromFile(ctx, testCRDClient, "test-files/test-crds.yaml")).To(Succeed())
58+
59+
By("verifying CRDs are applied")
60+
crds, err := testCRDClient.List(ctx, metav1.ListOptions{})
61+
Expect(err).NotTo(HaveOccurred())
62+
Expect(crds.Items).To(HaveLen(2))
63+
})
64+
5565
It("should update CRDs", func() {
5666
By("applying CRDs")
5767
Expect(applyCRDsFromFile(ctx, testCRDClient, "test-files/test-crds.yaml")).To(Succeed())

0 commit comments

Comments
 (0)