Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kpt fn commands includes functionConfigs and Kptfile by defaults as function input #2894

Merged
merged 17 commits into from
Mar 31, 2022

Conversation

droot
Copy link
Contributor

@droot droot commented Mar 15, 2022

This PR introduces following (breaking) changes:

  • kpt does not treat meta-resources (functionConfigs and Kptfile) in any special way. That means:
    - Include functionConfigs and Kptfile by default in the function inputs in kpt fn commands.
    - Apply all resources to the cluster in kpt live commands unless local-config annotation is specified
  • --include-meta-resources flag has been removed fromkpt fn eval and kpt fn source commands.
  • kpt fn render now includes functionConfigs and Kptfile as input to the functions like other kpt fn

Note: I will add more details about the rationale behind this breaking change. Also this change will depend on following enhancements before merging in main:

  • kpt should support a way to exclude KRM resources (especially Kptfile and local-configs) so that users who are impacted by this breaking change can use the exclude directive to migrate to this change.
  • Horizontal transforms (set-namespace, set-labels etc. should handle unrecognized resources such as Kptfile gracefully).

@droot droot marked this pull request as ready for review March 15, 2022 22:05
@droot droot changed the title WIP: include functionConfigs by defaults include functionConfig by defaults in kpt fn commands Mar 15, 2022
@droot droot changed the title include functionConfig by defaults in kpt fn commands include functionConfig and Kptfile by defaults in kpt fn commands Mar 17, 2022
@@ -16,5 +16,6 @@ runCount: 2
testType: eval
image: gcr.io/kpt-fn/set-namespace:v0.1.3
includeMetaResources: true
exitCode: 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this PR removes --includeMetaResources commandline flag, so instead of deleting this test, I re-purposed to test the error if someone specifies this flag. I should probably add comment explaining that.

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff is not useful. We should to tweak it to get rid of this diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out. Fixed it.

--- a/db/fn-config.yaml
+++ b/db/fn-config.yaml
@@ -1,17 +1,3 @@
-# Copyright 2021 Google LLC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the license header become part of the diff. It's verbose and not useful.
I guess we don't want the license header to be part of the diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the license header.

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here and below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@mengqiy
Copy link
Contributor

mengqiy commented Mar 21, 2022

kpt should support a way to exclude KRM resources (especially Kptfile and local-configs) so that users who are impacted by this breaking change can use the exclude directive to migrate to this change.

This is important step. Otherwise, users may not have a clear way to upgrade.

Horizontal transforms (set-namespace, set-labels etc. should handle unrecognized resources such as Kptfile gracefully).

It will impact not only horizontal transformers but also any functions uses OpenAPI (e.g. kubeval).

We also need to verify if the comments in Kptfile can be preserved after going through KRM functions.

@droot
Copy link
Contributor Author

droot commented Mar 22, 2022

This is important step. Otherwise, users may not have a clear way to upgrade.

This PR should be merged after this enhancement has been implemented. /cc @natasha41575

@droot droot changed the title include functionConfig and Kptfile by defaults in kpt fn commands kpt fn commands includes functionConfigs and Kptfile by defaults as function input Mar 23, 2022
@bharathkkb
Copy link
Contributor

Apply functionConfigs to the cluster in kpt live commands unless local-config annotation is specified

@droot I am wondering what is the usecase for this? Afaict in a cluster functionConfig would not mean anything and only seems useful in the context of a function.

@droot
Copy link
Contributor Author

droot commented Mar 23, 2022

Apply functionConfigs to the cluster in kpt live commands unless local-config annotation is specified

@droot I am wondering what is the usecase for this? Afaict in a cluster functionConfig would not mean anything and only seems useful in the context of a function.

I should have worded it better (updated it in the PR description). Basically from kpt live standpoint, the model is very simple: A KRM resource is either local or not. If it is local mark it as local-config else it is assumed to be applied to the cluster. From function's perspective, input can come from the items in the resource list, or functionConfig field in resourceList. It shouldn't be be treated in any special manner if it is specified as configPath in the pipeline. The current model around meta-resources leads to lot of confusion and restriction in kpt fn commands (and edge cases as well), and so far we haven't seen any benefits.

@@ -57,8 +57,6 @@ func GetEvalFnRunner(ctx context.Context, parent string) *EvalFnRunner {
&r.Exec, "exec", "", "run an executable as a function")
r.Command.Flags().StringVar(
&r.FnConfigPath, "fn-config", "", "path to the function config file")
r.Command.Flags().BoolVarP(
&r.IncludeMetaResources, "include-meta-resources", "m", false, "include package meta resources in function input")
Copy link
Contributor

