Skip to content

Commit

Permalink
Merge branch 'master' into comp-md-dynamodb-2587
Browse files Browse the repository at this point in the history
  • Loading branch information
robertojrojas authored Jun 26, 2023
2 parents 206c85a + 7ab8fde commit eb03530
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
35 changes: 31 additions & 4 deletions nameresolution/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ limitations under the License.
package kubernetes

import (
"bytes"
"strconv"
"text/template"

"github.com/dapr/components-contrib/nameresolution"
"github.com/dapr/kit/config"
Expand All @@ -24,18 +26,29 @@ import (
const (
DefaultClusterDomain = "cluster.local"
ClusterDomainKey = "clusterDomain"
TemplateKey = "template"
)

func executeTemplateWithResolveRequest(tmpl *template.Template, req nameresolution.ResolveRequest) (string, error) {
var addr bytes.Buffer
if err := tmpl.Execute(&addr, req); err != nil {
return "", err
}
return addr.String(), nil
}

type resolver struct {
logger logger.Logger
clusterDomain string
tmpl *template.Template
}

// NewResolver creates Kubernetes name resolver.
func NewResolver(logger logger.Logger) nameresolution.Resolver {
return &resolver{
logger: logger,
clusterDomain: DefaultClusterDomain,
tmpl: nil,
}
}

Expand All @@ -45,21 +58,35 @@ func (k *resolver) Init(metadata nameresolution.Metadata) error {
if err != nil {
return err
}
if config, ok := configInterface.(map[string]interface{}); ok {
clusterDomainPtr := config[ClusterDomainKey]
if clusterDomainPtr != nil {
clusterDomain, _ := clusterDomainPtr.(string)

if cfg, ok := configInterface.(map[string]interface{}); ok {
clusterDomainAny := cfg[ClusterDomainKey]
tmplStrAny := cfg[TemplateKey]

if clusterDomainAny != nil {
clusterDomain, _ := clusterDomainAny.(string)
if clusterDomain != "" {
k.clusterDomain = clusterDomain
}
}

if tmplStrAny != nil {
tmplStr, _ := tmplStrAny.(string)
if tmplStr != "" {
k.tmpl = template.Must(template.New("kubernetes-template").Parse(tmplStr))
k.logger.Debugf("using custom template %s", tmplStr)
}
}
}

return nil
}

// ResolveID resolves name to address in Kubernetes.
func (k *resolver) ResolveID(req nameresolution.ResolveRequest) (string, error) {
if k.tmpl != nil {
return executeTemplateWithResolveRequest(k.tmpl, req)
}
// Dapr requires this formatting for Kubernetes services
return req.ID + "-dapr." + req.Namespace + ".svc." + k.clusterDomain + ":" + strconv.Itoa(req.Port), nil
}
39 changes: 39 additions & 0 deletions nameresolution/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,42 @@ func TestResolveWithCustomClusterDomain(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expect, target)
}

func TestResolveWithTemplate(t *testing.T) {
resolver := NewResolver(logger.NewLogger("test"))
_ = resolver.Init(nameresolution.Metadata{
Configuration: map[string]interface{}{
"template": "{{.ID}}-{{.Namespace}}.internal:{{.Port}}",
},
})

request := nameresolution.ResolveRequest{ID: "myid", Namespace: "abc", Port: 1234}
const expected = "myid-abc.internal:1234"
target, err := resolver.ResolveID(request)

assert.NoError(t, err)
assert.Equal(t, target, expected)
}

func TestResolveWithTemplateAndData(t *testing.T) {
resolver := NewResolver(logger.NewLogger("test"))
_ = resolver.Init(nameresolution.Metadata{
Configuration: map[string]interface{}{
"template": "{{.ID}}-{{.Data.region}}.internal:{{.Port}}",
},
})

request := nameresolution.ResolveRequest{
ID: "myid",
Namespace: "abc",
Port: 1234,
Data: map[string]string{
"region": "myland",
},
}
const expected = "myid-myland.internal:1234"
target, err := resolver.ResolveID(request)

assert.NoError(t, err)
assert.Equal(t, target, expected)
}

0 comments on commit eb03530

Please sign in to comment.