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

Replace manual types with generated types and cut a new tag for v1 #23

Merged
merged 1 commit into from
Jan 26, 2024
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
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# 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
```

**NOTE**: if you project is still using the hand-written types, you will need to stay on `github.com/score-spec/score-go`
and any important fixes to the schema may be back-ported to that branch.

## Parsing SCORE files

Expand All @@ -10,8 +20,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 +50,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
Loading