Skip to content

Commit

Permalink
feat!: replace manual types with generated types and cut a v1 module
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza committed Jan 25, 2024
1 parent 992082d commit b6ae672
Show file tree
Hide file tree
Showing 13 changed files with 776 additions and 561 deletions.
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# score-go
Reference library for the parsing and loading SCORE files

Reference library for the parsing and loading SCORE files in Go.

This can be added to your project via:

```sh
$ go get -u github.com/score-spec/score-go/v1
```

## Parsing SCORE files

Expand All @@ -10,8 +17,8 @@ import (
"io"
"os"

"github.com/score-spec/score-go/loader"
score "github.com/score-spec/score-go/types"
"github.com/score-spec/score-go/v1/loader"
score "github.com/score-spec/score-go/v1/types"
)

func main() {
Expand Down Expand Up @@ -40,3 +47,25 @@ func main() {
}

```

## Upgrading the schema version

When the Score JSON schema is updated in https://github.com/score-spec/schema, this repo should be updated to match.

First update the subtree:

```
git subtree pull --prefix schema/files [email protected]:score-spec/schema.git main --squash
```

Then regenerate the defined types:

```
go generate -v ./...
```

And ensure the tests still pass:

```
go test -v ./...
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/score-spec/score-go
module github.com/score-spec/score-go/v1

go 1.19
go 1.21

require (
github.com/mitchellh/mapstructure v1.5.0
Expand Down
5 changes: 3 additions & 2 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
"io"

"github.com/mitchellh/mapstructure"
"github.com/score-spec/score-go/types"
"gopkg.in/yaml.v3"

"github.com/score-spec/score-go/v1/types"
)

// ParseYAML parses YAML into the target mapping structure.
Expand All @@ -22,7 +23,7 @@ func ParseYAML(dest *map[string]interface{}, r io.Reader) error {
}

// MapSpec converts the source mapping structure into the target WorkloadSpec.
func MapSpec(dest *types.WorkloadSpec, src map[string]interface{}) error {
func MapSpec(dest *types.Workload, src map[string]interface{}) error {
mapper, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: dest,
TagName: "json",
Expand Down
95 changes: 55 additions & 40 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@ import (
"io"
"testing"

"github.com/score-spec/score-go/types"
"github.com/stretchr/testify/assert"

"github.com/score-spec/score-go/v1/types"
)

func stringRef(input string) *string {
return &input
}

func intRef(input int) *int {
return &input
}

func boolRef(input bool) *bool {
return &input
}

func schemeRef(input types.HttpProbeScheme) *types.HttpProbeScheme {
return &input
}

func TestDecodeYaml(t *testing.T) {
var tests = []struct {
Name string
Source io.Reader
Output types.WorkloadSpec
Output types.Workload
Error error
}{
{
Expand Down Expand Up @@ -113,84 +130,82 @@ resources:
}
}
`)),
Output: types.WorkloadSpec{
Output: types.Workload{
ApiVersion: "score.dev/v1b1",
Metadata: types.WorkloadMeta{
Metadata: types.WorkloadMetadata{
Name: "hello-world",
},
Service: types.ServiceSpec{
Ports: types.ServicePortsSpecs{
"www": types.ServicePortSpec{
Service: &types.WorkloadService{
Ports: types.WorkloadServicePorts{
"www": types.ServicePort{
Port: 80,
Protocol: "",
TargetPort: 8080,
TargetPort: intRef(8080),
},
},
},
Containers: types.ContainersSpecs{
"hello": types.ContainerSpec{
Containers: types.WorkloadContainers{
"hello": types.Container{
Image: "busybox",
Command: []string{"/bin/echo"},
Args: []string{"Hello $(FRIEND)"},
Variables: map[string]string{
"FRIEND": "World!",
},
Files: []types.FileMountSpec{
Files: []types.ContainerFilesElem{
{
Target: "/etc/hello-world/config.yaml",
Mode: "666",
Source: "",
Mode: stringRef("666"),
Content: "---\n${resources.env.APP_CONFIG}\n",
NoExpand: true,
NoExpand: boolRef(true),
},
},
Volumes: []types.VolumeMountSpec{
Volumes: []types.ContainerVolumesElem{
{
Source: "${resources.data}",
Path: "sub/path",
Path: stringRef("sub/path"),
Target: "/mnt/data",
ReadOnly: true,
ReadOnly: boolRef(true),
},
},
Resources: types.ContainerResourcesRequirementsSpec{
Limits: map[string]interface{}{
"memory": "128Mi",
"cpu": "500m",
Resources: &types.ContainerResources{
Limits: &types.ResourcesLimits{
Memory: stringRef("128Mi"),
Cpu: stringRef("500m"),
},
Requests: map[string]interface{}{
"memory": "64Mi",
"cpu": "250m",
Requests: &types.ResourcesLimits{
Memory: stringRef("64Mi"),
Cpu: stringRef("250m"),
},
},
LivenessProbe: types.ContainerProbeSpec{
HTTPGet: types.HTTPGetActionSpec{
LivenessProbe: &types.ContainerProbe{
HttpGet: &types.HttpProbe{
Path: "/alive",
Port: 8080,
Port: intRef(8080),
},
},
ReadinessProbe: types.ContainerProbeSpec{
HTTPGet: types.HTTPGetActionSpec{
Host: "1.1.1.1",
Scheme: "HTTPS",
ReadinessProbe: &types.ContainerProbe{
HttpGet: &types.HttpProbe{
Host: stringRef("1.1.1.1"),
Scheme: schemeRef(types.HttpProbeSchemeHTTPS),
Path: "/ready",
Port: 8080,
HTTPHeaders: []types.HTTPHeaderSpec{
{Name: "Custom-Header", Value: "Awesome"},
Port: intRef(8080),
HttpHeaders: []types.HttpProbeHttpHeadersElem{
{Name: stringRef("Custom-Header"), Value: stringRef("Awesome")},
},
},
},
},
},
Resources: map[string]types.ResourceSpec{
Resources: types.WorkloadResources{
"env": {
Type: "environment",
},
"dns": {Type: "dns", Class: "sensitive"},
"dns": {Type: "dns", Class: stringRef("sensitive")},
"data": {Type: "volume"},
"db": {
Type: "postgres",
Class: "large",
Metadata: types.ResourceMeta{
Class: stringRef("large"),
Metadata: &types.ResourceMetadata{
Annotations: map[string]string{
"my.org/version": "0.1",
},
Expand All @@ -212,7 +227,7 @@ resources:
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
var srcMap map[string]interface{}
var spec types.WorkloadSpec
var spec types.Workload

var err = ParseYAML(&srcMap, tt.Source)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions schema/files/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# JSON schemas for Score files

| version | file |
| --- | --- |
| version | file |
|----------|-----------------|
| v1-beta1 | score-v1b1.json |

## Embed schemas into project
Expand Down
Loading

0 comments on commit b6ae672

Please sign in to comment.