...

Source file src/edge-infra.dev/pkg/edge/gitops/fns/trimmer/trimmer.go

Documentation: edge-infra.dev/pkg/edge/gitops/fns/trimmer

     1  // Package trimmer contains the logic for the kpt fn so that it can be
     2  // leveraged in non-kpt contexts
     3  package trimmer
     4  
     5  import (
     6  	"errors"
     7  
     8  	"sigs.k8s.io/kustomize/kyaml/yaml"
     9  )
    10  
    11  // Trim removes the 'status', empty/null 'metadata.creationTimestamp' and empty/null 'spec' fields
    12  // by mutating the provided yaml.RNode pointer
    13  func Trim(resource *yaml.RNode) error {
    14  	return errors.Join(
    15  		trimStatus(resource),
    16  		trimCreationTimestamp(resource),
    17  		trimRefreshTime(resource),
    18  		trimEmptySpec(resource),
    19  	)
    20  }
    21  
    22  // Run implements the kio.FilterFunc interface, allowing the trimmer to be
    23  // composed into a KRM function pipeline
    24  func (n *Trimmer) Run(input []*yaml.RNode) ([]*yaml.RNode, error) {
    25  	for i := range input {
    26  		err := Trim(input[i])
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  	}
    31  	return input, nil
    32  }
    33  
    34  // Clears the 'status' field
    35  func trimStatus(r *yaml.RNode) error {
    36  	return r.PipeE(yaml.Clear("status"))
    37  }
    38  
    39  // trimCreationTimestamp clears the 'metadata.creationTimestamp' when it is empty or null.
    40  func trimCreationTimestamp(r *yaml.RNode) error {
    41  	metadata, err := r.Pipe(yaml.Lookup("metadata"))
    42  	if yaml.IsMissingOrError(metadata, err) {
    43  		// If the 'metadata' field is missing, the returned error will be nil
    44  		// and this function will gracefully exit since there is nothing to trim.
    45  		return err
    46  	}
    47  	creationTimestamp, err := metadata.Pipe(yaml.Lookup("creationTimestamp"))
    48  	if yaml.IsMissingOrError(creationTimestamp, err) {
    49  		// If the 'creationTimestamp' field is missing, the returned error will be nil
    50  		// and this function will gracefully exit since there is nothing to trim.
    51  		return err
    52  	} else if yaml.IsMissingOrNull(creationTimestamp) || yaml.IsEmptyMap(creationTimestamp) {
    53  		return metadata.PipeE(yaml.Clear("creationTimestamp"))
    54  	}
    55  	// The 'creationTimestamp' is kept when it is not an empty map or null.
    56  	return nil
    57  }
    58  
    59  // trimRefreshTime clears the 'metadata.creationTimestamp' when it is empty or null.
    60  func trimRefreshTime(r *yaml.RNode) error {
    61  	metadata, err := r.Pipe(yaml.Lookup("metadata"))
    62  	if yaml.IsMissingOrError(metadata, err) {
    63  		// If the 'metadata' field is missing, the returned error will be nil
    64  		// and this function will gracefully exit since there is nothing to trim.
    65  		return err
    66  	}
    67  	refreshTime, err := metadata.Pipe(yaml.Lookup("refreshTime"))
    68  	if yaml.IsMissingOrError(refreshTime, err) {
    69  		// If the 'refreshTime' field is missing, the returned error will be nil
    70  		// and this function will gracefully exit since there is nothing to trim.
    71  		return err
    72  	} else if yaml.IsMissingOrNull(refreshTime) || yaml.IsEmptyMap(refreshTime) {
    73  		return metadata.PipeE(yaml.Clear("refreshTime"))
    74  	}
    75  	// The 'refreshTime' is kept when it is not an empty map or null.
    76  	return nil
    77  }
    78  
    79  // trimEmptySpec remove the 'spec' field if empty
    80  func trimEmptySpec(r *yaml.RNode) error {
    81  	spec, err := r.Pipe(yaml.Lookup("spec"))
    82  	if yaml.IsMissingOrError(spec, err) {
    83  		// If the 'spec' field is missing, the returned error will be nil
    84  		// and this function will gracefully exit since there is nothing to trim.
    85  		return err
    86  	} else if spec.IsNilOrEmpty() {
    87  		return r.PipeE(yaml.Clear("spec"))
    88  	}
    89  	return nil
    90  }
    91  

View as plain text