diff --git a/pkg/config/configobserver/insighgtsdatagather_observer.go b/pkg/config/configobserver/insighgtsdatagather_observer.go index 230e10d9f..3931a640b 100644 --- a/pkg/config/configobserver/insighgtsdatagather_observer.go +++ b/pkg/config/configobserver/insighgtsdatagather_observer.go @@ -19,15 +19,14 @@ import ( type InsightsDataGatherObserver interface { factory.Controller GatherConfig() *v1alpha1.GatherConfig - GatherDataPolicy() *v1alpha1.DataPolicy GatherDisabled() bool } type insightsDataGatherController struct { factory.Controller - lock sync.Mutex - configV1Alpha1Cli *configCliv1alpha1.ConfigV1alpha1Client - gatherConfig *v1alpha1.GatherConfig + lock sync.Mutex + cli configCliv1alpha1.ConfigV1alpha1Interface + gatherConfig *v1alpha1.GatherConfig } func NewInsightsDataGatherObserver(kubeConfig *rest.Config, @@ -39,10 +38,10 @@ func NewInsightsDataGatherObserver(kubeConfig *rest.Config, return nil, err } c := &insightsDataGatherController{ - configV1Alpha1Cli: configV1Alpha1Cli, + cli: configV1Alpha1Cli, } - insightDataGatherConf, err := c.configV1Alpha1Cli.InsightsDataGathers().Get(context.Background(), "cluster", metav1.GetOptions{}) + insightDataGatherConf, err := c.cli.InsightsDataGathers().Get(context.Background(), "cluster", metav1.GetOptions{}) if err != nil { klog.Errorf("Cannot read API gathering configuration: %v", err) } @@ -57,7 +56,7 @@ func NewInsightsDataGatherObserver(kubeConfig *rest.Config, } func (i *insightsDataGatherController) sync(ctx context.Context, _ factory.SyncContext) error { - insightDataGatherConf, err := i.configV1Alpha1Cli.InsightsDataGathers().Get(ctx, "cluster", metav1.GetOptions{}) + insightDataGatherConf, err := i.cli.InsightsDataGathers().Get(ctx, "cluster", metav1.GetOptions{}) if err != nil { return err } @@ -83,10 +82,3 @@ func (i *insightsDataGatherController) GatherDisabled() bool { } return false } - -// GatherDataPolicy provides DataPolicy attribute value defined in the API -func (i *insightsDataGatherController) GatherDataPolicy() *v1alpha1.DataPolicy { - i.lock.Lock() - defer i.lock.Unlock() - return &i.gatherConfig.DataPolicy -} diff --git a/pkg/config/configobserver/insightsdatagather_observer_test.go b/pkg/config/configobserver/insightsdatagather_observer_test.go new file mode 100644 index 000000000..fb9995d77 --- /dev/null +++ b/pkg/config/configobserver/insightsdatagather_observer_test.go @@ -0,0 +1,85 @@ +package configobserver + +import ( + "context" + "testing" + + "github.com/openshift/api/config/v1alpha1" + fakeConfigCli "github.com/openshift/client-go/config/clientset/versioned/fake" + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestInsightsDataGatherSync(t *testing.T) { + tests := []struct { + name string + insightsDatagatherToUpdated *v1alpha1.InsightsDataGather + expectedGatherConfig *v1alpha1.GatherConfig + expectedDisable bool + }{ + { + name: "Obfuscation configured and some disabled gatherers", + insightsDatagatherToUpdated: &v1alpha1.InsightsDataGather{ + ObjectMeta: metav1.ObjectMeta{ + Name: "cluster", + }, + Spec: v1alpha1.InsightsDataGatherSpec{ + GatherConfig: v1alpha1.GatherConfig{ + DataPolicy: v1alpha1.ObfuscateNetworking, + DisabledGatherers: []string{"fooGather", "barGather"}, + }, + }, + }, + expectedGatherConfig: &v1alpha1.GatherConfig{ + DataPolicy: v1alpha1.ObfuscateNetworking, + DisabledGatherers: []string{"fooGather", "barGather"}, + }, + expectedDisable: false, + }, + { + name: "Gathering disabled and no obfuscation", + insightsDatagatherToUpdated: &v1alpha1.InsightsDataGather{ + ObjectMeta: metav1.ObjectMeta{ + Name: "cluster", + }, + Spec: v1alpha1.InsightsDataGatherSpec{ + GatherConfig: v1alpha1.GatherConfig{ + DataPolicy: v1alpha1.NoPolicy, + DisabledGatherers: []string{"ALL"}, + }, + }, + }, + expectedGatherConfig: &v1alpha1.GatherConfig{ + DataPolicy: v1alpha1.NoPolicy, + DisabledGatherers: []string{"ALL"}, + }, + expectedDisable: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + insightDefaultConfig := &v1alpha1.InsightsDataGather{ + ObjectMeta: metav1.ObjectMeta{ + Name: "cluster", + }, + } + + client := fakeConfigCli.NewSimpleClientset(insightDefaultConfig) + idgObserver := insightsDataGatherController{ + gatherConfig: &insightDefaultConfig.Spec.GatherConfig, + cli: client.ConfigV1alpha1(), + } + err := idgObserver.sync(context.Background(), nil) + assert.NoError(t, err) + + assert.Equal(t, &insightDefaultConfig.Spec.GatherConfig, idgObserver.GatherConfig()) + _, err = idgObserver.cli.InsightsDataGathers().Update(context.Background(), tt.insightsDatagatherToUpdated, metav1.UpdateOptions{}) + assert.NoError(t, err) + err = idgObserver.sync(context.Background(), nil) + assert.NoError(t, err) + assert.Equal(t, tt.expectedGatherConfig, idgObserver.GatherConfig()) + assert.Equal(t, tt.expectedDisable, idgObserver.GatherDisabled()) + }) + } +}