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 <[email protected]>
  • Loading branch information
tobiasgiese committed Nov 27, 2024
1 parent 1ad8938 commit 43b027c
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions pkg/crdutil/crdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,24 @@ func (s *StringList) Set(value string) error {
}

var (
crdsDir StringList
files StringList
recursive bool
)

func initFlags() {
flag.Var(&crdsDir, "crds-dir", "Path to the directory containing the CRD manifests")
flag.Var(&files, "filename", "The files that contain the configurations to apply.")
flag.Var(&files, "f", "The files that contain the configurations to apply.")
flag.BoolVar(&recursive, "recursive", false, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.")
flag.BoolVar(&recursive, "R", false, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.")
flag.Parse()

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

for _, crdDir := range crdsDir {
for _, crdDir := range files {
if _, err := os.Stat(crdDir); os.IsNotExist(err) {
log.Fatalf("CRDs directory %s does not exist", crdsDir)
log.Fatalf("CRDs directory %s does not exist", files)
}
}
}
Expand All @@ -84,20 +88,26 @@ func EnsureCRDsCmd() {
log.Fatalf("Failed to create API extensions client: %v", err)
}

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

// walkCrdsDir walks the CRDs directory and applies each YAML file.
func walkCrdsDir(ctx context.Context, crdClient v1.CustomResourceDefinitionInterface) error {
for _, crdDir := range crdsDir {
// applyCRDs walks the CRDs directory and applies each YAML file.
func applyCRDs(ctx context.Context, crdClient v1.CustomResourceDefinitionInterface) error {
for _, crdDir := range files {
// Walk the directory recursively and apply each YAML file.
err := filepath.Walk(crdDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || filepath.Ext(path) != ".yaml" {
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".yaml" && filepath.Ext(path) != ".yml" {
return nil
}
if !recursive && filepath.Dir(path) != crdDir {
return nil
}

Expand All @@ -108,7 +118,7 @@ func walkCrdsDir(ctx context.Context, crdClient v1.CustomResourceDefinitionInter
return nil
})
if err != nil {
return fmt.Errorf("walk the path %s: %w", crdsDir, err)
return fmt.Errorf("walk the path %s: %w", files, err)
}
}
return nil
Expand Down Expand Up @@ -170,14 +180,16 @@ func applyCRD(
if err != nil {
return fmt.Errorf("create CRD %s: %w", crd.Name, err)
}
} else {
} else if apierrors.IsAlreadyExists(err) {
log.Printf("Update CRD %s", crd.Name)
// Set resource version to update an existing CRD.
crd.SetResourceVersion(curCRD.GetResourceVersion())
_, err = crdClient.Update(ctx, crd, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("update CRD %s: %w", crd.Name, err)
}
} else {
return fmt.Errorf("get CRD %s: %w", crd.Name, err)
}
return nil
}

0 comments on commit 43b027c

Please sign in to comment.