Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/bluele/gcache"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -77,25 +76,14 @@ func Start(ctx context.Context, buildInfo trivyoperator.BuildInfo, operatorConfi
},
Cache: cache.Options{
DefaultTransform: func(obj any) (any, error) {
obj, err := cache.TransformStripManagedFields()(obj)
if err != nil {
return obj, err
}
if metaObj, ok := obj.(metav1.ObjectMetaAccessor); ok {
annotations := metaObj.GetObjectMeta().GetAnnotations()
if annotations != nil {
delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
metaObj.GetObjectMeta().SetAnnotations(annotations)
}
}

if cm, ok := obj.(*corev1.ConfigMap); ok {
// Strip data from ALL ConfigMaps except the two operator ConfigMaps
// Strip data from ALL ConfigMaps except the operator ConfigMaps
if cm.Name != trivyoperator.PoliciesConfigMapName && cm.Name != trivyoperator.TrivyConfigMapName {
cm.Data = nil
cm.BinaryData = nil
}
}

return obj, nil
},
},
Expand Down
35 changes: 35 additions & 0 deletions tests/itest/trivy-operator/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package trivy_operator

import (
"context"
"fmt"
"testing"
"time"

corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -49,6 +53,28 @@ func TestTrivyOperator(t *testing.T) {
RunSpecs(t, "Trivy Operator")
}

// ensureCRDsInstalled checks if the required trivy-operator CRDs are installed
func ensureCRDsInstalled(c client.Client) error {
requiredCRDs := []string{
"configauditreports.aquasecurity.github.io",
"vulnerabilityreports.aquasecurity.github.io",
"clusterconfigauditreports.aquasecurity.github.io",
}

ctx := context.Background()
for _, crdName := range requiredCRDs {
crd := &apiextensionsv1.CustomResourceDefinition{}
err := c.Get(ctx, types.NamespacedName{Name: crdName}, crd)
if err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("required CRD %s not found in cluster", crdName)
}
return err
}
}
return nil
}

var _ = BeforeSuite(func() {
operatorConfig, err := etc.GetOperatorConfig()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -66,6 +92,15 @@ var _ = BeforeSuite(func() {
})
Expect(err).ToNot(HaveOccurred())

// Ensure CRDs are installed before running tests
By("Checking if trivy-operator CRDs are installed")
err = ensureCRDsInstalled(kubeClient)
if err != nil {
Fail(fmt.Sprintf("CRDs are not installed in the cluster. Please install them first:\n"+
"kubectl apply -f deploy/helm/crds/\n"+
"Error: %v", err))
}

inputs = behavior.Inputs{
AssertTimeout: 5 * time.Minute,
PollingInterval: 5 * time.Second,
Expand Down
Loading