Skip to content

Kustomize module for generation and var substitution - #203

Merged
stefanprodan merged 1 commit into
fluxcd:mainfrom
souleb:kustomization-module
Jan 18, 2022
Merged

Kustomize module for generation and var substitution#203
stefanprodan merged 1 commit into
fluxcd:mainfrom
souleb:kustomization-module

Conversation

@souleb

@souleb souleb commented Dec 21, 2021

Copy link
Copy Markdown
Member

This module declare methods used in both kustomize-controller

and the flux cli

Signed-off-by: Soule BA bah.soule@gmail.com

@souleb
souleb marked this pull request as draft December 21, 2021 16:12
Comment thread kustomization/go.mod Outdated
@souleb
souleb marked this pull request as ready for review December 22, 2021 13:58
@souleb
souleb force-pushed the kustomization-module branch from ff58df8 to 129742d Compare December 22, 2021 21:34
@souleb
souleb marked this pull request as draft December 22, 2021 22:30
@souleb
souleb force-pushed the kustomization-module branch 2 times, most recently from 4f30c6b to b7e97ad Compare December 25, 2021 23:31
@souleb
souleb marked this pull request as ready for review December 25, 2021 23:35
@souleb
souleb force-pushed the kustomization-module branch from b7e97ad to fbc99e1 Compare December 26, 2021 10:44
Comment thread kustomization/go.mod Outdated
@souleb
souleb force-pushed the kustomization-module branch 3 times, most recently from b68d034 to bcb7109 Compare January 4, 2022 09:57
Comment thread kustomization/kustomization_generator.go Outdated
@souleb
souleb force-pushed the kustomization-module branch 3 times, most recently from 3b979bd to d472340 Compare January 6, 2022 12:47
@souleb

souleb commented Jan 6, 2022

Copy link
Copy Markdown
Member Author

@stefanprodan as discussed I have made sure to restore a clean repository.

Comment thread kustomization/kustomization_generator.go Outdated
@souleb
souleb force-pushed the kustomization-module branch 3 times, most recently from 9fd9005 to 223a55e Compare January 6, 2022 22:24
Comment thread kustomization/kustomization_generator.go Outdated
@souleb
souleb force-pushed the kustomization-module branch 2 times, most recently from e356cf3 to 46ff99f Compare January 7, 2022 21:56
@souleb

souleb commented Jan 8, 2022

Copy link
Copy Markdown
Member Author

I have tested and benchmarked the generator code on the kustomize-controller. The tests are positive.

Here the benchmark results

# existing generator code
BenchmarkKustomizationGenerator-8           1000          19454609 ns/op        11205376 B/op      90431 allocs/op
BenchmarkKustomizationGenerator-8           1000          19825319 ns/op        11217971 B/op      90432 allocs/op
BenchmarkKustomizationGenerator-8           1000          20249546 ns/op        11207548 B/op      90431 allocs/op

name                      time/op
KustomizationGenerator-8  19.8ms ± 2%

name                      alloc/op
KustomizationGenerator-8  11.2MB ± 0%

name                      allocs/op
KustomizationGenerator-8   90.4k ± 0%

## new code using Unstructured objects
BenchmarkKustomizationGenerator-8           1000          19573560 ns/op        11237382 B/op      90454 allocs/op
BenchmarkKustomizationGenerator-8           1000          19637016 ns/op        11245801 B/op      90454 allocs/op
BenchmarkKustomizationGenerator-8           1000          19789540 ns/op        11245425 B/op      90454 allocs/op

name                      time/op
KustomizationGenerator-8  19.7ms ± 1%

name                      alloc/op
KustomizationGenerator-8  11.2MB ± 0%

name                      allocs/op
KustomizationGenerator-8   90.5k ± 0%

and the benchmark function

func BenchmarkKustomizationGenerator(b *testing.B) {
	g := NewWithT(b)

	f := func() {
		os.Remove("./testdata/generator/resources/kustomization.yaml")
	}

	b.Cleanup(f)

	// Create a kustomization file with varsub
	yamlKus, err := os.ReadFile("./testdata/generator/kustomization.yaml")
	g.Expect(err).NotTo(HaveOccurred())

	clientObjects, err := readYamlObjects(strings.NewReader(string(yamlKus)))
	g.Expect(err).NotTo(HaveOccurred())

	//Get a generator
	gen := NewGenerator(clientObjects[0])
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err = gen.WriteFile(resourcePath)
	}
	
        g.Expect(err).NotTo(HaveOccurred())
}

There does not seems to be any performance reduction.

Update:

the copyFile() call to save the kustomization.yaml take 9.95% of the total time (seen with pprof). This function call is not needed in kustomize-controller.

## new code using Unstructured objects without copyFile call
name                      time/op
KustomizationGenerator-8  19.4ms ± 0%

name                      alloc/op
KustomizationGenerator-8  11.2MB ± 0%

name                      allocs/op
KustomizationGenerator-8   90.4k ± 0%

@stefanprodan

stefanprodan commented Jan 9, 2022

Copy link
Copy Markdown
Member

@souleb I would've bet on the number of allocations to be way higher, that you for the benchmark. I feel better now about this change.

@souleb

souleb commented Jan 9, 2022

Copy link
Copy Markdown
Member Author

I will modify the code to use a functional option to save the kustomization.yaml, so we can avoid doing it in the controller.

Comment thread kustomization/doc.go Outdated
Comment thread kustomization/testdata/kustomization.yaml Outdated
Comment thread kustomization/testdata/kustomization_varsub.yaml Outdated
Comment thread kustomization/testdata/ns.yaml Outdated
@souleb
souleb force-pushed the kustomization-module branch from 46ff99f to 377d5a0 Compare January 10, 2022 16:58
@stefanprodan

Copy link
Copy Markdown
Member

@souleb please rename this package to kustomize to match the one in apis and also please remove the Kustomize prefix from the two types.

@souleb
souleb force-pushed the kustomization-module branch from 377d5a0 to eec9a90 Compare January 17, 2022 21:57
@souleb souleb changed the title Kustomization module for generation and var substitution Kustomize module for generation and var substitution Jan 17, 2022
@souleb
souleb force-pushed the kustomization-module branch 2 times, most recently from 8310c72 to 7ae071b Compare January 17, 2022 22:16

@stefanprodan stefanprodan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Thanks @souleb please rebase with main.

This module declare methods used in both kustomization-controller

and the flux cli

Signed-off-by: Soule BA <bah.soule@gmail.com>
@souleb
souleb force-pushed the kustomization-module branch from 7ae071b to 9c48995 Compare January 18, 2022 07:47
@stefanprodan
stefanprodan merged commit 11014c1 into fluxcd:main Jan 18, 2022
@stefanprodan

Copy link
Copy Markdown
Member

@souleb this is now released as github.com/fluxcd/pkg/kustomize@v0.0.1, you can use it in Flux CLI.

@souleb
souleb deleted the kustomization-module branch January 18, 2022 17:09
@stefanprodan stefanprodan added the area/kustomize Kustomize related issues and pull requests label Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/kustomize Kustomize related issues and pull requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants