Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env vars namings #226

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions charts/apiclarity/templates/post-install-job-kong.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ spec:
value: {{ .Values.trafficSource.kong.ingressName }}
- name: KONG_GATEWAY_INGRESS_NAMESPACE
value: {{ .Values.trafficSource.kong.ingressNamespace }}
- name: UPSTREAM_TELEMETRY_HOST_NAME
- name: UPSTREAM_TELEMETRY_ADDRESS
value: '{{ include "apiclarity.name" . }}.{{ .Release.Namespace }}:9000'
- name: TRACE_SAMPLING_HOST_NAME
- name: TRACE_SAMPLING_ADDRESS
value: '{{ include "apiclarity.name" . }}.{{ .Release.Namespace }}:9990'
- name: TRACE_SAMPLING_ENABLED
value: "{{ .Values.global.traceSampling.enable }}"
Expand Down
4 changes: 2 additions & 2 deletions charts/apiclarity/templates/post-install-job-tyk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ spec:
value: {{ .Values.trafficSource.tyk.deploymentName }}
- name: TYK_GATEWAY_DEPLOYMENT_NAMESPACE
value: {{ .Values.trafficSource.tyk.deploymentNamespace }}
- name: UPSTREAM_TELEMETRY_HOST_NAME
- name: UPSTREAM_TELEMETRY_ADDRESS
value: '{{ include "apiclarity.name" . }}.{{ .Release.Namespace }}:9000'
- name: TRACE_SAMPLING_HOST_NAME
- name: TRACE_SAMPLING_ADDRESS
value: '{{ include "apiclarity.name" . }}.{{ .Release.Namespace }}:9990'
- name: TRACE_SAMPLING_ENABLED
value: "{{ .Values.global.traceSampling.enable }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ spec:
env:
- name: GOGC
value: "12800"
- name: UPSTREAM_TELEMETRY_HOST_NAME
- name: UPSTREAM_TELEMETRY_ADDRESS
value: '{{ include "apiclarity.name" . }}.{{ .Release.Namespace }}:9000'
- name: TRACE_SAMPLING_HOST_NAME
- name: TRACE_SAMPLING_ADDRESS
value: '{{ include "apiclarity.name" . }}.{{ .Release.Namespace }}:9990'
- name: TRACE_SAMPLING_ENABLED
value: "{{ .Values.global.traceSampling.enable }}"
Expand Down
9 changes: 7 additions & 2 deletions plugins/common/trace_sampling_client/trace_sampling_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (

log "github.com/sirupsen/logrus"

"github.com/openclarity/apiclarity/plugins/common"
"github.com/openclarity/trace-sampling-manager/api/client/client"
"github.com/openclarity/trace-sampling-manager/api/client/client/operations"
"github.com/openclarity/apiclarity/plugins/common"
)

type Client struct {
Expand Down Expand Up @@ -74,13 +74,18 @@ func (t *Client) Start() {
}()
}

