Skip to content

Commit

Permalink
pkg/k8s: refactor CRD handling
Browse files Browse the repository at this point in the history
This patch refactors CRD handling.

It introduces a new package (crdutils) that includes a CRD type, meant
to hold all the information for registering a CRD.

Registration code is moved into crdutils, and the caller just needs to
specify the CRD objects to register.

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Oct 12, 2023
1 parent e3f180f commit c54bfc2
Show file tree
Hide file tree
Showing 11 changed files with 548 additions and 578 deletions.
2 changes: 1 addition & 1 deletion api/vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# github.com/cilium/tetragon v0.0.0-00010101000000-000000000000 => ../../tetragon
## explicit; go 1.21.1
## explicit; go 1.21
github.com/cilium/tetragon/pkg/matchers/bytesmatcher
github.com/cilium/tetragon/pkg/matchers/listmatcher
github.com/cilium/tetragon/pkg/matchers/stringmatcher
Expand Down
16 changes: 14 additions & 2 deletions operator/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"github.com/cilium/tetragon/pkg/k8s/crdutils"
)

var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "crd")
var (
log = logging.DefaultLogger.WithField(logfields.LogSubsys, "crd")
)

func RegisterCRDs() {
restConfig, err := getConfig()
Expand Down Expand Up @@ -52,11 +56,19 @@ func RegisterCRDs() {
version.Version(), version.MinimalVersionConstraint)
}

crds := []crdutils.CRD{}
for _, crd := range client.AllCRDs {
if option.Config.SkipPodInfoCRD && crd.CRDName == client.PodInfoCRD.CRDName {
continue
}
crds = append(crds, crd)
}

// Register the CRDs after validating that we are running on a supported
// version of K8s.
if !option.Config.SkipCRDCreation {
// if skipPodInfoCRD flag set true, don't register Pod Info CRD.
if err := client.RegisterCRDs(k8sAPIExtClient, option.Config.SkipPodInfoCRD); err != nil {
if err := crdutils.RegisterCRDs(k8sAPIExtClient, crds); err != nil {
log.WithError(err).Fatal("Unable to Register CRDs")
}
} else {
Expand Down
4 changes: 3 additions & 1 deletion operator/option/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package option

import "github.com/spf13/viper"
import (
"github.com/spf13/viper"
)

const (
TetragonOpEnvPrefix = "TETRAGON_OPERATOR"
Expand Down
43 changes: 43 additions & 0 deletions pkg/k8s/apis/cilium.io/client/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package client

import (
_ "embed"

"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
crdutils "github.com/cilium/tetragon/pkg/k8s/crdutils"
)

var (
//go:embed crds/v1alpha1/cilium.io_tracingpolicies.yaml
crdsv1Alpha1TracingPolicies []byte

TracingPolicyCRD = crdutils.NewCRDBytes(
v1alpha1.TPCRDName,
v1alpha1.TPName,
crdsv1Alpha1TracingPolicies)

//go:embed crds/v1alpha1/cilium.io_tracingpoliciesnamespaced.yaml
crdsv1Alpha1TracingPoliciesNamespaced []byte

TracingPolicyNamespacedCRD = crdutils.NewCRDBytes(
v1alpha1.TPNamespacedCRDName,
v1alpha1.TPNamespacedName,
crdsv1Alpha1TracingPoliciesNamespaced)

//go:embed crds/v1alpha1/cilium.io_podinfo.yaml
crdsv1Alpha1PodInfo []byte

PodInfoCRD = crdutils.NewCRDBytes(
v1alpha1.PICRDName,
v1alpha1.PIName,
crdsv1Alpha1PodInfo)

AllCRDs = []crdutils.CRD{
TracingPolicyCRD,
TracingPolicyNamespacedCRD,
PodInfoCRD,
}
)
204 changes: 0 additions & 204 deletions pkg/k8s/apis/cilium.io/client/register_v1beta1_crd.go

This file was deleted.

Loading

0 comments on commit c54bfc2

Please sign in to comment.