fix(schema): enable $ref resolution for directory charts

Set ChartDir only for directory-based charts to enable $ref resolution in
JSON schemas. Archived charts (.tgz) are loaded into memory without filesystem
extraction, so $ref resolution is not supported for them.

This fixes the original issue where `helm template .` and `helm install .`
failed to validate schemas with relative $ref references.

Fixes #31260

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
pull/31274/head
Benoit Tigeot 4 weeks ago
parent dd0b8f40ba
commit 088959aae9
No known key found for this signature in database
GPG Key ID: 8E6D4FC8AEBDA62C

@ -248,6 +248,13 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options
return nil, err
}
// Only set ChartDir for directory-based charts to enable $ref resolution.
// Archived charts (.tgz) are loaded into memory without filesystem extraction,
// so $ref resolution is not supported for them.
if fi, err := os.Stat(cp); err == nil && fi.IsDir() {
client.ChartDir = cp
}
slog.Debug("Chart path", "path", cp)
p := getter.All(settings)

@ -231,6 +231,11 @@ func TestInstall(t *testing.T) {
cmd: "install schema testdata/testcharts/chart-with-schema-and-subchart --set lastname=doe --set subchart-with-schema.age=-25 --skip-schema-validation",
golden: "output/schema.txt",
},
{
name: "install with schema file containing $ref",
cmd: "install reftest testdata/testcharts/chart-with-schema-ref",
golden: "output/schema-ref.txt",
},
// Install deprecated chart
{
name: "install with warning about deprecated chart",

@ -166,6 +166,11 @@ func TestTemplateCmd(t *testing.T) {
cmd: fmt.Sprintf("template '%s' -f %s/extra_values.yaml", chartPath, chartPath),
golden: "output/template-subchart-cm-set-file.txt",
},
{
name: "template with schema file containing $ref",
cmd: "template reftest testdata/testcharts/chart-with-schema-ref",
golden: "output/template-schema-ref.txt",
},
}
runTestCmd(t, tests)
}

@ -0,0 +1,7 @@
NAME: reftest
LAST DEPLOYED: Fri Sep 2 22:04:05 1977
NAMESPACE: default
STATUS: deployed
REVISION: 1
DESCRIPTION: Install complete
TEST SUITE: None

@ -0,0 +1,8 @@
Release "reftest" has been upgraded. Happy Helming!
NAME: reftest
LAST DEPLOYED: Fri Sep 2 22:04:05 1977
NAMESPACE: default
STATUS: deployed
REVISION: 2
DESCRIPTION: Upgrade complete
TEST SUITE: None

@ -0,0 +1,3 @@
apiVersion: v2
name: chart-with-schema-ref
version: 0.1.0

@ -0,0 +1,4 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string"
}

@ -0,0 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": { "$ref": "name.schema.json" }
}
}

@ -187,6 +187,13 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
return err
}
// Only set ChartDir for directory-based charts to enable $ref resolution.
// Archived charts (.tgz) are loaded into memory without filesystem extraction,
// so $ref resolution is not supported for them.
if fi, err := os.Stat(chartPath); err == nil && fi.IsDir() {
client.ChartDir = chartPath
}
p := getter.All(settings)
vals, err := valueOpts.MergeValues(p)
if err != nil {

@ -190,6 +190,12 @@ func TestUpgradeCmd(t *testing.T) {
golden: "output/upgrade-uninstalled-with-keep-history.txt",
rels: []*release.Release{relWithStatusMock("funny-bunny", 2, ch, rcommon.StatusUninstalled)},
},
{
name: "upgrade with schema file containing $ref",
cmd: "upgrade reftest testdata/testcharts/chart-with-schema-ref",
golden: "output/upgrade-schema-ref.txt",
rels: []*release.Release{relMock("reftest", 1, ch)},
},
}
runTestCmd(t, tests)
}

Loading…
Cancel
Save