Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for schema version and allow unknown fields to keep backwards compatibility with old designs with minor undocumented schema changes #686

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions files/identification.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/layer5io/meshkit/utils/kubernetes/kompose"
"github.com/layer5io/meshkit/utils/walker"
"github.com/meshery/schemas/models/core"
"github.com/meshery/schemas/models/v1beta1"
"github.com/meshery/schemas/models/v1beta1/pattern"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -162,6 +163,9 @@ func ParseCompressedOCIArtifactIntoDesign(artifact []byte) (*pattern.PatternFile

return &patternFile, nil
}

// we are allowing unknown fields to keep compabitibilty with old designs when
// we make unversioned changes to schema
func ParseFileAsMesheryDesign(file SanitizedFile) (pattern.PatternFile, error) {

var parsed pattern.PatternFile
Expand All @@ -171,17 +175,29 @@ func ParseFileAsMesheryDesign(file SanitizedFile) (pattern.PatternFile, error) {
case ".yml", ".yaml":

decoder := yaml.NewDecoder(bytes.NewReader(file.RawData))
decoder.KnownFields(true)
err := decoder.Decode(&parsed)
return parsed, err
if err != nil {
return pattern.PatternFile{}, err
}
if parsed.SchemaVersion == v1beta1.DesignSchemaVersion {
return parsed, err
} else {
return pattern.PatternFile{}, utils.ErrInvalidConstructSchemaVersion("design", parsed.SchemaVersion, v1beta1.DesignSchemaVersion)
}

case ".json":

decoder := json.NewDecoder(bytes.NewReader(file.RawData))

decoder.DisallowUnknownFields()
err := decoder.Decode(&parsed)
return parsed, err

if err != nil {
return pattern.PatternFile{}, err
}
if parsed.SchemaVersion == v1beta1.DesignSchemaVersion {
return parsed, err
}
return pattern.PatternFile{}, utils.ErrInvalidConstructSchemaVersion("design", parsed.SchemaVersion, v1beta1.DesignSchemaVersion)

case ".tgz", ".tar", ".tar.gz", ".zip": // try to parse oci artifacts
parsed_design, err := ParseCompressedOCIArtifactIntoDesign(file.RawData)
Expand Down
13 changes: 13 additions & 0 deletions utils/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ var (

ErrWritingIntoFileCode = "meshkit-11281"
)

func ErrInvalidConstructSchemaVersion(contruct string, version string, supportedVersion string) error {

return errors.New(
ErrInvalidSchemaVersionCode,
errors.Critical,
[]string{"Invalid schema version " + version},
[]string{"The `schemaVersion` key is either empty or has an incorrect value."},
[]string{fmt.Sprintf("The schema is not of type '%s'", contruct)},
[]string{"Verify that `schemaVersion` key should be '%s'", supportedVersion},
)
}

var (
ErrExtractType = errors.New(
ErrUnmarshalTypeCode,
Expand Down
Loading