func (t *Client) ShouldTrace(host string) bool {
func (t *Client) ShouldTrace(host, port string) bool {
t.lock.RLock()
defer t.lock.RUnlock()

if len(t.Hosts) == 0 {
return false
}

if port != "" {
host = host + ":" + port
}

if t.Hosts[allHosts] || t.Hosts[host] {
return true
}
Expand Down
37 changes: 34 additions & 3 deletions plugins/common/trace_sampling_client/trace_sampling_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestTraceSamplingManager_ShouldTrace(t1 *testing.T) {
}
type args struct {
host string
port string
}
tests := []struct {
name string
Expand All @@ -96,7 +97,7 @@ func TestTraceSamplingManager_ShouldTrace(t1 *testing.T) {
want bool
}{
{
name: "should trace",
name: "should trace - no port",
fields: fields{
Hosts: map[string]bool{
"host1": true,
Expand All @@ -106,11 +107,12 @@ func TestTraceSamplingManager_ShouldTrace(t1 *testing.T) {
},
args: args{
host: "host1",
port: "",
},
want: true,
},
{
name: "should not trace",
name: "should not trace - no port",
fields: fields{
Hosts: map[string]bool{
"host1": true,
Expand All @@ -123,6 +125,35 @@ func TestTraceSamplingManager_ShouldTrace(t1 *testing.T) {
},
want: false,
},
{
name: "should trace - with port",
fields: fields{
Hosts: map[string]bool{
"host1:8080": true,
"host2": true,
"host3": true,
},
},
args: args{
host: "host1",
port: "8080",
},
want: true,
},
{
name: "should not trace - with port",
fields: fields{
Hosts: map[string]bool{
"host1:9000": true,
"host2": true,
"host3": true,
},
},
args: args{
host: "host1:8000",
},
want: false,
},
{
name: "should trace by wildcard",
fields: fields{
Expand Down Expand Up @@ -153,7 +184,7 @@ func TestTraceSamplingManager_ShouldTrace(t1 *testing.T) {
t := &Client{
Hosts: tt.fields.Hosts,
}
if got := t.ShouldTrace(tt.args.host); got != tt.want {
if got := t.ShouldTrace(tt.args.host, tt.args.port); got != tt.want {
t1.Errorf("ShouldTrace() = %v, want %v", got, tt.want)
}
})
Expand Down
55 changes: 51 additions & 4 deletions plugins/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ import (
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"

tracesamplingclient "github.com/openclarity/trace-sampling-manager/api/client/client"

"github.com/openclarity/apiclarity/plugins/api/client/client"
"github.com/openclarity/apiclarity/plugins/api/client/models"
)

const (
MaxBodySize = 1000 * 1000
RequestIDHeaderKey = "X-Request-Id"
RequestTimeContextKey = "request_time"
SamplingInterval = 10 * time.Second
MaxBodySize = 1000 * 1000
RequestIDHeaderKey = "X-Request-Id"
RequestTimeContextKey = "request_time"
SamplingInterval = 10 * time.Second
MinimumSeparatedHostSize = 2
)

func ReadBody(body io.ReadCloser) ([]byte, bool, error) {
Expand Down Expand Up @@ -158,3 +161,47 @@ func createClientTransportTLS(host string, tlsOptions *ClientTLSOptions) (runtim

return transport, nil
}

// If host from URL is not containing any dots, the defaultNamespace will be added to the host name
func GetHostAndPortFromURL(URL, defaultNamespace string) (host, port string) {
if !strings.Contains(URL, "://") {
// need to add scheme to host in order for url.Parse to parse properly
URL = "http://" + URL
}

parsedHost, err := url.Parse(URL)
if err != nil {
log.Errorf("Failed to parse URL=%v: %v", URL, err)
return URL, ""
}

host = parsedHost.Hostname()
port = parsedHost.Port()

host = strings.TrimSuffix(host, ".svc.cluster.local")
host = strings.TrimSuffix(host, ".svc.cluster")
host = strings.TrimSuffix(host, ".svc")

// we assume that host with no dots is an internal host, so we will "fix" it with default namespace.
if !strings.Contains(host, ".") && defaultNamespace != "" {
host = host + "." + defaultNamespace
}

if port == "" {
if parsedHost.Scheme == "https" {
port = "443"
} else {
port = "80"
}
}

return
}

// Will try to extract the namespace from the host name, and if not found, will use the provided default namespace.
func GetDestinationNamespaceFromHostOrDefault(host, defaultNamespace string) string {
if sp := strings.Split(host, "."); len(sp) >= MinimumSeparatedHostSize {
return sp[1]
}
return defaultNamespace
}
90 changes: 90 additions & 0 deletions plugins/common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,93 @@ func TestCreateHeaders(t *testing.T) {
})
}
}

func Test_getHostAndPortFromTargetURL(t *testing.T) {
type args struct {
url string
defaultNamespace string
}
tests := []struct {
name string
args args
wantHost string
wantPort string
}{
{
name: "no port",
args: args{
url: "http://catalogue.sock-shop",
},
wantHost: "catalogue.sock-shop",
wantPort: "80",
},
{
name: "with port",
args: args{
url: "http://catalogue.sock-shop:8080",
},
wantHost: "catalogue.sock-shop",
wantPort: "8080",
},
{
name: "with port - no namespace - use default",
args: args{
url: "http://catalogue:8080",
defaultNamespace: "sock-shop",
},
wantHost: "catalogue.sock-shop",
wantPort: "8080",
},
{
name: "with port, no scheme - remove svc.cluster.local suffix",
args: args{
url: "catalogue.sock-shop.svc.cluster.local:8080",
},
wantHost: "catalogue.sock-shop",
wantPort: "8080",
},
{
name: "with port, no scheme - remove svc.cluster suffix",
args: args{
url: "catalogue.sock-shop.svc.cluster:8080",
},
wantHost: "catalogue.sock-shop",
wantPort: "8080",
},
{
name: "with port, no scheme - remove svc suffix",
args: args{
url: "catalogue.sock-shop.svc:8080",
},
wantHost: "catalogue.sock-shop",
wantPort: "8080",
},
{
name: "https",
args: args{
url: "https://catalogue.sock-shop:8080",
},
wantHost: "catalogue.sock-shop",
wantPort: "8080",
},
{
name: "https no port",
args: args{
url: "https://catalogue.sock-shop",
},
wantHost: "catalogue.sock-shop",
wantPort: "443",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHost, gotPort := GetHostAndPortFromURL(tt.args.url, tt.args.defaultNamespace)
if gotHost != tt.wantHost {
t.Errorf("GetHostAndPortFromURL() gotHost = %v, want %v", gotHost, tt.wantHost)
}
if gotPort != tt.wantPort {
t.Errorf("GetHostAndPortFromURL() gotPort = %v, want %v", gotPort, tt.wantPort)
}
})
}
}
4 changes: 2 additions & 2 deletions plugins/gateway/kong/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ If you just want to try it out with a demo application, and you don't have kong
KONG_GATEWAY_DEPLOYMENT_NAMESPACE=kong \
KONG_GATEWAY_INGRESS_NAME=catalogue \
KONG_GATEWAY_INGRESS_NAMESPACE=sock-shop \
UPSTREAM_TELEMETRY_HOST_NAME=apiclarity-apiclarity.apiclarity:9000 \
UPSTREAM_TELEMETRY_ADDRESS=apiclarity-apiclarity.apiclarity:9000 \
deploy/deploy.sh
```
* Note: If you installed Kong using helm, the deployment name might be different. Please change the KONG_GATEWAY_DEPLOYMENT_NAME env var accordingly.
Expand Down Expand Up @@ -104,7 +104,7 @@ If you just want to try it out with a demo application, and you don't have kong
KONG_GATEWAY_DEPLOYMENT_NAMESPACE=<namespace> \
KONG_GATEWAY_INGRESS_NAME=<name> \
KONG_GATEWAY_INGRESS_NAMESPACE=<namespace> \
UPSTREAM_TELEMETRY_HOST_NAME=<telemetry service address> ./deploy/deploy.sh
UPSTREAM_TELEMETRY_ADDRESS=<telemetry service address> ./deploy/deploy.sh
```

2. Helm installation
Expand Down
6 changes: 3 additions & 3 deletions plugins/gateway/kong/deploy/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ KongGatewayDeploymentName="${KONG_GATEWAY_DEPLOYMENT_NAME:-kong}"
KongGatewayDeploymentNamespace="${KONG_GATEWAY_DEPLOYMENT_NAMESPACE:-default}"
KongGatewayIngressName="${KONG_GATEWAY_INGRESS_NAME:-demo}"
KongGatewayIngressNamespace="${KONG_GATEWAY_INGRESS_NAMESPACE:-default}"
UpstreamTelemetryHostName="${UPSTREAM_TELEMETRY_HOST_NAME:-apiclarity-apiclarity.apiclarity:9000}"
TraceSamplingHostName="${TRACE_SAMPLING_HOST_NAME:-apiclarity-apiclarity.apiclarity:9990}"
UpstreamTelemetryAddress="${UPSTREAM_TELEMETRY_ADDRESS:-apiclarity-apiclarity.apiclarity:9000}"
TraceSamplingAddress="${TRACE_SAMPLING_ADDRESS:-apiclarity-apiclarity.apiclarity:9990}"
TraceSamplingEnabled="${TRACE_SAMPLING_ENABLED:-false}"

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

cat "${DIR}/kongPlugin.yaml" | sed "s/{{UPSTREAM_TELEMETRY_HOST_NAME}}/$UpstreamTelemetryHostName/g" | sed "s/{{TRACE_SAMPLING_HOST_NAME}}/$TraceSamplingHostName/g" | sed "s/{{TRACE_SAMPLING_ENABLED}}/$TraceSamplingEnabled/g" | kubectl -n ${KongGatewayIngressNamespace} apply -f -
cat "${DIR}/kongPlugin.yaml" | sed "s/{{UPSTREAM_TELEMETRY_ADDRESS}}/$UpstreamTelemetryAddress/g" | sed "s/{{TRACE_SAMPLING_ADDRESS}}/$TraceSamplingAddress/g" | sed "s/{{TRACE_SAMPLING_ENABLED}}/$TraceSamplingEnabled/g" | kubectl -n ${KongGatewayIngressNamespace} apply -f -

deploymentPatch=`cat "${DIR}/patch-deployment.yaml" | sed "s/{{KONG_PROXY_CONTAINER_NAME}}/$KongProxyContainerName/g"`

Expand Down
4 changes: 2 additions & 2 deletions plugins/gateway/kong/deploy/kongPlugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ metadata:
name: apiclarity-plugin
plugin: kong-plugin
config:
host: {{UPSTREAM_TELEMETRY_HOST_NAME}}
trace_sampling_host: {{TRACE_SAMPLING_HOST_NAME}}
upstream_telemetry_address: {{UPSTREAM_TELEMETRY_ADDRESS}}
trace_sampling_address: {{TRACE_SAMPLING_ADDRESS}}
trace_sampling_enabled: {{TRACE_SAMPLING_ENABLED}}
Loading