Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions util/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/Masterminds/semver/v3"
"sigs.k8s.io/yaml"

"github.com/argoproj/argo-cd/v3/util/io"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -24,8 +26,6 @@ import (
executil "github.com/argoproj/argo-cd/v3/util/exec"
"github.com/argoproj/argo-cd/v3/util/git"
"github.com/argoproj/argo-cd/v3/util/proxy"

securejoin "github.com/cyphar/filepath-securejoin"
)

// Image represents a Docker image in the format NAME[:TAG].
Expand Down Expand Up @@ -346,12 +346,18 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
foundComponents := opts.Components
if opts.IgnoreMissingComponents {
foundComponents = make([]string, 0)
root, err := os.OpenRoot(k.repoRoot)
defer io.Close(root)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to open the repo folder: %w", err)
}

for _, c := range opts.Components {
resolvedPath, err := securejoin.SecureJoin(k.path, c)
resolvedPath, err := filepath.Rel(k.repoRoot, filepath.Join(k.path, c))
if err != nil {
return nil, nil, nil, fmt.Errorf("Kustomize components path failed: %w", err)
return nil, nil, nil, fmt.Errorf("kustomize components path failed: %w", err)
}
_, err = os.Stat(resolvedPath)
_, err = root.Stat(resolvedPath)
if err != nil {
log.Debugf("%s component directory does not exist", resolvedPath)
continue
Expand Down
26 changes: 26 additions & 0 deletions util/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
kustomization6 = "kustomization_yaml_components"
kustomization7 = "label_without_selector"
kustomization8 = "kustomization_yaml_patches_empty"
kustomization9 = "kustomization_yaml_components_monorepo"
)

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

func TestKustomizeBuildComponentsMonoRepo(t *testing.T) {
rootPath, err := testDataDir(t, kustomization9)
require.NoError(t, err)
appPath := path.Join(rootPath, "envs/inseng-pdx-egert-sandbox/namespaces/inst-system/apps/hello-world")
kustomize := NewKustomizeApp(rootPath, appPath, git.NopCreds{}, "", "", "", "")
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
Components: []string{"../../../../../../kustomize/components/all"},
IgnoreMissingComponents: true,
}
objs, _, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
require.NoError(t, err)
obj := objs[2]
require.Equal(t, "hello-world-kustomize", obj.GetName())
require.Equal(t, map[string]string{
"app.kubernetes.io/name": "hello-world-kustomize",
"app.kubernetes.io/owner": "fire-team",
}, obj.GetLabels())
replicas, ok, err := unstructured.NestedSlice(obj.Object, "spec", "template", "spec", "tolerations")
require.NoError(t, err)
require.True(t, ok)
require.Len(t, replicas, 1)
require.Equal(t, "my-special-toleration", replicas[0].(map[string]any)["key"])
require.Equal(t, "Exists", replicas[0].(map[string]any)["operator"])
}

func TestKustomizeBuildPatches(t *testing.T) {
appPath, err := testDataDir(t, kustomization5)
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
kustomize:
componentsPath: ../../../../../../kustomize/components
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

# All the members of this group are meant be populated from the
# same nonproduction overlay of the matching app
resources:
- ../../../../../../kustomize/apps/hello-world/base

nameSuffix: -kustomize

labels:
- pairs:
app.kubernetes.io/name: hello-world-kustomize
includeSelectors: true
includeTemplates: true

patches:
# Adjusting the serviceAccount ref
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
template:
spec:
serviceAccountName: hello-world-kustomize
# Container image versions across the members
images:
- name: hello-world
newTag: 1.17.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
labels:
app.kubernetes.io/name: hello-world
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: hello-world
template:
metadata:
labels:
app.kubernetes.io/name: hello-world
spec:
serviceAccountName: hello-world
containers:
- name: hello-world
image: "nginx:1.16.0"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
tolerations: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- deployment.yaml
- service.yaml
- serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: v1
kind: Service
metadata:
name: hello-world
labels:
app.kubernetes.io/name: hello-world
spec:
type: ClusterIP
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
app.kubernetes.io/name: hello-world
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: hello-world
labels:
app.kubernetes.io/name: hello-world
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component

labels:
- pairs:
app.kubernetes.io/owner: fire-team
includeSelectors: false
includeTemplates: false

patches:
- target:
kind: Deployment
patch: |-
- op: add
path: /spec/template/spec/tolerations/-
value:
key: my-special-toleration
operator: Exists
Loading