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

roll our own strict config loading #912

Merged
merged 4 commits into from
Oct 15, 2019

Conversation

BenTheElder
Copy link
Member

/hold

this is an interesting possible alternative to #911

it's less code, simpler, doesn't use unsafe, doesn't fail lints ....

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 3, 2019
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Oct 3, 2019
@BenTheElder BenTheElder force-pushed the bespoke-config branch 2 times, most recently from 994b539 to 346ad70 Compare October 3, 2019 01:04
@BenTheElder BenTheElder changed the title [WIP] roll our own config loading roll our own strict config loading Oct 3, 2019
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 3, 2019
@BenTheElder
Copy link
Member Author

/hold cancel

reasonably happy with this now, might experiment with other strict parsers later... (namely: https://godoc.org/gopkg.in/yaml.v3)

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 3, 2019
@aojea
Copy link
Contributor

aojea commented Oct 3, 2019

What are the pros and cons of moving away from apimachinery?

@BenTheElder
Copy link
Member Author

What are the pros and cons of moving away from apimachinery?

pros:

  • less code
  • less dependency on generated code (easier to develop, and the generators need work... especially to work well with go modules)
  • apimachinery has unsafe code (conversions trying to be simple to generate and fast), we can eliminate that
  • loading config files without the kubernetes api machinery is a perfectly normal task to do in any language without large dependencies and generate code ...
  • we stop leaking internal types via apimachinery's runtime type system
  • we're already writing most of eg the defaulting, conversions by hand anyhow. any non-trivial changes to the types requires this already.
  • developers do not need to learn the apimachinery libraries / generators to work on kind / contribute to the config
  • we're able to do things like stricter parsing with tight control over it and without pulling in super new kubernetes dependencies. go modules users are somewhat stuck on client-go v11 / kubernetes 1.14...

cons:

  • experienced kubernetes developers know the api machinery
  • you need to be thorough with eg writing conversions. this is mitigated by tests.
  • this diff could be smaller

The api machinery makes sense for large sets of kubernetes APIs. It's overkill for loading a config file and doing some defaulting / converting between sets of types.

DeepCopy is always purely mechanical though, so it's kinda nice to keep for those, and has a very simple API.

@aojea
Copy link
Contributor

aojea commented Oct 3, 2019

then, it's clear the path here 😄

@neolit123
Copy link
Member

the picture around component-config vs usage of api-machinery is not very clear, yet component-config is still leaning towards continuing to use api-machinery for config management.

the alternatives introduces by this approach are fine and would work.

my personal vote would be to continue using api-machinery, because without it kind will deviate from the plan for the ecosystem. yet in the end of the day the maintainers know best what is best for the project.

@BenTheElder
Copy link
Member Author

this gives considerably better errors.

$ kind create cluster --config=$HOME/kind.yaml --name=aa
Creating cluster "aa" ...
ERROR: failed to create cluster: unable to decode config: yaml: unmarshal errors:
  line 12: field aa not found in type v1alpha3.Node
  line 13: field cc not found in type v1alpha3.Node

@BenTheElder
Copy link
Member Author

my personal vote would be to continue using api-machinery, because without it kind will deviate from the plan for the ecosystem. yet in the end of the day the maintainers know best what is best for the project.

Totally understand and sympathize with that, but the ecosystem has it's own tech-debt (EG switching to yaml.v3 has been "fun" I gather and not looking super feasible soon...) that kind does not need to be held back by, and I think many of the components have different needs. kind is not a component, it's a user facing CLI tool.

kind is a good place to try things and report back, it's OK to be a little different. We can always switch back at a later date if the trade-off changes.

With a little more cleanup this:

  • is less code (yay!)
  • less dependency on code generation (code-gen = bad contributor experience)
  • gives better errors (main reason I'm interested)

@BenTheElder
Copy link
Member Author

/hold
forgot to finish converting everything to have yaml tags.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 15, 2019
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Oct 15, 2019
@BenTheElder
Copy link
Member Author

/test all

@BenTheElder
Copy link
Member Author

(confirming https://status.docker.com/pages/533c6539221ae15e3f000031 impact, see #951 for temporary work around)

@aojea
Copy link
Contributor

aojea commented Oct 15, 2019

how is the workflow of adding a new api field or adding a new api version?

@BenTheElder
Copy link
Member Author

BenTheElder commented Oct 15, 2019

how is the workflow of adding a new api field or adding a new api version?

~unchanged.
EDIT: slightly changed, you need less changes to hack/update/generated.sh and the conversion / load methods are now just normal code. otherwise you need to do almost exactly the same thing. all of the same methods need modifying etc.

@amwat
Copy link
Contributor

amwat commented Oct 15, 2019

/retest

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 15, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: amwat, BenTheElder

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@BenTheElder
Copy link
Member Author

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 15, 2019
@k8s-ci-robot k8s-ci-robot merged commit f97b720 into kubernetes-sigs:master Oct 15, 2019
@BenTheElder BenTheElder deleted the bespoke-config branch October 15, 2019 20:11

// Nodes contains the list of nodes defined in the `kind` Cluster
// If unset this will default to a single control-plane node
// Note that if more than one control plane is specified, an external
// control plane load balancer will be provisioned implicitly
Nodes []Node `json:"nodes,omitempty"`
Nodes []Node `yaml:"nodes,omitempty" json:"nodes,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

sorry for not having a look at this PR again.

i think this dropped support for user provided JSON kind config files?
which is not very critical but diverges from the "component config" / api-machinery trend.

Copy link
Member Author

@BenTheElder BenTheElder Oct 15, 2019

Choose a reason for hiding this comment

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

  • this also has json tags for now? (reread the diff?)
  • we never supported JSON config files. I don't intend to. yaml is a superset of json.

Copy link
Member Author

Choose a reason for hiding this comment

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

Kubernetes needs a seperate JSON parser for other reasons (because it gives better errors?). yaml.v3 also gives better errors kubernetes/kubernetes#78946

Copy link
Member

Choose a reason for hiding this comment

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

missed the extra tags in the diff.


// get kind & apiVersion
tm := typeMeta{}
if err := yaml.Unmarshal(raw, &tm); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

hm, i've heard that v3 enables strict by default for the Unmarshal function.
kubernetes-sigs/yaml#26 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

it doesn't. we parse a config file in CI.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

(I took a pass through the actual yaml.v3 code before switching)

Copy link
Member

Choose a reason for hiding this comment

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

i see, perhaps the linked commend was misleading.

Copy link
Member Author

Choose a reason for hiding this comment

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

also the new Node object's Decode() does not preserve strict being enabled 😞

AFAICT implementing the secret "obsolete" (v2 api) custom YamlUnmarshal method works fine though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants