Skip to content

Commit

Permalink
feat: update score spec to 0.2.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
astromechza committed Jan 17, 2025
1 parent eb4256a commit 9e3eb99
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 343 deletions.
1 change: 1 addition & 0 deletions loader/fixtures/test_binary_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
]RΌL��R�1����+��
17 changes: 15 additions & 2 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ containers:
---
${resources.env.APP_CONFIG}
noExpand: true
- target: /etc/hello-world/binary
binaryContent: aGVsbG8=
volumes:
- source: ${resources.data}
path: sub/path
Expand All @@ -105,6 +107,10 @@ containers:
httpGet:
path: /alive
port: 8080
exec:
command:
- echo
- hello
readinessProbe:
httpGet:
host: "1.1.1.1"
Expand Down Expand Up @@ -166,6 +172,10 @@ resources:
Content: stringRef("---\n${resources.env.APP_CONFIG}\n"),
NoExpand: boolRef(true),
},
{
Target: "/etc/hello-world/binary",
BinaryContent: stringRef("aGVsbG8="),
},
},
Volumes: []types.ContainerVolumesElem{
{
Expand All @@ -186,13 +196,16 @@ resources:
},
},
LivenessProbe: &types.ContainerProbe{
HttpGet: types.HttpProbe{
HttpGet: &types.HttpProbe{
Path: "/alive",
Port: 8080,
},
Exec: &types.ExecProbe{
Command: []string{"echo", "hello"},
},
},
ReadinessProbe: &types.ContainerProbe{
HttpGet: types.HttpProbe{
HttpGet: &types.HttpProbe{
Host: stringRef("1.1.1.1"),
Scheme: schemeRef(types.HttpProbeSchemeHTTPS),
Path: "/ready",
Expand Down
20 changes: 11 additions & 9 deletions loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package loader

import (
"encoding/base64"
"fmt"
"os"
"path/filepath"
Expand All @@ -33,9 +34,14 @@ func Normalize(w *types.Workload, baseDir string) error {
if err != nil {
return fmt.Errorf("embedding file '%s' for container '%s': %w", *f.Source, name, err)
}

c.Files[i].Source = nil
c.Files[i].Content = &raw
if utf8.Valid(raw) {
content := string(raw)
c.Files[i].Content = &content
} else {
content := base64.StdEncoding.EncodeToString(raw)
c.Files[i].BinaryContent = &content
}
}
}
}
Expand All @@ -44,19 +50,15 @@ func Normalize(w *types.Workload, baseDir string) error {
}

// readFile reads a text file into memory
func readFile(baseDir, path string) (string, error) {
func readFile(baseDir, path string) ([]byte, error) {
if !filepath.IsAbs(path) {
path = filepath.Join(baseDir, path)
}

raw, err := os.ReadFile(path)
if err != nil {
return "", err
}

if !utf8.Valid(raw) {
return "", fmt.Errorf("file contains non-utf8 characters")
return nil, err
}

return string(raw), nil
return raw, nil
}
13 changes: 11 additions & 2 deletions loader/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"io"
"testing"

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

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

func TestNormalize(t *testing.T) {
Expand All @@ -32,7 +33,7 @@ func TestNormalize(t *testing.T) {
Error error
}{
{
Name: "Embeds source file",
Name: "Embeds source files",
Input: &types.Workload{
ApiVersion: "score.dev/v1b1",
Metadata: types.WorkloadMetadata{
Expand All @@ -47,6 +48,10 @@ func TestNormalize(t *testing.T) {
Mode: stringRef("666"),
NoExpand: boolRef(true),
},
{
Source: stringRef("./test_binary_file"),
Target: "/etc/hello-world/binary",
},
},
},
},
Expand All @@ -65,6 +70,10 @@ func TestNormalize(t *testing.T) {
Content: stringRef("Hello World\n"),
NoExpand: boolRef(true),
},
{
Target: "/etc/hello-world/binary",
BinaryContent: stringRef("XVLOjEyq5FKgHDGMAYMdp+crq4I="),
},
},
},
},
Expand Down
41 changes: 24 additions & 17 deletions schema/files/samples/score-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,40 @@ containers:
variables:
SOME_VAR: some content here
files:
- target: /my/file
mode: "0600"
source: file.txt
- target: /my/other/file
content: |
some multiline
content
- target: /my/file
mode: "0600"
source: file.txt
- target: /my/other/file
content: |
some multiline
content
- target: /my/other/binaryfile
binaryContent: ADBgwpA=
volumes:
- source: volume-name
target: /mnt/something
path: /sub/path
readOnly: false
- source: volume-two
target: /mnt/something-else
- source: volume-name
target: /mnt/something
path: /sub/path
readOnly: false
- source: volume-two
target: /mnt/something-else
livenessProbe:
httpGet:
port: 8080
path: /livez
exec:
command:
- /bin/curl
- -f
- "http://localhost:8080/livez"
readinessProbe:
httpGet:
host: 127.0.0.1
port: 80
scheme: HTTP
path: /readyz
httpHeaders:
- name: SOME_HEADER
value: some-value-here
- name: SOME_HEADER
value: some-value-here
container-two2:
image: .
resources:
Expand All @@ -70,6 +77,6 @@ resources:
data: here
resource-two2:
type: Resource-Two
resource.three:
resource-three:
type: Type-Three
id: shared-type-three
id: shared-type-three
35 changes: 31 additions & 4 deletions schema/files/score-v1b1.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
"description": "The extra files to mount into the container.",
"type": "array",
"items": {
"description": "The details of a file to mount in the container. One of 'source', 'content', or 'binaryContent' must be provided.",
"type": "object",
"required": [
"target"
Expand All @@ -257,7 +258,11 @@
"minLength": 1
},
"content": {
"description": "The inline content for the file.",
"description": "The inline content for the file. Only supports valid utf-8.",
"type": "string"
},
"binaryContent": {
"description": "Inline standard-base64 encoded content for the file. Does not support placeholder expansion.",
"type": "string"
},
"noExpand": {
Expand All @@ -272,6 +277,12 @@
"content"
]
},
{
"required": [
"target",
"binaryContent"
]
},
{
"required": [
"target",
Expand Down Expand Up @@ -338,13 +349,29 @@
},
"containerProbe": {
"type": "object",
"required": [
"httpGet"
],
"description": "The probe may be defined as either http, command execution, or both. The execProbe should be preferred if the Score implementation supports both types.",
"additionalProperties": false,
"properties": {
"httpGet": {
"$ref": "#/$defs/httpProbe"
},
"exec": {
"$ref": "#/$defs/execProbe"
}
}
},
"execProbe": {
"description": "An executable health probe.",
"type": "object",
"additionalProperties": false,
"required": ["command"],
"properties": {
"command": {
"description": "The command and arguments to execute within the container.",
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
49 changes: 40 additions & 9 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ containers:
- target: /etc/hello-world/config.yaml
mode: "666"
content: "${resources.env.APP_CONFIG}"
- target: /etc/hello-world/binary
mode: "755"
binaryContent: "aGVsbG8="
volumes:
- source: ${resources.data}
path: sub/path
Expand All @@ -65,6 +68,10 @@ containers:
httpGet:
path: /alive
port: 8080
exec:
command:
- echo
- hello
readinessProbe:
httpGet:
path: /ready
Expand Down Expand Up @@ -599,7 +606,19 @@ func TestSchema(t *testing.T) {
Message: "/containers/hello/files/0/source",
},
{
Name: "containers.*.files.*.noExpand isset to true",
Name: "containers.*.files.*.binaryContent is bad format",
Src: func() map[string]interface{} {
src := newTestDocument()
var hello = src["containers"].(map[string]interface{})["hello"].(map[string]interface{})
var file = hello["files"].([]interface{})[0].(map[string]interface{})
delete(file, "content")
file["binaryContent"] = map[string]interface{}{}
return src
}(),
Message: "/containers/hello/files/0/binaryContent",
},
{
Name: "containers.*.files.*.noExpand is set to true",
Src: func() map[string]interface{} {
src := newTestDocument()
var hello = src["containers"].(map[string]interface{})["hello"].(map[string]interface{})
Expand Down Expand Up @@ -988,17 +1007,29 @@ func TestSchema(t *testing.T) {
Message: "/containers/hello/livenessProbe",
},
{
Name: "containers.*.livenessProbe is empty",
Name: "containers.*.livenessProbe.exec is nil",
Src: func() map[string]interface{} {
src := newTestDocument()
var hello = src["containers"].(map[string]interface{})["hello"].(map[string]interface{})
hello["livenessProbe"] = map[string]interface{}{}
hello["livenessProbe"].(map[string]interface{})["exec"] = nil
return src
}(),
Message: "/containers/hello/livenessProbe",
Message: "/containers/hello/livenessProbe/exec",
},
{
Name: "containers.*.livenessProbe.httpGet is not set",
Name: "containers.*.livenessProbe.exec is bad",
Src: func() map[string]interface{} {
src := newTestDocument()
var hello = src["containers"].(map[string]interface{})["hello"].(map[string]interface{})
hello["livenessProbe"].(map[string]interface{})["exec"] = map[string]interface{}{
"command": true,
}
return src
}(),
Message: "/containers/hello/livenessProbe/exec/command",
},
{
Name: "containers.*.livenessProbe.httpGet is nil",
Src: func() map[string]interface{} {
src := newTestDocument()
var hello = src["containers"].(map[string]interface{})["hello"].(map[string]interface{})
Expand Down Expand Up @@ -1228,14 +1259,14 @@ func TestSchema(t *testing.T) {
Message: "/containers/hello/readinessProbe",
},
{
Name: "containers.*.readinessProbe is empty",
Name: "containers.*.readinessProbe.exec is nil",
Src: func() map[string]interface{} {
src := newTestDocument()
var hello = src["containers"].(map[string]interface{})["hello"].(map[string]interface{})
hello["readinessProbe"] = map[string]interface{}{}
hello["readinessProbe"].(map[string]interface{})["exec"] = nil
return src
}(),
Message: "/containers/hello/readinessProbe",
Message: "/containers/hello/readinessProbe/exec",
},
{
Name: "containers.*.readinessProbe.httpGet is not set",
Expand All @@ -1245,7 +1276,7 @@ func TestSchema(t *testing.T) {
hello["readinessProbe"].(map[string]interface{})["httpGet"] = nil
return src
}(),
Message: "/containers/hello/readinessProbe",
Message: "/containers/hello/readinessProbe/httpGet",
},
{
Name: "containers.*.readinessProbe.httpGet.path is missing",
Expand Down
2 changes: 2 additions & 0 deletions schema/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ containers:
- target: /etc/hello-world/config.yaml
mode: "666"
content: "${resources.env.APP_CONFIG}"
- target: /etc/hello-world/binary
content: "aGVsbG8="
volumes:
- source: ${resources.data}
path: sub/path
Expand Down
Loading

0 comments on commit 9e3eb99

Please sign in to comment.