Skip to content

Commit c448977

Browse files
blakepetterssonMangaal
authored andcommitted
fix: kustomize components + monorepos (argoproj#23486)
Signed-off-by: Blake Pettersson <[email protected]> Signed-off-by: Mangaal <[email protected]>
1 parent 2820757 commit c448977

File tree

9 files changed

+157
-5
lines changed

9 files changed

+157
-5
lines changed

util/kustomize/kustomize.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/Masterminds/semver/v3"
1616
"sigs.k8s.io/yaml"
1717

18+
"github.com/argoproj/argo-cd/v3/util/io"
19+
1820
"github.com/argoproj/gitops-engine/pkg/utils/kube"
1921
log "github.com/sirupsen/logrus"
2022
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -24,8 +26,6 @@ import (
2426
executil "github.com/argoproj/argo-cd/v3/util/exec"
2527
"github.com/argoproj/argo-cd/v3/util/git"
2628
"github.com/argoproj/argo-cd/v3/util/proxy"
27-
28-
securejoin "github.com/cyphar/filepath-securejoin"
2929
)
3030

3131
// Image represents a Docker image in the format NAME[:TAG].
@@ -346,12 +346,18 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
346346
foundComponents := opts.Components
347347
if opts.IgnoreMissingComponents {
348348
foundComponents = make([]string, 0)
349+
root, err := os.OpenRoot(k.repoRoot)
350+
defer io.Close(root)
351+
if err != nil {
352+
return nil, nil, nil, fmt.Errorf("failed to open the repo folder: %w", err)
353+
}
354+
349355
for _, c := range opts.Components {
350-
resolvedPath, err := securejoin.SecureJoin(k.path, c)
356+
resolvedPath, err := filepath.Rel(k.repoRoot, filepath.Join(k.path, c))
351357
if err != nil {
352-
return nil, nil, nil, fmt.Errorf("Kustomize components path failed: %w", err)
358+
return nil, nil, nil, fmt.Errorf("kustomize components path failed: %w", err)
353359
}
354-
_, err = os.Stat(resolvedPath)
360+
_, err = root.Stat(resolvedPath)
355361
if err != nil {
356362
log.Debugf("%s component directory does not exist", resolvedPath)
357363
continue

util/kustomize/kustomize_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
kustomization6 = "kustomization_yaml_components"
2626
kustomization7 = "label_without_selector"
2727
kustomization8 = "kustomization_yaml_patches_empty"
28+
kustomization9 = "kustomization_yaml_components_monorepo"
2829
)
2930

3031
func testDataDir(tb testing.TB, testData string) (string, error) {
@@ -512,6 +513,31 @@ func TestKustomizeBuildComponents(t *testing.T) {
512513
assert.Equal(t, int64(3), replicas)
513514
}
514515

516+
func TestKustomizeBuildComponentsMonoRepo(t *testing.T) {
517+
rootPath, err := testDataDir(t, kustomization9)
518+
require.NoError(t, err)
519+
appPath := path.Join(rootPath, "envs/inseng-pdx-egert-sandbox/namespaces/inst-system/apps/hello-world")
520+
kustomize := NewKustomizeApp(rootPath, appPath, git.NopCreds{}, "", "", "", "")
521+
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
522+
Components: []string{"../../../../../../kustomize/components/all"},
523+
IgnoreMissingComponents: true,
524+
}
525+
objs, _, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
526+
require.NoError(t, err)
527+
obj := objs[2]
528+
require.Equal(t, "hello-world-kustomize", obj.GetName())
529+
require.Equal(t, map[string]string{
530+
"app.kubernetes.io/name": "hello-world-kustomize",
531+
"app.kubernetes.io/owner": "fire-team",
532+
}, obj.GetLabels())
533+
replicas, ok, err := unstructured.NestedSlice(obj.Object, "spec", "template", "spec", "tolerations")
534+
require.NoError(t, err)
535+
require.True(t, ok)
536+
require.Len(t, replicas, 1)
537+
require.Equal(t, "my-special-toleration", replicas[0].(map[string]any)["key"])
538+
require.Equal(t, "Exists", replicas[0].(map[string]any)["operator"])
539+
}
540+
515541
func TestKustomizeBuildPatches(t *testing.T) {
516542
appPath, err := testDataDir(t, kustomization5)
517543
require.NoError(t, err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
kustomize:
3+
componentsPath: ../../../../../../kustomize/components
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
apiVersion: kustomize.config.k8s.io/v1beta1
3+
kind: Kustomization
4+
5+
# All the members of this group are meant be populated from the
6+
# same nonproduction overlay of the matching app
7+
resources:
8+
- ../../../../../../kustomize/apps/hello-world/base
9+
10+
nameSuffix: -kustomize
11+
12+
labels:
13+
- pairs:
14+
app.kubernetes.io/name: hello-world-kustomize
15+
includeSelectors: true
16+
includeTemplates: true
17+
18+
patches:
19+
# Adjusting the serviceAccount ref
20+
- patch: |-
21+
apiVersion: apps/v1
22+
kind: Deployment
23+
metadata:
24+
name: hello-world
25+
spec:
26+
template:
27+
spec:
28+
serviceAccountName: hello-world-kustomize
29+
30+
# Container image versions across the members
31+
images:
32+
- name: hello-world
33+
newTag: 1.17.0
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: hello-world
5+
labels:
6+
app.kubernetes.io/name: hello-world
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app.kubernetes.io/name: hello-world
12+
template:
13+
metadata:
14+
labels:
15+
app.kubernetes.io/name: hello-world
16+
spec:
17+
serviceAccountName: hello-world
18+
containers:
19+
- name: hello-world
20+
image: "nginx:1.16.0"
21+
imagePullPolicy: IfNotPresent
22+
ports:
23+
- name: http
24+
containerPort: 80
25+
protocol: TCP
26+
livenessProbe:
27+
httpGet:
28+
path: /
29+
port: http
30+
readinessProbe:
31+
httpGet:
32+
path: /
33+
port: http
34+
tolerations: []
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
apiVersion: kustomize.config.k8s.io/v1beta1
3+
kind: Kustomization
4+
5+
resources:
6+
- deployment.yaml
7+
- service.yaml
8+
- serviceaccount.yaml
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
name: hello-world
6+
labels:
7+
app.kubernetes.io/name: hello-world
8+
spec:
9+
type: ClusterIP
10+
ports:
11+
- port: 80
12+
targetPort: http
13+
protocol: TCP
14+
name: http
15+
selector:
16+
app.kubernetes.io/name: hello-world
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: hello-world
6+
labels:
7+
app.kubernetes.io/name: hello-world
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
apiVersion: kustomize.config.k8s.io/v1alpha1
3+
kind: Component
4+
5+
labels:
6+
- pairs:
7+
app.kubernetes.io/owner: fire-team
8+
includeSelectors: false
9+
includeTemplates: false
10+
11+
patches:
12+
- target:
13+
kind: Deployment
14+
patch: |-
15+
- op: add
16+
path: /spec/template/spec/tolerations/-
17+
value:
18+
key: my-special-toleration
19+
operator: Exists

0 commit comments

Comments
 (0)