-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change kustomize edit fix to split patch files that contain multiple …
…patches into multiple patch files, each containing a single patch. Fixes line breaks in output. Suppress warning message for different output when old build fails and new build succeeds.
- Loading branch information
1 parent
22dbd3e
commit 8013049
Showing
3 changed files
with
548 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2023 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package fix | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"sigs.k8s.io/kustomize/api/types" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
// splitPatches splits patches that contain multiple documents into multiple files and updates the kustomization file's | ||
// Patch field to point to the new files. | ||
func splitPatches(fSys filesys.FileSystem, k *types.Kustomization, w io.Writer) error { | ||
yamlSeparatorRegexp := regexp.MustCompile(`(?:^|\n)---.*`) // Matches any line that starts with --- | ||
|
||
var messages []string | ||
|
||
var splitPatches []types.Patch | ||
for _, patch := range k.Patches { | ||
patchContentBytes, err := fSys.ReadFile(patch.Path) | ||
if errors.Is(err, os.ErrNotExist) { | ||
// If the patch file does not exist, there is nothing to do. It is not this function's responsibility | ||
// to validate the existence of patch files. | ||
splitPatches = append(splitPatches, patch) | ||
continue | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
splitPatchContent := yamlSeparatorRegexp.Split(string(patchContentBytes), -1) | ||
|
||
// If there are no separators, there is nothing to do, so keep the original patch file and continue. | ||
if len(splitPatchContent) == 1 { | ||
splitPatches = append(splitPatches, patch) | ||
continue | ||
} | ||
|
||
// Find the new patches, removing any empty ones. | ||
var newPatches []string | ||
for _, pc := range splitPatchContent { | ||
trimmedPatchContent := strings.TrimSpace(pc) | ||
if len(trimmedPatchContent) > 0 { | ||
newPatches = append(newPatches, trimmedPatchContent+"\n") | ||
} | ||
} | ||
|
||
// If there is only one new patch, Overwrite the original patch file and continue. | ||
if len(newPatches) == 1 { | ||
err := fSys.WriteFile(patch.Path, []byte(newPatches[0])) | ||
if err != nil { | ||
return err | ||
} | ||
splitPatches = append(splitPatches, patch) | ||
messages = append(messages, fmt.Sprintf("%s: removed unnecessary document separators", patch.Path)) | ||
continue | ||
} | ||
|
||
// If there are multiple new patches, create new patch files for each one and remove the original patch file. | ||
var newPatchPaths []string | ||
for i, newPatchContent := range newPatches { | ||
newPatchPath, err := availableFilename(fSys, patch.Path, i+1) | ||
if err != nil { | ||
return err | ||
} | ||
err = fSys.WriteFile(newPatchPath, []byte(newPatchContent)) | ||
if err != nil { | ||
return err | ||
} | ||
splitPatches = append(splitPatches, types.Patch{Path: newPatchPath}) | ||
newPatchPaths = append(newPatchPaths, newPatchPath) | ||
} | ||
messages = append(messages, fmt.Sprintf("%s -> %s", patch.Path, strings.Join(newPatchPaths, ", "))) | ||
|
||
err = fSys.RemoveAll(patch.Path) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
k.Patches = splitPatches | ||
|
||
if len(messages) > 0 { | ||
fmt.Fprintf(w, "\nSplit patches:\n %s\n", strings.Join(messages, "\n ")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// availableFilename returns a filename that does not already exist in the filesystem, by repeatedly appending a suffix | ||
// to the filename until a non-existing filename is found. | ||
func availableFilename(fSys filesys.FileSystem, originalFilename string, suffix int) (string, error) { | ||
ext := filepath.Ext(originalFilename) | ||
base := strings.TrimSuffix(originalFilename, ext) | ||
for i := 0; i < 100; i++ { | ||
base += fmt.Sprintf("-%d", suffix) | ||
if !fSys.Exists(base + ext) { | ||
return base + ext, nil | ||
} | ||
} | ||
return "", fmt.Errorf("unable to find available filename for %s and suffix %d", originalFilename, suffix) | ||
} |
Oops, something went wrong.