Skip to content

Commit

Permalink
Updating DraftConfig to contain more metadata (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfoley13 authored and manasachi committed Oct 7, 2024
1 parent 6d7ec03 commit a03a06d
Show file tree
Hide file tree
Showing 28 changed files with 573 additions and 137 deletions.
34 changes: 21 additions & 13 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ updates:
schedule:
interval: "weekly"
groups:
# Group updates together, so that they are all applied in a single PR.
# Grouped updates are currently in beta and is subject to change.
# xref: https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#groups
k8s-go-deps:
patterns:
- "k8s.io/*"
- "sigs.k8s.io/*"
go-deps:
patterns:
- "*"
exclude-patterns:
- "k8s.io/*"
- "sigs.k8s.io/*"
# Group updates together, so that they are all applied in a single PR.
# Grouped updates are currently in beta and is subject to change.
# xref: https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#groups
k8s-go-deps:
patterns:
- "k8s.io/*"
- "sigs.k8s.io/*"
go-deps:
patterns:
- "*"
exclude-patterns:
- "k8s.io/*"
- "sigs.k8s.io/*"
- package-ecosystem: "github-actions"
directory: ".github/workflows"
schedule:
interval: weekly
groups:
actions:
patterns:
- "*"
- package-ecosystem: "github-actions"
directory: "/template/workflows/helm/.github/workflows"
schedule:
Expand Down
58 changes: 29 additions & 29 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ name: "CodeQL"

on:
push:
branches: [ main ]
branches: [main]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
branches: [main]
schedule:
- cron: '33 14 * * 4'
- cron: "33 14 * * 4"

jobs:
analyze:
Expand All @@ -32,39 +32,39 @@ jobs:
strategy:
fail-fast: false
matrix:
language: [ 'go' ]
language: ["go"]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release
#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
25 changes: 25 additions & 0 deletions pkg/config/draftconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ package config
import (
"errors"
"fmt"
"io/fs"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

const draftConfigFile = "draft.yaml"

type DraftConfig struct {
TemplateName string `yaml:"templateName"`
DisplayName string `yaml:"displayName"`
Description string `yaml:"description"`
Type string `yaml:"type"`
Versions string `yaml:"versions"`
DefaultVersion string `yaml:"defaultVersion"`
Variables []*BuilderVar `yaml:"variables"`
FileNameOverrideMap map[string]string `yaml:"filenameOverrideMap"`
}
Expand All @@ -19,7 +28,9 @@ type BuilderVar struct {
Description string `yaml:"description"`
ExampleValues []string `yaml:"exampleValues"`
Type string `yaml:"type"`
Kind string `yaml:"kind"`
Value string `yaml:"value"`
Versions string `yaml:"versions"`
}

type BuilderVarDefault struct {
Expand All @@ -28,6 +39,20 @@ type BuilderVarDefault struct {
Value string `yaml:"value"`
}

func NewConfigFromFS(fileSys fs.FS, path string) (*DraftConfig, error) {
configBytes, err := fs.ReadFile(fileSys, path)
if err != nil {
return nil, err
}

var draftConfig DraftConfig
if err = yaml.Unmarshal(configBytes, &draftConfig); err != nil {
return nil, err
}

return &draftConfig, nil
}

func (d *DraftConfig) GetVariableExampleValues() map[string][]string {
variableExampleValues := make(map[string][]string)
for _, variable := range d.Variables {
Expand Down
114 changes: 114 additions & 0 deletions pkg/config/draftconfig_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package config

import (
"fmt"
"io/fs"
"strings"
"testing"

"github.com/Azure/draft/template"
"github.com/stretchr/testify/assert"
)

var allTemplates = map[string]*DraftConfig{}

var validTemplateTypes = map[string]bool{
"manifest": true,
"dockerfile": true,
"workflow": true,
"deployment": true,
}

var validVariableTypes = map[string]bool{
"string": true,
"bool": true,
"int": true,
"float": true,
"object": true,
}
var validVariableKinds = map[string]bool{
"azureContainerRegistry": true,
"azureKeyvaultUri": true,
"azureManagedCluster": true,
"azureResourceGroup": true,
"azureServiceConnection": true,
"containerImageName": true,
"containerImageVersion": true,
"dirPath": true,
"filePath": true,
"flag": true,
"helmChartOverrides": true,
"ingressHostName": true,
"kubernetesNamespace": true,
"kubernetesResourceName": true,
"label": true,
"port": true,
"repositoryBranch": true,
"workflowName": true,
}

/*
This test will validate all the templates in the templates directory have:
1. a unique template name
2. a valid template type
3. a non-empty variable name
4. a valid variable type
5. a valid variable kind
Append this for more validation
*/
func TestTempalteValidation(t *testing.T) {
assert.Nil(t, loadTemplatesWithValidation())
}

func loadTemplatesWithValidation() error {
return fs.WalkDir(template.Templates, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil
}

if !strings.EqualFold(d.Name(), draftConfigFile) {
return nil
}

currTemplate, err := NewConfigFromFS(template.Templates, path)
if err != nil {
return err
}

if currTemplate == nil {
return fmt.Errorf("template %s is nil", path)
}

if currTemplate.TemplateName == "" {
return fmt.Errorf("template %s has no template name", path)
}

if _, ok := allTemplates[currTemplate.TemplateName]; ok {
return fmt.Errorf("template %s has a duplicate template name", path)
}

if _, ok := validTemplateTypes[currTemplate.Type]; !ok {
return fmt.Errorf("template %s has an invalid type: %s", path, currTemplate.Type)
}

for _, variable := range currTemplate.Variables {
if variable.Name == "" {
return fmt.Errorf("template %s has a variable with no name", path)
}

if _, ok := validVariableTypes[variable.Type]; !ok {
return fmt.Errorf("template %s has an invalid variable(%s) type: %s", path, variable.Name, variable.Type)
}

if _, ok := validVariableKinds[variable.Kind]; !ok {
return fmt.Errorf("template %s has an invalid variable kind: %s", path, variable.Kind)
}
}
return nil
})
}
14 changes: 12 additions & 2 deletions template/addons/azure/webapp_routing/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
templateName: "app-routing-ingress"
description: "This template is used to create an ingress resource for use with the app-routing addon in AKS"
type: "manifest"
variables:
- name: "ingress-tls-cert-keyvault-uri"
type: "string"
kind: "azureKeyvaultUri"
description: "the keyvault uri for the tls certificate"
- name: "ingress-use-osm-mtls"
description: "use open service mesh mutual-tls"
type: "bool"
kind: "flag"
- name: "ingress-host"
type: "string"
kind: "ingressHostName"
description: "specify the host of the ingress resource"
- name: "GENERATORLABEL"
default:
type: "string"
kind: "label"
default:
disablePrompt: true
value: "draft"
description: "the label to identify who generated the resource"
Expand All @@ -18,4 +28,4 @@ references:
- name: "service-port"
path: "spec.ports.port"
- name: "service-namespace"
path: "metadata.namespace"
path: "metadata.namespace"
25 changes: 24 additions & 1 deletion template/azurePipelines/kustomize/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
templateName: "azure-pipeline-kustomize"
description: "This template is used to create an Azure Pipeline for deploying an app to AKS using Kustomize"
type: "workflow"
variables:
- name: "PIPELINENAME"
type: "string"
kind: "workflowName"
default:
value: "Build and deploy an app to AKS"
description: "the name of the azure pipeline"
- name: "BRANCHNAME"
type: "string"
kind: "repositoryBranch"
default:
value: "main"
description: "the branch to trigger the pipeline"
- name: "ARMSERVICECONNECTION"
type: "string"
kind: "azureServiceConnection"
description: "the name of the Azure Resource Manager service connection"
- name: "AZURECONTAINERREGISTRY"
type: "string"
kind: "azureContainerRegistry"
description: "the name of the Azure Container Registry"
- name: "CONTAINERNAME"
type: "string"
kind: "containerImageName"
description: "the container image name"
- name: "CLUSTERRESOURCEGROUP"
type: "string"
kind: "azureResourceGroup"
description: "the AKS cluster resource group"
- name: "ACRRESOURCEGROUP"
type: "string"
kind: "azureResourceGroup"
description: "the ACR resource group"
- name: "CLUSTERNAME"
type: "string"
kind: "azureManagedCluster"
description: "the AKS cluster name"
- name: "KUSTOMIZEPATH"
type: "string"
kind: "dirPath"
default:
disablePrompt: true
value: "./overlays/production" # keeping this as default since draft generates the manifests in the overlays/production directory
description: "the path to the Kustomize directory"
- name: "NAMESPACE"
type: "string"
kind: "kubernetesNamespace"
default:
value: "default"
description: "the Kubernetes namespace"
description: "the Kubernetes namespace"
Loading

0 comments on commit a03a06d

Please sign in to comment.