Skip to content

Commit 934ec2d

Browse files
committed
Refactor charts
1 parent ca79294 commit 934ec2d

File tree

13 files changed

+98
-48
lines changed

13 files changed

+98
-48
lines changed

agent/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ type AgentConfigurations struct {
1111
SANamespace string `envconfig:"SA_NAMESPACE" default:"default"`
1212
SAName string `envconfig:"SA_NAME" default:"default"`
1313
OutdatedInterval string `envconfig:"OUTDATED_INTERVAL" default:"0"`
14-
KubeAllResources string `envconfig:"KUBE_ALL_RESOURCES" default:"*/30 * * * *"`
14+
KubeAllResourcesInterval string `envconfig:"KUBE_ALL_RESOURCES_INTERVAL" default:"*/30 * * * *"`
15+
KubeAllResourcesEnabled bool `envconfig:"KUBE_ALL_RESOURCES_ENABLED" default:"true"`
1516
GetAllInterval string `envconfig:"GETALL_INTERVAL" default:"*/30 * * * *"`
1617
KubeScoreInterval string `envconfig:"KUBESCORE_INTERVAL" default:"*/40 * * * *"`
1718
RakkessInterval string `envconfig:"RAKKESS_INTERVAL" default:"*/50 * * * *"`

agent/kubviz/k8smetrics_agent.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ func main() {
160160
events.LogErr(err)
161161
err = kubepreupgrade.KubePreUpgradeDetector(config, js)
162162
events.LogErr(err)
163-
err = kubeallresources.PublishAllResources(config)
164-
events.LogErr(err)
163+
164+
if cfg.KubeAllResourcesEnabled {
165+
err = kubeallresources.PublishAllResources(config)
166+
events.LogErr(err)
167+
}
168+
165169
err = ketall.GetAllResources(config, js)
166170
events.LogErr(err)
167171
err = rakkess.RakeesOutput(config, js)

agent/kubviz/scheduler/scheduler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ func InitScheduler(config *rest.Config, js nats.JetStreamContext, cfg config.Age
104104
log.Fatal("failed to do job", err)
105105
}
106106
}
107-
if cfg.GetAllInterval != "" && cfg.GetAllInterval != "0" {
108-
sj, err := NewKubeAllResourcesJob(config, cfg.GetAllInterval)
107+
108+
if cfg.KubeAllResourcesEnabled && cfg.KubeAllResourcesInterval != "" && cfg.KubeAllResourcesInterval != "0" {
109+
sj, err := NewKubeAllResourcesJob(config, cfg.KubeAllResourcesInterval)
109110
if err != nil {
110111
log.Fatal("no time interval", err)
111112
}
@@ -114,6 +115,7 @@ func InitScheduler(config *rest.Config, js nats.JetStreamContext, cfg config.Age
114115
log.Fatal("failed to do job", err)
115116
}
116117
}
118+
117119
if cfg.GetAllInterval != "" && cfg.GetAllInterval != "0" {
118120
sj, err := NewKetallJob(config, js, cfg.GetAllInterval)
119121
if err != nil {

charts/agent/templates/deployment.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ spec:
101101
value: {{ .Values.opentelemetry.url }}
102102
- name : APPLICATION_NAME
103103
value : {{ .Values.opentelemetry.appName }}
104+
{{- if .Values.transportV2.enabled }}
104105
- name: OTEL_EXPORTER_OTLP_ENDPOINT
105-
value: {{ .Values.otelCollector.exporterOtlpEndpoint }}
106+
value: {{ .Values.transportV2.exporterOtlpEndpoint }}
107+
{{- end }}
108+
- name: KUBE_ALL_RESOURCES_ENABLED
109+
value: "{{ .Values.kubeAllResources.enabled }}"
106110
volumeMounts:
107111
{{- if .Values.mtls.enabled }}
108112
- name: mtls

charts/agent/values.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,13 @@ opentelemetry:
267267
url: "otelcollector.local"
268268
appName: "kubviz"
269269

270-
# TODO: Merge with opentelemetry key above
271-
otelCollector:
270+
transportV2:
271+
enabled: true
272272
exporterOtlpEndpoint: grpc://kubviz-client-otel-collector:4317
273273

274+
kubeAllResources:
275+
enabled: true
276+
274277
clusterName: "kubviz"
275278

276279
externalSecrets:

charts/client/templates/configmap-otel-collector-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- if .Values.otelCollector.enabled }}
12
apiVersion: v1
23
kind: ConfigMap
34
metadata:
@@ -30,3 +31,4 @@ data:
3031
receivers: [otlp]
3132
processors: [batch]
3233
exporters: [nats]
34+
{{- end }}

charts/client/templates/deployment-otel-collector.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- if .Values.otelCollector.enabled }}
12
apiVersion: apps/v1
23
kind: Deployment
34
metadata:
@@ -33,4 +34,4 @@ spec:
3334
- name: otel-collector-config
3435
configMap:
3536
name: {{ include "client.fullname" . }}-otel-collector-config
36-
37+
{{- end }}

