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

Commit

Permalink
feat: override humanitec resource id with annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Yarshevich <yarshevich@gmail.com>
  • Loading branch information
ghen committed Apr 19, 2023
1 parent 0f98c26 commit af6aebe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
32 changes: 18 additions & 14 deletions internal/humanitec/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

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

// getProbeDetails extracts an httpGet probe details from the source spec.
Expand Down Expand Up @@ -171,28 +171,33 @@ func ConvertSpec(name, envID string, spec *score.WorkloadSpec, ext *extensions.H
continue

default:
scope, hasAnnotation := res.Metadata.Annotations[ResourceScopeAnnotationLabel]
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 Score resource annotations instead. Extensions are stil configured for '%s'.\n", name)
if !hasAnnotation {
scope = meta.Scope
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)

switch scope {

case "", "external":
if strings.HasPrefix(resId, "externals.") {
var resName = strings.Replace(resId, "externals.", "", 1)
var extRes = map[string]interface{}{
"type": res.Type,
}
if len(res.Params) > 0 {
extRes["params"] = res.Params
}
externals[name] = extRes

case "shared":
externals[resName] = extRes
} else if strings.HasPrefix(resId, "shared.") {
var resName = strings.Replace(resId, "shared.", "", 1)
var sharedRes = map[string]interface{}{
"type": res.Type,
}
Expand All @@ -201,12 +206,11 @@ func ConvertSpec(name, envID string, spec *score.WorkloadSpec, ext *extensions.H
}
shared = append(shared, humanitec.UpdateAction{
Operation: "add",
Path: "/" + name,
Path: "/" + resName,
Value: sharedRes,
})

default:
log.Printf("Warning: Invalid scope value '%s' for resource '%s'. Not supported.\n", scope, name)
} else {
log.Printf("Warning: Invalid resource id value '%s'. Not supported.\n", resId)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/humanitec/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestScoreConvert(t *testing.T) {
"env": {
Metadata: score.ResourceMeta{
Annotations: map[string]string{
ResourceScopeAnnotationLabel: "external",
AnnotationLabelResourceId: "externals.should-ignore-this-one",
},
},
Type: "environment",
Expand All @@ -217,7 +217,7 @@ func TestScoreConvert(t *testing.T) {
"db": {
Metadata: score.ResourceMeta{
Annotations: map[string]string{
ResourceScopeAnnotationLabel: "external",
AnnotationLabelResourceId: "externals.annotations-db-id",
},
},
Type: "postgres",
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestScoreConvert(t *testing.T) {
"DEBUG": "${values.DEBUG}",
"LOGS_LEVEL": "${pod.debug.level}",
"ORDERS_SERVICE": "http://${modules.orders.service.name}:${modules.orders.service.port}/api",
"CONNECTION_STRING": "postgresql://${externals.db.host}:${externals.db.port}/${externals.db.name}",
"CONNECTION_STRING": "postgresql://${externals.annotations-db-id.host}:${externals.annotations-db-id.port}/${externals.annotations-db-id.name}",
"DOMAIN_NAME": "${shared.dns.domain}",
},
"files": map[string]interface{}{
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestScoreConvert(t *testing.T) {
"data": map[string]interface{}{
"type": "volume",
},
"db": map[string]interface{}{
"annotations-db-id": map[string]interface{}{
"type": "postgres",
"params": map[string]interface{}{
"extensions": map[string]interface{}{
Expand Down
13 changes: 9 additions & 4 deletions internal/humanitec/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ func buildContext(metadata score.WorkloadMeta, resources score.ResourcesSpecs, e
if res.Type == "workload" {
log.Println("Warning: 'workload' is a reserved resource type. Its usage may lead to compatibility issues with future releases of this application.")
}
scope, hasAnnotation := res.Metadata.Annotations[ResourceScopeAnnotationLabel]
resId, hasAnnotation := res.Metadata.Annotations[AnnotationLabelResourceId]
// DEPRECATED: Should use resource annotations instead
if resExt, hasMeta := ext[resName]; hasMeta && !hasAnnotation {
scope = resExt.Scope
if resExt.Scope == "" || resExt.Scope == "external" {
resId = fmt.Sprintf("externals.%s", resName)
} else if resExt.Scope == "shared" {
resId = fmt.Sprintf("shared.%s", resName)
}
}
// END (DEPRECATED)
if scope == "shared" {
source = fmt.Sprintf("shared.%s", resName)

if resId != "" {
source = resId
} else {
source = fmt.Sprintf("externals.%s", resName)
}
Expand Down

0 comments on commit af6aebe

Please sign in to comment.