Skip to content

Commit 3af991e

Browse files
authored
Allow disabling ytt validations while building packages (#1077)
* Disable ytt validations while building packages Signed-off-by: Soumik Majumder <[email protected]> * Add test to ensure that kctrl disables ytt validations while releasing packages Signed-off-by: Soumik Majumder <[email protected]> * Add flag to disable ytt validations while releasing package Signed-off-by: Soumik Majumder <[email protected]> * Add test case for using ytt validations while building packages Signed-off-by: Soumik Majumder <[email protected]> --------- Signed-off-by: Soumik Majumder <[email protected]>
1 parent ab03cc2 commit 3af991e

File tree

4 files changed

+173
-8
lines changed

4 files changed

+173
-8
lines changed

cli/pkg/kctrl/cmd/app/release/app_spec_builder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type AppSpecBuilderOpts struct {
3434
BundleImage string
3535
Debug bool
3636
BundleTag string
37+
38+
BuildYttValidations bool
3739
}
3840

3941
func NewAppSpecBuilder(depsFactory cmdcore.DepsFactory, logger logger.Logger, ui cmdcore.AuthoringUI, opts AppSpecBuilderOpts) *AppSpecBuilder {
@@ -83,7 +85,7 @@ func (b *AppSpecBuilder) Build() (kcv1alpha1.AppSpec, error) {
8385

8486
// Build images and resolve references using reconciler
8587
tempImgpkgLockPath := filepath.Join(LockOutputFolder, LockOutputFile)
86-
cmdRunner := NewReleaseCmdRunner(os.Stdout, b.opts.Debug, tempImgpkgLockPath, b.ui)
88+
cmdRunner := NewReleaseCmdRunner(os.Stdout, b.opts.Debug, tempImgpkgLockPath, b.opts.BuildYttValidations, b.ui)
8789
reconciler := cmdlocal.NewReconciler(b.depsFactory, cmdRunner, b.logger)
8890

8991
err = reconciler.Reconcile(buildConfigs, cmdlocal.ReconcileOpts{

cli/pkg/kctrl/cmd/app/release/release_cmd_runner.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ type ReleaseCmdRunner struct {
1818
log io.Writer
1919
fullOutput bool
2020
tempImgLockFilepath string
21+
buildYttValidations bool
2122
ui cmdcore.AuthoringUI
2223
}
2324

2425
var _ exec.CmdRunner = &ReleaseCmdRunner{}
2526

26-
func NewReleaseCmdRunner(log io.Writer, fullOutput bool, tempImgLockFilepath string, ui cmdcore.AuthoringUI) *ReleaseCmdRunner {
27-
return &ReleaseCmdRunner{log: log, fullOutput: fullOutput, tempImgLockFilepath: tempImgLockFilepath, ui: ui}
27+
func NewReleaseCmdRunner(log io.Writer, fullOutput bool, tempImgLockFilepath string, buildYttValidation bool, ui cmdcore.AuthoringUI) *ReleaseCmdRunner {
28+
return &ReleaseCmdRunner{log: log, fullOutput: fullOutput, tempImgLockFilepath: tempImgLockFilepath,
29+
buildYttValidations: buildYttValidation, ui: ui}
2830
}
2931

3032
func (r ReleaseCmdRunner) Run(cmd *goexec.Cmd) error {
@@ -39,6 +41,9 @@ func (r ReleaseCmdRunner) Run(cmd *goexec.Cmd) error {
3941
if filepath.Base(cmd.Path) == "kbld" {
4042
cmd.Args = append(cmd.Args, fmt.Sprintf("--imgpkg-lock-output=%s", r.tempImgLockFilepath))
4143
}
44+
if filepath.Base(cmd.Path) == "ytt" && !r.buildYttValidations {
45+
cmd.Args = append(cmd.Args, "--dangerous-data-values-disable-validation")
46+
}
4247
if r.fullOutput {
4348
cmd.Stdout = io.MultiWriter(r.log, cmd.Stdout)
4449
cmd.Stderr = io.MultiWriter(r.log, cmd.Stderr)

cli/pkg/kctrl/cmd/package/release/release.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ReleaseOptions struct {
3232
repoOutputLocation string
3333
debug bool
3434
generateOpenAPISchema bool
35+
buildYttValidations bool
3536
tag string
3637
}
3738

@@ -61,6 +62,7 @@ func NewReleaseCmd(o *ReleaseOptions) *cobra.Command {
6162
cmd.Flags().BoolVar(&o.debug, "debug", false, "Print verbose debug output")
6263
cmd.Flags().StringVarP(&o.tag, "tag", "t", "", "Tag pushed with imgpkg bundle (default build-<TIMESTAMP>)")
6364
cmd.Flags().BoolVar(&o.generateOpenAPISchema, "openapi-schema", true, "Generates openapi schema for ytt and helm templated files and adds it to generated package")
65+
cmd.Flags().BoolVar(&o.buildYttValidations, "build-ytt-validations", true, "Ignore ytt validation errors while releasing packages")
6466

6567
return cmd
6668
}
@@ -111,11 +113,12 @@ func (o *ReleaseOptions) Run() error {
111113
return fmt.Errorf("Releasing package: 'package init' was not run successfully. (hint: re-run the 'init' command)")
112114
}
113115
builderOpts := cmdapprelease.AppSpecBuilderOpts{
114-
BuildTemplate: buildAppSpec.Template,
115-
BuildDeploy: buildAppSpec.Deploy,
116-
BuildExport: pkgBuild.GetExport(),
117-
Debug: o.debug,
118-
BundleTag: o.tag,
116+
BuildTemplate: buildAppSpec.Template,
117+
BuildDeploy: buildAppSpec.Deploy,
118+
BuildExport: pkgBuild.GetExport(),
119+
Debug: o.debug,
120+
BundleTag: o.tag,
121+
BuildYttValidations: o.buildYttValidations,
119122
}
120123
appSpec, err := cmdapprelease.NewAppSpecBuilder(o.depsFactory, o.logger, o.ui, builderOpts).Build()
121124
if err != nil {
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package e2e
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestPackageReleaseWithRestrictiveValuesSchema(t *testing.T) {
13+
env := BuildEnv(t)
14+
logger := Logger{}
15+
kappCtrl := Kctrl{t, env.Namespace, env.KctrlBinaryPath, logger}
16+
17+
configYAML := `
18+
#@ load("@ytt:data", "data")
19+
---
20+
apiVersion: v1
21+
kind: ConfigMap
22+
metadata:
23+
name: pkg-cm
24+
namespace: #@ data.values.namespace
25+
data:
26+
foo: bar
27+
`
28+
schemaYAML := `
29+
#@data/values-schema
30+
---
31+
#@schema/validation min_len=1
32+
namespace: ""
33+
`
34+
packageBuildYAML := fmt.Sprintf(`
35+
apiVersion: kctrl.carvel.dev/v1alpha1
36+
kind: PackageBuild
37+
metadata:
38+
creationTimestamp: null
39+
name: samplepackage.corp.com
40+
spec:
41+
release:
42+
- resource: {}
43+
template:
44+
spec:
45+
app:
46+
spec:
47+
deploy:
48+
- kapp: {}
49+
template:
50+
- ytt:
51+
paths:
52+
- config
53+
- kbld: {}
54+
export:
55+
- imgpkgBundle:
56+
image: %s
57+
useKbldImagesLock: true
58+
includePaths:
59+
- config
60+
`, env.Image)
61+
packageResourcesYAML := `
62+
apiVersion: data.packaging.carvel.dev/v1alpha1
63+
kind: Package
64+
metadata:
65+
creationTimestamp: null
66+
name: samplepackage.corp.com.0.0.0
67+
spec:
68+
refName: samplepackage.corp.com
69+
releasedAt: null
70+
template:
71+
spec:
72+
deploy:
73+
- kapp: {}
74+
fetch:
75+
- git: {}
76+
template:
77+
- ytt:
78+
paths:
79+
- config
80+
- kbld: {}
81+
valuesSchema:
82+
openAPIv3: null
83+
version: 0.0.0
84+
---
85+
apiVersion: data.packaging.carvel.dev/v1alpha1
86+
kind: PackageMetadata
87+
metadata:
88+
creationTimestamp: null
89+
name: samplepackage.corp.com
90+
spec:
91+
displayName: samplepackage
92+
longDescription: samplepackage.corp.com
93+
shortDescription: samplepackage.corp.com
94+
---
95+
apiVersion: packaging.carvel.dev/v1alpha1
96+
kind: PackageInstall
97+
metadata:
98+
annotations:
99+
kctrl.carvel.dev/local-fetch-0: .
100+
creationTimestamp: null
101+
name: samplepackage
102+
spec:
103+
packageRef:
104+
refName: samplepackage.corp.com
105+
versionSelection:
106+
constraints: 0.0.0
107+
serviceAccountName: samplepackage-sa
108+
status:
109+
conditions: null
110+
friendlyDescription: ""
111+
observedGeneration: 0
112+
`
113+
114+
cleanUp := func() {
115+
os.RemoveAll(workingDir)
116+
}
117+
cleanUp()
118+
defer cleanUp()
119+
120+
configDir := "config"
121+
err := os.Mkdir(workingDir, os.ModePerm)
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
err = os.Mkdir(path.Join(workingDir, configDir), os.ModePerm)
126+
if err != nil {
127+
t.Fatal(err)
128+
}
129+
err = os.WriteFile(path.Join(workingDir, configDir, "config.yaml"), []byte(configYAML), os.ModePerm)
130+
if err != nil {
131+
t.Fatal(err)
132+
}
133+
err = os.WriteFile(path.Join(workingDir, configDir, "schema.yaml"), []byte(schemaYAML), os.ModePerm)
134+
if err != nil {
135+
t.Fatal(err)
136+
}
137+
err = os.WriteFile(path.Join(workingDir, "package-build.yml"), []byte(packageBuildYAML), os.ModePerm)
138+
if err != nil {
139+
t.Fatal(err)
140+
}
141+
err = os.WriteFile(path.Join(workingDir, "package-resources.yml"), []byte(packageResourcesYAML), os.ModePerm)
142+
if err != nil {
143+
t.Fatal(err)
144+
}
145+
146+
logger.Section("run package release with ytt validations", func() {
147+
_, err := kappCtrl.RunWithOpts([]string{"package", "release", "--chdir", workingDir}, RunOpts{NoNamespace: true, AllowError: true})
148+
require.Error(t, err)
149+
})
150+
151+
logger.Section("run package release with ytt validations deactivated", func() {
152+
// Verify that validation checks are not performed while running ytt to build packages
153+
kappCtrl.RunWithOpts([]string{"package", "release", "--chdir", workingDir, "--build-ytt-validations=false"}, RunOpts{NoNamespace: true})
154+
})
155+
}

0 commit comments

Comments
 (0)