kpt icon indicating copy to clipboard operation
kpt copied to clipboard

No change happens if delete all the resources through kpt ts functions

Open patrickchense opened this issue 5 years ago • 2 comments

No change happens when we try to filter out all the resources through kpt function. kpt version: 0.36.1

We have a kpt ts function filtering resource not fit the conditions. Simply like below:

export async function filterResources(configs: Configs): Promise<void> {
  
  configs.delete(
    ...configs
      .getAll()
      .filter(
        ko =>
          condition1 || condition2 || condition3 ...
      ),
  );

If all resources in the configs match and should be deleted, then the input source directory keeps as unchanged.

Expectation: The input directory becomes empty.

Reproduce: It is not hard to reproduce the bug.

  • Init a kpt work environment follow the official document.
  • Add an extra ts function like below:
import { Configs } from "kpt-functions";

export async function filterInfraResources(configs: Configs): Promise<void> {
  configs.deleteAll(); 
}

filterInfraResources.usage = "";
  • Build it and Run the function on the example configs.
$ mkdir result
$ kpt fn source ../../example-configs/shipping-dev | node dist/filter_infra_resources_run.js | kpt fn sink result
$ ls result 
  • Run ls result, which shows all the files from the original folder.

But if we make the kpt function return at least one resource, this works. Modify the kpt function like below:

export async function filterInfraResources(configs: Configs): Promise<void> {
  configs.delete(
    ...configs.getAll().filter(ko => ko.metadata.name != "viewers"),
  );
}

Then, rebuild and re-run the above steps. I can get only rolebinding_viewers.yaml in the result folder.

So the deletion only works when there is at least one config left, so we have to add a temporary config into the configs to make sure kpt deletes the rest of the resources. And delete the temp resources after kpt fn run finishes in our Java code.

Any ideas?

Thanks in advance!

patrickchense avatar Feb 15 '21 17:02 patrickchense

This behavior is from kio.Pipeline. https://github.com/kubernetes-sigs/kustomize/blob/14dc3dfb81ce52e1b9d7e90a1b673de7e78c0c52/kyaml/kio/kio.go#L124

Pipeline has an option ContinueOnEmptyResult to control it.

Shell32-Natsu avatar Apr 08 '21 17:04 Shell32-Natsu

I am not sure what's the reasoning behind this behavior, but it seems to be inconsistent. Prioritizing it.

droot avatar Apr 15 '21 15:04 droot