mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
* perf: MakeDefaultConfig once
This shaves of another 2 seconds (62%) of the remaining execution
time for a kustomization tree with 4000 documents, reducing the execution
time from 4.79s to 1.82s
0 0% 1.38% 2.98s 37.25% sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig.MakeDefaultConfig
before:
(pprof) top30 -cum
Showing nodes accounting for 1.82s, 22.75% of 8s total
Dropped 408 nodes (cum <= 0.04s)
Showing top 30 nodes out of 308
flat flat% sum% cum cum%
0 0% 0% 4.79s 59.88% github.com/spf13/cobra.(*Command).Execute
0 0% 0% 4.79s 59.88% github.com/spf13/cobra.(*Command).ExecuteC
0 0% 0% 4.79s 59.88% github.com/spf13/cobra.(*Command).execute
0 0% 0% 4.79s 59.88% main.main
0 0% 0% 4.79s 59.88% runtime.main
0 0% 0% 4.79s 59.88% sigs.k8s.io/kustomize/kustomize/v5/commands/build.NewCmdBuild.func1
0 0% 0% 4.22s 52.75% sigs.k8s.io/kustomize/api/krusty.(*Kustomizer).Run
0 0% 0% 4.18s 52.25% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).MakeCustomizedResMap (inline)
0 0% 0% 4.18s 52.25% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).makeCustomizedResMap
0 0% 0% 4.06s 50.75% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).AccumulateTarget
0 0% 0% 4.06s 50.75% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateResources
0 0% 0% 4.06s 50.75% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateTarget
0 0% 0% 4.05s 50.62% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateDirectory
0 0% 0% 3.22s 40.25% runtime.systemstack
0 0% 0% 3.03s 37.88% runtime.gcBgMarkWorker
0 0% 0% 3.03s 37.88% runtime.gcBgMarkWorker.func2
0.11s 1.38% 1.38% 3.03s 37.88% runtime.gcDrain
0 0% 1.38% 2.98s 37.25% sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig.MakeDefaultConfig
0 0% 1.38% 2.98s 37.25% sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig.MakeTransformerConfig
0 0% 1.38% 2.98s 37.25% sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig.makeTransformerConfigFromBytes
0 0% 1.38% 2.97s 37.12% sigs.k8s.io/yaml.yamlUnmarshal
0 0% 1.38% 2.91s 36.38% sigs.k8s.io/yaml.Unmarshal (inline)
1.34s 16.75% 18.12% 2.87s 35.88% runtime.scanobject
0 0% 18.12% 2.53s 31.62% sigs.k8s.io/yaml.yamlToJSON
0 0% 18.12% 1.91s 23.88% gopkg.in/yaml%2ev2.unmarshal
0 0% 18.12% 1.86s 23.25% gopkg.in/yaml%2ev2.Unmarshal (partial-inline)
0 0% 18.12% 1.43s 17.88% gopkg.in/yaml%2ev2.(*parser).parse
0.01s 0.12% 18.25% 1.27s 15.88% gopkg.in/yaml%2ev2.(*parser).document
0.01s 0.12% 18.38% 1.25s 15.62% gopkg.in/yaml%2ev2.(*parser).mapping
0.35s 4.38% 22.75% 1.21s 15.12% runtime.mallocgc
after:
(pprof) top30 -cum
Showing nodes accounting for 0.84s, 24.42% of 3.44s total
Dropped 225 nodes (cum <= 0.02s)
Showing top 30 nodes out of 345
flat flat% sum% cum cum%
0 0% 0% 1.82s 52.91% github.com/spf13/cobra.(*Command).Execute
0 0% 0% 1.82s 52.91% github.com/spf13/cobra.(*Command).ExecuteC
0 0% 0% 1.82s 52.91% github.com/spf13/cobra.(*Command).execute
0 0% 0% 1.82s 52.91% main.main
0 0% 0% 1.82s 52.91% runtime.main
0 0% 0% 1.82s 52.91% sigs.k8s.io/kustomize/kustomize/v5/commands/build.NewCmdBuild.func1
0 0% 0% 1.57s 45.64% runtime.systemstack
0 0% 0% 1.49s 43.31% runtime.gcBgMarkWorker
0 0% 0% 1.49s 43.31% runtime.gcBgMarkWorker.func2
0.03s 0.87% 0.87% 1.49s 43.31% runtime.gcDrain
0.62s 18.02% 18.90% 1.45s 42.15% runtime.scanobject
0 0% 18.90% 1.25s 36.34% sigs.k8s.io/kustomize/api/krusty.(*Kustomizer).Run
0 0% 18.90% 1.21s 35.17% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).MakeCustomizedResMap (inline)
0 0% 18.90% 1.21s 35.17% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).makeCustomizedResMap
0 0% 18.90% 1.08s 31.40% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).AccumulateTarget
0 0% 18.90% 1.08s 31.40% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateResources
0 0% 18.90% 1.08s 31.40% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateTarget
0 0% 18.90% 1.07s 31.10% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateDirectory
0 0% 18.90% 0.57s 16.57% sigs.k8s.io/kustomize/api/resmap.(*resWrangler).AsYaml
0 0% 18.90% 0.57s 16.57% sigs.k8s.io/kustomize/api/resource.(*Resource).AsYAML
0.11s 3.20% 22.09% 0.48s 13.95% runtime.mallocgc
0 0% 22.09% 0.45s 13.08% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).accumulateFile
0 0% 22.09% 0.45s 13.08% sigs.k8s.io/kustomize/api/resmap.(*Factory).FromFile
0 0% 22.09% 0.33s 9.59% sigs.k8s.io/kustomize/kyaml/yaml.(*RNode).MarshalJSON
0.08s 2.33% 24.42% 0.32s 9.30% runtime.greyobject
0 0% 24.42% 0.30s 8.72% sigs.k8s.io/kustomize/api/resmap.(*Factory).NewResMapFromBytes
0 0% 24.42% 0.27s 7.85% sigs.k8s.io/yaml.JSONToYAML
0 0% 24.42% 0.25s 7.27% sigs.k8s.io/kustomize/api/internal/target.(*KustTarget).runTransformers
0 0% 24.42% 0.25s 7.27% sigs.k8s.io/kustomize/api/resource.(*Factory).RNodesFromBytes
0 0% 24.42% 0.25s 7.27% sigs.k8s.io/kustomize/api/resource.(*Factory).SliceFromBytes
* Tests and comments for MakeDefaultConfig perf work
Document updated code in MakeDefaultConfig.
Add unit tests to ensure DeepCopy works.
Add hints to other code to ensure DeepCopy is kept up to date.
101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
// Copyright 2019 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package types
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"sigs.k8s.io/kustomize/kyaml/resid"
|
|
)
|
|
|
|
// FieldSpec completely specifies a kustomizable field in a k8s API object.
|
|
// It helps define the operands of transformations.
|
|
//
|
|
// For example, a directive to add a common label to objects
|
|
// will need to know that a 'Deployment' object (in API group
|
|
// 'apps', any version) can have labels at field path
|
|
// 'spec/template/metadata/labels', and further that it is OK
|
|
// (or not OK) to add that field path to the object if the
|
|
// field path doesn't exist already.
|
|
//
|
|
// This would look like
|
|
// {
|
|
// group: apps
|
|
// kind: Deployment
|
|
// path: spec/template/metadata/labels
|
|
// create: true
|
|
// }
|
|
type FieldSpec struct {
|
|
resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
|
|
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
|
CreateIfNotPresent bool `json:"create,omitempty" yaml:"create,omitempty"`
|
|
|
|
// Note: If any new pointer based members are added, FsSlice.DeepCopy needs to be updated
|
|
}
|
|
|
|
func (fs FieldSpec) String() string {
|
|
return fmt.Sprintf(
|
|
"%s:%v:%s", fs.Gvk.String(), fs.CreateIfNotPresent, fs.Path)
|
|
}
|
|
|
|
// If true, the primary key is the same, but other fields might not be.
|
|
func (fs FieldSpec) effectivelyEquals(other FieldSpec) bool {
|
|
return fs.IsSelected(&other.Gvk) && fs.Path == other.Path
|
|
}
|
|
|
|
type FsSlice []FieldSpec
|
|
|
|
func (s FsSlice) Len() int { return len(s) }
|
|
func (s FsSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
func (s FsSlice) Less(i, j int) bool {
|
|
return s[i].Gvk.IsLessThan(s[j].Gvk)
|
|
}
|
|
|
|
// DeepCopy returns a new copy of FsSlice
|
|
func (s FsSlice) DeepCopy() FsSlice {
|
|
ret := make(FsSlice, len(s))
|
|
copy(ret, s)
|
|
return ret
|
|
}
|
|
|
|
// MergeAll merges the argument into this, returning the result.
|
|
// Items already present are ignored.
|
|
// Items that conflict (primary key matches, but remain data differs)
|
|
// result in an error.
|
|
func (s FsSlice) MergeAll(incoming FsSlice) (result FsSlice, err error) {
|
|
result = s
|
|
for _, x := range incoming {
|
|
result, err = result.MergeOne(x)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// MergeOne merges the argument into this, returning the result.
|
|
// If the item's primary key is already present, and there are no
|
|
// conflicts, it is ignored (we don't want duplicates).
|
|
// If there is a conflict, the merge fails.
|
|
func (s FsSlice) MergeOne(x FieldSpec) (FsSlice, error) {
|
|
i := s.index(x)
|
|
if i > -1 {
|
|
// It's already there.
|
|
if s[i].CreateIfNotPresent != x.CreateIfNotPresent {
|
|
return nil, fmt.Errorf("conflicting fieldspecs")
|
|
}
|
|
return s, nil
|
|
}
|
|
return append(s, x), nil
|
|
}
|
|
|
|
func (s FsSlice) index(fs FieldSpec) int {
|
|
for i, x := range s {
|
|
if x.effectivelyEquals(fs) {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|