From 0d9c01e97d52c66ff6e62f973f48de1643488898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20T=C3=BCrken?= Date: Thu, 29 Aug 2024 11:36:03 +0300 Subject: [PATCH] Add description for functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fatih Türken --- cmd/cleanupexamples/main.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/cleanupexamples/main.go b/cmd/cleanupexamples/main.go index 7697f1b..9b136e5 100644 --- a/cmd/cleanupexamples/main.go +++ b/cmd/cleanupexamples/main.go @@ -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 { @@ -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") @@ -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) @@ -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 {