Skip to content

Commit e9d1e7f

Browse files
Merge branch 'master' into master
2 parents 7807f22 + de78414 commit e9d1e7f

File tree

15 files changed

+2438
-15
lines changed

15 files changed

+2438
-15
lines changed

files/error.go

+71
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@ package files
33
import (
44
"fmt"
55
"maps"
6+
"path/filepath"
67
"slices"
78
"strings"
89

910
"github.com/layer5io/meshkit/errors"
1011
)
1112

13+
var (
14+
ErrFileReadCode = "meshkit-11304"
15+
)
16+
17+
func ErrFileRead(err error) error {
18+
return errors.New(ErrFileReadCode, errors.Alert,
19+
[]string{"File read error"},
20+
[]string{err.Error()},
21+
[]string{"The provided file is not present or has an invalid path."},
22+
[]string{"To proceed, provide a valid file path with a valid file."})
23+
}
24+
1225
var (
1326
// Error code
1427
ErrUnsupportedExtensionCode = "meshkit-11282"
@@ -92,12 +105,14 @@ func ErrInvalidYaml(fileName string, err error) error {
92105
probableCause := []string{
93106
"The YAML file may contain syntax errors, such as incorrect indentation, missing colons, or invalid characters.",
94107
"The file may have been corrupted or improperly edited.",
108+
"The YAML does not conform to the Meshery model schema.",
95109
}
96110

97111
remedy := []string{
98112
"Review the YAML syntax in the file and correct any errors.",
99113
"Use a YAML validator or linter to identify and fix issues.",
100114
"Ensure the file adheres to the YAML specification.",
115+
"Ensure the YAML conforms to the Meshery model schema. You can refer to the following documentation for more details: https://docs.meshery.io/project/contributing/contributing-models",
101116
}
102117

103118
return errors.New(ErrInvalidYamlCode, errors.Critical, sdescription, ldescription, probableCause, remedy)
@@ -117,13 +132,15 @@ func ErrInvalidJson(fileName string, err error) error {
117132
"The JSON file may contain syntax errors, such as missing commas, curly braces, or square brackets.",
118133
"The file may have been corrupted or improperly edited.",
119134
"Special characters or escape sequences may not have been properly formatted.",
135+
"The JSON does not conform to the Meshery model schema.",
120136
}
121137

122138
remedy := []string{
123139
"Review the JSON syntax in the file and correct any errors.",
124140
"Use a JSON validator or linter to identify and fix issues.",
125141
"Ensure the file adheres to the JSON specification (e.g., double quotes for keys and strings).",
126142
"Check for common issues like trailing commas or unescaped special characters.",
143+
"Ensure the JSON conforms to the Meshery model schema. You can refer to the following documentation for more details: https://docs.meshery.io/project/contributing/contributing-models",
127144
}
128145

129146
return errors.New(ErrInvalidJsonCode, errors.Critical, sdescription, ldescription, probableCause, remedy)
@@ -342,3 +359,57 @@ func ErrWaklingLocalDirectory(err error) error {
342359
func ErrDecodePattern(err error) error {
343360
return errors.New(ErrDecodePatternCode, errors.Alert, []string{"Error failed to decode design data into go slice"}, []string{err.Error()}, []string{}, []string{})
344361
}
362+
363+
var (
364+
ErrInvalidModelCode = "meshkit-11301"
365+
ErrInvalidModelArchiveCode = "meshkit-11302"
366+
ErrEmptyModelCode = "meshkit-11303"
367+
)
368+
369+
func ErrInvalidModel(operation string, filename string, err error) error {
370+
// return error based on file extension
371+
fileExt := filepath.Ext(filename)
372+
373+
switch {
374+
case fileExt == ".json":
375+
return ErrInvalidJson(filename, err)
376+
case fileExt == ".yaml", fileExt == ".yml":
377+
return ErrInvalidYaml(filename, err)
378+
case strings.HasPrefix(fileExt, ".tar"), strings.HasPrefix(fileExt, ".tgz"), strings.HasPrefix(fileExt, ".tar.gz"):
379+
// check prefix as random numeric suffix is appended to archive during file handling (eg: .tar becomes .tar263831)
380+
return ErrInvalidModelArchive(filename, err)
381+
default:
382+
supportedExtensions := slices.Collect(maps.Keys(ValidIacExtensions))
383+
supportedExtensions = slices.DeleteFunc(supportedExtensions, func(ext string) bool {
384+
return ext == ".zip"
385+
})
386+
return ErrUnsupportedExtensionForOperation(operation, filename, fileExt, supportedExtensions)
387+
}
388+
}
389+
390+
func ErrInvalidModelArchive(fileName string, err error) error {
391+
sdescription := []string{
392+
fmt.Sprintf("Failed to extract the archive '%s'.", fileName),
393+
}
394+
395+
ldescription := []string{
396+
fmt.Sprintf("An error occurred while attempting to extract the TAR archive '%s'.", fileName),
397+
fmt.Sprintf("Error details: %s", err.Error()),
398+
}
399+
400+
probableCause := []string{
401+
"The archive may be non OCI compliant.",
402+
"The archive may have been created with a different tool or version that is incompatible.",
403+
}
404+
405+
remedy := []string{
406+
"Make sure the archive is OCI compliant. Meshery Models should be OCI compliant archive.",
407+
"Ensure the archive is created using a compatible tool (eg: ORAS) and version that follows OCI standards.",
408+
}
409+
410+
return errors.New(ErrInvalidModelArchiveCode, errors.Critical, sdescription, ldescription, probableCause, remedy)
411+
}
412+
413+
func ErrEmptyModel() error {
414+
return errors.New(ErrEmptyModelCode, errors.Alert, []string{"No component found in model provided."}, []string{"No component found in model provided. Models must have at least one component."}, []string{}, []string{})
415+
}

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/fluxcd/pkg/tar v0.10.0
1919
github.com/go-git/go-git/v5 v5.13.2
2020
github.com/go-logr/logr v1.4.2
21+
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
2122
github.com/gofrs/uuid v4.4.0+incompatible
2223
github.com/google/go-containerregistry v0.20.3
2324
github.com/google/uuid v1.6.0
@@ -32,6 +33,7 @@ require (
3233
github.com/spf13/cobra v1.8.1
3334
github.com/spf13/viper v1.19.0
3435
golang.org/x/oauth2 v0.25.0
36+
golang.org/x/sync v0.10.0
3537
golang.org/x/text v0.21.0
3638
google.golang.org/api v0.218.0
3739
gopkg.in/yaml.v2 v2.4.0
@@ -247,7 +249,6 @@ require (
247249
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 // indirect
248250
golang.org/x/mod v0.22.0 // indirect
249251
golang.org/x/net v0.34.0 // indirect
250-
golang.org/x/sync v0.10.0 // indirect
251252
golang.org/x/sys v0.29.0 // indirect
252253
golang.org/x/term v0.28.0 // indirect
253254
golang.org/x/time v0.9.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIx
257257
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
258258
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
259259
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
260+
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 h1:FWNFq4fM1wPfcK40yHE5UO3RUdSNPaBC+j3PokzA6OQ=
261+
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
260262
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
261263
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
262264
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=

helpers/component_info.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "meshkit",
33
"type": "library",
4-
"next_error_code": 11301
4+
"next_error_code": 11311
55
}

models/registration/dir.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/layer5io/meshkit/models/meshmodel/entity"
1010
"github.com/layer5io/meshkit/models/oci"
1111

12+
meshkitFileUtils "github.com/layer5io/meshkit/files"
1213
"github.com/layer5io/meshkit/utils"
1314

1415
"github.com/meshery/schemas/models/v1alpha3/relationship"
@@ -48,12 +49,7 @@ func (d Dir) PkgUnit(regErrStore RegistrationErrorStore) (_ PackagingUnit, err e
4849
// Process the path (file or directory)
4950
err = processDir(d.dirpath, &pkg, regErrStore)
5051
if err != nil {
51-
modelName := ""
52-
if !reflect.ValueOf(pkg.Model).IsZero() {
53-
modelName = pkg.Model.Name
54-
}
55-
regErrStore.InsertEntityRegError("", modelName, entity.EntityType("unknown"), filename, ErrDirPkgUnitParseFail(d.dirpath, fmt.Errorf("could not process the path: %w", err)))
56-
return pkg, ErrDirPkgUnitParseFail(d.dirpath, fmt.Errorf("could not process the path: %w", err))
52+
return pkg, err
5753
}
5854

5955
if reflect.ValueOf(pkg.Model).IsZero() {
@@ -105,6 +101,7 @@ func processDir(dirPath string, pkg *PackagingUnit, regErrStore RegistrationErro
105101
tempDirs = append(tempDirs, tempDir)
106102
err = oci.UnCompressOCIArtifact(path, tempDir)
107103
if err != nil {
104+
err := meshkitFileUtils.ErrUnCompressOCIArtifact(err)
108105
regErrStore.InsertEntityRegError("", "", entity.EntityType("unknown"), filepath.Base(path), err)
109106
regErrStore.AddInvalidDefinition(path, err)
110107
return nil
@@ -127,6 +124,7 @@ func processDir(dirPath string, pkg *PackagingUnit, regErrStore RegistrationErro
127124
}
128125
tempDirs = append(tempDirs, tempDir)
129126
if err := utils.ExtractFile(path, tempDir); err != nil {
127+
err := meshkitFileUtils.ErrFailedToExtractArchive(filepath.Base(path), err)
130128
regErrStore.InsertEntityRegError("", "", entity.EntityType("unknown"), filepath.Base(path), err)
131129
regErrStore.AddInvalidDefinition(path, err)
132130
return nil
@@ -139,16 +137,20 @@ func processDir(dirPath string, pkg *PackagingUnit, regErrStore RegistrationErro
139137
}
140138

141139
content := data
142-
content, err = utils.YAMLToJSON(content)
143-
if err != nil {
144-
regErrStore.InsertEntityRegError("", "", entity.EntityType("unknown"), filepath.Base(path), err)
145-
return nil
140+
if utils.IsYaml(path) {
141+
content, err = utils.YAMLToJSON(content)
142+
if err != nil {
143+
regErrStore.InsertEntityRegError("", "", entity.EntityType("unknown"), filepath.Base(path), err)
144+
return nil
145+
}
146146
}
147+
147148
// Determine the entity type
148149
entityType, err := utils.FindEntityType(content)
149150
if err != nil {
150-
regErrStore.InsertEntityRegError("", "", entity.EntityType("unknown"), filepath.Base(path), err)
151-
regErrStore.AddInvalidDefinition(path, err)
151+
errMsg := meshkitFileUtils.ErrInvalidModel("import", filepath.Base(path), err)
152+
regErrStore.InsertEntityRegError("", filepath.Base(path), entity.EntityType("unknown"), filepath.Base(path), errMsg)
153+
regErrStore.AddInvalidDefinition(path, errMsg)
152154
return nil
153155
}
154156

0 commit comments

Comments
 (0)