Skip to content

Commit 4d6e6be

Browse files
authored
Simplify some classes (#1760)
Simplify some classes
1 parent dd65615 commit 4d6e6be

File tree

2 files changed

+109
-148
lines changed

2 files changed

+109
-148
lines changed

source/Calamari/ArgoCD/Conventions/UpdateArgoCDAppImagesInstallConvention.cs

Lines changed: 63 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,23 @@ ProcessApplicationResult ProcessApplication(ArgoCDApplicationDto application,
110110

111111
result.MatchingSourceCount = applicationFromYaml.Spec.Sources.Count(s => deploymentScope.Matches(ScopingAnnotationReader.GetScopeForApplicationSource(s.Name.ToApplicationSourceName(), applicationFromYaml.Metadata.Annotations, containsMultipleSources)));
112112

113-
var sourceResults = applicationFromYaml.GetSourcesWithMetadata()
114-
.Select(applicationSource =>
115-
ProcessSource(applicationSource,
116-
applicationFromYaml,
117-
containsMultipleSources,
118-
deploymentScope,
119-
gitCredentials,
120-
repositoryFactory,
121-
deploymentConfig,
122-
application.DefaultRegistry))
123-
.ToList();
113+
var updatedSourcesResults = applicationFromYaml.GetSourcesWithMetadata()
114+
.Select(applicationSource => new
115+
{
116+
Updated = ProcessSource(applicationSource,
117+
applicationFromYaml,
118+
containsMultipleSources,
119+
deploymentScope,
120+
gitCredentials,
121+
repositoryFactory,
122+
deploymentConfig,
123+
application.DefaultRegistry),
124+
applicationSource,
125+
}).Where(r => r.Updated.Any()).ToList();
124126

125-
var updatedSourcesResults = sourceResults.Where(r => r.Updated).ToList();
126127
result.UpdatedSourceCount = updatedSourcesResults.Count;
127-
result.GitReposUpdated.AddRange(updatedSourcesResults.Select(r => r.RepositoryUrl.AbsoluteUri));
128-
result.UpdatedImages.AddRange(updatedSourcesResults.SelectMany(r => r.UpdatedImages));
128+
result.GitReposUpdated.AddRange(updatedSourcesResults.Select(r => r.applicationSource.Source.RepoUrl.AbsoluteUri));
129+
result.UpdatedImages.AddRange(updatedSourcesResults.SelectMany(r => r.Updated));
129130

130131
//if we have links, use that to generate a link, otherwise just put the name there
131132
var instanceLinks = application.InstanceWebUiUrl != null ? new ArgoCDInstanceLinks(application.InstanceWebUiUrl) : null;
@@ -142,7 +143,8 @@ ProcessApplicationResult ProcessApplication(ArgoCDApplicationDto application,
142143
return result;
143144
}
144145

145-
ProcessApplicationSourceResult ProcessSource(ApplicationSourceWithMetadata sourceWithMetadata,
146+
/// <returns>Images that were updated</returns>
147+
HashSet<string> ProcessSource(ApplicationSourceWithMetadata sourceWithMetadata,
146148
Application applicationFromYaml,
147149
bool containsMultipleSources,
148150
DeploymentScope deploymentScope,
@@ -151,14 +153,13 @@ ProcessApplicationSourceResult ProcessSource(ApplicationSourceWithMetadata sourc
151153
UpdateArgoCDAppDeploymentConfig deploymentConfig,
152154
string defaultRegistry)
153155
{
154-
var result = new ProcessApplicationSourceResult(sourceWithMetadata.Source.RepoUrl);
155156

156157
var applicationSource = sourceWithMetadata.Source;
157158
var annotatedScope = ScopingAnnotationReader.GetScopeForApplicationSource(applicationSource.Name.ToApplicationSourceName(), applicationFromYaml.Metadata.Annotations, containsMultipleSources);
158159

159160
log.LogApplicationSourceScopeStatus(annotatedScope, applicationSource.Name.ToApplicationSourceName(), deploymentScope);
160161
if (!deploymentScope.Matches(annotatedScope))
161-
return result;
162+
return new HashSet<string>();
162163

163164
switch (sourceWithMetadata.SourceType)
164165
{
@@ -197,26 +198,26 @@ ProcessApplicationSourceResult ProcessSource(ApplicationSourceWithMetadata sourc
197198
case SourceType.Plugin:
198199
{
199200
log.WarnFormat("Unable to update source '{0}' as Plugin sources aren't currently supported.", sourceWithMetadata.SourceIdentity);
200-
return result;
201+
return new HashSet<string>();
201202
}
202203
default:
203204
throw new ArgumentOutOfRangeException();
204205
}
205206
}
206207

207-
ProcessApplicationSourceResult ProcessKustomize(Dictionary<string, GitCredentialDto> gitCredentials,
208+
/// <returns>Images that were updated</returns>
209+
HashSet<string> ProcessKustomize(Dictionary<string, GitCredentialDto> gitCredentials,
208210
RepositoryFactory repositoryFactory,
209211
UpdateArgoCDAppDeploymentConfig deploymentConfig,
210212
ApplicationSourceWithMetadata sourceWithMetadata,
211213
string defaultRegistry)
212214
{
213215
var applicationSource = sourceWithMetadata.Source;
214216

215-
ProcessApplicationSourceResult result = new ProcessApplicationSourceResult(applicationSource.RepoUrl);
216217
if (applicationSource.Path == null)
217218
{
218219
log.WarnFormat("Unable to update source '{0}' as a path has not been specified.", sourceWithMetadata.SourceIdentity);
219-
return result;
220+
return new HashSet<string>();
220221
}
221222

222223
using (var repository = CreateRepository(gitCredentials, applicationSource, repositoryFactory))
@@ -234,24 +235,24 @@ ProcessApplicationSourceResult ProcessKustomize(Dictionary<string, GitCredential
234235

235236
if (didPush)
236237
{
237-
result.UpdatedImages = updatedImages;
238+
return updatedImages;
238239
}
239240
}
240241
}
241242

242-
return result;
243+
return new HashSet<string>();
243244
}
244245

245-
ProcessApplicationSourceResult ProcessRef(Application applicationFromYaml,
246+
/// <returns>Images that were updated</returns>
247+
HashSet<string> ProcessRef(Application applicationFromYaml,
246248
Dictionary<string, GitCredentialDto> gitCredentials,
247249
RepositoryFactory repositoryFactory,
248250
UpdateArgoCDAppDeploymentConfig deploymentConfig,
249251
ApplicationSourceWithMetadata sourceWithMetadata,
250252
string defaultRegistry)
251253
{
252254
var applicationSource = sourceWithMetadata.Source;
253-
254-
ProcessApplicationSourceResult result = new ProcessApplicationSourceResult(applicationSource.RepoUrl);
255+
255256
if (applicationSource.Path != null)
256257
{
257258
log.WarnFormat("The source '{0}' contains a Ref, only referenced files will be updated. Please create another source with the same URL if you wish to update files under the path.", sourceWithMetadata.SourceIdentity);
@@ -262,32 +263,27 @@ ProcessApplicationSourceResult ProcessRef(Application applicationFromYaml,
262263

263264
LogHelmSourceConfigurationProblems(helmTargetsForRefSource.Problems);
264265

265-
using (var repository = CreateRepository(gitCredentials, applicationSource, repositoryFactory))
266-
{
267-
var updatedImages = ProcessHelmUpdateTargets(repository,
268-
deploymentConfig,
269-
applicationSource,
270-
helmTargetsForRefSource.Targets);
271-
272-
result.UpdatedImages.AddRange(updatedImages);
273-
}
266+
using var repository = CreateRepository(gitCredentials, applicationSource, repositoryFactory);
267+
var updatedImages = ProcessHelmUpdateTargets(repository,
268+
deploymentConfig,
269+
applicationSource,
270+
helmTargetsForRefSource.Targets);
274271

275-
return result;
272+
return updatedImages;
276273
}
277-
278-
ProcessApplicationSourceResult ProcessDirectory(Dictionary<string, GitCredentialDto> gitCredentials,
274+
275+
/// <returns>Images that were updated</returns>
276+
HashSet<string> ProcessDirectory(Dictionary<string, GitCredentialDto> gitCredentials,
279277
RepositoryFactory repositoryFactory,
280278
UpdateArgoCDAppDeploymentConfig deploymentConfig,
281279
ApplicationSourceWithMetadata sourceWithMetadata,
282280
string defaultRegistry)
283281
{
284282
var applicationSource = sourceWithMetadata.Source;
285-
ProcessApplicationSourceResult result = new ProcessApplicationSourceResult(applicationSource.RepoUrl);
286-
287283
if (applicationSource.Path == null)
288284
{
289285
log.WarnFormat("Unable to update source '{0}' as a path has not been specified.", sourceWithMetadata.SourceIdentity);
290-
return result;
286+
return new HashSet<string>();
291287
}
292288

293289
using (var repository = CreateRepository(gitCredentials, applicationSource, repositoryFactory))
@@ -303,32 +299,26 @@ ProcessApplicationSourceResult ProcessDirectory(Dictionary<string, GitCredential
303299
updatedFiles,
304300
updatedImages);
305301

306-
if (didPush)
307-
{
308-
result.UpdatedImages.AddRange(updatedImages);
309-
}
310-
311-
return result;
302+
return didPush ? updatedImages : new HashSet<string>();
312303
}
313304
}
314-
315-
return result;
305+
return new HashSet<string>();
316306
}
317307

318-
ProcessApplicationSourceResult ProcessHelm(Application applicationFromYaml,
308+
/// <returns>Images that were updated</returns>
309+
HashSet<string> ProcessHelm(Application applicationFromYaml,
319310
ApplicationSourceWithMetadata sourceWithMetadata,
320311
Dictionary<string, GitCredentialDto> gitCredentials,
321312
RepositoryFactory repositoryFactory,
322313
UpdateArgoCDAppDeploymentConfig deploymentConfig,
323314
string defaultRegistry)
324315
{
325316
var applicationSource = sourceWithMetadata.Source;
326-
ProcessApplicationSourceResult result = new ProcessApplicationSourceResult(applicationSource.RepoUrl);
327-
317+
328318
if (applicationSource.Path == null)
329319
{
330320
log.WarnFormat("Unable to update source '{0}' as a path has not been specified.", sourceWithMetadata.SourceIdentity);
331-
return result;
321+
return new HashSet<string>();
332322
}
333323

334324
var explicitHelmSources = new HelmValuesFileUpdateTargetParser(applicationFromYaml, defaultRegistry)
@@ -338,34 +328,31 @@ ProcessApplicationSourceResult ProcessHelm(Application applicationFromYaml,
338328
var valueFileProblems = new HashSet<HelmSourceConfigurationProblem>(explicitHelmSources.Problems);
339329

340330
//Add the implicit value file if needed
341-
using (var repository = CreateRepository(gitCredentials, applicationSource, repositoryFactory))
331+
using var repository = CreateRepository(gitCredentials, applicationSource, repositoryFactory);
332+
var repoSubPath = Path.Combine(repository.WorkingDirectory, applicationSource.Path!);
333+
var implicitValuesFile = HelmDiscovery.TryFindValuesFile(fileSystem, repoSubPath);
334+
if (implicitValuesFile != null && explicitHelmSources.Targets.None(t => t.FileName == implicitValuesFile))
342335
{
343-
var repoSubPath = Path.Combine(repository.WorkingDirectory, applicationSource.Path!);
344-
var implicitValuesFile = HelmDiscovery.TryFindValuesFile(fileSystem, repoSubPath);
345-
if (implicitValuesFile != null && explicitHelmSources.Targets.None(t => t.FileName == implicitValuesFile))
346-
{
347-
var (target, problem) = AddImplicitValuesFile(applicationFromYaml,
348-
sourceWithMetadata,
349-
implicitValuesFile,
350-
defaultRegistry);
351-
if (target != null)
352-
valuesFilesToUpdate.Add(target);
353-
354-
if (problem != null)
355-
valueFileProblems.Add(problem);
356-
}
357-
358-
LogHelmSourceConfigurationProblems(valueFileProblems);
359-
360-
result.UpdatedImages.AddRange(ProcessHelmUpdateTargets(repository,
361-
deploymentConfig,
362-
applicationSource,
363-
valuesFilesToUpdate));
336+
var (target, problem) = AddImplicitValuesFile(applicationFromYaml,
337+
sourceWithMetadata,
338+
implicitValuesFile,
339+
defaultRegistry);
340+
if (target != null)
341+
valuesFilesToUpdate.Add(target);
342+
343+
if (problem != null)
344+
valueFileProblems.Add(problem);
364345
}
365346

366-
return result;
347+
LogHelmSourceConfigurationProblems(valueFileProblems);
348+
349+
return ProcessHelmUpdateTargets(repository,
350+
deploymentConfig,
351+
applicationSource,
352+
valuesFilesToUpdate);
367353
}
368354

355+
/// <returns>Images that were updated</returns>
369356
HashSet<string> ProcessHelmUpdateTargets(RepositoryWrapper repository,
370357
UpdateArgoCDAppDeploymentConfig deploymentConfig,
371358
ApplicationSource source,
@@ -598,19 +585,6 @@ IEnumerable<string> FindYamlFiles(string rootPath)
598585
return fileSystem.EnumerateFilesWithGlob(rootPath, yamlFileGlob);
599586
}
600587

601-
class ProcessApplicationSourceResult
602-
{
603-
public Uri RepositoryUrl { get; }
604-
605-
public ProcessApplicationSourceResult(Uri repositoryUrl)
606-
{
607-
RepositoryUrl = repositoryUrl;
608-
}
609-
610-
public bool Updated => UpdatedImages.Any();
611-
public HashSet<string> UpdatedImages { get; set; } = new HashSet<string>();
612-
}
613-
614588
class ProcessApplicationResult
615589
{
616590
public ProcessApplicationResult(ApplicationName applicationName)

0 commit comments

Comments
 (0)