Skip to content

Commit

Permalink
Reduce start-amazon-cloudwatch-agent binary size
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien committed Aug 5, 2024
1 parent dab6b24 commit 26e308f
Show file tree
Hide file tree
Showing 30 changed files with 132 additions and 180 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ build-for-docker-amd64:
$(LINUX_AMD64_BUILD)/amazon-cloudwatch-agent github.com/aws/amazon-cloudwatch-agent/cmd/amazon-cloudwatch-agent
$(LINUX_AMD64_BUILD)/start-amazon-cloudwatch-agent github.com/aws/amazon-cloudwatch-agent/cmd/start-amazon-cloudwatch-agent
$(LINUX_AMD64_BUILD)/config-translator github.com/aws/amazon-cloudwatch-agent/cmd/config-translator
cp $(BASE_SPACE)/packaging/opentelemetry-jmx-metrics.jar $(BUILD_SPACE)/bin/linux_amd64/opentelemetry-jmx-metrics.jar

build-for-docker-windows-amd64:
$(WIN_BUILD)/amazon-cloudwatch-agent.exe github.com/aws/amazon-cloudwatch-agent/cmd/amazon-cloudwatch-agent
Expand All @@ -120,7 +119,6 @@ build-for-docker-arm64:
$(LINUX_ARM64_BUILD)/amazon-cloudwatch-agent github.com/aws/amazon-cloudwatch-agent/cmd/amazon-cloudwatch-agent
$(LINUX_ARM64_BUILD)/start-amazon-cloudwatch-agent github.com/aws/amazon-cloudwatch-agent/cmd/start-amazon-cloudwatch-agent
$(LINUX_ARM64_BUILD)/config-translator github.com/aws/amazon-cloudwatch-agent/cmd/config-translator
cp $(BASE_SPACE)/packaging/opentelemetry-jmx-metrics.jar $(BUILD_SPACE)/bin/linux_arm64/opentelemetry-jmx-metrics.jar

