Skip to content

Commit c04af72

Browse files
committed
feat: kustomize flux resources
1 parent 2ddd64b commit c04af72

File tree

10 files changed

+166
-2
lines changed

10 files changed

+166
-2
lines changed

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ require (
1212
k8s.io/apimachinery v0.34.0
1313
k8s.io/utils v0.0.0-20250820121507-0af2bda4dd1d
1414
sigs.k8s.io/controller-runtime v0.22.0
15+
sigs.k8s.io/kustomize/api v0.20.1
16+
sigs.k8s.io/kustomize/kyaml v0.20.1
1517
sigs.k8s.io/yaml v1.6.0
1618
)
1719

@@ -20,6 +22,7 @@ require (
2022
github.com/Microsoft/go-winio v0.6.2 // indirect
2123
github.com/ProtonMail/go-crypto v1.1.6 // indirect
2224
github.com/beorn7/perks v1.0.1 // indirect
25+
github.com/blang/semver/v4 v4.0.0 // indirect
2326
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2427
github.com/cloudflare/circl v1.6.1 // indirect
2528
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
@@ -29,6 +32,7 @@ require (
2932
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
3033
github.com/fsnotify/fsnotify v1.9.0 // indirect
3134
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
35+
github.com/go-errors/errors v1.4.2 // indirect
3236
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
3337
github.com/go-git/go-billy/v5 v5.6.2 // indirect
3438
github.com/go-logr/logr v1.4.3 // indirect
@@ -49,6 +53,7 @@ require (
4953
github.com/mailru/easyjson v0.9.0 // indirect
5054
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5155
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
56+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
5257
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5358
github.com/pjbgf/sha1cd v0.3.2 // indirect
5459
github.com/pkg/errors v0.9.1 // indirect
@@ -62,6 +67,7 @@ require (
6267
github.com/spf13/pflag v1.0.9 // indirect
6368
github.com/x448/float16 v0.8.4 // indirect
6469
github.com/xanzy/ssh-agent v0.3.3 // indirect
70+
github.com/xlab/treeprint v1.2.0 // indirect
6571
go.yaml.in/yaml/v2 v2.4.2 // indirect
6672
go.yaml.in/yaml/v3 v3.0.4 // indirect
6773
golang.org/x/crypto v0.41.0 // indirect

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
1313
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
1414
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
1515
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
16+
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
17+
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
1618
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
1719
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
1820
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
@@ -40,6 +42,8 @@ github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sa
4042
github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
4143
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
4244
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
45+
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
46+
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
4347
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
4448
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
4549
github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UNbRM=
@@ -108,6 +112,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
108112
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
109113
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFdJifH4BDsTlE89Zl93FEloxaWZfGcifgq8=
110114
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
115+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0=
116+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
111117
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
112118
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
113119
github.com/onsi/ginkgo/v2 v2.25.2 h1:hepmgwx1D+llZleKQDMEvy8vIlCxMGt7W5ZxDjIEhsw=
@@ -158,6 +164,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
158164
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
159165
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
160166
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
167+
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
168+
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
161169
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
162170
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
163171
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
@@ -262,6 +270,10 @@ sigs.k8s.io/controller-runtime v0.22.0 h1:mTOfibb8Hxwpx3xEkR56i7xSjB+nH4hZG37Srl
262270
sigs.k8s.io/controller-runtime v0.22.0/go.mod h1:FwiwRjkRPbiN+zp2QRp7wlTCzbUXxZ/D4OzuQUDwBHY=
263271
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 h1:gBQPwqORJ8d8/YNZWEjoZs7npUVDpVXUUOFfW6CgAqE=
264272
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg=
273+
sigs.k8s.io/kustomize/api v0.20.1 h1:iWP1Ydh3/lmldBnH/S5RXgT98vWYMaTUL1ADcr+Sv7I=
274+
sigs.k8s.io/kustomize/api v0.20.1/go.mod h1:t6hUFxO+Ph0VxIk1sKp1WS0dOjbPCtLJ4p8aADLwqjM=
275+
sigs.k8s.io/kustomize/kyaml v0.20.1 h1:PCMnA2mrVbRP3NIB6v9kYCAc38uvFLVs8j/CD567A78=
276+
sigs.k8s.io/kustomize/kyaml v0.20.1/go.mod h1:0EmkQHRUsJxY8Ug9Niig1pUMSCGHxQ5RklbpV/Ri6po=
265277
sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU=
266278
sigs.k8s.io/randfill v1.0.0/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY=
267279
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 h1:jTijUJbW353oVOd9oTlifJqOGEkUw2jB/fXCbTiQEco=

internal/flux_deployer/deployer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (d *FluxDeployer) applyManifests(ctx context.Context, manifests []byte) err
196196

197197
// Parse manifests into unstructured objects
198198
reader := bytes.NewReader(manifests)
199-
unstructuredObjects, err := d.parseManifests(reader)
199+
unstructuredObjects, err := ParseManifests(reader)
200200
if err != nil {
201201
return fmt.Errorf("error parsing manifests: %w", err)
202202
}
@@ -211,7 +211,7 @@ func (d *FluxDeployer) applyManifests(ctx context.Context, manifests []byte) err
211211
return nil
212212
}
213213

214-
func (d *FluxDeployer) parseManifests(reader io.Reader) ([]*unstructured.Unstructured, error) {
214+
func ParseManifests(reader io.Reader) ([]*unstructured.Unstructured, error) {
215215
decoder := yaml.NewYAMLOrJSONDecoder(reader, 4096)
216216
var result []*unstructured.Unstructured
217217
for {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package flux_deployer
2+
3+
import (
4+
"fmt"
5+
6+
"sigs.k8s.io/kustomize/api/krusty"
7+
"sigs.k8s.io/kustomize/kyaml/filesys"
8+
)
9+
10+
// Kustomize runs kustomize on the given directory and returns the resulting yaml as a byte slice.
11+
func Kustomize(dir string) ([]byte, error) {
12+
fmt.Println("kustomize")
13+
14+
fs := filesys.MakeFsOnDisk()
15+
16+
opts := krusty.MakeDefaultOptions()
17+
kustomizer := krusty.MakeKustomizer(opts)
18+
resourceMap, err := kustomizer.Run(fs, dir)
19+
if err != nil {
20+
return nil, fmt.Errorf("error running kustomization: %w", err)
21+
}
22+
23+
resourcesYaml, err := resourceMap.AsYaml()
24+
if err != nil {
25+
return nil, fmt.Errorf("error converting resources to yaml: %w", err)
26+
}
27+
28+
return resourcesYaml, nil
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package flux_deployer_test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9+
10+
"github.com/openmcp-project/bootstrapper/internal/flux_deployer"
11+
)
12+
13+
func TestKustomize(t *testing.T) {
14+
resourcesYaml, err := flux_deployer.Kustomize("./testdata/02/dev/fluxcd")
15+
assert.NoError(t, err, "Error running kustomization")
16+
assert.NotNil(t, resourcesYaml, "Resources yaml should not be nil")
17+
18+
resources, err := flux_deployer.ParseManifests(bytes.NewReader(resourcesYaml))
19+
assert.NoError(t, err, "Error parsing manifests")
20+
assert.Len(t, resources, 3, "There should be 3 resources")
21+
22+
deploy := getResource(resources, "Deployment", "source-controller", "flux-system")
23+
assert.NotNil(t, deploy, "Deployment should be present")
24+
25+
repo := getResource(resources, "GitRepository", "environments", "flux-system")
26+
assert.NotNil(t, repo, "GitRepository should be present")
27+
branch, found, err := unstructured.NestedString(repo.Object, "spec", "ref", "branch")
28+
assert.NoError(t, err, "Error getting GitRepository branch")
29+
assert.True(t, found, "GitRepository branch should be found")
30+
assert.Equal(t, "dev", branch, "GitRepository branch should have the expected value")
31+
32+
kustomization := getResource(resources, "Kustomization", "flux-system", "flux-system")
33+
assert.NotNil(t, kustomization, "Kustomization should be present")
34+
path, found, err := unstructured.NestedString(kustomization.Object, "spec", "path")
35+
assert.NoError(t, err, "Error getting Kustomization path")
36+
assert.True(t, found, "Kustomization path should be found")
37+
assert.Equal(t, "dev/fluxcd", path, "Kustomization path should have the expected value")
38+
}
39+
40+
func getResource(resources []*unstructured.Unstructured, kind, name, namespace string) *unstructured.Unstructured {
41+
for _, res := range resources {
42+
if res.GetKind() == kind && res.GetName() == name && res.GetNamespace() == namespace {
43+
return res
44+
}
45+
}
46+
return nil
47+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- ../../resources/fluxcd
5+
patches:
6+
- path: sync.yaml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
apiVersion: source.toolkit.fluxcd.io/v1
3+
kind: GitRepository
4+
metadata:
5+
name: environments
6+
namespace: flux-system
7+
spec:
8+
ref:
9+
branch: dev
10+
---
11+
apiVersion: kustomize.toolkit.fluxcd.io/v1
12+
kind: Kustomization
13+
metadata:
14+
name: flux-system
15+
namespace: flux-system
16+
spec:
17+
path: dev/fluxcd
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: source-controller
6+
namespace: flux-system
7+
spec:
8+
template:
9+
spec:
10+
containers:
11+
- image: ghcr.io/fluxcd/source-controller:v0.1.0
12+
name: manager
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- components.yaml
5+
- sync.yaml
6+
images:
7+
- name: ghcr.io/fluxcd/source-controller
8+
newName: ghcr.io/fluxcd/source-controller-new
9+
newTag: v0.2.0
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
apiVersion: source.toolkit.fluxcd.io/v1
3+
kind: GitRepository
4+
metadata:
5+
name: environments
6+
namespace: flux-system
7+
spec:
8+
interval: 5m
9+
url: environments.git
10+
ref:
11+
branch: KUSTOMIZABLE
12+
secretRef:
13+
name: git
14+
---
15+
apiVersion: kustomize.toolkit.fluxcd.io/v1
16+
kind: Kustomization
17+
metadata:
18+
name: flux-system
19+
namespace: flux-system
20+
spec:
21+
interval: 5m
22+
path: KUSTOMIZABLE
23+
prune: true
24+
sourceRef:
25+
kind: GitRepository
26+
name: environments

0 commit comments

Comments
 (0)