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

Commit

Permalink
Added resource annotations and parameters support (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghen authored May 23, 2023
2 parents 26be9e0 + 31ce21a commit 5619629
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 35 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ 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-20221019054335-3510902b5f8b
github.com/score-spec/score-go v0.0.0-20230417150859-c1bf3fbe372b
github.com/sendgrid/rest v2.6.9+incompatible
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/score-spec/score-go v0.0.0-20221019054335-3510902b5f8b h1:Ws6TNwu+OuoR+K7C3fKLCuaeyAiGU0G7Xkaz9UUygWA=
github.com/score-spec/score-go v0.0.0-20221019054335-3510902b5f8b/go.mod h1:eNU0evgibNfV6ESUfRKjWcfGPmd92dI8dsUN/GBouZs=
github.com/score-spec/score-go v0.0.0-20230417150859-c1bf3fbe372b h1:s6DuDF4QC/jjXvBYYJfYKaRgsTGc3dTNwbT43eJCC04=
github.com/score-spec/score-go v0.0.0-20230417150859-c1bf3fbe372b/go.mod h1:eNU0evgibNfV6ESUfRKjWcfGPmd92dI8dsUN/GBouZs=
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 All @@ -23,9 +23,11 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
118 changes: 96 additions & 22 deletions internal/humanitec/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package humanitec

import (
"fmt"
"log"
"strings"

mergo "github.com/imdario/mergo"
Expand All @@ -17,6 +18,48 @@ import (
humanitec "github.com/score-spec/score-humanitec/internal/humanitec_go/types"
)

const (
AnnotationLabelResourceId = "score.humanitec.io/resId"
)

// parseResourceId extracts resource ID details from a resource reference string.
// Supported reference string formants:
//
// {resId}
// {externals|shared}.{resId}
// modules.{workloadId}.{externals|shared}.{resId}
func parseResourceId(ref string) (workload, scope, resId string, err error) {
var segments = strings.SplitN(ref, ".", 4)
switch len(segments) {
case 4:
if segments[0] != "modules" {
err = fmt.Errorf("invalid resource reference '%s': not supported", ref)
return
}
workload = segments[1]
scope = segments[2]
resId = segments[3]
case 3:
if segments[0] != "modules" {
err = fmt.Errorf("invalid resource reference '%s': not supported", ref)
return
}
case 2:
workload = ""
scope = segments[0]
resId = segments[1]
case 1:
workload = ""
scope = ""
resId = segments[0]
default:
workload = ""
scope = ""
resId = ""
}
return
}

// 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{} {
Expand Down Expand Up @@ -157,12 +200,57 @@ func ConvertSpec(name, envID string, spec *score.WorkloadSpec, ext *extensions.H
"spec": workloadSpec,
}

var externals = map[string]interface{}{}
var externals = make(map[string]interface{})
var shared = make([]humanitec.UpdateAction, 0)
for name, res := range spec.Resources {
if meta, exists := ext.Resources[name]; !exists || meta.Scope == "" || meta.Scope == "external" {
if res.Type != "service" && res.Type != "environment" {
externals[name] = map[string]interface{}{
"type": res.Type,
switch res.Type {

case "service", "environment":
continue

default:
resId, hasAnnotation := res.Metadata.Annotations[AnnotationLabelResourceId]
if resId == "" {
resId = fmt.Sprintf("externals.%s", name)
}

// DEPRECATED: Should use resource annotations instead
if meta, hasMeta := ext.Resources[name]; hasMeta {
log.Printf("Warning: Extensions for resources has been deprecated. Use '%s' resource annotation instead. Extensions are still configured for '%s'.\n", AnnotationLabelResourceId, name)
if !hasAnnotation && (meta.Scope == "" || meta.Scope == "externals") {
resId = fmt.Sprintf("externals.%s", name)
} else if !hasAnnotation && meta.Scope == "shared" {
resId = fmt.Sprintf("shared.%s", name)
}
}
// END (DEPRECATED)

if mod, scope, resName, err := parseResourceId(resId); err != nil {
log.Printf("Warning: %v.\n", err)
} else if mod == "" || mod == spec.Metadata.Name {
if scope == "externals" {
var extRes = map[string]interface{}{
"type": res.Type,
}
if len(res.Params) > 0 {
extRes["params"] = res.Params
}
externals[resName] = extRes
} else if scope == "shared" {
var resName = strings.Replace(resId, "shared.", "", 1)
var sharedRes = map[string]interface{}{
"type": res.Type,
}
if len(res.Params) > 0 {
sharedRes["params"] = res.Params
}
shared = append(shared, humanitec.UpdateAction{
Operation: "add",
Path: "/" + resName,
Value: sharedRes,
})
} else {
log.Printf("Warning: invalid resource reference '%s': not supported.\n", resId)
}
}
}
Expand All @@ -171,22 +259,6 @@ func ConvertSpec(name, envID string, spec *score.WorkloadSpec, ext *extensions.H
workload["externals"] = externals
}

var shared []humanitec.UpdateAction
for name, res := range spec.Resources {
if meta, exists := ext.Resources[name]; exists && meta.Scope == "shared" {
if shared == nil {
shared = make([]humanitec.UpdateAction, 0)
}
shared = append(shared, humanitec.UpdateAction{
Operation: "add",
Path: "/" + name,
Value: map[string]interface{}{
"type": res.Type,
},
})
}
}

var res = humanitec.CreateDeploymentDeltaRequest{
Metadata: humanitec.DeltaMetadata{
Name: name,
Expand All @@ -197,7 +269,9 @@ func ConvertSpec(name, envID string, spec *score.WorkloadSpec, ext *extensions.H
spec.Metadata.Name: workload,
},
},
Shared: shared,
}
if len(shared) > 0 {
res.Shared = shared
}

return &res, nil
Expand Down
Loading

0 comments on commit 5619629

Please sign in to comment.