From a5607c625c11134af117467bbb90c46d23bac559 Mon Sep 17 00:00:00 2001 From: Brandon Foley Date: Thu, 21 Nov 2024 13:13:20 -0500 Subject: [PATCH] Add function to get valid template versions (#430) --- pkg/config/draftconfig.go | 19 ++++++++++--------- pkg/config/draftconfig_template_test.go | 6 ++++-- pkg/config/draftconfig_test.go | 12 ++++++------ pkg/handlers/template_utils.go | 12 ++++-------- .../addons/azure/webapp_routing/draft.yaml | 2 +- template/azurePipelines/kustomize/draft.yaml | 2 +- template/azurePipelines/manifests/draft.yaml | 2 +- template/deployments/helm/draft.yaml | 2 +- template/deployments/kustomize/draft.yaml | 2 +- template/deployments/manifests/draft.yaml | 2 +- template/dockerfiles/clojure/draft.yaml | 2 +- template/dockerfiles/csharp/draft.yaml | 2 +- template/dockerfiles/erlang/draft.yaml | 2 +- template/dockerfiles/go/draft.yaml | 2 +- template/dockerfiles/gomodule/draft.yaml | 2 +- template/dockerfiles/gradle/draft.yaml | 2 +- template/dockerfiles/gradlew/draft.yaml | 2 +- template/dockerfiles/java/draft.yaml | 2 +- template/dockerfiles/javascript/draft.yaml | 2 +- template/dockerfiles/php/draft.yaml | 2 +- template/dockerfiles/python/draft.yaml | 2 +- template/dockerfiles/ruby/draft.yaml | 2 +- template/dockerfiles/rust/draft.yaml | 2 +- template/dockerfiles/swift/draft.yaml | 2 +- .../manifests/draft.yaml | 2 +- .../PodDisruptionBudget/manifests/draft.yaml | 2 +- .../manifests/Service/manifests/draft.yaml | 2 +- template/workflows/helm/draft.yaml | 2 +- template/workflows/kustomize/draft.yaml | 2 +- template/workflows/manifests/draft.yaml | 2 +- 30 files changed, 50 insertions(+), 51 deletions(-) diff --git a/pkg/config/draftconfig.go b/pkg/config/draftconfig.go index b28ae9a1..1504e482 100644 --- a/pkg/config/draftconfig.go +++ b/pkg/config/draftconfig.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io/fs" + "slices" "github.com/Azure/draft/pkg/config/transformers" "github.com/Azure/draft/pkg/config/validators" @@ -34,7 +35,7 @@ type DraftConfig struct { DisplayName string `yaml:"displayName"` Description string `yaml:"description"` Type string `yaml:"type"` - Versions string `yaml:"versions"` + Versions []string `yaml:"versions"` DefaultVersion string `yaml:"defaultVersion"` Variables []*BuilderVar `yaml:"variables"` FileNameOverrideMap map[string]string `yaml:"filenameOverrideMap"` @@ -232,13 +233,8 @@ func (d *DraftConfig) ApplyDefaultVariablesForVersion(version string) error { return fmt.Errorf("invalid version: %w", err) } - expectedConfigVersionRange, err := semver.ParseRange(d.Versions) - if err != nil { - return fmt.Errorf("invalid config version range: %w", err) - } - - if !expectedConfigVersionRange(v) { - return fmt.Errorf("version %s is outside of config version range %s", version, d.Versions) + if !slices.Contains(d.Versions, version) { + return fmt.Errorf("requested version outside of valid versions: %s", version) } for _, variable := range d.Variables { @@ -376,11 +372,16 @@ func (d *DraftConfig) DeepCopy() *DraftConfig { DisplayName: d.DisplayName, Description: d.Description, Type: d.Type, - Versions: d.Versions, + Versions: make([]string, len(d.Versions)), DefaultVersion: d.DefaultVersion, Variables: make([]*BuilderVar, len(d.Variables)), FileNameOverrideMap: make(map[string]string), } + + for i, version := range d.Versions { + newConfig.Versions[i] = version + } + for i, variable := range d.Variables { newConfig.Variables[i] = variable.DeepCopy() } diff --git a/pkg/config/draftconfig_template_test.go b/pkg/config/draftconfig_template_test.go index ae131b4f..b798a4a1 100644 --- a/pkg/config/draftconfig_template_test.go +++ b/pkg/config/draftconfig_template_test.go @@ -121,8 +121,10 @@ func loadTemplatesWithValidation() error { return fmt.Errorf("template %s has an invalid type: %s", path, currTemplate.Type) } - if _, err := semver.ParseRange(currTemplate.Versions); err != nil { - return fmt.Errorf("template %s has an invalid version range: %s", path, currTemplate.Versions) + for _, version := range currTemplate.Versions { + if _, err := semver.Parse(version); err != nil { + return fmt.Errorf("template %s has an invalid version: %s", path, version) + } } referenceVarMap := map[string]*BuilderVar{} diff --git a/pkg/config/draftconfig_test.go b/pkg/config/draftconfig_test.go index 22c8e223..9f18a32c 100644 --- a/pkg/config/draftconfig_test.go +++ b/pkg/config/draftconfig_test.go @@ -211,7 +211,7 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) { testName: "excludeOutOfVersionRangeVariables", version: "0.0.1", draftConfig: DraftConfig{ - Versions: ">=0.0.1 <=0.0.2", + Versions: []string{"0.0.1", "0.0.2"}, Variables: []*BuilderVar{ { Name: "var1", @@ -239,7 +239,7 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) { testName: "emptyInputVersion", version: "", draftConfig: DraftConfig{ - Versions: ">=0.0.1 <=0.0.2", + Versions: []string{"0.0.1", "0.0.2"}, Variables: []*BuilderVar{ { Name: "var1", @@ -265,7 +265,7 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) { testName: "inputVersionOutOfRange", version: "0.0.3", draftConfig: DraftConfig{ - Versions: ">=0.0.1 <=0.0.2", + Versions: []string{"0.0.1", "0.0.2"}, Variables: []*BuilderVar{ { Name: "var1", @@ -285,13 +285,13 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) { }, }, }, - wantErrMsg: "version 0.0.3 is outside of config version range >=0.0.1 <=0.0.2", + wantErrMsg: "requested version outside of valid versions: 0.0.3", }, { testName: "overwriteDevfaultValue", version: "0.0.2", draftConfig: DraftConfig{ - Versions: ">=0.0.1 <=0.0.2", + Versions: []string{"0.0.1", "0.0.2"}, Variables: []*BuilderVar{ { Name: "var1", @@ -320,7 +320,7 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) { testName: "referenceVarOverwrite", version: "0.0.2", draftConfig: DraftConfig{ - Versions: ">=0.0.1 <=0.0.2", + Versions: []string{"0.0.1", "0.0.2"}, Variables: []*BuilderVar{ { Name: "var1", diff --git a/pkg/handlers/template_utils.go b/pkg/handlers/template_utils.go index ff757f9e..04bed47a 100644 --- a/pkg/handlers/template_utils.go +++ b/pkg/handlers/template_utils.go @@ -5,6 +5,7 @@ import ( "io/fs" "path/filepath" "runtime" + "slices" "strings" "github.com/Azure/draft/pkg/config" @@ -90,18 +91,13 @@ func loadTemplates() error { } // IsValidVersion checks if a version is valid for a given version range -func IsValidVersion(versionRange, version string) bool { - v, err := semver.Parse(version) +func IsValidVersion(versions []string, version string) bool { + _, err := semver.Parse(version) if err != nil { return false } - expectedRange, err := semver.ParseRange(versionRange) - if err != nil { - return false - } - - return expectedRange(v) + return slices.Contains(versions, version) } func sanatizeTemplateSrcDir(src string) string { diff --git a/template/addons/azure/webapp_routing/draft.yaml b/template/addons/azure/webapp_routing/draft.yaml index cf18c7a9..492f7b3b 100644 --- a/template/addons/azure/webapp_routing/draft.yaml +++ b/template/addons/azure/webapp_routing/draft.yaml @@ -1,6 +1,6 @@ templateName: "app-routing-ingress" description: "This template is used to create an ingress resource for use with the app-routing addon in AKS" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "manifest" variables: diff --git a/template/azurePipelines/kustomize/draft.yaml b/template/azurePipelines/kustomize/draft.yaml index 205ea36d..bcacdae1 100644 --- a/template/azurePipelines/kustomize/draft.yaml +++ b/template/azurePipelines/kustomize/draft.yaml @@ -1,6 +1,6 @@ templateName: "azure-pipeline-kustomize" description: "This template is used to create an Azure Pipeline for deploying an app to AKS using Kustomize" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "workflow" variables: diff --git a/template/azurePipelines/manifests/draft.yaml b/template/azurePipelines/manifests/draft.yaml index 1a6b4653..b3241e1e 100644 --- a/template/azurePipelines/manifests/draft.yaml +++ b/template/azurePipelines/manifests/draft.yaml @@ -1,6 +1,6 @@ templateName: "azure-pipeline-manifests" description: "Azure Pipeline for deploying a containerized application to AKS using kubernetes manifests" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "workflow" variables: diff --git a/template/deployments/helm/draft.yaml b/template/deployments/helm/draft.yaml index e779471a..4265fedc 100644 --- a/template/deployments/helm/draft.yaml +++ b/template/deployments/helm/draft.yaml @@ -1,7 +1,7 @@ templateName: "deployment-helm" description: "This template is used to create a Helm deployment for an application" type: "deployment" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" variables: - name: "PORT" diff --git a/template/deployments/kustomize/draft.yaml b/template/deployments/kustomize/draft.yaml index 53c85b65..8908041f 100644 --- a/template/deployments/kustomize/draft.yaml +++ b/template/deployments/kustomize/draft.yaml @@ -1,5 +1,5 @@ templateName: "deployment-kustomize" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" description: "This template is used to create a Kustomize deployment for an application" type: "deployment" diff --git a/template/deployments/manifests/draft.yaml b/template/deployments/manifests/draft.yaml index 225a429a..d2104ed5 100644 --- a/template/deployments/manifests/draft.yaml +++ b/template/deployments/manifests/draft.yaml @@ -1,6 +1,6 @@ templateName: "deployment-manifests" description: "This template is used to create a Kubernetes manifest deployment for an application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "deployment" variables: diff --git a/template/dockerfiles/clojure/draft.yaml b/template/dockerfiles/clojure/draft.yaml index 1ccbff76..d718c129 100644 --- a/template/dockerfiles/clojure/draft.yaml +++ b/template/dockerfiles/clojure/draft.yaml @@ -2,7 +2,7 @@ language: clojure displayName: Clojure templateName: "dockerfile-clojure" description: "This template is used to create a Dockerfile for a Clojure application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/csharp/draft.yaml b/template/dockerfiles/csharp/draft.yaml index 7fcbe52c..480e36d2 100644 --- a/template/dockerfiles/csharp/draft.yaml +++ b/template/dockerfiles/csharp/draft.yaml @@ -2,7 +2,7 @@ language: csharp displayName: C# templateName: "dockerfile-csharp" description: "This template is used to create a Dockerfile for a C# application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/erlang/draft.yaml b/template/dockerfiles/erlang/draft.yaml index e456c522..c27ab278 100644 --- a/template/dockerfiles/erlang/draft.yaml +++ b/template/dockerfiles/erlang/draft.yaml @@ -2,7 +2,7 @@ language: erlang displayName: Erlang templateName: "dockerfile-erlang" description: "This template is used to create a Dockerfile for an Erlang application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/go/draft.yaml b/template/dockerfiles/go/draft.yaml index 999f8c59..ae853ef8 100644 --- a/template/dockerfiles/go/draft.yaml +++ b/template/dockerfiles/go/draft.yaml @@ -2,7 +2,7 @@ language: go displayName: Go templateName: "dockerfile-go" description: "This template is used to create a Dockerfile for a Go application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/gomodule/draft.yaml b/template/dockerfiles/gomodule/draft.yaml index c4d6d316..b9a92092 100644 --- a/template/dockerfiles/gomodule/draft.yaml +++ b/template/dockerfiles/gomodule/draft.yaml @@ -2,7 +2,7 @@ language: gomodule displayName: Go Module templateName: "dockerfile-gomodule" description: "This template is used to create a Dockerfile for a Go Module application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/gradle/draft.yaml b/template/dockerfiles/gradle/draft.yaml index cff58e9f..310575fd 100644 --- a/template/dockerfiles/gradle/draft.yaml +++ b/template/dockerfiles/gradle/draft.yaml @@ -2,7 +2,7 @@ language: gradle displayName: Gradle templateName: "dockerfile-gradle" description: "This template is used to create a Dockerfile for a Gradle application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/gradlew/draft.yaml b/template/dockerfiles/gradlew/draft.yaml index 0b9ca9c4..c5a76abe 100644 --- a/template/dockerfiles/gradlew/draft.yaml +++ b/template/dockerfiles/gradlew/draft.yaml @@ -2,7 +2,7 @@ language: gradle displayName: Gradle templateName: "dockerfile-gradlew" description: "This template is used to create a Dockerfile for a Gradle application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/java/draft.yaml b/template/dockerfiles/java/draft.yaml index 59a825ec..24eafc93 100644 --- a/template/dockerfiles/java/draft.yaml +++ b/template/dockerfiles/java/draft.yaml @@ -2,7 +2,7 @@ language: java displayName: Java templateName: "dockerfile-java" description: "This template is used to create a Dockerfile for a Java application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/javascript/draft.yaml b/template/dockerfiles/javascript/draft.yaml index f3d8bbda..4a4fa7d4 100644 --- a/template/dockerfiles/javascript/draft.yaml +++ b/template/dockerfiles/javascript/draft.yaml @@ -2,7 +2,7 @@ language: javascript displayName: JavaScript templateName: "dockerfile-javascript" description: "This template is used to create a Dockerfile for a JavaScript application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/php/draft.yaml b/template/dockerfiles/php/draft.yaml index 6f71621b..d460606f 100644 --- a/template/dockerfiles/php/draft.yaml +++ b/template/dockerfiles/php/draft.yaml @@ -2,7 +2,7 @@ language: php displayName: PHP templateName: "dockerfile-php" description: "This template is used to create a Dockerfile for a PHP application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/python/draft.yaml b/template/dockerfiles/python/draft.yaml index 809c8324..178fceff 100644 --- a/template/dockerfiles/python/draft.yaml +++ b/template/dockerfiles/python/draft.yaml @@ -2,7 +2,7 @@ language: python displayName: Python templateName: "dockerfile-python" description: "This template is used to create a Dockerfile for a Python application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/ruby/draft.yaml b/template/dockerfiles/ruby/draft.yaml index 1a64243f..29c6b7d2 100644 --- a/template/dockerfiles/ruby/draft.yaml +++ b/template/dockerfiles/ruby/draft.yaml @@ -2,7 +2,7 @@ language: ruby displayName: Ruby templateName: "dockerfile-ruby" description: "This template is used to create a Dockerfile for a Ruby application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/rust/draft.yaml b/template/dockerfiles/rust/draft.yaml index 22a7d218..4b770346 100644 --- a/template/dockerfiles/rust/draft.yaml +++ b/template/dockerfiles/rust/draft.yaml @@ -2,7 +2,7 @@ language: rust displayName: Rust templateName: "dockerfile-rust" description: "This template is used to create a Dockerfile for a Rust application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/dockerfiles/swift/draft.yaml b/template/dockerfiles/swift/draft.yaml index caf72f3c..75236a34 100644 --- a/template/dockerfiles/swift/draft.yaml +++ b/template/dockerfiles/swift/draft.yaml @@ -2,7 +2,7 @@ language: swift displayName: Swift templateName: "dockerfile-swift" description: "This template is used to create a Dockerfile for a Swift application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "dockerfile" variables: diff --git a/template/manifests/HorizontalPodAutoscaler/manifests/draft.yaml b/template/manifests/HorizontalPodAutoscaler/manifests/draft.yaml index 9195c51e..021b8df7 100644 --- a/template/manifests/HorizontalPodAutoscaler/manifests/draft.yaml +++ b/template/manifests/HorizontalPodAutoscaler/manifests/draft.yaml @@ -1,6 +1,6 @@ templateName: "horizontalPodAutoscaler-manifests" description: "This template is used to create a horizontalPodAutoscaling for an application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "manifest" variables: diff --git a/template/manifests/PodDisruptionBudget/manifests/draft.yaml b/template/manifests/PodDisruptionBudget/manifests/draft.yaml index 7602d2d4..f4766977 100644 --- a/template/manifests/PodDisruptionBudget/manifests/draft.yaml +++ b/template/manifests/PodDisruptionBudget/manifests/draft.yaml @@ -1,6 +1,6 @@ templateName: "podDisruptionBudget-manifests" description: "This template is used to create a PodDisruptionBudget for an application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersions: "0.0.1" type: "manifest" variables: diff --git a/template/manifests/Service/manifests/draft.yaml b/template/manifests/Service/manifests/draft.yaml index 3a884919..063c44c2 100644 --- a/template/manifests/Service/manifests/draft.yaml +++ b/template/manifests/Service/manifests/draft.yaml @@ -1,6 +1,6 @@ templateName: "service-manifests" description: "This template is used to create a generic Service for an application" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" type: "manifest" variables: diff --git a/template/workflows/helm/draft.yaml b/template/workflows/helm/draft.yaml index 31bbe504..378a3b11 100644 --- a/template/workflows/helm/draft.yaml +++ b/template/workflows/helm/draft.yaml @@ -1,7 +1,7 @@ templateName: "github-workflow-helm" description: "This template is used to create a GitHub workflow for building and deploying an app to AKS with Helm" type: "workflow" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" variables: - name: "WORKFLOWNAME" diff --git a/template/workflows/kustomize/draft.yaml b/template/workflows/kustomize/draft.yaml index ac73ea1a..378da838 100644 --- a/template/workflows/kustomize/draft.yaml +++ b/template/workflows/kustomize/draft.yaml @@ -1,7 +1,7 @@ templateName: "github-workflow-kustomize" description: "This template is used to create a GitHub workflow for building and deploying an app to AKS with Kustomize" type: "workflow" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" variables: - name: "WORKFLOWNAME" diff --git a/template/workflows/manifests/draft.yaml b/template/workflows/manifests/draft.yaml index c7dac3ef..014ab065 100644 --- a/template/workflows/manifests/draft.yaml +++ b/template/workflows/manifests/draft.yaml @@ -1,7 +1,7 @@ templateName: "github-workflow-manifests" description: "This template is used to create a GitHub workflow for building and deploying an app to AKS with kubernetes manifests" type: "workflow" -versions: "0.0.1" +versions: ["0.0.1"] defaultVersion: "0.0.1" variables: - name: "WORKFLOWNAME"