Skip to content

Commit

Permalink
crdutil: Add feature to also apply also single files
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Giese <tgiese@nvidia.com>
  • Loading branch information
tobiasgiese committed Nov 25, 2024
1 parent 1ad8938 commit 56fdeb6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/crdutil/crdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,29 @@ func (s *StringList) Set(value string) error {

var (
crdsDir StringList
crds StringList
)

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

if len(crdsDir) == 0 {
log.Fatalf("CRDs directory is required")
if len(crdsDir) == 0 && len(crds) == 0 {
log.Fatalf("CRDs directory or single CRDs are required")
}

for _, crdDir := range crdsDir {
if _, err := os.Stat(crdDir); os.IsNotExist(err) {
log.Fatalf("CRDs directory %s does not exist", crdsDir)
}
}

for _, crd := range crds {
if _, err := os.Stat(crd); os.IsNotExist(err) {
log.Fatalf("CRD file %s does not exist", crd)
}
}
}

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

if err := applyCrdFiles(ctx, client.ApiextensionsV1().CustomResourceDefinitions()); err != nil {
log.Fatalf("Failed to apply CRDs: %v", err)
}
}

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

func applyCrdFiles(ctx context.Context, crdClient v1.CustomResourceDefinitionInterface) error {
for _, crdFile := range crds {
log.Printf("Apply CRDs from file: %s", crdFile)
if err := applyCRDsFromFile(ctx, crdClient, crdFile); err != nil {
return fmt.Errorf("apply CRD %s: %w", crdFile, err)
}
}
return nil
}

// applyCRDsFromFile reads a YAML file, splits it into documents, and applies each CRD to the cluster.
func applyCRDsFromFile(ctx context.Context, crdClient v1.CustomResourceDefinitionInterface, filePath string) error {
file, err := os.Open(filePath)
Expand Down
10 changes: 10 additions & 0 deletions pkg/crdutil/crdutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ var _ = Describe("CRD Application", func() {
Expect(crds.Items).To(HaveLen(2))
})

It("should apply CRDs from a valid YAML file", func() {
By("applying CRDs")
Expect(applyCRDsFromFile(ctx, testCRDClient, "test-files/test-crds.yaml")).To(Succeed())

By("verifying CRDs are applied")
crds, err := testCRDClient.List(ctx, metav1.ListOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(crds.Items).To(HaveLen(2))
})

It("should update CRDs", func() {
By("applying CRDs")
Expect(applyCRDsFromFile(ctx, testCRDClient, "test-files/test-crds.yaml")).To(Succeed())
Expand Down

0 comments on commit 56fdeb6

Please sign in to comment.