Skip to content

Commit

Permalink
refactor Makefiles, move to alpine
Browse files Browse the repository at this point in the history
  • Loading branch information
bakshi41c committed May 22, 2024
1 parent dd749df commit 5e4ac12
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 96 deletions.
65 changes: 6 additions & 59 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,79 +1,26 @@
default: all
GOFLAGS=-ldflags=-w -ldflags=-s
SYNTEST_PLUGIN_SUBDIRS = $(notdir $(wildcard ./agent/plugins/syntests/*))

LOCAL_BUILD_PATH=./bin

## Proto
build-go-proto:
@echo Building Golang proto files...
protoc --go_out=plugins=grpc:common/proto/ common/proto/syntest.proto --proto_path=common/proto/

## Agent
build-go-syntest-plugins:
@echo "Building Go syntest plugins"
cd agent && \
for dir in $(SYNTEST_PLUGIN_SUBDIRS); do \
echo "Building plugin: $$dir" && \
CGO_ENABLED=0 go build $(GOFLAGS) -o $(LOCAL_BUILD_PATH)/plugins/test-$$dir ./plugins/syntests/$$dir/ && \
echo "$$dir compiled!" || { echo "$$dir failed!"; exit 1; }; \
done

build-agent-only:
@echo "Building agent"
cd agent && CGO_ENABLED=0 go build $(GOFLAGS) -o $(LOCAL_BUILD_PATH)/agent .

.PHONY: build-agent
build-agent: build-agent-only build-go-syntest-plugins
default: docker-all

docker-agent:
@echo "Building agent container image"
cd agent && podman build -f Dockerfile -t synheart-agent:dev_latest ..

## Rest Api
build-restapi:
@echo "Building restapi binary"
cd restapi && CGO_ENABLED=0 go build $(GOFLAGS) -o $(LOCAL_BUILD_PATH)/restapi .
cd agent && podman build -f Dockerfile-NoPlugins -t synheart-agent:dev-latest-no-plugins ..
cd agent && podman build -f Dockerfile -t synheart-agent:dev-latest ..

.PHONY: docker-restapi
docker-restapi:
@echo "Building restapi container image"
cd restapi && podman build -f Dockerfile -t synheart-restapi:dev_latest ..
cd restapi && podman build -f Dockerfile -t synheart-restapi:dev-latest ..

## Controller
.PHONY: docker-controller
docker-controller:
@echo "Building controller container image"
cd controller && podman build -f Dockerfile -t synheart-controller:dev_latest ..
cd controller && podman build -f Dockerfile -t synheart-controller:dev-latest ..

.PHONY : docker-all
docker-all: docker-agent docker-restapi docker-controller

#.PHONY : test
#test: build-dummy-test-plugins build-syn-heart
# go test -race -v synthetic-heart/agent
# go test -race -v synthetic-heart/broadcaster


## SDK - Creating new syntest plugin
.PHONY : new-go-test
new-go-test:
@echo Creating Directory plugins/syntests/$(name)
mkdir agent/plugins/syntests/$(name)

@echo Creating file plugins/syntests/$(name)/$(name).go
cp agent/plugins/templates/go/test.tpl.go plugins/syntests/$(name)/$(name).go

sed -i .backup s/{{TestName}}/$(call capitalize,$(name))/g plugins/syntests/$(name)/$(name).go
rm agent/plugins/syntests/$(name)/$(name).go.backup


.PHONY : clean
clean:
rm -rf agent/bin
rm -rf controller/bin
rm -rf restapi/bin

define capitalize
$(shell echo $(1) | awk '{$$1=toupper(substr($$1,0,1))substr($$1,2); print $$0}')
endef
rm -rf restapi/bin
30 changes: 6 additions & 24 deletions agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@
FROM docker.io/library/golang:1.22-alpine3.19 as builder

# Install git + SSL ca certificates + make
# Git is required for fetching the dependencies.
# Ca-certificates is required to call HTTPS endpoints.
# Make to build go application
ARG TARGETARCH
RUN if [ "$TARGETARCH" = "arm64" ]; then ln -s /bin/run-parts /usr/bin/run-parts; fi
RUN apk update && apk upgrade && apk add --no-cache git ca-certificates make unzip g++ && update-ca-certificates && apk --no-cache add openssl wget && rm -rf /var/cache/apk/*

# Create appuser
RUN adduser -D -g '' appuser

# Install curl
RUN apk add --no-cache curl

# Install protoc
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v3.20.1/protoc-3.20.1-linux-x86_64.zip
RUN unzip protoc-3.20.1-linux-x86_64.zip
Expand All @@ -38,29 +30,19 @@ COPY . .

# Build the binary
RUN mkdir -p /app/synthetic-heart/
RUN touch /app/synthetic-heart/.emptyfile

# Compile the binary
RUN make build-agent
RUN cd agent/ && make build-go-syntest-plugins

RUN mv agent/bin/* /app/synthetic-heart

############################
# STEP 2 build a small image with only the executable
# STEP 2 build a small image with only the executables
############################
FROM gcr.io/distroless/static:nonroot
FROM localhost/synheart-agent:dev-latest-no-plugins

# Copy our static executables
COPY --from=builder /app/synthetic-heart /app/synthetic-heart

# COPY curl for curl syntest
COPY --from=builder /usr/bin/curl /usr/bin/curl

# Create a /tmp/ diretctory (required for go plugin for Unix Domain Socket)
COPY --from=builder /app/synthetic-heart/.emptyfile /tmp/.emptyfile

WORKDIR /app/synthetic-heart

# Run the hello binary.
ENTRYPOINT ["./agent"]
COPY --from=builder /app/synthetic-heart/plugins/* /app/synthetic-heart/plugins/*

# Install CURL for syntest
RUN apk add curl
56 changes: 56 additions & 0 deletions agent/Dockerfile-NoPlugins
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
############################
# STEP 1 build the image for creating the executable
############################
FROM docker.io/library/golang:1.22-alpine3.19 as builder

# Install git + SSL ca certificates + make
RUN apk update && apk upgrade && apk add --no-cache git ca-certificates make unzip g++ && update-ca-certificates && apk --no-cache add openssl wget && rm -rf /var/cache/apk/*

# Create appuser
RUN adduser -D -g '' appuser

# Install protoc
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v3.20.1/protoc-3.20.1-linux-x86_64.zip
RUN unzip protoc-3.20.1-linux-x86_64.zip
RUN cp ./bin/protoc /usr/local/bin/protoc
RUN cp -a ./include/. /usr/local/include/

# Install GRPC golang-plugin for protoc
RUN go install github.com/golang/protobuf/[email protected]

WORKDIR /app

COPY agent/go.mod agent/.
COPY agent/go.sum agent/.
COPY common/go.mod common/.
COPY common/go.sum common/.
RUN cd agent && go mod download

COPY . .

# Build the binary
RUN mkdir -p /app/synthetic-heart/
RUN touch /app/synthetic-heart/.emptyfile

# Compile the binary
RUN cd agent/ && make build-agent-only

RUN mv agent/bin/* /app/synthetic-heart
RUN mkdir /app/synthetic-heart/plugins

############################
# STEP 2 build a small image with only the executable
############################
FROM docker.io/library/alpine:3.19

# Copy our static executables
COPY --from=builder /app/synthetic-heart /app/synthetic-heart

# Create a /tmp/ diretctory (required for go plugin for Unix Domain Socket)
COPY --from=builder /app/synthetic-heart/.emptyfile /tmp/.emptyfile

WORKDIR /app/synthetic-heart

# Run the hello binary.
ENTRYPOINT ["./agent"]

20 changes: 20 additions & 0 deletions agent/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
GOFLAGS=-ldflags=-w -ldflags=-s
SYNTEST_PLUGIN_SUBDIRS = $(notdir $(wildcard ./plugins/syntests/*))
LOCAL_BUILD_PATH=./bin

## Agent
build-go-syntest-plugins:
@echo "Building Go syntest plugins"
for dir in $(SYNTEST_PLUGIN_SUBDIRS); do \
echo "Building plugin: $$dir" && \
CGO_ENABLED=0 go build $(GOFLAGS) -o $(LOCAL_BUILD_PATH)/plugins/test-$$dir ./plugins/syntests/$$dir/ && \
echo "$$dir compiled!" || { echo "$$dir failed!"; exit 1; }; \
done

build-agent-only:
@echo "Building agent"
CGO_ENABLED=0 go build $(GOFLAGS) -o $(LOCAL_BUILD_PATH)/agent .

.PHONY: build-agent
build-agent: build-agent-only build-go-syntest-plugins

## SDK - Creating new syntest plugin
.PHONY : new-go-syntest
new-go-syntest:
Expand Down
10 changes: 9 additions & 1 deletion agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ storage: # External storage configuration
type: redis # Type of external storage
address: redis:6379 # Address of the external storage
buffer: 1000 # The size on import buffer (approximately: no_of_nodes)
exportRate: 15s # How often to export health status of plugins/agent
exportRate: 15s # How often to export health statugracePeriod: 3s # When the agent is exiting, how long to wait to process/export any pending test results
syncFrequency: 30s # How often to poll external storage for new syntest configs

storage: # External storage configuration
type: redis # Type of external storage
address: redis:6379 # Address of the external storage
buffer: 1000 # The size on import buffer (approximately: no_of_nodes)
exportRate: 15s # How often to export health status of plugins/agent
pollRate: 60s # How often to poll for new test runss of plugins/agent
pollRate: 60s # How often to poll for new test runs

prometheus: # Whether to run prometheus exporter
Expand Down
1 change: 1 addition & 0 deletions agent/pluginmanager/pluginManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func NewPluginManager(configPath string) (*PluginManager, error) {
if err != nil {
return nil, errors.Wrap(err, "error discovering plugins")
}
pm.logger.Info("discovered plugins", "plugins", plugins)
pm.config.EnabledPlugins = plugins
}
// register all the plugins
Expand Down
6 changes: 3 additions & 3 deletions chart/synthetic-heart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ controller:
logLevel: INFO
image:
repository: bakshi41c/synheart-controller
tag: "1715904641"
tag: "v1.2.0-dev2"
pullPolicy: IfNotPresent
ports:
- containerPort: 2112 # For prometheus
Expand All @@ -28,7 +28,7 @@ agent:
exportRate: 15s # How often to export health status of plugins/agent
image:
repository: bakshi41c/synheart-agent
tag: "1715904641"
tag: "v1.2.0-dev2"
pullPolicy: IfNotPresent
ports:
- containerPort: 2112 # For prometheus
Expand Down Expand Up @@ -60,7 +60,7 @@ restapi:
logLevel: INFO
image:
repository: bakshi41c/synheart-restapi
tag: "1715904641"
tag: "v1.2.0-dev2"
pullPolicy: IfNotPresent
annotations: {}
ports:
Expand Down
26 changes: 21 additions & 5 deletions push.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
export TAG=$(date +%s)
podman tag localhost/synheart-controller:dev_latest bakshi41c/synheart-controller:$TAG
podman tag localhost/synheart-restapi:dev_latest bakshi41c/synheart-restapi:$TAG
podman tag localhost/synheart-agent:dev_latest bakshi41c/synheart-agent:$TAG
echo $1
export TAG=$1

podman tag localhost/synheart-controller:dev-latest bakshi41c/synheart-controller:$TAG
podman tag localhost/synheart-restapi:dev-latest bakshi41c/synheart-restapi:$TAG
podman tag localhost/synheart-agent:dev-latest bakshi41c/synheart-agent:$TAG
podman tag localhost/synheart-agent:dev-latest-no-plugins bakshi41c/synheart-agent:$TAG-no-plugins

podman tag localhost/synheart-controller:dev-latest containers.cisco.com/synthetic-heart/controller:$TAG
podman tag localhost/synheart-restapi:dev-latest containers.cisco.com/synthetic-heart/restapi:$TAG
podman tag localhost/synheart-agent:dev-latest containers.cisco.com/synthetic-heart/agent:$TAG
podman tag localhost/synheart-agent:dev-latest-no-plugins containers.cisco.com/synthetic-heart/agent:$TAG-no-plugins

podman push bakshi41c/synheart-controller:$TAG
podman push bakshi41c/synheart-restapi:$TAG
podman push bakshi41c/synheart-agent:$TAG
echo $TAG
podman push bakshi41c/synheart-agent:$TAG-no-plugins

podman push containers.cisco.com/synthetic-heart/controller:$TAG
podman push containers.cisco.com/synthetic-heart/restapi:$TAG
podman push containers.cisco.com/synthetic-heart/agent:$TAG
podman push containers.cisco.com/synthetic-heart/agent:$TAG-no-plugins

echo $1
6 changes: 2 additions & 4 deletions restapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ FROM docker.io/library/golang:1.22-alpine3.19 as builder
# Git is required for fetching the dependencies.
# Ca-certificates is required to call HTTPS endpoints.
# Make to build go application
ARG TARGETARCH
RUN if [ "$TARGETARCH" = "arm64" ]; then ln -s /bin/run-parts /usr/bin/run-parts; fi
RUN apk update && apk add --no-cache git ca-certificates make unzip g++ && update-ca-certificates
RUN apk update && apk upgrade && apk add --no-cache git ca-certificates make unzip g++ && update-ca-certificates && apk --no-cache add openssl wget && rm -rf /var/cache/apk/*

# Create appuser
RUN adduser -D -g '' appuser
Expand All @@ -18,7 +16,7 @@ WORKDIR /app
COPY . .

# Compile the binary
RUN make build-restapi
RUN cd restapi && make build-restapi

############################
# STEP 2 build a small image with only the executable
Expand Down
7 changes: 7 additions & 0 deletions restapi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GOFLAGS=-ldflags=-w -ldflags=-s
LOCAL_BUILD_PATH=./bin

## Rest Api
build-restapi:
@echo "Building restapi binary"
CGO_ENABLED=0 go build $(GOFLAGS) -o $(LOCAL_BUILD_PATH)/restapi .

0 comments on commit 5e4ac12

Please sign in to comment.