Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
fix: update score-go to 1.1.0 and update go to 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
astromechza committed Feb 27, 2024
1 parent 6355a99 commit 4e15ce1
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 152 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/score-spec/score-humanitec

go 1.19
go 1.21

require (
github.com/golang/mock v1.6.0
github.com/imdario/mergo v0.3.13
github.com/mitchellh/mapstructure v1.5.0
github.com/score-spec/score-go v0.0.0-20231024094556-308a07ae2536
github.com/score-spec/score-go v1.1.0
github.com/sendgrid/rest v2.6.9+incompatible
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.8.1
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/score-spec/score-go v0.0.0-20230905115428-131acdd2f5cf h1:0Dt+qyYoGTXPPU5Xq/KxLDtQMMA8hX+oJ8EsjFWUajQ=
github.com/score-spec/score-go v0.0.0-20230905115428-131acdd2f5cf/go.mod h1:3l9mvrtYKzxXDQVcYkQBD3ABTPkTzWhUMYNfGlpctoo=
github.com/score-spec/score-go v0.0.0-20231024093959-ed5a5d548352 h1:yycML7iVHv/65wo4ao7uH5cvIYgQhB184iKAy7suNBk=
github.com/score-spec/score-go v0.0.0-20231024093959-ed5a5d548352/go.mod h1:3l9mvrtYKzxXDQVcYkQBD3ABTPkTzWhUMYNfGlpctoo=
github.com/score-spec/score-go v0.0.0-20231024094556-308a07ae2536 h1:WBWWWSbAR2oJapUZyCm4IZ5LJfkT5IUlHtmcJNBLFiY=
github.com/score-spec/score-go v0.0.0-20231024094556-308a07ae2536/go.mod h1:3l9mvrtYKzxXDQVcYkQBD3ABTPkTzWhUMYNfGlpctoo=
github.com/score-spec/score-go v1.1.0 h1:63WM1u93NtGgMuPtVZ/UBfzg/BpYuY8sBquaL0BkrXU=
github.com/score-spec/score-go v1.1.0/go.mod h1:nt6TOq2Ld9SiH3Fd9NF8tiJ9L7S17OE3FNgCrSet5GQ=
github.com/sendgrid/rest v2.6.9+incompatible h1:1EyIcsNdn9KIisLW50MKwmSRSK+ekueiEMJ7NEoxJo0=
github.com/sendgrid/rest v2.6.9+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE=
github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI=
Expand Down
18 changes: 14 additions & 4 deletions internal/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (

"github.com/imdario/mergo"
"github.com/mitchellh/mapstructure"
"github.com/score-spec/score-humanitec/internal/humanitec"
"github.com/score-spec/score-humanitec/internal/humanitec/extensions"
"github.com/spf13/cobra"
"github.com/tidwall/sjson"

"github.com/score-spec/score-humanitec/internal/humanitec"
"github.com/score-spec/score-humanitec/internal/humanitec/extensions"

yaml "gopkg.in/yaml.v3"

loader "github.com/score-spec/score-go/loader"
Expand Down Expand Up @@ -86,7 +87,7 @@ func run(cmd *cobra.Command, args []string) error {
return nil
}

func loadSpec(scoreFile, overridesFile, extensionsFile string, skipValidation bool) (*score.WorkloadSpec, *extensions.HumanitecExtensionsSpec, error) {
func loadSpec(scoreFile, overridesFile, extensionsFile string, skipValidation bool) (*score.Workload, *extensions.HumanitecExtensionsSpec, error) {
// Open source file
//
log.Printf("Reading '%s'...\n", scoreFile)
Expand Down Expand Up @@ -188,6 +189,15 @@ func loadSpec(scoreFile, overridesFile, extensionsFile string, skipValidation bo
}
}

// Apply upgrades to fix backports or backward incompatible things
if changes, err := schema.ApplyCommonUpgradeTransforms(srcMap); err != nil {
return nil, nil, fmt.Errorf("failed to upgrade spec: %w", err)
} else if len(changes) > 0 {
for _, change := range changes {
log.Printf("Applying upgrade to specification: %s\n", change)
}
}

// Validate SCORE spec
//
if !skipValidation {
Expand All @@ -200,7 +210,7 @@ func loadSpec(scoreFile, overridesFile, extensionsFile string, skipValidation bo
// Convert SCORE spec
//

var spec score.WorkloadSpec
var spec score.Workload
log.Print("Applying SCORE spec...\n")
if err = mapstructure.Decode(srcMap, &spec); err != nil {
return nil, nil, fmt.Errorf("applying workload spec: %w", err)
Expand Down
102 changes: 59 additions & 43 deletions internal/humanitec/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

mergo "github.com/imdario/mergo"
score "github.com/score-spec/score-go/types"

extensions "github.com/score-spec/score-humanitec/internal/humanitec/extensions"
humanitec "github.com/score-spec/score-humanitec/internal/humanitec_go/types"
)
Expand Down Expand Up @@ -64,21 +65,23 @@ func parseResourceId(ref string) (workload, scope, resId string, err error) {

// getProbeDetails extracts an httpGet probe details from the source spec.
// Returns nil if the source spec is empty.
func getProbeDetails(probe *score.ContainerProbeSpec) map[string]interface{} {
if probe.HTTPGet.Path == "" {
func getProbeDetails(probe *score.ContainerProbe) map[string]interface{} {
if probe.HttpGet.Path == "" {
return nil
}

var res = map[string]interface{}{
"type": "http",
"path": probe.HTTPGet.Path,
"port": probe.HTTPGet.Port,
"path": probe.HttpGet.Path,
"port": probe.HttpGet.Port,
}

if len(probe.HTTPGet.HTTPHeaders) > 0 {
if len(probe.HttpGet.HttpHeaders) > 0 {
var hdrs = map[string]string{}
for _, hdr := range probe.HTTPGet.HTTPHeaders {
hdrs[hdr.Name] = hdr.Value
for _, hdr := range probe.HttpGet.HttpHeaders {
if hdr.Name != nil && hdr.Value != nil {
hdrs[*hdr.Name] = *hdr.Value
}
}
res["headers"] = hdrs
}
Expand Down Expand Up @@ -123,35 +126,37 @@ func mergeFileContent(content interface{}, target string) (string, error) {
}

// convertFileMountSpec extracts a mount file details from the source spec.
func convertFileMountSpec(f *score.FileMountSpec, context *templatesContext, baseDir string) (string, map[string]interface{}, error) {
func convertFileMountSpec(f *score.ContainerFilesElem, context *templatesContext, baseDir string) (string, map[string]interface{}, error) {
var err error
var content string

if f.Source != "" {
content, err = readFile(baseDir, f.Source)
if f.Source != nil {
content, err = readFile(baseDir, *f.Source)
} else if f.Content != nil {
content, err = mergeFileContent(*f.Content, f.Target)
} else {
content, err = mergeFileContent(f.Content, f.Target)
err = fmt.Errorf("file is missing source or content")
}
if err != nil {
return "", nil, err
}

if f.NoExpand {
if f.NoExpand != nil && *f.NoExpand {
content = context.Escape(content)
} else {
content = context.Substitute(content)
}

return f.Target,
map[string]interface{}{
"mode": f.Mode,
"mode": DerefOr(f.Mode, ""),
"value": content,
},
nil
}

// convertContainerSpec extracts a container details from the source spec.
func convertContainerSpec(name string, spec *score.ContainerSpec, context *templatesContext, baseDir string) (map[string]interface{}, error) {
func convertContainerSpec(name string, spec *score.Container, context *templatesContext, baseDir string) (map[string]interface{}, error) {
var containerSpec = map[string]interface{}{
"id": name,
}
Expand All @@ -171,17 +176,27 @@ func convertContainerSpec(name string, spec *score.ContainerSpec, context *templ
}
containerSpec["variables"] = envVars
}
if len(spec.Resources.Requests) > 0 || len(spec.Resources.Limits) > 0 {
containerSpec["resources"] = map[string]interface{}{
"requests": spec.Resources.Requests,
"limits": spec.Resources.Limits,
if spec.Resources != nil {
containerResources := make(map[string]interface{})
if out := getContainerResources(spec.Resources.Limits); len(out) > 0 {
containerResources["limits"] = out
}
if out := getContainerResources(spec.Resources.Requests); len(out) > 0 {
containerResources["requests"] = out
}
if len(containerResources) > 0 {
containerSpec["resources"] = containerResources
}
}
if probe := getProbeDetails(&spec.LivenessProbe); len(probe) > 0 {
containerSpec["liveness_probe"] = probe
if spec.LivenessProbe != nil {
if probe := getProbeDetails(spec.LivenessProbe); len(probe) > 0 {
containerSpec["liveness_probe"] = probe
}
}
if probe := getProbeDetails(&spec.ReadinessProbe); len(probe) > 0 {
containerSpec["readiness_probe"] = probe
if spec.ReadinessProbe != nil {
if probe := getProbeDetails(spec.ReadinessProbe); len(probe) > 0 {
containerSpec["readiness_probe"] = probe
}
}
if len(spec.Files) > 0 {
var files = map[string]interface{}{}
Expand All @@ -199,8 +214,8 @@ func convertContainerSpec(name string, spec *score.ContainerSpec, context *templ
for _, vol := range spec.Volumes {
volumes[vol.Target] = map[string]interface{}{
"id": context.Substitute(vol.Source),
"sub_path": vol.Path,
"read_only": vol.ReadOnly,
"sub_path": DerefOr(vol.Path, ""),
"read_only": DerefOr(vol.ReadOnly, false),
}
}
containerSpec["volume_mounts"] = volumes
Expand All @@ -209,8 +224,19 @@ func convertContainerSpec(name string, spec *score.ContainerSpec, context *templ
return containerSpec, nil
}

func getContainerResources(requests *score.ResourcesLimits) map[string]interface{} {
out := make(map[string]interface{})
if requests.Cpu != nil {
out["cpu"] = *requests.Cpu
}
if requests.Memory != nil {
out["memory"] = *requests.Memory
}
return out
}

// ConvertSpec converts SCORE specification into Humanitec deployment delta.
func ConvertSpec(name, envID, baseDir, workloadSourceURL string, spec *score.WorkloadSpec, ext *extensions.HumanitecExtensionsSpec) (*humanitec.CreateDeploymentDeltaRequest, error) {
func ConvertSpec(name, envID, baseDir, workloadSourceURL string, spec *score.Workload, ext *extensions.HumanitecExtensionsSpec) (*humanitec.CreateDeploymentDeltaRequest, error) {
ctx, err := buildContext(spec.Metadata, spec.Resources, ext.Resources)
if err != nil {
return nil, fmt.Errorf("preparing context: %w", err)
Expand All @@ -235,21 +261,13 @@ func ConvertSpec(name, envID, baseDir, workloadSourceURL string, spec *score.Wor
"annotations": annotations,
"containers": containers,
}
if len(spec.Service.Ports) > 0 {
if spec.Service != nil && len(spec.Service.Ports) > 0 {
var ports = map[string]interface{}{}
for pName, pSpec := range spec.Service.Ports {
var proto = pSpec.Protocol
if proto == "" {
proto = "TCP" // Defaults to "TCP"
}
var targetPport = pSpec.TargetPort
if targetPport == 0 {
targetPport = pSpec.Port // Defaults to the published port
}
ports[pName] = map[string]interface{}{
"protocol": proto,
"protocol": string(DerefOr(pSpec.Protocol, score.ServicePortProtocolTCP)),
"service_port": pSpec.Port,
"container_port": targetPport,
"container_port": DerefOr(pSpec.TargetPort, pSpec.Port),
}
}
workloadSpec["service"] = map[string]interface{}{
Expand Down Expand Up @@ -283,7 +301,8 @@ func ConvertSpec(name, envID, baseDir, workloadSourceURL string, spec *score.Wor
continue

default:
resId, hasAnnotation := res.Metadata.Annotations[AnnotationLabelResourceId]
resAnnotations, _ := res.Metadata["annotations"].(map[string]interface{})
resId, hasAnnotation := resAnnotations[AnnotationLabelResourceId].(string)
if resId == "" {
resId = fmt.Sprintf("externals.%s", name)
}
Expand All @@ -298,13 +317,10 @@ func ConvertSpec(name, envID, baseDir, workloadSourceURL string, spec *score.Wor
}
}
// END (DEPRECATED)
var class = "default"
if res.Class != "" {
class = res.Class
}
var class = DerefOr(res.Class, "default")
if mod, scope, resName, err := parseResourceId(resId); err != nil {
log.Printf("Warning: %v.\n", err)
} else if mod == "" || mod == spec.Metadata.Name {
} else if mod == "" || mod == spec.Metadata["name"].(string) {
if scope == "externals" {
var extRes = map[string]interface{}{
"type": res.Type,
Expand Down Expand Up @@ -349,7 +365,7 @@ func ConvertSpec(name, envID, baseDir, workloadSourceURL string, spec *score.Wor
},
Modules: humanitec.ModuleDeltas{
Add: map[string]map[string]interface{}{
spec.Metadata.Name: workload,
spec.Metadata["name"].(string): workload,
},
},
}
Expand Down
Loading

0 comments on commit 4e15ce1

Please sign in to comment.