Skip to content

Commit

Permalink
feat: added support for argocd multisource
Browse files Browse the repository at this point in the history
  • Loading branch information
piximos committed Feb 27, 2023
1 parent 9a5a770 commit db42083
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 28 deletions.
36 changes: 36 additions & 0 deletions lib/modules/manager/argocd/__fixtures__/validApplication.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,39 @@ spec:
chart: some/image3
repoURL: somecontainer.registry.io:443/
targetRevision: 1.0.0
---
apiVersion: argoproj.io/v1alpha1
kind: Application
spec:
sources:
- chart: some/image3
repoURL: somecontainer.registry.io:443/
targetRevision: 1.0.0
---
apiVersion: argoproj.io/v1alpha1
kind: Application
spec:
sources:
- ref: foo
repoURL: https://git.example.com/foo/bar.git
targetRevision: v1.2.0
- chart: some/image3
repoURL: somecontainer.registry.io:443/
targetRevision: 1.0.0

---
apiVersion: argoproj.io/v1alpha1
kind: Application
spec:
sources:
- ref: foo
repoURL: https://git.example.com/foo/bar.git
targetRevision: v1.2.0
path: bar
- chart: traefik
repoURL: gs://helm-charts-internal
targetRevision: 0.0.2
helm:
valueFiles:
- $foo/values.yaml

43 changes: 43 additions & 0 deletions lib/modules/manager/argocd/__fixtures__/validApplicationSet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,46 @@ spec:
destination:
name: '{{server}}'
namespace: podinfo
---
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: podinfo
spec:
generators:
- clusters: {}
template:
metadata:
name: '{{name}}-podinfo'
spec:
project: default
sources:
- chart: some/image3
repoURL: somecontainer.registry.io:443/
targetRevision: 1.0.0
destination:
name: '{{server}}'
namespace: podinfo
---
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: podinfo
spec:
generators:
- clusters: {}
template:
metadata:
name: '{{name}}-podinfo'
spec:
project: default
sources:
- ref: foo
repoURL: https://git.example.com/foo/bar.git
targetRevision: v1.2.0
- chart: some/image3
repoURL: somecontainer.registry.io:443/
targetRevision: 1.0.0
destination:
name: '{{server}}'
namespace: podinfo
41 changes: 41 additions & 0 deletions lib/modules/manager/argocd/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ describe('modules/manager/argocd/extract', () => {
datasource: 'docker',
depName: 'somecontainer.registry.io:443/some/image3',
},
{
currentValue: '1.0.0',
datasource: 'docker',
depName: 'somecontainer.registry.io:443/some/image3',
},
{
currentValue: 'v1.2.0',
datasource: 'git-tags',
depName: 'https://git.example.com/foo/bar.git',
},
{
currentValue: '1.0.0',
datasource: 'docker',
depName: 'somecontainer.registry.io:443/some/image3',
},
{
currentValue: 'v1.2.0',
datasource: 'git-tags',
depName: 'https://git.example.com/foo/bar.git',
},
{
currentValue: '0.0.2',
datasource: 'helm',
depName: 'traefik',
registryUrls: ['gs://helm-charts-internal'],
},
],
});
});
Expand Down Expand Up @@ -110,6 +136,21 @@ describe('modules/manager/argocd/extract', () => {
depName: 'podinfo',
registryUrls: ['https://stefanprodan.github.io/podinfo'],
},
{
currentValue: '1.0.0',
datasource: 'docker',
depName: 'somecontainer.registry.io:443/some/image3',
},
{
currentValue: 'v1.2.0',
datasource: 'git-tags',
depName: 'https://git.example.com/foo/bar.git',
},
{
currentValue: '1.0.0',
datasource: 'docker',
depName: 'somecontainer.registry.io:443/some/image3',
},
],
});
});
Expand Down
86 changes: 60 additions & 26 deletions lib/modules/manager/argocd/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,66 @@ import { fileTestRegex } from './util';

function createDependency(
definition: ApplicationDefinition
): PackageDependency | null {
let source: ApplicationSource;
): Array<PackageDependency | null> {
switch (definition.kind) {
case 'Application':
source = definition?.spec?.source;
if (definition?.spec?.source) {
return processMultipleSources([definition?.spec?.source]);
}
if (definition?.spec?.sources) {
return processMultipleSources(definition?.spec?.sources);
}
break;
case 'ApplicationSet':
source = definition?.spec?.template?.spec?.source;
if (definition?.spec?.template?.spec?.source) {
return processMultipleSources([
definition?.spec?.template?.spec?.source,
]);
}
if (definition?.spec?.template?.spec?.sources) {
return processMultipleSources(
definition?.spec?.template?.spec?.sources
);
}
break;
}

return [];
}

export function extractPackageFile(
content: string,
fileName: string,
_config?: ExtractConfig
): PackageFileContent | null {
// check for argo reference. API version for the kind attribute is used
if (fileTestRegex.test(content) === false) {
return null;
}

let definitions: ApplicationDefinition[];
try {
definitions = loadAll(content) as ApplicationDefinition[];
} catch (err) {
logger.debug({ err, fileName }, 'Failed to parse ArgoCD definition.');
return null;
}

const deps: Array<Array<PackageDependency>> = definitions
.filter(is.plainObject)
.map((definition) => createDependency(definition).filter(is.truthy))
.filter(is.truthy);

const output = new Array<PackageDependency>()
.concat(...deps)
.filter(is.truthy);

return output.length ? { deps: output } : null;
}

export function processSingleSource(
source: ApplicationSource
): PackageDependency | null {
if (
!source ||
!is.nonEmptyString(source.repoURL) ||
Expand Down Expand Up @@ -66,28 +115,13 @@ function createDependency(
};
}

export function extractPackageFile(
content: string,
fileName: string,
_config?: ExtractConfig
): PackageFileContent | null {
// check for argo reference. API version for the kind attribute is used
if (fileTestRegex.test(content) === false) {
return null;
}

let definitions: ApplicationDefinition[];
try {
definitions = loadAll(content) as ApplicationDefinition[];
} catch (err) {
logger.debug({ err, fileName }, 'Failed to parse ArgoCD definition.');
return null;
export function processMultipleSources(
sources: Array<ApplicationSource>
): Array<PackageDependency | null> {
const deps: Array<PackageDependency | null> = [];
for (const source of sources) {
deps.push(processSingleSource(source));
}

const deps = definitions
.filter(is.plainObject)
.map((definition) => createDependency(definition))
.filter(is.truthy);

return deps.length ? { deps } : null;
return deps;
}
7 changes: 5 additions & 2 deletions lib/modules/manager/argocd/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export interface ApplicationSource {
chart?: string;
repoURL: string;
targetRevision: string;
ref?: string;
}

export interface Application extends KubernetesResource {
kind: 'Application';
spec: {
source: ApplicationSource;
source?: ApplicationSource;
sources?: Array<ApplicationSource>;
};
}

Expand All @@ -20,7 +22,8 @@ export interface ApplicationSet extends KubernetesResource {
spec: {
template: {
spec: {
source: ApplicationSource;
source?: ApplicationSource;
sources?: Array<ApplicationSource>;
};
};
};
Expand Down

0 comments on commit db42083

Please sign in to comment.