docker-build: build-for-docker-amd64 build-for-docker-arm64
docker buildx build --platform linux/amd64,linux/arm64 . -f amazon-cloudwatch-container-insights/cloudwatch-agent-dockerfile/localbin/Dockerfile -t $(IMAGE)
Expand Down
1 change: 1 addition & 0 deletions cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func runAgent(ctx context.Context,
c := config.NewConfig()
c.OutputFilters = outputFilters
c.InputFilters = inputFilters
c.AllowUnusedFields = true

err = loadTomlConfigIntoAgent(c)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config-downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/aws/aws-sdk-go/service/ssm"

configaws "github.com/aws/amazon-cloudwatch-agent/cfg/aws"
commonconfig "github.com/aws/amazon-cloudwatch-agent/cfg/commonconfig"
"github.com/aws/amazon-cloudwatch-agent/cfg/commonconfig"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/util"
Expand Down
3 changes: 2 additions & 1 deletion cmd/config-translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"

"github.com/aws/amazon-cloudwatch-agent/cfg/commonconfig"
userutil "github.com/aws/amazon-cloudwatch-agent/internal/util/user"
"github.com/aws/amazon-cloudwatch-agent/translator"
"github.com/aws/amazon-cloudwatch-agent/translator/cmdutil"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
Expand Down Expand Up @@ -104,7 +105,7 @@ func main() {
// run as user only applies to non container situation.
current, err := user.Current()
if err == nil && current.Name == "root" {
runAsUser, err := cmdutil.DetectRunAsUser(mergedJsonConfigMap)
runAsUser, err := userutil.DetectRunAsUser(mergedJsonConfigMap)
if err != nil {
log.Panic("E! Failed to detectRunAsUser")
}
Expand Down
37 changes: 23 additions & 14 deletions cmd/start-amazon-cloudwatch-agent/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
"os/exec"
"syscall"

"github.com/BurntSushi/toml"

"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/aws/amazon-cloudwatch-agent/internal/util/user"
"github.com/aws/amazon-cloudwatch-agent/tool/paths"
"github.com/aws/amazon-cloudwatch-agent/translator/cmdutil"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
)

func startAgent(writer io.WriteCloser) error {
if os.Getenv(config.RUN_IN_CONTAINER) == config.RUN_IN_CONTAINER_TRUE {
if envconfig.IsRunningInContainer() {
// Use exec so PID 1 changes to agent from start-agent.
execArgs := []string{
paths.AgentBinaryPath, // when using syscall.Exec, must pass binary name as args[0]
Expand All @@ -37,13 +38,16 @@ func startAgent(writer io.WriteCloser) error {
return nil
}

mergedJsonConfigMap, err := generateMergedJsonConfigMap()
configMap, err := getTOMLConfigMap()
if err != nil {
log.Printf("E! Failed to generate merged json config: %v ", err)
log.Printf("E! Failed to read TOML config: %v ", err)
return err
}

_, err = cmdutil.ChangeUser(mergedJsonConfigMap)
runAsUser, _ := user.DetectRunAsUser(configMap)
log.Printf("I! Detected runAsUser: %v", runAsUser)

_, err = user.ChangeUser(runAsUser)
if err != nil {
log.Printf("E! Failed to ChangeUser: %v ", err)
return err
Expand Down Expand Up @@ -77,11 +81,16 @@ func startAgent(writer io.WriteCloser) error {
return nil
}

func generateMergedJsonConfigMap() (map[string]interface{}, error) {
ctx := context.CurrentContext()
setCTXOS(ctx)
ctx.SetInputJsonFilePath(paths.JsonConfigPath)
ctx.SetInputJsonDirPath(paths.JsonDirPath)
ctx.SetMultiConfig("remove")
return cmdutil.GenerateMergedJsonConfigMap(ctx)
func getTOMLConfigMap() (map[string]any, error) {
f, err := os.Open(paths.TomlConfigPath)
if err != nil {
return nil, err
}
defer f.Close()
var m map[string]any
_, err = toml.NewDecoder(f).Decode(&m)
if err != nil {
return nil, err
}
return m, nil
}
16 changes: 0 additions & 16 deletions cmd/start-amazon-cloudwatch-agent/path_darwin.go

This file was deleted.

16 changes: 0 additions & 16 deletions cmd/start-amazon-cloudwatch-agent/path_linux.go

This file was deleted.

5 changes: 2 additions & 3 deletions cmd/start-amazon-cloudwatch-agent/path_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (
"os"
"os/exec"

"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/aws/amazon-cloudwatch-agent/tool/paths"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
)

func startAgent(writer io.WriteCloser) error {

if os.Getenv(config.RUN_IN_CONTAINER) != config.RUN_IN_CONTAINER_TRUE {
if !envconfig.IsRunningInContainer() {
if err := writer.Close(); err != nil {
log.Printf("E! Cannot close the log file, ERROR is %v \n", err)
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ import (

"gopkg.in/natefinch/lumberjack.v2"

"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/aws/amazon-cloudwatch-agent/internal/constants"
"github.com/aws/amazon-cloudwatch-agent/tool/paths"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
)

// We use an environment variable here because we need this condition before the translator reads agent config json file.
var runInContainer = os.Getenv(config.RUN_IN_CONTAINER)

func translateConfig() error {
args := []string{"--output", paths.TomlConfigPath, "--mode", "auto"}
if runInContainer == config.RUN_IN_CONTAINER_TRUE {
if envconfig.IsRunningInContainer() {
args = append(args, "--input-dir", paths.CONFIG_DIR_IN_CONTAINER)
} else {
args = append(args, "--input", paths.JsonConfigPath, "--input-dir", paths.JsonDirPath, "--config", paths.CommonConfigPath)
Expand All @@ -39,7 +37,7 @@ func translateConfig() error {
case status.Exited():
log.Printf("I! Return exit error: exit code=%d\n", status.ExitStatus())

if status.ExitStatus() == config.ERR_CODE_NOJSONFILE {
if status.ExitStatus() == constants.ExitCodeNoJSONFile {
log.Printf("I! No json config files found, please provide config, exit now\n")
os.Exit(0)
}
Expand All @@ -55,7 +53,7 @@ func translateConfig() error {
func main() {
var writer io.WriteCloser

if runInContainer != config.RUN_IN_CONTAINER_TRUE {
if !envconfig.IsRunningInContainer() {
writer = &lumberjack.Logger{
Filename: paths.AgentLogFilePath,
MaxSize: 100, //MB
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/aws/amazon-cloudwatch-agent

go 1.22.4

replace github.com/influxdata/telegraf => github.com/aws/telegraf v0.10.2-0.20240423220441-63baeaedb379
replace github.com/influxdata/telegraf => github.com/aws/telegraf v0.10.2-0.20240802204712-483bd77d26a2

replace (
go.opentelemetry.io/collector/config/configgrpc => github.com/amazon-contributing/opentelemetry-collector-contrib/config/configgrpc v0.0.0-20240709194807-b0f0c0eda01b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ github.com/aws/aws-sdk-go-v2/service/timestreamwrite v1.13.6/go.mod h1:akrYtxss2
github.com/aws/smithy-go v1.8.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
github.com/aws/smithy-go v1.17.0 h1:wWJD7LX6PBV6etBUwO0zElG0nWN9rUhp0WdYeHSHAaI=
github.com/aws/smithy-go v1.17.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/aws/telegraf v0.10.2-0.20240423220441-63baeaedb379 h1:EaMA5kc5yQzobctnBE8MYD9h4HPQ/YtCg4u0mFKXAj8=
github.com/aws/telegraf v0.10.2-0.20240423220441-63baeaedb379/go.mod h1:tSaq8qDvwntXHIWy6YTHPoWttYsOnF7Hm3mpZfHkIrA=
github.com/aws/telegraf v0.10.2-0.20240802204712-483bd77d26a2 h1:MxKmCOpGDxB6nrezuG/kUbzVNq5I5TMXt6ymrjiL5xU=
github.com/aws/telegraf v0.10.2-0.20240802204712-483bd77d26a2/go.mod h1:5LhWLYfsZ7isLfw+TJUxPdTuzYuP8qiMiXz/DvqovRY=
github.com/aws/telegraf/patches/gopsutil/v3 v3.0.0-20231109213610-a8c21c54a2be h1:sF6OUdk1hpuX7lf74vn+zBUFtQRe+hky0jmMYyFp5Kk=
github.com/aws/telegraf/patches/gopsutil/v3 v3.0.0-20231109213610-a8c21c54a2be/go.mod h1:1W1wnODUDv+FBSAtAa878Kxto5kj8eV+kI0AF4LIjq4=
github.com/awslabs/kinesis-aggregation/go v0.0.0-20210630091500-54e17340d32f h1:Pf0BjJDga7C98f0vhw+Ip5EaiE07S3lTKpIYPNS0nMo=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT

package config
package constants

const (
ERR_CODE_NOJSONFILE = 99
ExitCodeNoJSONFile = 99
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build linux && go1.16
// +build linux,go1.16

package cmdutil
package user

import "syscall"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// +build 386 arm
// +build !go1.16

package cmdutil
package user

import (
"golang.org/x/sys/unix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// +build arm64 amd64 mips mipsle mips64 mips64le ppc ppc64 ppc64le riscv64 s390x
// +build !go1.16

package cmdutil
package user

import (
"golang.org/x/sys/unix"
Expand Down
35 changes: 9 additions & 26 deletions translator/cmdutil/userutil.go → internal/util/user/userutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
//go:build linux || darwin
// +build linux darwin

package cmdutil
package user

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
)

var (
Expand All @@ -27,23 +24,21 @@ type ChownFunc func(name string, uid, gid int) error
var chown ChownFunc = os.Chown

// DetectRunAsUser get the user name from toml config. It runs on all platforms except windows.
func DetectRunAsUser(mergedJsonConfigMap map[string]interface{}) (runAsUser string, err error) {
func DetectRunAsUser(configMap map[string]any) (string, error) {
fmt.Printf("I! Detecting run_as_user...\n")
if agentSection, ok := mergedJsonConfigMap["agent"]; ok {
agent := agentSection.(map[string]interface{})
if agentSection, ok := configMap["agent"]; ok {
agent := agentSection.(map[string]any)
if user, ok := agent["run_as_user"]; ok {
if runasuser, ok := user.(string); ok {
return runasuser, nil
if runAsUser, ok := user.(string); ok {
return runAsUser, nil
}

log.Panicf("E! run_as_user is not string %v", user)
}

// agent section exists, but "runasuser" does not exist, then use "root"
// agent section exists, but "run_as_user" does not exist, then use "root"
return "root", nil
}

// no agent section, it means no runasuser, use "root"
// no agent section, it means no run_as_user, use "root"
return "root", nil
}

Expand All @@ -70,7 +65,6 @@ func changeFileOwner(uid, gid int) error {
// or with special purpose to be changed to be owned by root when run_as_user option
// is removed from the configuration
func chownRecursive(uid, gid int, dir string) error {

err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -100,7 +94,7 @@ func chownRecursive(uid, gid int, dir string) error {
return nil
}

if err := chown(path, uid, gid); err != nil {
if err = chown(path, uid, gid); err != nil {
return err
}
return nil
Expand All @@ -111,14 +105,3 @@ func chownRecursive(uid, gid int, dir string) error {
}
return nil
}

func VerifyCredentials(ctx *context.Context, runAsUser string) {
credentials := ctx.Credentials()
if (config.ModeOnPrem == ctx.Mode()) || (config.ModeOnPremise == ctx.Mode()) {
if runAsUser != "root" {
if _, ok := credentials["shared_credential_file"]; !ok {
log.Panic("E! Credentials path is not set while runasuser is not root")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build darwin
// +build darwin

package cmdutil
package user

import (
"fmt"
Expand Down Expand Up @@ -113,9 +113,7 @@ func switchUser(execUser *user.User) error {
return nil
}

func ChangeUser(mergedJsonConfigMap map[string]interface{}) (string, error) {
runAsUser, _ := DetectRunAsUser(mergedJsonConfigMap)
log.Printf("I! Detected runAsUser: %v", runAsUser)
func ChangeUser(runAsUser string) (string, error) {
if runAsUser == "" {
return "root", nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build darwin
// +build darwin

package cmdutil
package user

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build linux
// +build linux

package cmdutil
package user

import (
"bufio"
Expand Down Expand Up @@ -141,9 +141,7 @@ func getRunAsExecUser(runasuser string) (*ExecUser, error) {
return toExecUser(newUser)
}

func ChangeUser(mergedJsonConfigMap map[string]interface{}) (string, error) {
runAsUser, _ := DetectRunAsUser(mergedJsonConfigMap)
log.Printf("I! Detected runAsUser: %v", runAsUser)
func ChangeUser(runAsUser string) (string, error) {
if runAsUser == "" {
runAsUser = "root"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build linux
// +build linux

package cmdutil
package user

import (
"fmt"
Expand Down
Loading

0 comments on commit 26e308f

Please sign in to comment.