|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + |
| 7 | + "github.com/cyberark/conjur-inspect/pkg/check" |
| 8 | + "github.com/cyberark/conjur-inspect/pkg/log" |
| 9 | +) |
| 10 | + |
| 11 | +// ContainerAvailability checks for the availability of container runtimes |
| 12 | +// (Docker and Podman) and caches the results in the RunContext to prevent |
| 13 | +// duplicate error messages for unavailable runtimes. |
| 14 | +type ContainerAvailability struct{} |
| 15 | + |
| 16 | +// Describe provides a textual description of what this check gathers info on |
| 17 | +func (ca *ContainerAvailability) Describe() string { |
| 18 | + return "Container runtime availability" |
| 19 | +} |
| 20 | + |
| 21 | +// Run checks the availability of Docker and Podman runtimes |
| 22 | +func (ca *ContainerAvailability) Run(runContext *check.RunContext) []check.Result { |
| 23 | + // If not already initialized, this shouldn't happen but be safe |
| 24 | + if runContext.ContainerRuntimeAvailability == nil { |
| 25 | + runContext.ContainerRuntimeAvailability = make(map[string]check.RuntimeAvailability) |
| 26 | + } |
| 27 | + |
| 28 | + // Check Docker availability |
| 29 | + dockerAvailability := ca.checkRuntimeAvailability("docker") |
| 30 | + runContext.ContainerRuntimeAvailability["docker"] = dockerAvailability |
| 31 | + |
| 32 | + // Check Podman availability |
| 33 | + podmanAvailability := ca.checkRuntimeAvailability("podman") |
| 34 | + runContext.ContainerRuntimeAvailability["podman"] = podmanAvailability |
| 35 | + |
| 36 | + results := []check.Result{} |
| 37 | + |
| 38 | + // Log availability for debugging |
| 39 | + if dockerAvailability.Available { |
| 40 | + log.Debug("Docker runtime is available") |
| 41 | + } else { |
| 42 | + log.Debug("Docker runtime is not available: %v", dockerAvailability.Error) |
| 43 | + } |
| 44 | + |
| 45 | + if podmanAvailability.Available { |
| 46 | + log.Debug("Podman runtime is available") |
| 47 | + } else { |
| 48 | + log.Debug("Podman runtime is not available: %v", podmanAvailability.Error) |
| 49 | + } |
| 50 | + |
| 51 | + // Only return results in verbose mode or if no runtimes are available |
| 52 | + if runContext.VerboseErrors { |
| 53 | + if !dockerAvailability.Available { |
| 54 | + results = append(results, check.Result{ |
| 55 | + Title: "Docker availability", |
| 56 | + Status: check.StatusWarn, |
| 57 | + Value: "N/A", |
| 58 | + Message: fmt.Sprintf("Docker is not available: %v", dockerAvailability.Error), |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + if !podmanAvailability.Available { |
| 63 | + results = append(results, check.Result{ |
| 64 | + Title: "Podman availability", |
| 65 | + Status: check.StatusWarn, |
| 66 | + Value: "N/A", |
| 67 | + Message: fmt.Sprintf("Podman is not available: %v", podmanAvailability.Error), |
| 68 | + }) |
| 69 | + } |
| 70 | + } else if !dockerAvailability.Available && !podmanAvailability.Available { |
| 71 | + // Warn if no container runtimes are available |
| 72 | + results = append(results, check.Result{ |
| 73 | + Title: "Container runtimes", |
| 74 | + Status: check.StatusWarn, |
| 75 | + Value: "N/A", |
| 76 | + Message: "No container runtimes (Docker or Podman) are available. Container-related checks will be skipped.", |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + return results |
| 81 | +} |
| 82 | + |
| 83 | +// checkRuntimeAvailability checks if a runtime executable is available |
| 84 | +func (ca *ContainerAvailability) checkRuntimeAvailability(runtimeName string) check.RuntimeAvailability { |
| 85 | + _, err := exec.LookPath(runtimeName) |
| 86 | + if err != nil { |
| 87 | + return check.RuntimeAvailability{ |
| 88 | + Available: false, |
| 89 | + Error: err, |
| 90 | + } |
| 91 | + } |
| 92 | + return check.RuntimeAvailability{ |
| 93 | + Available: true, |
| 94 | + Error: nil, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// IsRuntimeAvailable is a helper function to check if a runtime is available |
| 99 | +// from any check |
| 100 | +func IsRuntimeAvailable(runContext *check.RunContext, runtimeName string) bool { |
| 101 | + if runContext.ContainerRuntimeAvailability == nil { |
| 102 | + return true // Assume available if cache not initialized |
| 103 | + } |
| 104 | + availability, exists := runContext.ContainerRuntimeAvailability[runtimeName] |
| 105 | + if !exists { |
| 106 | + return true // Assume available if not cached |
| 107 | + } |
| 108 | + return availability.Available |
| 109 | +} |
0 commit comments