Add a recommendation for pipeline catalogs

This commit is contained in:
Lennart Kats 2024-12-20 09:39:30 +01:00
parent 65f10d187d
commit e0b6faddbe
No known key found for this signature in database
GPG Key ID: 1EB8B57673197023
2 changed files with 39 additions and 2 deletions

View File

@ -65,13 +65,24 @@ func (m *applyPresetsCatalogSchema) Apply(ctx context.Context, b *bundle.Bundle)
}
// Pipelines
for _, pl := range r.Pipelines {
allSameCatalog := allPipelinesSameCatalog(&r)
for key, pl := range r.Pipelines {
if pl.PipelineSpec == nil {
continue
}
if pl.Catalog == "" {
pl.Catalog = p.Catalog
}
if allSameCatalog && pl.Catalog == p.Catalog {
// Just for the common case where all pipelines have the same catalog,
// we show a recommendation to leave it out and rely on presets.
// This can happen when using the original default template.
diags = diags.Extend(diag.Diagnostics{{
Summary: "Omit the catalog field since it will be automatically populated from presets.catalog",
Severity: diag.Recommendation,
Locations: b.Config.GetLocations("resources.pipelines." + key + ".catalog"),
}})
}
if pl.GatewayDefinition != nil {
if pl.GatewayDefinition.GatewayStorageCatalog == "" {
pl.GatewayDefinition.GatewayStorageCatalog = p.Catalog
@ -347,3 +358,19 @@ func fileIncludesPattern(ctx context.Context, filePath string, expected string)
}
return matched
}
func allPipelinesSameCatalog(r *config.Resources) bool {
var firstCatalog string
for _, pl := range r.Pipelines {
if pl.PipelineSpec == nil || pl.PipelineSpec.Catalog == "" {
return false
}
if firstCatalog == "" {
firstCatalog = pl.PipelineSpec.Catalog
} else if pl.PipelineSpec.Catalog != firstCatalog {
return false
}
}
return firstCatalog != ""
}

View File

@ -179,7 +179,7 @@ func TestApplyPresetsCatalogSchemaWhenAlreadySet(t *testing.T) {
b := mockPresetsCatalogSchema()
recordedFields := recordPlaceholderFields(t, b)
diags := bundle.Apply(context.Background(), b, mutator.ApplyPresets())
diags := bundle.Apply(context.Background(), b, mutator.ApplyPresetsCatalogSchema())
require.NoError(t, diags.Error())
for _, f := range recordedFields {
@ -190,6 +190,16 @@ func TestApplyPresetsCatalogSchemaWhenAlreadySet(t *testing.T) {
}
}
func TestApplyPresetsCatalogSchemaRecommmendRemovingCatalog(t *testing.T) {
b := mockPresetsCatalogSchema()
b.Config.Resources.Jobs["key"].Parameters = nil // avoid warnings about the job parameters
b.Config.Resources.Pipelines["key"].Catalog = "my_catalog"
diags := bundle.Apply(context.Background(), b, mutator.ApplyPresetsCatalogSchema())
require.Equal(t, 1, len(diags))
require.Equal(t, "Omit the catalog field since it will be automatically populated from presets.catalog", diags[0].Summary)
}
func TestApplyPresetsCatalogSchemaWhenNotSet(t *testing.T) {
b := mockPresetsCatalogSchema()
recordedFields := recordPlaceholderFields(t, b)