diff --git a/api/v1alpha1/pattern_types.go b/api/v1alpha1/pattern_types.go index ac36d0f46..d752829f8 100644 --- a/api/v1alpha1/pattern_types.go +++ b/api/v1alpha1/pattern_types.go @@ -100,6 +100,10 @@ type GitConfig struct { // +operator-sdk:csv:customresourcedefinitions:type=spec,order=16 TargetRevision string `json:"targetRevision,omitempty"` + // Path within git repo where values files are located. Default: repository root + // +operator-sdk:csv:customresourcedefinitions:type=spec,order=17 + TargetPath string `json:"targetPath,omitempty"` + // Upstream git repo containing the pattern to deploy. Used when in-cluster fork to point to the upstream pattern repository. // Takes precedence over TargetRepo // +operator-sdk:csv:customresourcedefinitions:type=spec,order=14,xDescriptors={"urn:alm:descriptor:com.tectonic.ui:fieldDependency:gitSpec.inClusterGitServer:true"} diff --git a/bundle/manifests/gitops.hybrid-cloud-patterns.io_patterns.yaml b/bundle/manifests/gitops.hybrid-cloud-patterns.io_patterns.yaml index ac2ed3325..7a570d7ca 100644 --- a/bundle/manifests/gitops.hybrid-cloud-patterns.io_patterns.yaml +++ b/bundle/manifests/gitops.hybrid-cloud-patterns.io_patterns.yaml @@ -1,9 +1,9 @@ +--- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: controller-gen.kubebuilder.io/version: v0.16.4 - creationTimestamp: null name: patterns.gitops.hybrid-cloud-patterns.io spec: group: gitops.hybrid-cloud-patterns.io @@ -107,6 +107,10 @@ spec: description: (DEPRECATED) Branch, tag or commit in the upstream git repository. Does not support short-sha's. Default to HEAD type: string + targetPath: + description: 'Path within git repo where values files are located. + Default: repository root' + type: string targetRepo: description: Git repo containing the pattern to deploy. Must use https/http or, for ssh, git@server:foo/bar.git @@ -241,9 +245,3 @@ spec: storage: true subresources: status: {} -status: - acceptedNames: - kind: "" - plural: "" - conditions: null - storedVersions: null diff --git a/config/crd/bases/gitops.hybrid-cloud-patterns.io_patterns.yaml b/config/crd/bases/gitops.hybrid-cloud-patterns.io_patterns.yaml index 2af229e86..7a570d7ca 100644 --- a/config/crd/bases/gitops.hybrid-cloud-patterns.io_patterns.yaml +++ b/config/crd/bases/gitops.hybrid-cloud-patterns.io_patterns.yaml @@ -107,6 +107,10 @@ spec: description: (DEPRECATED) Branch, tag or commit in the upstream git repository. Does not support short-sha's. Default to HEAD type: string + targetPath: + description: 'Path within git repo where values files are located. + Default: repository root' + type: string targetRepo: description: Git repo containing the pattern to deploy. Must use https/http or, for ssh, git@server:foo/bar.git diff --git a/internal/controller/argo.go b/internal/controller/argo.go index a25fa0e20..9dfb7ad98 100644 --- a/internal/controller/argo.go +++ b/internal/controller/argo.go @@ -369,6 +369,10 @@ func newApplicationParameters(p *api.Pattern) []argoapi.HelmParameter { Name: "global.targetRevision", Value: p.Spec.GitConfig.TargetRevision, }, + { + Name: "global.targetPath", + Value: p.Spec.GitConfig.TargetPath, + }, { Name: "global.hubClusterDomain", Value: p.Status.AppClusterDomain, @@ -457,6 +461,14 @@ func convertArgoHelmParametersToMap(params []argoapi.HelmParameter) map[string]a } func newApplicationValueFiles(p *api.Pattern, prefix string) []string { + basePath := p.Spec.GitConfig.TargetPath + if basePath != "" { + if prefix != "" { + prefix = fmt.Sprintf("%s/%s", prefix, basePath) + } else { + prefix = basePath + } + } files := []string{ fmt.Sprintf("%s/values-global.yaml", prefix), fmt.Sprintf("%s/values-%s.yaml", prefix, p.Spec.ClusterGroupName), @@ -668,6 +680,7 @@ func newMultiSourceApplication(p *api.Pattern) *argoapi.Application { valuesSource := &argoapi.ApplicationSource{ RepoURL: p.Spec.GitConfig.TargetRepo, TargetRevision: p.Spec.GitConfig.TargetRevision, + Path: p.Spec.GitConfig.TargetPath, Ref: "patternref", } sources = append(sources, *valuesSource) diff --git a/internal/controller/argo_test.go b/internal/controller/argo_test.go index ff5a6089e..3d11645b3 100644 --- a/internal/controller/argo_test.go +++ b/internal/controller/argo_test.go @@ -187,6 +187,34 @@ var _ = Describe("Argo Pattern", func() { Expect(newMultiSourceApplication(pattern)).To(Equal(multiSourceArgoApp)) }) }) + Context("multiSource with targetPath set", func() { + It("Returns an argo application with path in values source", func() { + format.MaxDepth = 100 + format.MaxLength = 0 + pattern.Spec.GitConfig.TargetPath = "envs/dev" + tmpTrue := true + pattern.Spec.MultiSourceConfig.Enabled = &tmpTrue + multiSourceArgoApp = argoApp.DeepCopy() + multiSourceArgoApp.Spec.Source = nil + multiSourceArgoApp.Spec.Sources = []argoapi.ApplicationSource{ + { + RepoURL: pattern.Spec.GitConfig.TargetRepo, + TargetRevision: pattern.Spec.GitConfig.TargetRevision, + Path: "envs/dev", + Ref: "patternref", + }, + { + RepoURL: pattern.Spec.MultiSourceConfig.HelmRepoUrl, + Chart: "clustergroup", + Path: "", + TargetRevision: pattern.Spec.MultiSourceConfig.ClusterGroupChartVersion, + Helm: commonApplicationSourceHelm(pattern, "$patternref"), + }, + } + Expect(newMultiSourceApplication(pattern)).To(Equal(multiSourceArgoApp)) + pattern.Spec.GitConfig.TargetPath = "" + }) + }) }) Describe("Testing newApplicationValueFiles function", func() { @@ -242,6 +270,23 @@ var _ = Describe("Argo Pattern", func() { "myprefix/test2.yaml"))) }) }) + + Context("With targetPath set", func() { + BeforeEach(func() { + pattern.Spec.GitConfig.TargetPath = "envs/dev" + }) + AfterEach(func() { + pattern.Spec.GitConfig.TargetPath = "" + }) + It("Returns values with targetPath prefix", func() { + valueFiles := newApplicationValueFiles(pattern, "") + Expect(valueFiles).To(Equal(prefixArray(defaultValueFiles, "envs/dev"))) + }) + It("Returns values with combined prefix and targetPath", func() { + valueFiles := newApplicationValueFiles(pattern, "$patternref") + Expect(valueFiles).To(Equal(prefixArray(defaultValueFiles, "$patternref/envs/dev"))) + }) + }) }) Describe("Argo Helm Functions", func() { @@ -367,6 +412,10 @@ var _ = Describe("Argo Pattern", func() { Value: "main", ForceString: false, }, + { + Name: "global.targetPath", + Value: "", + }, { Name: "global.hubClusterDomain", Value: "apps.hub-cluster.validatedpatterns.io",