@natasha41575 natasha41575 Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to print a helpful message when they are using this flag, we can't remove these lines. As you asked, I created a draft PR (#2939) for how to gracefully let users know this is no longer available, you can integrate that code into this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, re-introduced include-meta-resources as hidden flag and throwing an error as suggested in your PR. Thanks.

@droot
Copy link
Contributor Author

droot commented Mar 25, 2022

@mortent Can you take a look at the kpt live specific changes ? Thanks.

pkg/live/rgpath.go Outdated Show resolved Hide resolved
Copy link
Contributor

@yuwenma yuwenma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. Some minor comments about backward compatible handling

@droot
Copy link
Contributor Author

droot commented Mar 29, 2022

Overall LGTM. Some minor comments about backward compatible handling

Thanks for the review Yuwen. Addressed the comments.

@droot
Copy link
Contributor Author

droot commented Mar 31, 2022

Thanks everyone for reviewing!

@droot droot merged commit eb171fc into kptdev:main Mar 31, 2022
mengqiy pushed a commit to mengqiy/kpt-functions-catalog that referenced this pull request Apr 4, 2022
mengqiy pushed a commit to GoogleContainerTools/kpt-functions-catalog that referenced this pull request Apr 4, 2022
* refactor starlark function

* use kpt before kptdev/kpt#2894 merged

* fix a test
yuwenma pushed a commit to GoogleContainerTools/kpt-functions-catalog that referenced this pull request May 4, 2022
* refactor starlark function

* use kpt before kptdev/kpt#2894 merged

* fix a test
@nadworny
Copy link
Contributor

nadworny commented Aug 25, 2022

hi @droot
I'm a bit confused if this part has been implemented?
kpt should support a way to exclude KRM resources (especially Kptfile and local-configs) so that users who are impacted by this breaking change can use the exclude directive to migrate to this change.

If so, how can I make the kpt fn source to exclude certain resources, in my case the local-config ones?

@droot
Copy link
Contributor Author

droot commented Aug 25, 2022

hi @droot I'm a bit confused if this part has been implemented? kpt should support a way to exclude KRM resources (especially Kptfile and local-configs) so that users who are impacted by this breaking change can use the exclude directive to migrate to this change.

If so, how can I make the kpt fn source to exclude certain resources, in my case the local-config ones?

We added mechanism to exclude resources on the basis of labels, annotations, group/version/kind/namespace/name in kpt fn eval and kpt fn render. For example, https://kpt.dev/book/04-using-functions/02-imperative-function-execution?id=specifying-exclusions, https://kpt.dev/book/04-using-functions/01-declarative-function-execution?id=specifying-exclusions

I don't think we added to kpt fn source.

If you are combining kpt fn source with kpt fn eval, then you can use the exclude support in kpt fn eval. Can you share how how you are using kpt fn source or your use-case, I might be able to suggest a solution ?

@nadworny
Copy link
Contributor

nadworny commented Aug 25, 2022

Thanks for the quick reply.

The use case is following:

  • We are using terraform to apply k8s resources generated by the kpt
  • First we get the package and render it
  • After we use kpt fn source to generate the yaml files which are then read by the terraform
  • Since resources of kind Kptfile or SetLabels aren't valid CRDs in our cluster, we want to exclude them completely

If I understand it correctly, the function exclude capability mentioned above will simply not apply transformations to the excluded resources but will still pass the whole pkg to the next pipe.

@droot
Copy link
Contributor Author

droot commented Aug 25, 2022

Thanks for the quick reply.

The use case is following:

  • We are using terraform to apply k8s resources generated by the kpt
  • First we get the package and render it
  • After we use kpt fn source to generate the yaml files which are then read by the terraform
  • Since resources of kind Kptfile or SetLabels aren't valid CRDs in our cluster, we want to exclude them completely

If I understand it correctly, the function exclude capability mentioned above will simply not apply transformations to the excluded resources but will still pass the whole pkg to the next pipe.

Thanks for the sharing the use-case. The best solution will be for kpt fn commands to provide more flexbility around filtering resources especially for -o unwrap option where people are piping to other tools. Sharing a workaround here:

It requires using another tool called kubectl-slice. kubectl-slice can filter out resources on the basis of comma separates names.

kpt fn source -o unwrap|kubectl-slice --exclude-name kptfile.kpt.dev,mariadb --stdout

Hope this helps.

@nadworny
Copy link
Contributor

nadworny commented Aug 26, 2022

I wasn't aware of kubectl-slice - nice tool!

I have found another workaround using yq:
kpt fn source . | yq '.items[] |= del(select(.kind == "Kptfile"))'

Thanks for your help 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants