Skip to content

Commit

Permalink
Merge branch 'main' into install-walktourhg
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirdavid1 authored Oct 9, 2024
2 parents b65b1ee + 03453bb commit 3b89ff4
Show file tree
Hide file tree
Showing 64 changed files with 1,865 additions and 126 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: build

on:
merge_group:
pull_request:
branches:
- main
- stable

jobs:
build-autoscaler:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: E2E Tests

on:
merge_group:
pull_request:
branches:
- main
- stable

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/go-mod-tidy.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: go mod tidy
on:
merge_group:
pull_request:

jobs:
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ node_modules
.DS_Store
go.work.sum
cli/odigos
.venv
.venv
**/__pycache__/
**/*.pyc
serving-certs/
5 changes: 4 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/instrumentor",
"cwd": "${workspaceFolder}/instrumentor"
"cwd": "${workspaceFolder}/instrumentor",
"env": {
"LOCAL_MUTATING_WEBHOOK_CERT_DIR": "${workspaceFolder}/serving-certs"
}
},
{
"name": "frontend",
Expand Down
33 changes: 33 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,36 @@ make debug-odiglet

Then, you can attach a debugger to the Odiglet pod. For example, if you are using Goland, you can follow the instructions [here](https://www.jetbrains.com/help/go/attach-to-running-go-processes-with-debugger.html#step-3-create-the-remote-run-debug-configuration-on-the-client-computer) to attach to a remote process.
For Visual Studio Code, you can use the `.vscode/launch.json` file in this repo to attach to the Odiglet pod.



## Instrumentor

### Debugging
If the Mutating Webhook is enabled, follow these steps:

1. Copy the TLS certificate and key:
Create a local directory and extract the certificate and key by running the following command:
```
mkdir -p serving-certs && kubectl get secret instrumentor-webhook-cert -n odigos-system -o jsonpath='{.data.tls\.crt}' | base64 -d > serving-certs/tls.crt && kubectl get secret instrumentor-webhook-cert -n odigos-system -o jsonpath='{.data.tls\.key}' | base64 -d > serving-certs/tls.key
```


2. Apply this service to the cluster, it will replace the existing `odigos-instrumentor` service:

```
apiVersion: v1
kind: Service
metadata:
name: odigos-instrumentor
namespace: odigos-system
spec:
type: ExternalName
externalName: host.docker.internal
ports:
- name: webhook-server
port: 9443
protocol: TCP
```

Once this is done, you can use the .vscode/launch.json configuration and run instrumentor local for debugging.
7 changes: 3 additions & 4 deletions agents/python/configurator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
MINIMUM_PYTHON_SUPPORTED_VERSION = (3, 8)

class OdigosPythonConfigurator(sdk_config._BaseConfigurator):

def _configure(self, **kwargs):
trace_exporters, metric_exporters, log_exporters = sdk_config._import_exporters(
sdk_config._get_exporter_names("traces"),
sdk_config._get_exporter_names("metrics"),
sdk_config._get_exporter_names("logs"),
['otlp_proto_http'] if sdk_config._get_exporter_names("traces") else [],
[],
[],
)
initialize_components(trace_exporters, metric_exporters, log_exporters)
2 changes: 1 addition & 1 deletion cli/cmd/resources/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func NewAutoscalerDeployment(ns string, version string, imagePrefix string, imag
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.MustParse("500m"),
"memory": *resource.NewQuantity(134217728, resource.BinarySI),
"memory": *resource.NewQuantity(536870912, resource.BinarySI),
},
Requests: corev1.ResourceList{
"cpu": resource.MustParse("10m"),
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/resources/instrumentor.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func NewInstrumentorDeployment(ns string, version string, telemetryEnabled bool,
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.MustParse("500m"),
"memory": *resource.NewQuantity(134217728, resource.BinarySI),
"memory": *resource.NewQuantity(536870912, resource.BinarySI),
},
Requests: corev1.ResourceList{
"cpu": resource.MustParse("10m"),
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/resources/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func NewSchedulerDeployment(ns string, version string, imagePrefix string) *apps
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.MustParse("500m"),
"memory": *resource.NewQuantity(134217728, resource.BinarySI),
"memory": *resource.NewQuantity(536870912, resource.BinarySI),
},
Requests: corev1.ResourceList{
"cpu": resource.MustParse("10m"),
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/resources/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func NewUIDeployment(ns string, version string, imagePrefix string) *appsv1.Depl
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.MustParse("500m"),
"memory": *resource.NewQuantity(134217728, resource.BinarySI),
"memory": *resource.NewQuantity(536870912, resource.BinarySI),
},
Requests: corev1.ResourceList{
"cpu": resource.MustParse("10m"),
Expand Down
1 change: 1 addition & 0 deletions docs/instrumentations/python/python.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The following Python modules will be auto instrumented by Odigos:
### HTTP Frameworks
- [`asgi`](https://pypi.org/project/asgiref/) versions `asgiref >= 3.0.0, < 4.0.0`
- [`django`](https://pypi.org/project/Django/) versions `django >= 1.10.0`
<Info>Please note that for Django instrumentation to work, you need to set the [DJANGO_SETTINGS_MODULE](https://docs.djangoproject.com/en/5.1/topics/settings/#envvar-DJANGO_SETTINGS_MODULE) environment variable. Make sure to set this in your Kubernetes manifest or Dockerfile to ensure proper configuration.</Info>
- [`fastapi`](https://pypi.org/project/fastapi/) versions `fastapi >= 0.58.0, < 0.59.0`, `fastapi-slim >= 0.111.0, < 0.112.0`
- [`flask`](https://pypi.org/project/Flask/) versions `flask >= 1.0.0`
- [`pyramid`](https://pypi.org/project/pyramid/) versions `pyramid >= 1.7.0`
Expand Down
2 changes: 1 addition & 1 deletion helm/odigos/templates/autoscaler/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ spec:
resources:
limits:
cpu: 500m
memory: 128Mi
memory: 512Mi
requests:
cpu: 10m
memory: 64Mi
Expand Down
2 changes: 1 addition & 1 deletion helm/odigos/templates/instrumentor/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ spec:
resources:
limits:
cpu: 500m
memory: 128Mi
memory: 512Mi
requests:
cpu: 10m
memory: 64Mi
Expand Down
2 changes: 1 addition & 1 deletion helm/odigos/templates/scheduler/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
resources:
limits:
cpu: 500m
memory: 128Mi
memory: 512Mi
requests:
cpu: 10m
memory: 64Mi
Expand Down
2 changes: 1 addition & 1 deletion helm/odigos/templates/ui/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ spec:
resources:
limits:
cpu: 500m
memory: 128Mi
memory: 512Mi
requests:
cpu: 10m
memory: 64Mi
Expand Down
80 changes: 74 additions & 6 deletions instrumentor/controllers/instrumentationdevice/pods_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ package instrumentationdevice
import (
"context"
"fmt"
"strings"

common "github.com/odigos-io/odigos/common"
"sigs.k8s.io/controller-runtime/pkg/webhook"

corev1 "k8s.io/api/core/v1"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"k8s.io/apimachinery/pkg/runtime"
)

const (
EnvVarNamespace = "ODIGOS_WORKLOAD_NAMESPACE"
EnvVarContainerName = "ODIGOS_CONTAINER_NAME"
EnvVarPodName = "ODIGOS_POD_NAME"
)

type PodsWebhook struct{}

var _ webhook.CustomDefaulter = &PodsWebhook{}

func (p *PodsWebhook) Default(ctx context.Context, obj runtime.Object) error {
// TODO(edenfed): add object selector to mutatingwebhookconfiguration
log := logf.FromContext(ctx)
pod, ok := obj.(*corev1.Pod)
if !ok {
return fmt.Errorf("expected a Pod but got a %T", obj)
Expand All @@ -28,7 +32,71 @@ func (p *PodsWebhook) Default(ctx context.Context, obj runtime.Object) error {
pod.Annotations = map[string]string{}
}

//pod.Annotations["odigos.io/instrumented-webhook"] = "true"
log.V(0).Info("Defaulted Pod", "name", pod.Name)
// Inject ODIGOS environment variables into all containers
injectOdigosEnvVars(pod)

return nil
}

func injectOdigosEnvVars(pod *corev1.Pod) {
namespace := pod.Namespace

// Common environment variables that do not change across containers
commonEnvVars := []corev1.EnvVar{
{
Name: EnvVarNamespace,
Value: namespace,
},
{
Name: EnvVarPodName,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
}

for i := range pod.Spec.Containers {
container := &pod.Spec.Containers[i]

// Check if the container does NOT have device in conatiner limits. If so, skip the environment injection.
if !hasOdigosInstrumentationInLimits(container.Resources) {
continue
}

// Check if the environment variables are already present, if so skip inject them again.
if envVarsExist(container.Env, commonEnvVars) {
continue
}

container.Env = append(container.Env, append(commonEnvVars, corev1.EnvVar{
Name: EnvVarContainerName,
Value: container.Name,
})...)
}
}

func envVarsExist(containerEnv []corev1.EnvVar, commonEnvVars []corev1.EnvVar) bool {
envMap := make(map[string]struct{})
for _, envVar := range containerEnv {
envMap[envVar.Name] = struct{}{} // Inserting empty struct as value
}

for _, commonEnvVar := range commonEnvVars {
if _, exists := envMap[commonEnvVar.Name]; exists { // Checking if key exists
return true
}
}
return false
}

// Helper function to check if a container's resource limits have a key starting with the specified namespace
func hasOdigosInstrumentationInLimits(resources corev1.ResourceRequirements) bool {
for resourceName := range resources.Limits {
if strings.HasPrefix(string(resourceName), common.OdigosResourceNamespace) {
return true
}
}
return false
}
25 changes: 13 additions & 12 deletions instrumentor/controllers/startlangdetection/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package startlangdetection

import (
"github.com/odigos-io/odigos/instrumentor/controllers/utils"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -59,18 +60,18 @@ func SetupWithManager(mgr ctrl.Manager) error {
return err
}

//err = builder.
// ControllerManagedBy(mgr).
// Named("startlangdetection-configmaps").
// For(&corev1.ConfigMap{}).
// WithEventFilter(&utils.OnlyUpdatesPredicate{}).
// Complete(&OdigosConfigReconciler{
// Client: mgr.GetClient(),
// Scheme: mgr.GetScheme(),
// })
//if err != nil {
// return err
//}
err = builder.
ControllerManagedBy(mgr).
Named("startlangdetection-configmaps").
For(&corev1.ConfigMap{}).
WithEventFilter(&utils.OnlyUpdatesPredicate{}).
Complete(&OdigosConfigReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})
if err != nil {
return err
}

return nil
}
23 changes: 21 additions & 2 deletions instrumentor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"flag"
"os"

"github.com/odigos-io/odigos/k8sutils/pkg/env"

"github.com/odigos-io/odigos/instrumentor/controllers/instrumentationconfig"
"github.com/odigos-io/odigos/instrumentor/controllers/startlangdetection"

Expand All @@ -33,6 +35,7 @@ import (

"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook"

metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

Expand Down Expand Up @@ -98,7 +101,7 @@ func main() {
logger := zapr.NewLogger(zapLogger)
ctrl.SetLogger(logger)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
mgrOptions := ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
Expand Down Expand Up @@ -171,9 +174,25 @@ func main() {
&corev1.Namespace{}: {
Label: labels.Set{consts.OdigosInstrumentationLabel: consts.InstrumentationEnabled}.AsSelector(),
},
&corev1.ConfigMap{}: {
Field: client.InNamespace(env.GetCurrentNamespace()).AsSelector(),
},
},
},
})
}

// Check if the environment variable `LOCAL_WEBHOOK_CERT_DIR` is set.
// If defined, add WebhookServer options with the specified certificate directory.
// This is used primarily for local development environments to provide a custom path for serving TLS certificates.
localCertDir := os.Getenv("LOCAL_MUTATING_WEBHOOK_CERT_DIR")
if localCertDir != "" {
mgrOptions.WebhookServer = webhook.NewServer(webhook.Options{
CertDir: localCertDir,
})
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), mgrOptions)

if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand Down
Loading

0 comments on commit 3b89ff4

Please sign in to comment.