Skip to content

Commit

Permalink
add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed Apr 3, 2024
1 parent 5b1d8b3 commit e6ddaf9
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions check_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {

var err error
// Parse the docker-compose file
p, err := getProjectWithNameFromFiles(name, files)
p, err := createProjectWithNameFromFiles(name, files)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -130,6 +130,7 @@ func main() {
}
}

// Container names seem to have a leading slash
func getContainerName(n []string) string {
return strings.TrimLeft(n[0], "/")
}
Expand Down Expand Up @@ -177,6 +178,7 @@ func checkContainer(c d_types.Container, checksCh chan Result) {
}
}

// hasHealthCheck checks if the container has a health check
func hasHealthCheck(cID string) (bool, error) {
container, err := apiClient.ContainerInspect(context.Background(), cID)
if err != nil {
Expand All @@ -190,6 +192,7 @@ func hasHealthCheck(cID string) (bool, error) {
return false, nil
}

// isRunning returns true if the container is in the "running" state
func isRunning(cID string) (bool, error) {
container, err := apiClient.ContainerInspect(context.Background(), cID)
if err != nil {
Expand All @@ -199,6 +202,7 @@ func isRunning(cID string) (bool, error) {
return container.State.Running, nil
}

// getHealth returns the health status of the container
func getHealth(cID string) (string, error) {
container, err := apiClient.ContainerInspect(context.Background(), cID)
if err != nil {
Expand All @@ -208,16 +212,13 @@ func getHealth(cID string) (string, error) {
return container.State.Health.Status, nil
}

// getFailedProbeLogs returns the logs of the failed probes
func getFailedProbeLogs(cID string) (string, error) {
container, err := apiClient.ContainerInspect(context.Background(), cID)
if err != nil {
return "", fmt.Errorf("failed to inspect container: %w", err)
}

if container.State.Health == nil {
return "", nil
}

var buf strings.Builder
for _, log := range container.State.Health.Log {
if log.ExitCode != 0 && log.Output != "" {
Expand All @@ -229,7 +230,8 @@ func getFailedProbeLogs(cID string) (string, error) {
return buf.String(), nil
}

func getProjectWithNameFromFiles(name string, files []string) (*c_types.Project, error) {
// createProjectWithNameFromFiles creates a project from the given files and name
func createProjectWithNameFromFiles(name string, files []string) (*c_types.Project, error) {
// Create the project options
composeOpts, err := c_cli.NewProjectOptions(
files, c_cli.WithName(name),
Expand All @@ -246,6 +248,7 @@ func getProjectWithNameFromFiles(name string, files []string) (*c_types.Project,
return project, nil
}

// getLogs returns the logs of the container
func getLogs(cID string) (string, error) {
body, err := apiClient.ContainerLogs(context.Background(), cID,
d_container.LogsOptions{
Expand All @@ -264,6 +267,7 @@ func getLogs(cID string) (string, error) {
return string(out), err
}

// getContainersFromProject returns the containers from the project
func getContainersFromProject(composeName string) ([]d_types.Container, error) {
containers, err := apiClient.ContainerList(
context.Background(),
Expand Down

0 comments on commit e6ddaf9

Please sign in to comment.