Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions pkg/event-consumers/kafka/kafka-consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import (
)

var (
stopM map[string](chan struct{})
stoppedM map[string](chan struct{})
consumerM map[string]bool
brokers string
logLevel string
config *cluster.Config
stopM map[string](chan struct{})
stoppedM map[string](chan struct{})
consumerM map[string]bool
brokers string
logLevel string
config *cluster.Config
clusterDomain string
)

func init() {
Expand Down Expand Up @@ -80,6 +81,9 @@ func init() {
}
}

// local utils
clusterDomain = utils.GetClusterDomain()

}

// createConsumerProcess gets messages to a Kafka topic from the broker and send the payload to function service
Expand All @@ -106,7 +110,7 @@ func createConsumerProcess(broker, topic, funcName, ns, consumerGroupID string,
logrus.Debugf("Sending message %v to function %s", msg, funcName)
consumer.MarkOffset(msg, "")
go func() {
req, err := utils.GetHTTPReq(clientset, funcName, ns, "kafkatriggers.kubeless.io", "POST", string(msg.Value))
req, err := utils.GetHTTPReq(clientset, funcName, ns, clusterDomain, "kafkatriggers.kubeless.io", "POST", string(msg.Value))
if err != nil {
logrus.Errorf("Unable to elaborate request: %v", err)
} else {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/event_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func GetFunctionPort(clientset kubernetes.Interface, namespace, functionName str
}

// GetHTTPReq returns the http request object that can be used to send a event with payload to function service
func GetHTTPReq(clientset kubernetes.Interface, funcName, namespace, eventNamespace, method, body string) (*http.Request, error) {
func GetHTTPReq(clientset kubernetes.Interface, funcName, namespace, clusterDomain, eventNamespace, method, body string) (*http.Request, error) {

funcPort, err := GetFunctionPort(clientset, namespace, funcName)
if err != nil {
return nil, err
}

req, err := http.NewRequest(method, fmt.Sprintf("http://%s.%s.svc.cluster.local:%s", funcName, namespace, funcPort), strings.NewReader(body))
req, err := http.NewRequest(method, fmt.Sprintf("http://%s.%s.svc.%s:%s", funcName, namespace, clusterDomain, funcPort), strings.NewReader(body))
if err != nil {
return nil, fmt.Errorf("Unable to create request %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/event_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGetHTTPRequest(t *testing.T) {
},
}
clientset := fake.NewSimpleClientset(&svc)
req, err := GetHTTPReq(clientset, "foo", "myns", "kafkatriggers.kubeless.io", "POST", "my msg")
req, err := GetHTTPReq(clientset, "foo", "myns", "cluster.local", "kafkatriggers.kubeless.io", "POST", "my msg")
if err != nil {
t.Errorf("Unexpected error %v", err)
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestGetJSONHTTPRequest(t *testing.T) {
},
}
clientset := fake.NewSimpleClientset(&svc)
req, err := GetHTTPReq(clientset, "foo", "myns", "kafkatriggers.kubeless.io", "POST", `{"hello": "world"}`)
req, err := GetHTTPReq(clientset, "foo", "myns", "cluster.local", "kafkatriggers.kubeless.io", "POST", `{"hello": "world"}`)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/utils/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"strings"

kafkaApi "github.com/kubeless/kafka-trigger/pkg/apis/kubeless/v1beta1"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -312,3 +314,20 @@ func GetSecretsAsLocalObjectReference(secrets ...string) []v1.LocalObjectReferen
}
return res
}

// GetClusterDomain returns Kubernetes cluster domain, default to "cluster.local"
func GetClusterDomain() string {
apiSvc := "kubernetes.default.svc."

clusterDomain := "cluster.local"

cname, err := net.LookupCNAME(apiSvc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue with this solution is that this function is called every time there is a new message. This could lead to a bottleneck. You will need to move the call to getClusterDomain outside. I would put it in the init() method of kafka-consumer.go and store it as a package variable (for example) so you don't need to re-request it.

if err != nil {
return clusterDomain
}

clusterDomain = strings.TrimPrefix(cname, apiSvc)
clusterDomain = strings.TrimSuffix(clusterDomain, ".")

return clusterDomain
}