charts/client/templates/deployment.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ spec:
149149
key: {{ .Values.existingClickhouse.secret.passwordkey }}
150150
{{- end }}
151151
{{- end }}
152+
{{- if .Values.dgraph.enabled }}
152153
- name: DGRAPH_GRPC_ADDRESS
153-
value: {{ .Values.dgraph.grpc_address }}
154+
value: {{ .Values.dgraph.grpcAddress }}
155+
{{- end }}
154156
- name: DB_PORT
155157
value: "9000"
156158
- name: TTL_INTERVAL

charts/client/values.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ externalSecrets:
8484
create: false
8585

8686
otelCollector:
87+
enabled: true
8788
image:
8889
repository: localhost:5000/kubviz-otel-collector
8990
pullPolicy: Always
@@ -132,7 +133,8 @@ existingClickhouse:
132133
# passwordkey: ""
133134

134135
dgraph:
135-
grpc_address: dgraph-public:9080
136+
enabled: true
137+
grpcAddress: dgraph-public:9080
136138

137139
grafana:
138140
enabled: false

client/pkg/application/application.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
)
2525

2626
type Application struct {
27-
Config *config.Config
28-
conn *clients.NATSContext
29-
dbClient clickhouse.DBInterface
27+
Config *config.Config
28+
conn *clients.NATSContext
29+
dbClient clickhouse.DBInterface
30+
dgraphGrpc *grpc.ClientConn
3031
}
3132

3233
const (
@@ -72,19 +73,22 @@ func Start() *Application {
7273
}
7374

7475
// K8s Dgraph
76+
var dgraphClient *dgo.Dgraph
77+
var grpcConn *grpc.ClientConn
7578

76-
// Initialize Dgraph client
77-
grpcConn, err := grpc.Dial(cfg.DgraphGrpcAddress, grpc.WithInsecure())
78-
if err != nil {
79-
log.Fatalf("Error connecting to Dgraph: %v", err)
80-
}
81-
defer conn.Close()
82-
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(grpcConn))
79+
if cfg.DgraphGrpcAddress != "" {
80+
// Initialize Dgraph client
81+
grpcConn, err = grpc.Dial(cfg.DgraphGrpcAddress, grpc.WithInsecure())
82+
if err != nil {
83+
log.Fatalf("Error connecting to Dgraph: %v", err)
84+
}
85+
dgraphClient = dgo.NewDgraphClient(api.NewDgraphClient(grpcConn))
8386

84-
// Setup schema
85-
err = dgraph.SetupDgraphSchema(ctx, dgraphClient)
86-
if err != nil {
87-
log.Fatalf("Failed to set up Dgraph schema: %v", err)
87+
// Setup schema
88+
err = dgraph.SetupDgraphSchema(ctx, dgraphClient)
89+
if err != nil {
90+
log.Fatalf("Failed to set up Dgraph schema: %v", err)
91+
}
8892
}
8993

9094
// Connect to NATS
@@ -140,17 +144,29 @@ func Start() *Application {
140144
}
141145

142146
return &Application{
143-
Config: cfg,
144-
conn: natsContext,
145-
dbClient: dbClient,
147+
Config: cfg,
148+
conn: natsContext,
149+
dbClient: dbClient,
150+
dgraphGrpc: grpcConn,
146151
}
147152
}
148153

