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 9588771
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 28 deletions.
20 changes: 20 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,23 @@ 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

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
84 changes: 58 additions & 26 deletions lib/modules/manager/argocd/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,64 @@ 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);

return deps.length
? { deps: new Array<PackageDependency>().concat(...deps) }
: null;
}

export function processSingleSource(
source: ApplicationSource
): PackageDependency | null {
if (
!source ||
!is.nonEmptyString(source.repoURL) ||
Expand Down Expand Up @@ -66,28 +113,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;
export function processMultipleSources(
sources: Array<ApplicationSource>
): Array<PackageDependency | null> {
const deps: Array<PackageDependency | null> = [];
for (const source of sources) {
deps.push(processSingleSource(source));
}

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

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 9588771

Please sign in to comment.