Skip to content
Open
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
53 changes: 53 additions & 0 deletions pkg/controller/common.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 17 additions & 6 deletions pkg/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down