Skip to content

Commit

Permalink
🐛 properly handle deprecated container scan setting (#1007)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <ivan@mondoo.com>
  • Loading branch information
imilchev authored Jan 23, 2024
1 parent 7dae755 commit 10b04ff
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions controllers/mondooauditconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func (r *MondooAuditConfigReconciler) Reconcile(ctx context.Context, req ctrl.Re

// Set the default cron tab if none is set
shouldUpdate := false
if mondooAuditConfig.Spec.KubernetesResources.ContainerImageScanning && !mondooAuditConfig.Spec.Containers.Enable {
mondooAuditConfig.Spec.Containers.Enable = true
shouldUpdate = true
}
if mondooAuditConfig.Spec.Nodes.Enable && mondooAuditConfig.Spec.Nodes.Schedule == "" {
mondooAuditConfig.Spec.Nodes.Schedule = fmt.Sprintf("%d * * * *", time.Now().Add(1*time.Minute).Minute())
shouldUpdate = true
Expand Down
46 changes: 46 additions & 0 deletions controllers/mondooauditconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,52 @@ func TestMondooAuditConfig_Containers_Schedule(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%d %d * * *", cronStart.Minute(), cronStart.Hour()), mondooAuditConfig.Spec.Containers.Schedule)
}

func TestMondooAuditConfig_Containers_Enable(t *testing.T) {
utilruntime.Must(v1alpha2.AddToScheme(scheme.Scheme))

testMondooServiceAccount.PrivateKey = credentials.MondooServiceAccount(t)

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mClient := mockmondoo.NewMockMondooClient(mockCtrl)
testMondooClientBuilder := func(mondooclient.MondooClientOptions) (mondooclient.MondooClient, error) {
return mClient, nil
}

mondooAuditConfig := testMondooAuditConfig()
mondooAuditConfig.Spec.KubernetesResources.ContainerImageScanning = true

fakeClient := fake.NewClientBuilder().
WithStatusSubresource(mondooAuditConfig).
WithObjects(mondooAuditConfig).
Build()

ctx := context.Background()
scanApiStore := scan_api_store.NewScanApiStore(ctx)
go scanApiStore.Start()
reconciler := &MondooAuditConfigReconciler{
MondooClientBuilder: testMondooClientBuilder,
Client: fakeClient,
ScanApiStore: scanApiStore,
}

_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: testMondooAuditConfigName,
Namespace: testNamespace,
},
})
require.NoError(t, err)

err = fakeClient.Get(ctx, client.ObjectKeyFromObject(mondooAuditConfig), mondooAuditConfig)
require.NoError(t, err)

assert.True(t, mondooAuditConfig.Spec.Containers.Enable)
cronStart := time.Now().Add(1 * time.Minute)
assert.Equal(t, fmt.Sprintf("%d %d * * *", cronStart.Minute(), cronStart.Hour()), mondooAuditConfig.Spec.Containers.Schedule)
}

func testMondooAuditConfig() *v1alpha2.MondooAuditConfig {
return &v1alpha2.MondooAuditConfig{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit 10b04ff

Please sign in to comment.