149154
func (app *Application) Close() {
150155
log.Printf("Closing the service gracefully")
151-
app.conn.Close()
152-
app.dbClient.Close()
156+
157+
if app.conn != nil {
158+
app.conn.Close()
159+
}
160+
161+
if app.dbClient != nil {
162+
app.dbClient.Close()
163+
}
164+
165+
if app.dgraphGrpc != nil {
166+
app.dgraphGrpc.Close()
167+
}
153168
}
169+
154170
func exportDataForTables(db *sql.DB) error {
155171
//pvcMountPath := "/mnt/client/kbz"
156172
tables := []string{

client/pkg/clients/kubviz_client.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ import (
55
"encoding/json"
66
"log"
77

8+
"github.com/dgraph-io/dgo/v240"
9+
"github.com/intelops/kubviz/client/pkg/clickhouse"
10+
"github.com/intelops/kubviz/client/pkg/config"
11+
"github.com/intelops/kubviz/client/pkg/dgraph"
12+
"github.com/intelops/kubviz/client/pkg/kubernetes"
813
"github.com/intelops/kubviz/constants"
914
"github.com/intelops/kubviz/model"
1015
"github.com/intelops/kubviz/pkg/opentelemetry"
1116
"github.com/kelseyhightower/envconfig"
1217
"github.com/kuberhealthy/kuberhealthy/v2/pkg/health"
1318
"github.com/nats-io/nats.go"
19+
"go.opentelemetry.io/collector/pdata/plog"
1420
"go.opentelemetry.io/otel"
1521
"go.opentelemetry.io/otel/attribute"
1622
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
17-
18-
"github.com/intelops/kubviz/client/pkg/clickhouse"
19-
"github.com/intelops/kubviz/client/pkg/config"
20-
"github.com/intelops/kubviz/client/pkg/dgraph"
21-
"github.com/intelops/kubviz/client/pkg/kubernetes"
22-
"go.opentelemetry.io/collector/pdata/plog"
23-
24-
"github.com/dgraph-io/dgo/v240"
2523
)
2624

2725
type SubscriptionInfo struct {
@@ -42,12 +40,6 @@ func (n *NATSContext) SubscribeAllKubvizNats(conn clickhouse.DBInterface, dgraph
4240
log.Fatalf("Could not parse env Config: %v", err)
4341
}
4442

45-
plogUnmarshaller := &plog.JSONUnmarshaler{}
46-
47-
// Create a new repository instance
48-
repo := dgraph.NewDgraphKubernetesResourceRepository(dgraphClient)
49-
relationshipRepo := dgraph.NewDgraphRelationshipRepository(dgraphClient)
50-
5143
subscriptions := []SubscriptionInfo{
5244
{
5345
Subject: constants.KetallSubject,
@@ -216,7 +208,16 @@ func (n *NATSContext) SubscribeAllKubvizNats(conn clickhouse.DBInterface, dgraph
216208
log.Println()
217209
},
218210
},
219-
{
211+
}
212+
213+
if dgraphClient != nil {
214+
plogUnmarshaller := &plog.JSONUnmarshaler{}
215+
216+
// Create a new repository instance
217+
repo := dgraph.NewDgraphKubernetesResourceRepository(dgraphClient)
218+
relationshipRepo := dgraph.NewDgraphRelationshipRepository(dgraphClient)
219+
220+
subscriptions = append(subscriptions, SubscriptionInfo{
220221
Subject: constants.KubeAllResourcesSubject,
221222
Consumer: cfg.KubeAllResourcesConsumer,
222223
Handler: func(msg *nats.Msg) {
@@ -237,6 +238,8 @@ func (n *NATSContext) SubscribeAllKubvizNats(conn clickhouse.DBInterface, dgraph
237238
sl := sls.At(j)
238239
logRecords := sl.LogRecords()
239240
for k := 0; k < logRecords.Len(); k++ {
241+
ctx := context.Background()
242+
240243
logRecord := logRecords.At(k)
241244

242245
// Print log body
@@ -291,7 +294,7 @@ func (n *NATSContext) SubscribeAllKubvizNats(conn clickhouse.DBInterface, dgraph
291294
}
292295
}
293296
},
294-
},
297+
})
295298
}
296299

297300
for _, sub := range subscriptions {

dockerfiles/agent/kubviz/Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
FROM golang:1.22 as builder
33

44
WORKDIR /workspace
5+
56
# Copy the Go Modules manifests
6-
COPY ./ ./
7+
COPY ./go.mod ./
8+
COPY ./go.sum ./
79
RUN go mod download
10+
11+
COPY ./ ./
12+
813
RUN gofmt -w -r '"github.com/googleapis/gnostic/OpenAPIv2" -> "github.com/googleapis/gnostic/openapiv2"' /go/pkg/mod/sigs.k8s.io/kustomize/
14+
915
# Build
1016
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o kubviz_agent agent/kubviz/*.go
1117

dockerfiles/client/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
FROM cgr.dev/chainguard/go:latest AS builder
33

44
WORKDIR /workspace
5+
56
# Copy the Go Modules manifests
6-
COPY ./ ./
7+
COPY ./go.mod ./
8+
COPY ./go.sum ./
79
RUN go mod download
810

11+
COPY ./ ./
12+
913
# Build
1014
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o k8smetrics_client client/main.go
1115

0 commit comments

Comments
 (0)