kubernetes-sigs / kustomize

Customization of kubernetes YAML configurations
Apache License 2.0
11.08k stars 2.26k forks source link

DATA RACE in kyaml 0.18.1, openapi package - `globalSchema.namespaceabilityByResourceType` #5805

Open chlunde opened 4 days ago

chlunde commented 4 days ago

What happened?

I ran krusty.(*Kustomizer).Run() in two goroutines with the Go race detector enabled, and a data race was reported. The two goroutines access globalSchema.namespaceabilityByResourceType:

The following lines is a shortened output whichs shows how to achieve this via two different public methods on the openapi package:

WARNING: DATA RACE

Write at 0x000006ddedb8 by goroutine 91:
  sigs.k8s.io/kustomize/kyaml/openapi.findNamespaceability()
      go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:760 +0x64
  sigs.k8s.io/kustomize/kyaml/openapi.parse()
      go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:749 +0x4cf
  sigs.k8s.io/kustomize/kyaml/openapi.parseBuiltinSchema()
      go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:713 +0x19d
  sigs.k8s.io/kustomize/kyaml/openapi.initSchema()
      go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:683 +0x2fb
  sigs.k8s.io/kustomize/kyaml/openapi.SchemaForResourceType()

Previous read at 0x000006ddedb8 by goroutine 83:
  sigs.k8s.io/kustomize/kyaml/openapi.IsNamespaceScoped()
      go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:410 +0x87
  sigs.k8s.io/kustomize/kyaml/openapi.IsCertainlyClusterScoped()
      go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:448 +0x1ec
full log ``` ================== WARNING: DATA RACE Write at 0x000006ddedb8 by goroutine 91: sigs.k8s.io/kustomize/kyaml/openapi.findNamespaceability() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:760 +0x64 sigs.k8s.io/kustomize/kyaml/openapi.parse() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:749 +0x4cf sigs.k8s.io/kustomize/kyaml/openapi.parseBuiltinSchema() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:713 +0x19d sigs.k8s.io/kustomize/kyaml/openapi.initSchema() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:683 +0x2fb sigs.k8s.io/kustomize/kyaml/openapi.SchemaForResourceType() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:198 +0xb99 sigs.k8s.io/kustomize/kyaml/yaml/walk.Walker.GetSchema() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/yaml/walk/walk.go:125 +0xc21 sigs.k8s.io/kustomize/kyaml/yaml/walk.Walker.Walk() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/yaml/walk/walk.go:57 +0x137 sigs.k8s.io/kustomize/kyaml/yaml/merge2.Merge() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/yaml/merge2/merge2.go:20 +0x217 sigs.k8s.io/kustomize/api/filters/patchstrategicmerge.Filter.Filter() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/filters/patchstrategicmerge/patchstrategicmerge.go:22 +0xa5 sigs.k8s.io/kustomize/api/resource.(*Resource).ApplyFilter() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resource/resource.go:516 +0x93 sigs.k8s.io/kustomize/api/resource.(*Resource).ApplySmPatch() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resource/resource.go:497 +0x133 sigs.k8s.io/kustomize/api/internal/builtins.(*PatchTransformerPlugin).transformStrategicMerge() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/builtins/PatchTransformer.go:116 +0x515 sigs.k8s.io/kustomize/api/internal/builtins.(*PatchTransformerPlugin).Transform() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/builtins/PatchTransformer.go:87 +0x50 sigs.k8s.io/kustomize/api/internal/target.(*multiTransformer).Transform() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/multitransformer.go:30 +0xd2 sigs.k8s.io/kustomize/api/internal/accumulator.(*ResAccumulator).Transform() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/accumulator/resaccumulator.go:141 +0x284 sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).runTransformers() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:343 +0x28f sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateTarget() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:237 +0x6ee sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).AccumulateTarget() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:194 +0x229 sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).makeCustomizedResMap() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:135 +0x105 sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).MakeCustomizedResMap() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:126 +0x4fc sigs.k8s.io/kustomize/api/krusty.(*Kustomizer).Run() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/krusty/kustomizer.go:90 +0x4ef Previous read at 0x000006ddedb8 by goroutine 83: sigs.k8s.io/kustomize/kyaml/openapi.IsNamespaceScoped() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:410 +0x87 sigs.k8s.io/kustomize/kyaml/openapi.IsCertainlyClusterScoped() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/openapi/openapi.go:448 +0x1ec sigs.k8s.io/kustomize/kyaml/resid.NewGvk() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/resid/gvk.go:27 +0xff sigs.k8s.io/kustomize/kyaml/resid.GvkFromNode() go/pkg/mod/sigs.k8s.io/kustomize/kyaml@v0.18.1/resid/gvk.go:33 +0x12b sigs.k8s.io/kustomize/api/resource.(*Resource).GetGvk() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resource/resource.go:57 +0x68 sigs.k8s.io/kustomize/api/resource.(*Resource).CurId() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resource/resource.go:462 +0xa1 sigs.k8s.io/kustomize/api/resmap.GetCurrentId() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resmap/reswrangler.go:171 +0x64 sigs.k8s.io/kustomize/api/resmap.(*resWrangler).filteredById() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resmap/reswrangler.go:198 +0xef sigs.k8s.io/kustomize/api/resmap.(*resWrangler).GetMatchingResourcesByCurrentId() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resmap/reswrangler.go:176 +0x154 sigs.k8s.io/kustomize/api/resmap.(*resWrangler).Append() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resmap/reswrangler.go:78 +0x1d sigs.k8s.io/kustomize/api/resmap.(*resWrangler).appendAll() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resmap/reswrangler.go:468 +0xf6 sigs.k8s.io/kustomize/api/resmap.(*resWrangler).AppendAll() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/resmap/reswrangler.go:462 +0x45 sigs.k8s.io/kustomize/api/internal/accumulator.(*ResAccumulator).AppendAll() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/accumulator/resaccumulator.go:45 +0x45e sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateFile() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:556 +0x437 sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateResources() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:421 +0xed sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateTarget() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:201 +0x9e sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).AccumulateTarget() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:194 +0x229 sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).makeCustomizedResMap() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:135 +0x105 sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).MakeCustomizedResMap() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/internal/target/kusttarget.go:126 +0x4fc sigs.k8s.io/kustomize/api/krusty.(*Kustomizer).Run() go/pkg/mod/sigs.k8s.io/kustomize/api@v0.18.0/krusty/kustomizer.go:90 +0x4ef ```

What did you expect to happen?

No data races

How can we reproduce it (as minimally and precisely as possible)?

Run krusty.Run in two or more goroutines with the race detector enabled.

Expected output

No race detected

Actual output

WARNING: DATA RACE

Kustomize version

kyaml 0.18.1

Operating system

Linux

k8s-ci-robot commented 4 days ago

This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
chlunde commented 4 days ago

I think a workaround is to call

 openapi.SchemaForResourceType(kyaml.TypeMeta{}) // https://github.com/kubernetes-sigs/kustomize/issues/5805

Before creating any goroutines.