Skip to content

Commit

Permalink
Merge pull request #2260 from cyastella/dockerfile_healthcheck
Browse files Browse the repository at this point in the history
Support Dockerfile HEALTHCHECK of agent
  • Loading branch information
cyastella authored Nov 7, 2019
2 parents 64e0698 + 69381ad commit 11fe7bd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions agent/app/args/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
licenseUsage = "Print the LICENSE and NOTICE files and exit"
blacholeEC2MetadataUsage = "Blackhole the EC2 Metadata requests. Setting this option can cause the ECS Agent to fail to work properly. We do not recommend setting this option"
windowsServiceUsage = "Run the ECS agent as a Windows Service"
healthcheckServiceUsage = "Run the agent healthcheck"

versionFlagName = "version"
logLevelFlagName = "loglevel"
Expand All @@ -31,6 +32,7 @@ const (
licenseFlagName = "license"
blackholeEC2MetadataFlagName = "blackhole-ec2-metadata"
windowsServiceFlagName = "windows-service"
healthCheckFlagName = "healthcheck"
)

// Args wraps various ECS Agent arguments
Expand All @@ -51,6 +53,8 @@ type Args struct {
ECSAttributes *bool
// WindowsService indicates that the agent should run as a Windows service
WindowsService *bool
// Healthcheck indicates that agent should run healthcheck
Healthcheck *bool
}

// New creates a new Args object from the argument list
Expand All @@ -65,6 +69,7 @@ func New(arguments []string) (*Args, error) {
BlackholeEC2Metadata: flagset.Bool(blackholeEC2MetadataFlagName, false, blacholeEC2MetadataUsage),
ECSAttributes: flagset.Bool(ecsAttributesFlagName, false, ecsAttributesUsage),
WindowsService: flagset.Bool(windowsServiceFlagName, false, windowsServiceUsage),
Healthcheck: flagset.Bool(healthCheckFlagName, false, healthcheckServiceUsage),
}

err := flagset.Parse(arguments)
Expand Down
31 changes: 31 additions & 0 deletions agent/app/healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package app

import (
"net/http"

"github.com/aws/amazon-ecs-agent/agent/sighandlers/exitcodes"
"github.com/cihub/seelog"
)

// runHealthcheck runs the Agent's healthcheck
func runHealthcheck() int {
_, err := http.Get("http://localhost:51678/v1/metadata")
if err != nil {
seelog.Warnf("Health check failed with error: %v", err)
return exitcodes.ExitError
}
return exitcodes.ExitSuccess
}
2 changes: 2 additions & 0 deletions agent/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func Run(arguments []string) int {
return printLicense()
} else if *parsedArgs.Version {
return version.PrintVersion()
} else if *parsedArgs.Healthcheck {
return runHealthcheck()
}

logger.SetLevel(*parsedArgs.LogLevel)
Expand Down
3 changes: 3 additions & 0 deletions scripts/dockerfiles/Dockerfile.release
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ COPY out/cni-plugins /amazon-ecs-cni-plugins
COPY misc/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

EXPOSE 51678 51679

HEALTHCHECK CMD ["/agent", "--healthcheck"]

ENTRYPOINT ["/agent"]

0 comments on commit 11fe7bd

Please sign in to comment.