Skip to content

Commit

Permalink
Dry-run for update command (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
bosesuneha authored and aamgayle committed Apr 12, 2023
1 parent 2cf3c92 commit 716739d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 36 deletions.
48 changes: 40 additions & 8 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@ package cmd

import (
"embed"
"encoding/json"
"fmt"
"os"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/Azure/draft/pkg/addons"
"github.com/Azure/draft/pkg/config"
dryrunpkg "github.com/Azure/draft/pkg/dryrun"
"github.com/Azure/draft/pkg/templatewriter"
"github.com/Azure/draft/pkg/templatewriter/writers"
"github.com/Azure/draft/template"
)

type updateCmd struct {
dest string
provider string
addon string
flagVariables []string
userInputs map[string]string
templateWriter templatewriter.TemplateWriter
addonFS embed.FS
dest string
provider string
addon string
flagVariables []string
userInputs map[string]string
templateWriter templatewriter.TemplateWriter
addonFS embed.FS
templateVariableRecorder config.TemplateVariableRecorder
}

var dryRunRecorder *dryrunpkg.DryRunRecorder

func newUpdateCmd() *cobra.Command {
uc := &updateCmd{}
// updateCmd represents the update command
Expand Down Expand Up @@ -81,7 +88,32 @@ func (uc *updateCmd) run() error {
}
log.Debugf("addonInputs is: %s", uc.userInputs)

return addons.GenerateAddon(template.Addons, uc.provider, uc.addon, uc.dest, uc.userInputs, uc.templateWriter)
if dryRun {
dryRunRecorder = dryrunpkg.NewDryRunRecorder()
uc.templateVariableRecorder = dryRunRecorder
uc.templateWriter = dryRunRecorder
for k, v := range uc.userInputs {
uc.templateVariableRecorder.Record(k, v)
}
}

err = addons.GenerateAddon(template.Addons, uc.provider, uc.addon, uc.dest, uc.userInputs, uc.templateWriter)

if dryRun {
dryRunText, err := json.MarshalIndent(dryRunRecorder.DryRunInfo, "", TWO_SPACES)
if err != nil {
return err
}
fmt.Println(string(dryRunText))
if dryRunFile != "" {
log.Printf("writing dry run info to file %s", dryRunFile)
err = os.WriteFile(dryRunFile, dryRunText, 0644)
if err != nil {
return err
}
}
}
return err
}

func init() {
Expand Down
52 changes: 24 additions & 28 deletions test/gen_integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,15 @@ languageVariables:
echo 'Curling service IP'
curl -m 3 \$SERVICEIP:$serviceport
kill \$tunnelPID
- run: ./draft -b main -v generate-workflow -d ./langtest/ -c someAksCluster -r someRegistry -g someResourceGroup --container-name someContainer --deploy-type helm
# Validate generated workflow yaml
- name: Checkout
uses: actions/checkout@v2
with:
submodules: true
- name: Install action-validator with asdf
uses: asdf-vm/actions/install@v1
with:
tool_versions: |
action-validator 0.1.2
- name: Lint Actions
- run: ./draft -b main -v generate-workflow -d ./langtest/ -c someAksCluster -r someRegistry -g someResourceGroup --container-name someContainer
- name: Execute dry run for update command
run: |
find .github/workflows -type f \( -iname \*.yaml -o -iname \*.yml \) \
| xargs -I {} action-validator --verbose {}
mkdir -p test/temp
./draft --dry-run --dry-run-file test/temp/update_dry_run.json update -d ./langtest/ $ingress_test_args
- name: Validate JSON
run: |
npm install -g ajv-cli@5.0.0
ajv validate -s test/update_dry_run_schema.json -d test/temp/update_dry_run.json
- run: ./draft -v update -d ./langtest/ $ingress_test_args
- name: Check default namespace
if: steps.deploy.outcome != 'success'
Expand Down Expand Up @@ -414,21 +408,15 @@ languageVariables:
echo 'Curling service IP'
curl -m 3 \$SERVICEIP:$serviceport
kill \$tunnelPID
- run: ./draft -v generate-workflow -b main -d ./langtest/ -c someAksCluster -r someRegistry -g someResourceGroup --container-name someContainer --deploy-type kustomize
# Validate generated workflow yaml
- name: Checkout
uses: actions/checkout@v2
with:
submodules: true
- name: Install action-validator with asdf
uses: asdf-vm/actions/install@v1
with:
tool_versions: |
action-validator 0.1.2
- name: Lint Actions
- run: ./draft -v generate-workflow -b main -d ./langtest/ -c someAksCluster -r someRegistry -g someResourceGroup --container-name someContainer
- name: Execute dry run for update command
run: |
find .github/workflows -type f \( -iname \*.yaml -o -iname \*.yml \) \
| xargs -I {} action-validator --verbose {}
mkdir -p test/temp
./draft --dry-run --dry-run-file test/temp/update_dry_run.json update -d ./langtest/ $ingress_test_args
- name: Validate JSON
run: |
npm install -g ajv-cli@5.0.0
ajv validate -s test/update_dry_run_schema.json -d test/temp/update_dry_run.json
- run: ./draft -v update -d ./langtest/ $ingress_test_args
- name: Check default namespace
if: steps.deploy.outcome != 'success'
Expand Down Expand Up @@ -574,6 +562,14 @@ languageVariables:
with:
name: $lang-manifests-create
path: ./langtest/
- name: Execute dry run for update command
run: |
mkdir -p test/temp
./draft --dry-run --dry-run-file test/temp/update_dry_run.json update -d ./langtest/ $ingress_test_args
- name: Validate JSON
run: |
npm install -g ajv-cli@5.0.0
ajv validate -s test/update_dry_run_schema.json -d test/temp/update_dry_run.json
- run: ./draft -v update -d ./langtest/ $ingress_test_args
- name: start minikube
id: minikube
Expand Down
71 changes: 71 additions & 0 deletions test/update_dry_run_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/object1670282842.json",
"title": "Root",
"type": "object",
"properties": {
"variables": {
"$id": "#root/variables",
"title": "Variables",
"type": "object",
"properties": {
"ingress-host": {
"$id": "#root/variables/ingress-host",
"title": "IngressHost",
"type": "string",
"default": "",
"pattern": "^.*$"
},
"ingress-tls-cert-keyvault-uri": {
"$id": "#root/variables/ingress-tls-cert-keyvault-uri",
"title": "IngressTlsCertKeyvaultUri",
"type": "string",
"default": "",
"pattern": "^.*$"
},
"ingress-use-osm-mtls": {
"$id": "#root/variables/ingress-use-osm-mtls",
"title": "IngressUseOsmMtls",
"type": "string",
"default": "",
"pattern": "^.*$"
},
"service-name": {
"$id": "#root/variables/service-name",
"title": "ServiceName",
"type": "string",
"default": "",
"pattern": "^.*$"
},
"service-namespace": {
"$id": "#root/variables/service-namespace",
"title": "ServiceNamespace",
"type": "string",
"default": "",
"pattern": "^.*$"
},
"service-port": {
"$id": "#root/variables/service-port",
"title": "ServicePort",
"type": "string",
"default": "",
"pattern": "^.*$"
}
}
},
"filesToWrite": {
"$id": "#root/filesToWrite",
"title": "FilesToWrite",
"type": "array",
"default": [],
"items": {
"$id": "#root/filesToWrite/items",
"title": "Items",
"type": "string",
"default": "",
"pattern": "^.*$"
}
}
}
}

0 comments on commit 716739d

Please sign in to comment.