// Package trimmer contains the logic for the kpt fn so that it can be // leveraged in non-kpt contexts package trimmer import ( "errors" "sigs.k8s.io/kustomize/kyaml/yaml" ) // Trim removes the 'status', empty/null 'metadata.creationTimestamp' and empty/null 'spec' fields // by mutating the provided yaml.RNode pointer func Trim(resource *yaml.RNode) error { return errors.Join( trimStatus(resource), trimCreationTimestamp(resource), trimRefreshTime(resource), trimEmptySpec(resource), ) } // Run implements the kio.FilterFunc interface, allowing the trimmer to be // composed into a KRM function pipeline func (n *Trimmer) Run(input []*yaml.RNode) ([]*yaml.RNode, error) { for i := range input { err := Trim(input[i]) if err != nil { return nil, err } } return input, nil } // Clears the 'status' field func trimStatus(r *yaml.RNode) error { return r.PipeE(yaml.Clear("status")) } // trimCreationTimestamp clears the 'metadata.creationTimestamp' when it is empty or null. func trimCreationTimestamp(r *yaml.RNode) error { metadata, err := r.Pipe(yaml.Lookup("metadata")) if yaml.IsMissingOrError(metadata, err) { // If the 'metadata' field is missing, the returned error will be nil // and this function will gracefully exit since there is nothing to trim. return err } creationTimestamp, err := metadata.Pipe(yaml.Lookup("creationTimestamp")) if yaml.IsMissingOrError(creationTimestamp, err) { // If the 'creationTimestamp' field is missing, the returned error will be nil // and this function will gracefully exit since there is nothing to trim. return err } else if yaml.IsMissingOrNull(creationTimestamp) || yaml.IsEmptyMap(creationTimestamp) { return metadata.PipeE(yaml.Clear("creationTimestamp")) } // The 'creationTimestamp' is kept when it is not an empty map or null. return nil } // trimRefreshTime clears the 'metadata.creationTimestamp' when it is empty or null. func trimRefreshTime(r *yaml.RNode) error { metadata, err := r.Pipe(yaml.Lookup("metadata")) if yaml.IsMissingOrError(metadata, err) { // If the 'metadata' field is missing, the returned error will be nil // and this function will gracefully exit since there is nothing to trim. return err } refreshTime, err := metadata.Pipe(yaml.Lookup("refreshTime")) if yaml.IsMissingOrError(refreshTime, err) { // If the 'refreshTime' field is missing, the returned error will be nil // and this function will gracefully exit since there is nothing to trim. return err } else if yaml.IsMissingOrNull(refreshTime) || yaml.IsEmptyMap(refreshTime) { return metadata.PipeE(yaml.Clear("refreshTime")) } // The 'refreshTime' is kept when it is not an empty map or null. return nil } // trimEmptySpec remove the 'spec' field if empty func trimEmptySpec(r *yaml.RNode) error { spec, err := r.Pipe(yaml.Lookup("spec")) if yaml.IsMissingOrError(spec, err) { // If the 'spec' field is missing, the returned error will be nil // and this function will gracefully exit since there is nothing to trim. return err } else if spec.IsNilOrEmpty() { return r.PipeE(yaml.Clear("spec")) } return nil }