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 <[email protected]>
  • Loading branch information
michi-covalent committed Oct 10, 2023
1 parent 10c3d07 commit 8f661fd
Show file tree
Hide file tree
Showing 13 changed files with 338 additions and 57 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
84 changes: 62 additions & 22 deletions cmd/tetragon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ 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"
"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 +315,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", 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 +391,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 +684,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 +811,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
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.

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

Loading

0 comments on commit 8f661fd

Please sign in to comment.