diff --git a/pkg/controller/common.go b/pkg/controller/common.go new file mode 100644 index 000000000..5a1459175 --- /dev/null +++ b/pkg/controller/common.go @@ -0,0 +1,53 @@ +package controller + +import ( + "fmt" + "os" + + insightsv1alpha1 "github.com/openshift/api/insights/v1alpha1" + "github.com/openshift/insights-operator/pkg/config/configobserver" +) + +// getCustomStoragePath determines a custom storage path by checking configuration sources +// in priority order: +// * DataGather CR specification (PersistentVolume.MountPath) +// * ConfigMap configuration (DataReporting.StoragePath) +func getCustomStoragePath(configAggregator configobserver.Interface, dataGatherCR *insightsv1alpha1.DataGather) string { + defaultPath := "" + + // Get the default path from ConfigMap configuration + if configStoragePath := configAggregator.Config().DataReporting.StoragePath; configStoragePath != "" { + defaultPath = configStoragePath + } + + if dataGatherCR == nil { + return defaultPath + } + + if dataGatherCR.Spec.Storage == nil || dataGatherCR.Spec.Storage.Type != insightsv1alpha1.StorageTypePersistentVolume { + return defaultPath + } + + if dataGatherCR.Spec.Storage.PersistentVolume != nil { + if storagePath := dataGatherCR.Spec.Storage.PersistentVolume.MountPath; storagePath != "" { + return storagePath + } + } + + return defaultPath +} + +// pathIsAvailable checks if the given path exists and is accessible. +// If the path does not exist, it attempts to create it (including all parent directories). +// +// Returns true if the path exists or was successfully created (is available) +// and false with an error if the path cannot be created. +func pathIsAvailable(path string) (bool, error) { + if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { + if err := os.MkdirAll(path, 0o777); err != nil { + return false, fmt.Errorf("can't create --path: %v", err) + } + } + + return true, nil +} diff --git a/pkg/controller/operator.go b/pkg/controller/operator.go index 14e97e918..9232281d7 100644 --- a/pkg/controller/operator.go +++ b/pkg/controller/operator.go @@ -135,12 +135,6 @@ func (s *Operator) Run(ctx context.Context, controller *controllercmd.Controller insightsConfigAPIEnabled := featureGates.Enabled(features.FeatureGateInsightsConfigAPI) runtimeExtractorEnabled := featureGates.Enabled(features.FeatureGateInsightsRuntimeExtractor) - // ensure the insight snapshot directory exists - if _, err = os.Stat(s.StoragePath); err != nil && os.IsNotExist(err) { - if err = os.MkdirAll(s.StoragePath, 0777); err != nil { - return fmt.Errorf("can't create --path: %v", err) - } - } var insightsDataGatherObserver configobserver.InsightsDataGatherObserver var dgInformer periodic.DataGatherInformer if insightsConfigAPIEnabled { @@ -179,6 +173,23 @@ func (s *Operator) Run(ctx context.Context, controller *controllercmd.Controller configAggregator := configobserver.NewConfigAggregator(secretConfigObserver, configMapObserver) go configAggregator.Listen(ctx) + // additional configurations may exist besides the default one + if customPath := getCustomStoragePath(configAggregator, nil); customPath != "" { + isValid, err := pathIsAvailable(customPath) + + if isValid { + s.StoragePath = customPath + } else { + klog.Errorf("the introduced storagePath '%s' is not available: %v", customPath, err) + klog.Infof("the default folder will be '%s'", s.StoragePath) + } + } + + // ensure the insight snapshot directory exists + if _, err := pathIsAvailable(s.StoragePath); err != nil { + return fmt.Errorf("the snapshot folder is not available: %v", err) + } + // the status controller initializes the cluster operator object and retrieves // the last sync time, if any was set statusReporter := status.NewController(configClient.ConfigV1(), configAggregator,