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 6fd8e14
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 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,15 @@ 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) {
// TODO(turkenf): Handle replacing UPTEST_DATASOURCE placeholders like ${data.aws_account_id}
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 +83,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 +101,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 6fd8e14

Please sign in to comment.