Skip to content

Commit

Permalink
Add description for functions
Browse files Browse the repository at this point in the history
Signed-off-by: Fatih Türken <[email protected]>
  • Loading branch information
turkenf committed Aug 29, 2024
1 parent 4181b24 commit 0d9c01e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/cleanupexamples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import (
"sigs.k8s.io/yaml"
)

// removeAnnotations removes specific annotations from a Kubernetes
// unstructured object. It looks for annotations with prefixes
// "upjet.upbound.io/" and "uptest.upbound.io/" and removes
// them if they are present.
func removeAnnotations(u *unstructured.Unstructured) {
annotations := u.GetAnnotations()
if annotations != nil {
Expand All @@ -56,9 +60,14 @@ func removeAnnotations(u *unstructured.Unstructured) {
}
}

// processYAML processes a YAML file by replacing specific placeholders
// and removing certain annotations from the Kubernetes objects within it.
// It returns the modified YAML content as a byte slice with YAML document
// separators ("---") between each object.
func processYAML(yamlData []byte) ([]byte, error) {
yamlData = bytes.ReplaceAll(yamlData, []byte("${Rand.RFC1123Subdomain}"), []byte("random"))

// Create a YAML or JSON decoder to read and decode the input YAML data
decoder := kyaml.NewYAMLOrJSONDecoder(bytes.NewReader(yamlData), 1024)
var modifiedYAMLs []byte
separator := []byte("---\n")
Expand All @@ -73,6 +82,7 @@ func processYAML(yamlData []byte) ([]byte, error) {
return nil, fmt.Errorf("cannot decode the YAML file: %w", err)
}

// Remove specific annotations from the decoded Kubernetes object.
removeAnnotations(u)

modifiedYAML, err := yaml.Marshal(u.Object)
Expand All @@ -90,6 +100,10 @@ func processYAML(yamlData []byte) ([]byte, error) {
return modifiedYAMLs, nil
}

// processDirectory walks through a directory structure, processes all YAML
// files within it by calling `processYAML`, and saves the modified files
// to a specified output directory while preserving the original
// directory structure.
func processDirectory(inputDir string, outputDir string) error { //nolint:gocyclo // sequential flow easier to follow
// Walk through the input directory
err := filepath.Walk(inputDir, func(path string, info os.FileInfo, err error) error {
Expand Down

0 comments on commit 0d9c01e

Please sign in to comment.