Skip to content

Commit

Permalink
get token from the first namespace with lumigo resource
Browse files Browse the repository at this point in the history
  • Loading branch information
roeyazroel committed May 23, 2024
1 parent f53fe21 commit e0bc1a2
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ Dockerfile.*.cross

.idea
.DS_Store

**/__debug_bin*
6 changes: 2 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,16 @@ $ curl localhost:5000/v2/_catalog -v
Deploy the Lumigo Kubernetes operator with:
```sh
export TOKEN=<token> # The lumigo environment token
make docker-build docker-push
helm upgrade --install lumigo charts/lumigo-operator --namespace lumigo-system --create-namespace --set token=${TOKEN} --set "debug.enabled=true"
helm upgrade --install lumigo charts/lumigo-operator --namespace lumigo-system --create-namespace --set "debug.enabled=true"
```
To avoid strange issues with Docker caching the wrong images in your test environment, it is usually a better to always build a new image tag:
```sh
export IMG_VERSION=1 # Incremend this every time to try a deploy
export TOKEN=<token> # The lumigo environment token
make docker-build docker-push
helm upgrade --install lumigo ./charts/lumigo-operator/ --namespace lumigo-system --create-namespace --set "controllerManager.manager.image.tag=${IMG_VERSION}" --set "controllerManager.telemetryProxy.image.tag=${IMG_VERSION}" --set "watchdog.image.tag=${IMG_VERSION} --set token=${TOKEN} --set "debug.enabled=true"
helm upgrade --install lumigo ./charts/lumigo-operator/ --namespace lumigo-system --create-namespace --set "controllerManager.manager.image.tag=${IMG_VERSION}" --set "controllerManager.telemetryProxy.image.tag=${IMG_VERSION}" --set "watchdog.image.tag=${IMG_VERSION} --set "debug.enabled=true"
```
(Notice that the `--set "debug.enabled=true"` is of course optional, but in development is very handy, as it will, among other things, make the `telemetry-proxy` container log which OTLP data it sends upstream.)
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Install the Lumigo Kubernetes operator in your Kubernets cluster with [helm](htt

```sh
helm repo add lumigo https://lumigo-io.github.io/lumigo-kubernetes-operator
export TOKEN=<lumigo token> # The Lumigo Environment Token
helm install lumigo lumigo/lumigo-operator --namespace lumigo-system --create-namespace --set cluster.name=<cluster_name> --set token=${TOKEN}
helm install lumigo lumigo/lumigo-operator --namespace lumigo-system --create-namespace --set cluster.name=<cluster_name>
```
**Note:** You have the option to alter the namespace from `lumigo-system` to a name of your choosing, but its important to be aware that doing so might cause slight discrepancies throughout the steps below.

Expand Down
13 changes: 0 additions & 13 deletions charts/lumigo-operator/templates/secret.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions charts/lumigo-operator/templates/watchdog-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,5 @@ spec:
value: "{{ .Values.watchdog.endpoint }}"
- name: NAMESPACE
value: "{{ .Release.Namespace }}"
- name: LUMITO_TOKEN
valueFrom:
secretKeyRef:
name: {{ include "helm.fullname" . }}-token
key: token
resources: {{- toYaml .Values.watchdog.resources | nindent 8 }}
{{- end }}
1 change: 0 additions & 1 deletion charts/lumigo-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ output:
showOperatorStatus: true
cluster:
name:
token: <base64-encoded-token>
watchdog:
enabled: true
image:
Expand Down
7 changes: 5 additions & 2 deletions watchdog/src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func LoadConfig() *Config {
MAX_BATCH_SIZE: getEnvInt("MAX_BATCH_SIZE", 5),
KUBE_INTERVAL: getEnvInt("MAX_INTERVAL", 10),
NAMESPACE: getEnvString("NAMESPACE", "lumigo-system"),
LUMIGO_ENDPOINT: getEnvString("LUMIGO_ENDPOINT", "http://localhost:8000/api/v1/"),
LUMITO_TOKEN: getEnvString("LUMITO_TOKEN", "lumigo-token"),
LUMIGO_ENDPOINT: getEnvString("LUMIGO_ENDPOINT", "http://localhost:8000"),
TELEMETRY_PROXY_ENDPOINT: getEnvString("TELEMETRY_PROXY_ENDPOINT", "http://localhost:8888/metrics"),
TELEMETRY_INTERVAL: getEnvInt("TELEMETRY_INTERVAL", 10),
TOP_INTERVAL: getEnvInt("TOP_INTERVAL", 10),
Expand All @@ -36,6 +35,10 @@ func getEnvString(key string, defaultValue string) string {
return defaultValue
}

func (c *Config) SetToken(token string) {
c.LUMITO_TOKEN = token
}

func getEnvInt(key string, defaultValue int) int {
if value, exists := os.LookupEnv(key); exists {
parsedValue, err := strconv.Atoi(value)
Expand Down
2 changes: 2 additions & 0 deletions watchdog/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ func main() {
KubeWatcher, _ := watchers.NewKubeWatcher(config)
TelemetryWatcher := watchers.NewTelemetryWatcher(config)
TopWatch, _ := watchers.NewTopWatcher(config)
TokenWatch, _ := watchers.NewTokenWatcher(config)
go KubeWatcher.Watch()
go TelemetryWatcher.Watch()
go TopWatch.Watch()
go TokenWatch.Watch()

select {} // Block forever

Expand Down
10 changes: 6 additions & 4 deletions watchdog/src/reporters/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reporters

import (
"bytes"
"encoding/base64"
"encoding/json"
"net/http"
"time"
Expand All @@ -18,7 +19,7 @@ type KubeReporter struct {
batchMaxSize int
timer *time.Ticker
endpoint string
token string
config *config.Config
}

type Event struct {
Expand All @@ -43,7 +44,7 @@ func NewKubeReporter() *KubeReporter {
batchMaxSize: lumigoConfig.MAX_BATCH_SIZE,
timer: time.NewTicker(time.Duration(lumigoConfig.KUBE_INTERVAL) * time.Second),
endpoint: lumigoConfig.LUMIGO_ENDPOINT + "/api/v1/",
token: lumigoConfig.LUMITO_TOKEN,
config: lumigoConfig,
}
}

Expand Down Expand Up @@ -90,7 +91,7 @@ func (r *KubeReporter) sendBatch() {
log.Printf("Sending batch of %d events", len(r.eventBatch))

url := r.endpoint + "events"
token := r.token
token := r.config.LUMITO_TOKEN

eventBatchJSON, err := json.Marshal(r.eventBatch)
if err != nil {
Expand All @@ -103,8 +104,9 @@ func (r *KubeReporter) sendBatch() {
log.Println("Error creating request:", err)
return
}
encodedToken := base64.StdEncoding.EncodeToString([]byte(token))

req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Authorization", "Bearer "+encodedToken)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
Expand Down
11 changes: 6 additions & 5 deletions watchdog/src/reporters/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reporters

import (
"bytes"
"encoding/base64"
"log"
"net/http"

Expand All @@ -10,28 +11,28 @@ import (

type MetricsReporter struct {
endpoint string
token string
config *config.Config
}

func NewMetricsReporter(config *config.Config) *MetricsReporter {
return &MetricsReporter{
endpoint: config.LUMIGO_ENDPOINT + "/api/v1/",
token: config.LUMITO_TOKEN,
config: config,
}
}

func (r *MetricsReporter) Report(metrics string) {

url := r.endpoint + "metrics"
token := r.token
token := r.config.LUMITO_TOKEN

req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(metrics)))
if err != nil {
log.Println("Error creating request:", err)
return
}

req.Header.Set("Authorization", "Bearer "+token)
encodedToken := base64.StdEncoding.EncodeToString([]byte(token))
req.Header.Set("Authorization", "Bearer "+encodedToken)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
Expand Down
10 changes: 8 additions & 2 deletions watchdog/src/watchers/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type KubeWatcher struct {
clientset *kubernetes.Clientset
namespace string
reporter *reporters.KubeReporter
config *config.Config
}

func NewKubeWatcher(config *config.Config) (*KubeWatcher, error) {
Expand All @@ -42,6 +43,7 @@ func NewKubeWatcher(config *config.Config) (*KubeWatcher, error) {
clientset: clientset,
namespace: config.NAMESPACE,
reporter: reporter,
config: config,
}, nil
}

Expand All @@ -57,7 +59,11 @@ func (w *KubeWatcher) Watch() {

log.Printf("Watching for namespace changes in %s...\n", w.namespace)
for event := range ch {
e := event.Object.(*coreV1.Event)
w.reporter.AddEvent(*e) // Pass event directly to reporters package
if w.config.LUMITO_TOKEN != "" {
e := event.Object.(*coreV1.Event)
w.reporter.AddEvent(*e) // Pass event directly to reporters package
} else {
log.Printf("No token found, skipping event collection")
}
}
}
8 changes: 7 additions & 1 deletion watchdog/src/watchers/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ type TelemetryWatcher struct {
endpoint string
interval time.Duration
reporter *reporters.MetricsReporter
config *config.Config
}

func NewTelemetryWatcher(config *config.Config) *TelemetryWatcher {
return &TelemetryWatcher{
endpoint: config.TELEMETRY_PROXY_ENDPOINT,
interval: time.Duration(config.TELEMETRY_INTERVAL),
reporter: reporters.NewMetricsReporter(config),
config: config,
}
}

func (w *TelemetryWatcher) Watch() {
for {
w.GetTelemetryMetrics()
if w.config.LUMITO_TOKEN != "" {
w.GetTelemetryMetrics()
} else {
log.Printf("No token found, skipping telemetry metrics collection")
}
time.Sleep(w.interval * time.Second)
}
}
Expand Down
Loading

0 comments on commit e0bc1a2

Please sign in to comment.