generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from meshery/nithish/refactor/compose_validation
[Refactor] Use CUE to validate docker compose files
- Loading branch information
Showing
9 changed files
with
176 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "meshkit", | ||
"type": "library", | ||
"next_error_code": 11083 | ||
"next_error_code": 11085 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package models | ||
|
||
// anything that can be validated is a Validator | ||
type Validator interface { | ||
Validate([]byte) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package kompose | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"cuelang.org/go/cmd/cue/cmd" | ||
"cuelang.org/go/cue/cuecontext" | ||
"cuelang.org/go/encoding/yaml" | ||
"github.com/layer5io/meshkit/utils" | ||
) | ||
|
||
type DockerComposeFile []byte | ||
|
||
// TODO: This should be dynamic | ||
// once we move to using encoding/jsonschema, this will be removed | ||
var ( | ||
DefaultSpecDirectory = filepath.Join(utils.GetHome(), ".meshery", "cue") | ||
DefaultSpecPath = filepath.Join(DefaultSpecDirectory, "compose-spec.cue") | ||
) | ||
|
||
func generateCueSchema(jsonSchema []byte, dir string) error { | ||
err := os.MkdirAll(dir, os.ModePerm) | ||
if err != nil { | ||
return ErrValidateDockerComposeFile(err) | ||
} | ||
err = utils.CreateFile(jsonSchema, "compose-spec.json", dir) | ||
if err != nil { | ||
return ErrValidateDockerComposeFile(err) | ||
} | ||
defer os.Remove(filepath.Join(DefaultSpecDirectory, "compose-spec.json")) | ||
//load and parse the schema | ||
// NOTE: this approach makes the process slow and is not recommended | ||
// TODO: use encoding/jsonschema. | ||
importCmd, err := cmd.New([]string{"import", "-f", "--files", filepath.Join(DefaultSpecDirectory, "compose-spec.json")}) | ||
if err != nil { | ||
return ErrValidateDockerComposeFile(err) | ||
} | ||
// no need for logs | ||
importCmd.SetOut(io.Discard) | ||
err = importCmd.Run(context.TODO()) | ||
if err != nil { | ||
return ErrValidateDockerComposeFile(err) | ||
} | ||
return nil | ||
} | ||
|
||
func (dc *DockerComposeFile) Validate(schema []byte) error { | ||
cueCtx := cuecontext.New() | ||
// check if the spec is already present | ||
if _, err := os.Stat(DefaultSpecPath); err != nil { | ||
// if not, create the spec | ||
err = generateCueSchema(schema, DefaultSpecDirectory) | ||
if err != nil { | ||
return ErrValidateDockerComposeFile(err) | ||
} | ||
} | ||
|
||
cueSchema, err := utils.ReadLocalFile(DefaultSpecPath) | ||
if err != nil { | ||
return ErrValidateDockerComposeFile(err) | ||
} | ||
sv := cueCtx.CompileString(cueSchema) | ||
if sv.Err() != nil { | ||
return ErrValidateDockerComposeFile(sv.Err()) | ||
} | ||
err = yaml.Validate(*dc, sv) | ||
if err != nil { | ||
return ErrValidateDockerComposeFile(err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.