Skip to content

Commit

Permalink
Remove the "operator" init container
Browse files Browse the repository at this point in the history
- Remove the "operator" init container. Now there is a proper operator
  deployment, so we don't need to create CRDs in the init container.
- Update the Tetragon daemonset ClusterRole accordingly.
- Modify the Tetragon daemonset initialization logic to wait for all the
  required CRDs to show up before proceeding.

Signed-off-by: Michi Mutsuzaki <michi@isovalent.com>
  • Loading branch information
michi-covalent committed Oct 9, 2023
1 parent 10c3d07 commit 6fdff09
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 58 deletions.
4 changes: 4 additions & 0 deletions cmd/tetragon/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const (
keyEnableMsgHandlingLatency = "enable-msg-handling-latency"

keyKmods = "kmods"

keyEnablePodInfo = "enable-pod-info"
)

func readAndSetFlags() {
Expand Down Expand Up @@ -144,6 +146,8 @@ func readAndSetFlags() {

option.Config.KMods = viper.GetStringSlice(keyKmods)

option.Config.EnablePodInfo = viper.GetBool(keyEnablePodInfo)

if viper.IsSet(keyTracingPolicy) {
option.Config.TracingPolicy = viper.GetString(keyTracingPolicy)
}
Expand Down
85 changes: 63 additions & 22 deletions cmd/tetragon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ import (
_ "github.com/cilium/tetragon/pkg/sensors"

"github.com/cilium/lumberjack/v2"
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
gops "github.com/google/gops/agent"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/exp/maps"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/durationpb"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apiextensionsinformer "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)

var (
Expand Down Expand Up @@ -310,16 +316,64 @@ func tetragonExecute() error {
// Probe runtime configuration and do not fail on errors
obs.UpdateRuntimeConf(option.Config.MapDir)

watcher, err := getWatcher()
if err != nil {
return err
}
_, err = cilium.InitCiliumState(ctx, option.Config.EnableCilium)
var k8sWatcher watcher.K8sResourceWatcher
if option.Config.EnableK8s {
log.Info("Enabling Kubernetes API")
crds := map[string]struct{}{
v1alpha1.TPName: {},
v1alpha1.TPNamespacedName: {},
}
if option.Config.EnablePodInfo {
crds[v1alpha1.PIName] = struct{}{}
}
config, err := k8sconf.K8sConfig()
if err != nil {
return err
}
log.WithField("crds", maps.Keys(crds)).Info("Waiting for required CRDs")
var wg sync.WaitGroup
wg.Add(1)
k8sClient := kubernetes.NewForConfigOrDie(config)
crdClient := apiextensionsclientset.NewForConfigOrDie(config)
crdInformer := apiextensionsinformer.NewCustomResourceDefinitionInformer(crdClient, 0*time.Second, nil)
_, err = crdInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
crdObject, ok := obj.(*v1.CustomResourceDefinition)
if !ok {
log.WithField("obj", obj).Warn("Received an invalid object")
return
}
if _, ok := crds[crdObject.Name]; ok {
log.WithField("crd", crdObject.Name).Info("Found CRD")
delete(crds, crdObject.Name)
}
if len(crds) == 0 {
log.Info("Found all the required CRDs")
wg.Done()
}
},
})
if err != nil {
log.WithError(err).Error("failed to add event handler")
return err
}
stop := make(chan struct{})
go func() {
crdInformer.Run(stop)
}()
wg.Wait()
close(stop)
k8sWatcher = watcher.NewK8sWatcher(k8sClient, 60*time.Second)
} else {
log.Info("Disabling Kubernetes API")
k8sWatcher = watcher.NewFakeK8sWatcher(nil)
}
_, err := cilium.InitCiliumState(ctx, option.Config.EnableCilium)
if err != nil {
return err
}

if err := process.InitCache(watcher, option.Config.ProcessCacheSize); err != nil {
if err := process.InitCache(k8sWatcher, option.Config.ProcessCacheSize); err != nil {
return err
}

Expand All @@ -338,7 +392,7 @@ func tetragonExecute() error {
ctx, cancel2 := context.WithCancel(ctx)
defer cancel2()

hookRunner := rthooks.GlobalRunner().WithWatcher(watcher)
hookRunner := rthooks.GlobalRunner().WithWatcher(k8sWatcher)

pm, err := tetragonGrpc.NewProcessManager(
ctx,
Expand Down Expand Up @@ -631,21 +685,6 @@ func Serve(ctx context.Context, listenAddr string, srv *server.Server) error {
return nil
}

func getWatcher() (watcher.K8sResourceWatcher, error) {
if option.Config.EnableK8s {
log.Info("Enabling Kubernetes API")
config, err := k8sconf.K8sConfig()
if err != nil {
return nil, err
}
k8sClient := kubernetes.NewForConfigOrDie(config)
return watcher.NewK8sWatcher(k8sClient, 60*time.Second), nil

}
log.Info("Disabling Kubernetes API")
return watcher.NewFakeK8sWatcher(nil), nil
}

func startGopsServer() error {
// Empty means no gops
if option.Config.GopsAddr == "" {
Expand Down Expand Up @@ -773,6 +812,8 @@ func execute() error {

flags.Int(keyRBQueueSize, 65535, "Set size of channel between ring buffer and sensor go routines (default 65k)")

flags.Bool(keyEnablePodInfo, false, "Enable PodInfo custom resource")

viper.BindPFlags(flags)
return rootCmd.Execute()
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/vishvananda/netlink v1.2.1-beta.2.0.20230807190133-6afddb37c1f0
go.uber.org/atomic v1.11.0
go.uber.org/multierr v1.11.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/sync v0.4.0
golang.org/x/sys v0.13.0
golang.org/x/time v0.3.0
Expand Down Expand Up @@ -167,7 +168,6 @@ require (
go.opentelemetry.io/otel/trace v1.17.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
Expand Down
11 changes: 0 additions & 11 deletions install/kubernetes/templates/_container_tetragon.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,3 @@
{{- end -}}
{{- end -}}

{{- define "container.tetragon.init-operator" -}}
- name: {{ include "container.tetragon.name" . }}-operator
image: "{{ if .Values.tetragonOperator.image.override }}{{ .Values.tetragonOperator.image.override }}{{ else }}{{ .Values.tetragonOperator.image.repository }}{{ .Values.tetragonOperator.image.suffix }}:{{ .Values.tetragonOperator.image.tag }}{{ end }}"
imagePullPolicy: {{ .Values.imagePullPolicy }}
args:
- --config-dir=/etc/tetragon/operator.conf.d/
volumeMounts:
- mountPath: /etc/tetragon/operator.conf.d/
name: tetragon-operator-config
readOnly: true
{{- end -}}
18 changes: 1 addition & 17 deletions install/kubernetes/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,7 @@ rules:
resources:
- customresourcedefinitions
verbs:
- create
- apiGroups:
- apiextensions.k8s.io
resources:
- customresourcedefinitions
verbs:
- create
- apiGroups:
- apiextensions.k8s.io
resources:
- customresourcedefinitions
resourceNames:
- tracingpolicies.cilium.io
- tracingpoliciesnamespaced.cilium.io
- podinfo.cilium.io
verbs:
- update
- get
- list
- watch
{{- end }}
7 changes: 0 additions & 7 deletions install/kubernetes/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ spec:
securityContext:
{{- toYaml . | nindent 6 }}
{{- end }}
{{- if .Values.tetragon.enabled }}
initContainers:
{{- include "container.tetragon.init-operator" . | nindent 6 -}}
{{- end }}
containers:
{{- if eq .Values.export.mode "stdout" }}
{{- include "container.export.stdout" . | nindent 6 -}}
Expand Down Expand Up @@ -96,9 +92,6 @@ spec:
name: metadata-files
{{- end }}
{{- end }}
- name: tetragon-operator-config
configMap:
name: {{ .Release.Name }}-operator-config
{{- with .Values.extraVolumes }}
{{- toYaml . | nindent 6 }}
{{- end }}
Expand Down
1 change: 1 addition & 0 deletions install/kubernetes/templates/tetragon_configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ data:
{{- if .Values.tetragon.enableMsgHandlingLatency }}
enable-msg-handling-latency: "true"
{{- end }}
enable-pod-info: {{ .Values.tetragonOperator.podInfo.enabled | quote }}
2 changes: 2 additions & 0 deletions pkg/option/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type config struct {
EnableMsgHandlingLatency bool

KMods []string

EnablePodInfo bool
}

var (
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6fdff09

Please sign in to comment.