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
17 changes: 9 additions & 8 deletions cmd/manager/cel-scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ func (c *CelScanner) runPlatformScan() {

// Save the scan result
outputFilePath := filepath.Join(c.celConfig.CheckResultDir, "result.json")
saveScanResult(outputFilePath, evalResultList)
if err := saveScanResult(outputFilePath, evalResultList); err != nil {
cmdLog.Error(err, "Failed to save scan results", "path", outputFilePath)
os.Exit(CelExitCodeError)
}

// Check if we need to generate ComplianceCheckResult objects
if c.celConfig.CCRGeneration {
Expand Down Expand Up @@ -808,18 +811,16 @@ func (c *CelScanner) getVariablesForTailoredProfile(tp *cmpv1alpha1.TailoredProf
return setVars, nil
}

// saveScanResult saves the scan results to a JSON file with proper indentation
func saveScanResult(filePath string, resultsList []*cmpv1alpha1.ComplianceCheckResult) {
func saveScanResult(filePath string, resultsList []*cmpv1alpha1.ComplianceCheckResult) error {
file, err := os.Create(filePath)
if err != nil {
panic(fmt.Sprintf("Failed to create result file %s: %v", filePath, err))
return fmt.Errorf("creating result file %s: %w", filePath, err)
}
defer file.Close()
// Serialize the results list to JSON
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err = encoder.Encode(resultsList)
if err != nil {
panic(fmt.Sprintf("Failed to encode results list to JSON: %v", err))
if err := encoder.Encode(resultsList); err != nil {
return fmt.Errorf("encoding results to JSON: %w", err)
}
return nil
}
20 changes: 4 additions & 16 deletions pkg/apis/compliance/v1alpha1/compliancescan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,24 +392,12 @@ func (cs *ComplianceScan) GetScannerTypeIfValid() (ScannerType, error) {
return "", ErrUnkownScanerType
}

// GetScanType get's the scan type for a scan
func (cs *ComplianceScan) GetScanType() ComplianceScanType {
scantype, err := cs.GetScanTypeIfValid()
if err != nil {
// This shouldn't happen
panic(err)
}
return scantype
func (cs *ComplianceScan) GetScanType() (ComplianceScanType, error) {
return cs.GetScanTypeIfValid()
}

// GetScannerType will get the scanner type for a scan
func (cs *ComplianceScan) GetScannerType() ScannerType {
scannertype, err := cs.GetScannerTypeIfValid()
if err != nil {
// This shouldn't happen
panic(err)
}
return scannertype
func (cs *ComplianceScan) GetScannerType() (ScannerType, error) {
return cs.GetScannerTypeIfValid()
}

// Returns whether remediation enforcement is off or not
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/compliancescan/scantype.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ func (nh *nodeScanTypeHandler) getScan() *compv1alpha1.ComplianceScan {
func (nh *nodeScanTypeHandler) getTargetNodes() ([]corev1.Node, error) {
var nodes corev1.NodeList

switch nh.scan.GetScanType() {
scanType, err := nh.scan.GetScanType()
if err != nil {
return nil, fmt.Errorf("getting scan type: %w", err)
}

switch scanType {
case compv1alpha1.ScanTypePlatform:
return nodes.Items, nil // Nodes are only relevant to the node scan type. Return the empty node list otherwise.
case compv1alpha1.ScanTypeNode:
Expand Down
Loading