Skip to content
This repository was archived by the owner on Jan 27, 2021. It is now read-only.
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,12 @@ The following table lists the supported annotations for Kubernetes `Deployments`

#### Pod Annotations

The following table lists the supported annotations for Kubernetes `Pods` and their default values.

| Annotation | Description | Default |
| ---------- | ----------- | ------- |
| `osiris.deislabs.io/enabled` | Enable the metrics collecting proxy sidecar container to be injected into this pod. Allowed values: `y`, `yes`, `true`, `on`, `1`. | _no value_ (= disabled) |
| `osiris.deislabs.io/ignoredPaths` | The list of (url) paths that should be "ignored" by Osiris. Requests to such paths won't be "counted" by the proxy. Format: comma-separated string. | _no value_ |

#### Service Annotations

Expand Down
1 change: 1 addition & 0 deletions example/hello-osiris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ spec:
app: hello-osiris
annotations:
osiris.deislabs.io/enabled: "true"
osiris.deislabs.io/ignoredPaths: "/first/path,/second-path"
spec:
containers:
- name: hello-osiris
Expand Down
1 change: 1 addition & 0 deletions pkg/kubernetes/osiris.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

const (
IgnoredPathsAnnotationName = "osiris.deislabs.io/ignoredPaths"
osirisEnabledAnnotationName = "osiris.deislabs.io/enabled"
metricsCheckIntervalAnnotationName = "osiris.deislabs.io/metricsCheckInterval"
)
Expand Down
4 changes: 4 additions & 0 deletions pkg/metrics/proxy/injector/pod_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (i *injector) getPodPatchOperations(
Name: "METRICS_AND_HEALTH_PORT",
Value: fmt.Sprintf("%d", metricsAndHealthPort),
},
{
Name: "IGNORED_PATHS",
Value: pod.Annotations[kubernetes.IgnoredPathsAnnotationName],
},
},
Ports: []corev1.ContainerPort{
{
Expand Down
11 changes: 11 additions & 0 deletions pkg/metrics/proxy/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ var portMappingRegex = regexp.MustCompile(`^(?:\d+\:\d+,)*(?:\d+\:\d+)$`)
type config struct {
PortMappings string `envconfig:"PORT_MAPPINGS" required:"true"`
MetricsAndHealthPort int `envconfig:"METRICS_AND_HEALTH_PORT" required:"true"` // nolint: lll
// comma-separated list of URL paths that won't be counted
IgnoredPaths string `envconfig:"IGNORED_PATHS"`
}

// Config represents configuration options for the Osiris Proxy
type Config struct {
PortMappings map[int]int
MetricsAndHealthPort int
IgnoredPaths map[string]struct{}
}

// NewConfigWithDefaults returns a Config object with default values already
Expand Down Expand Up @@ -59,5 +62,13 @@ func GetConfigFromEnvironment() (Config, error) {

c.MetricsAndHealthPort = internalC.MetricsAndHealthPort

ignoredPaths := strings.Split(internalC.IgnoredPaths, ",")
if len(ignoredPaths) > 0 {
c.IgnoredPaths = make(map[string]struct{}, len(ignoredPaths))
for _, ignoredPath := range ignoredPaths {
c.IgnoredPaths[ignoredPath] = struct{}{}
}
}

return c, nil
}
18 changes: 16 additions & 2 deletions pkg/metrics/proxy/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type proxy struct {
connectionsClosed *uint64
dynamicProxies []tcp.DynamicProxy
healthzAndMetricsSvr *http.Server
ignoredPaths map[string]struct{}
}

func NewProxy(cfg Config) (Proxy, error) {
Expand All @@ -40,20 +41,21 @@ func NewProxy(cfg Config) (Proxy, error) {
Addr: fmt.Sprintf(":%d", cfg.MetricsAndHealthPort),
Handler: healthzAndMetricsMux,
},
ignoredPaths: cfg.IgnoredPaths,
}

for listenPort, targetPort := range cfg.PortMappings {
tp := targetPort
dynamicProxy, err := tcp.NewDynamicProxy(
fmt.Sprintf(":%d", listenPort),
func(r *http.Request) (string, int, error) {
if !isKubeProbe(r) {
if !p.isIgnoredRequest(r) {
atomic.AddUint64(p.connectionsOpened, 1)
}
return "localhost", tp, nil
},
func(r *http.Request) error {
if !isKubeProbe(r) {
if !p.isIgnoredRequest(r) {
atomic.AddUint64(p.connectionsClosed, 1)
}
return nil
Expand Down Expand Up @@ -139,6 +141,18 @@ func (p *proxy) handleMetricsRequest(w http.ResponseWriter, _ *http.Request) {
}
}

func (p *proxy) isIgnoredRequest(r *http.Request) bool {
return p.isIgnoredPath(r) || isKubeProbe(r)
}

func (p *proxy) isIgnoredPath(r *http.Request) bool {
if r.URL == nil || len(r.URL.Path) == 0 {
return false
}
_, found := p.ignoredPaths[r.URL.Path]
return found
}

func isKubeProbe(r *http.Request) bool {
return strings.Contains(r.Header.Get("User-Agent"), "kube-probe")
}