Skip to content

Commit 056c062

Browse files
authored
Add local target in Dockerfile and build for centos8 (#66)
1 parent 150e57c commit 056c062

File tree

6 files changed

+108
-68
lines changed

6 files changed

+108
-68
lines changed

.github/workflows/ci.yml

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,20 @@ jobs:
3131
steps:
3232
- name: Checkout Repository
3333
uses: actions/checkout@v2
34-
- name: Fetch Cached Artifacts
34+
- name: Setup Golang Environment
35+
uses: actions/setup-go@v2
36+
with:
37+
go-version: '${{ env.GOLANG_VERSION }}'
38+
- name: Build Binary
39+
run: >
40+
make build
41+
env:
42+
GOFLAGS: '-mod=vendor -gcflags=-trimpath=${{ github.workspace }} -asmflags=-trimpath=${{ github.workspace }}'
43+
- name: Store Artifacts in Cache
3544
uses: actions/[email protected]
3645
with:
3746
path: ${{ github.workspace }}/nginx-asg-sync
3847
key: nginx-asg-sync-${{ github.run_id }}-${{ github.run_number }}
39-
- name: Docker Buildx
40-
uses: docker/setup-buildx-action@v1
41-
with:
42-
driver-opts: network=host
43-
- name: Cache Docker layers
44-
uses: actions/[email protected]
45-
with:
46-
path: /tmp/.buildx-cache
47-
key: ${{ runner.os }}-buildx-${{ github.sha }}
48-
restore-keys: |
49-
${{ runner.os }}-buildx-
50-
- name: Build Image
51-
uses: docker/build-push-action@v2
52-
with:
53-
file: build/Dockerfile
54-
context: '.'
55-
target: builder
56-
cache-from: type=local,src=/tmp/.buildx-cache
57-
cache-to: type=local,dest=/tmp/.buildx-cache
58-
tags: nginx/nginx-asg-sync:${{ github.sha }}
5948

6049
unit-tests:
6150
name: Unit Tests
@@ -98,13 +87,14 @@ jobs:
9887
with:
9988
file: build/Dockerfile
10089
context: '.'
101-
target: rpm_based
90+
target: local
10291
load: true
10392
cache-from: type=local,src=/tmp/.buildx-cache
10493
cache-to: type=local,dest=/tmp/.buildx-cache
10594
tags: amazon-builder:${{ github.sha }}
10695
build-args: |
10796
CONTAINER_VERSION=amazonlinux:1
97+
OS_TYPE=rpm_based
10898
- name: Run Amazon 1
10999
uses: addnab/docker-run-action@v2
110100
with:
@@ -116,13 +106,14 @@ jobs:
116106
with:
117107
file: build/Dockerfile
118108
context: '.'
119-
target: rpm_based
109+
target: local
120110
load: true
121111
cache-from: type=local,src=/tmp/.buildx-cache
122112
cache-to: type=local,dest=/tmp/.buildx-cache
123113
tags: amazon2-builder:${{ github.sha }}
124114
build-args: |
125115
CONTAINER_VERSION=amazonlinux:2
116+
OS_TYPE=rpm_based
126117
- name: Run Amazon 2
127118
uses: addnab/docker-run-action@v2
128119
with:
@@ -134,32 +125,52 @@ jobs:
134125
with:
135126
file: build/Dockerfile
136127
context: '.'
137-
target: rpm_based
128+
target: local
138129
load: true
139130
cache-from: type=local,src=/tmp/.buildx-cache
140131
cache-to: type=local,dest=/tmp/.buildx-cache
141132
tags: centos7-builder:${{ github.sha }}
142133
build-args: |
143134
CONTAINER_VERSION=centos:7
135+
OS_TYPE=rpm_based
144136
- name: Run Centos 7
145137
uses: addnab/docker-run-action@v2
146138
with:
147139
image: centos7-builder:${{ github.sha }}
148140
run: /build.sh
149141
options: -v ${{ github.workspace }}/build/package/rpm:/rpm -v ${{ github.workspace }}/build_output:/build_output
142+
- name: Build Image Centos 8
143+
uses: docker/build-push-action@v2
144+
with:
145+
file: build/Dockerfile
146+
context: '.'
147+
target: local
148+
load: true
149+
cache-from: type=local,src=/tmp/.buildx-cache
150+
cache-to: type=local,dest=/tmp/.buildx-cache
151+
tags: centos8-builder:${{ github.sha }}
152+
build-args: |
153+
CONTAINER_VERSION=centos:8
154+
OS_TYPE=rpm_based
155+
- name: Run Centos 8
156+
uses: addnab/docker-run-action@v2
157+
with:
158+
image: centos8-builder:${{ github.sha }}
159+
run: /build.sh
160+
options: -v ${{ github.workspace }}/build/package/rpm:/rpm -v ${{ github.workspace }}/build_output:/build_output
150161
- name: Build Image Ubuntu Xenial
151162
uses: docker/build-push-action@v2
152163
with:
153164
file: build/Dockerfile
154165
context: '.'
155-
target: deb_based
166+
target: local
156167
load: true
157168
cache-from: type=local,src=/tmp/.buildx-cache
158169
cache-to: type=local,dest=/tmp/.buildx-cache
159170
tags: ubuntu-xenial-builder:${{ github.sha }}
160171
build-args: |
161172
CONTAINER_VERSION=ubuntu:xenial
162-
OS_VERSION=xenial
173+
OS_TYPE=deb_based
163174
- name: Run Ubuntu Xenial
164175
uses: addnab/docker-run-action@v2
165176
with:
@@ -171,14 +182,14 @@ jobs:
171182
with:
172183
file: build/Dockerfile
173184
context: '.'
174-
target: deb_based
185+
target: local
175186
load: true
176187
cache-from: type=local,src=/tmp/.buildx-cache
177188
cache-to: type=local,dest=/tmp/.buildx-cache
178189
tags: ubuntu-bionic-builder:${{ github.sha }}
179190
build-args: |
180191
CONTAINER_VERSION=ubuntu:bionic
181-
OS_VERSION=bionic
192+
OS_TYPE=deb_based
182193
- name: Run Ubuntu Bionic
183194
uses: addnab/docker-run-action@v2
184195
with:
@@ -190,14 +201,14 @@ jobs:
190201
with:
191202
file: build/Dockerfile
192203
context: '.'
193-
target: deb_based
204+
target: local
194205
load: true
195206
cache-from: type=local,src=/tmp/.buildx-cache
196207
cache-to: type=local,dest=/tmp/.buildx-cache
197208
tags: ubuntu-focal-builder:${{ github.sha }}
198209
build-args: |
199210
CONTAINER_VERSION=ubuntu:focal
200-
OS_VERSION=focal
211+
OS_TYPE=deb_based
201212
- name: Run Ubuntu Focal
202213
uses: addnab/docker-run-action@v2
203214
with:
@@ -209,14 +220,14 @@ jobs:
209220
with:
210221
file: build/Dockerfile
211222
context: '.'
212-
target: deb_based
223+
target: local
213224
load: true
214225
cache-from: type=local,src=/tmp/.buildx-cache
215226
cache-to: type=local,dest=/tmp/.buildx-cache
216227
tags: ubuntu-groovy-builder:${{ github.sha }}
217228
build-args: |
218229
CONTAINER_VERSION=ubuntu:groovy
219-
OS_VERSION=groovy
230+
OS_TYPE=deb_based
220231
- name: Run Ubuntu Groovy
221232
uses: addnab/docker-run-action@v2
222233
with:

Makefile

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
GO_DOCKER_RUN = docker run --rm -v $(shell pwd):/go/src/github.com/nginxinc/nginx-asg-sync -v $(shell pwd)/build_output:/build_output -w /go/src/github.com/nginxinc/nginx-asg-sync/cmd/sync
22
GOFLAGS ?= -mod=vendor
3+
TARGET ?= local
34

45
export DOCKER_BUILDKIT = 1
56

6-
all: amazon centos7 ubuntu-xenial amazon2 ubuntu-bionic ubuntu-focal ubuntu-groovy
7+
all: amazon centos7 centos8 ubuntu-xenial amazon2 ubuntu-bionic ubuntu-focal ubuntu-groovy
78

89
.PHONY: test
910
test:
@@ -12,32 +13,42 @@ test:
1213
lint:
1314
golangci-lint run
1415

15-
amazon:
16-
docker build -t amazon-builder --target rpm_based --build-arg CONTAINER_VERSION=amazonlinux:1 -f build/Dockerfile .
16+
.PHONY: build
17+
build:
18+
ifeq (${TARGET},local)
19+
CGO_ENABLED=0 GO111MODULE=on GOFLAGS='$(GOFLAGS)' GOOS=linux go build -installsuffix cgo -o nginx-asg-sync github.com/nginxinc/nginx-asg-sync/cmd/sync
20+
endif
21+
22+
amazon: build
23+
docker build -t amazon-builder --target ${TARGET} --build-arg CONTAINER_VERSION=amazonlinux:1 --build-arg OS_TYPE=rpm_based -f build/Dockerfile .
1724
docker run --rm -v $(shell pwd)/build/package/rpm:/rpm -v $(shell pwd)/build_output:/build_output amazon-builder
1825

19-
amazon2:
20-
docker build -t amazon2-builder --target rpm_based --build-arg CONTAINER_VERSION=amazonlinux:2 -f build/Dockerfile .
26+
amazon2: build
27+
docker build -t amazon2-builder --target ${TARGET} --build-arg CONTAINER_VERSION=amazonlinux:2 --build-arg OS_TYPE=rpm_based -f build/Dockerfile .
2128
docker run --rm -v $(shell pwd)/build/package/rpm:/rpm -v $(shell pwd)/build_output:/build_output amazon2-builder
2229

23-
centos7:
24-
docker build -t centos7-builder --target rpm_based --build-arg CONTAINER_VERSION=centos:7 -f build/Dockerfile .
30+
centos7: build
31+
docker build -t centos7-builder --target ${TARGET} --build-arg CONTAINER_VERSION=centos:7 --build-arg OS_TYPE=rpm_based -f build/Dockerfile .
2532
docker run --rm -v $(shell pwd)/build/package/rpm:/rpm -v $(shell pwd)/build_output:/build_output centos7-builder
2633

27-
ubuntu-xenial:
28-
docker build -t ubuntu-xenial-builder --target deb_based --build-arg CONTAINER_VERSION=ubuntu:xenial --build-arg OS_VERSION=xenial -f build/Dockerfile .
34+
centos8: build
35+
docker build -t centos8-builder --target ${TARGET} --build-arg CONTAINER_VERSION=centos:8 --build-arg OS_TYPE=rpm_based -f build/Dockerfile .
36+
docker run --rm -v $(shell pwd)/build/package/rpm:/rpm -v $(shell pwd)/build_output:/build_output centos8-builder
37+
38+
ubuntu-xenial: build
39+
docker build -t ubuntu-xenial-builder --target ${TARGET} --build-arg CONTAINER_VERSION=ubuntu:xenial --build-arg OS_TYPE=deb_based -f build/Dockerfile .
2940
docker run --rm -v $(shell pwd)/build/package/debian:/debian -v $(shell pwd)/build_output:/build_output ubuntu-xenial-builder
3041

31-
ubuntu-bionic:
32-
docker build -t ubuntu-bionic-builder --target deb_based --build-arg CONTAINER_VERSION=ubuntu:bionic --build-arg OS_VERSION=bionic -f build/Dockerfile .
42+
ubuntu-bionic: build
43+
docker build -t ubuntu-bionic-builder --target ${TARGET} --build-arg CONTAINER_VERSION=ubuntu:bionic --build-arg OS_TYPE=deb_based -f build/Dockerfile .
3344
docker run --rm -v $(shell pwd)/build/package/debian:/debian -v $(shell pwd)/build_output:/build_output ubuntu-bionic-builder
3445

35-
ubuntu-focal:
36-
docker build -t ubuntu-focal-builder --target deb_based --build-arg CONTAINER_VERSION=ubuntu:focal --build-arg OS_VERSION=focal -f build/Dockerfile .
46+
ubuntu-focal: build
47+
docker build -t ubuntu-focal-builder --target ${TARGET} --build-arg CONTAINER_VERSION=ubuntu:focal --build-arg OS_TYPE=deb_based -f build/Dockerfile .
3748
docker run --rm -v $(shell pwd)/build/package/debian:/debian -v $(shell pwd)/build_output:/build_output ubuntu-focal-builder
3849

39-
ubuntu-groovy:
40-
docker build -t ubuntu-groovy-builder --target deb_based --build-arg CONTAINER_VERSION=ubuntu:groovy --build-arg OS_VERSION=groovy -f build/Dockerfile .
50+
ubuntu-groovy: build
51+
docker build -t ubuntu-groovy-builder --target ${TARGET} --build-arg CONTAINER_VERSION=ubuntu:groovy --build-arg OS_TYPE=deb_based -f build/Dockerfile .
4152
docker run --rm -v $(shell pwd)/build/package/debian:/debian -v $(shell pwd)/build_output:/build_output ubuntu-groovy-builder
4253

4354
.PHONY: clean

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ Below you will find documentation on how to use nginx-asg-sync.
4343

4444
We provide packages for the following operating systems:
4545

46-
* Ubuntu: 16.04 (Xenial), 18.04 (Bionic)
47-
* CentOS/RHEL: 7
46+
* Ubuntu: 16.04 (Xenial), 18.04 (Bionic), 20.04 (Focal), 20.10 (Groovy)
47+
* CentOS/RHEL: 7, 8
4848
* Amazon Linux: 1, 2
4949

5050
Support for other operating systems can be added.
@@ -150,7 +150,7 @@ nginx-asg-sync runs as a system service and supports the start/stop/restart comm
150150

151151
For Amazon Linux 1, run: `$ sudo start|stop|restart nginx-asg-sync`
152152

153-
For Ubuntu 16.04, 18.04, 20.04, CentOS7/RHEL7 and Amazon Linux 2, run: `$ sudo service nginx-asg-sync start|stop|restart`
153+
For Ubuntu 16.04, 18.04, 20.04, 20.10, CentOS7/RHEL7, CentOS8/RHEL8 and Amazon Linux 2, run: `$ sudo service nginx-asg-sync start|stop|restart`
154154

155155
## Troubleshooting
156156

@@ -161,17 +161,23 @@ If nginx-asg-sync doesn’t work as expected, check its log file available at **
161161
You can compile nginx-asg-sync and build a software package using the provided Makefile. Before you start building a package, make sure that the following software is installed on your system:
162162
* make
163163
* Docker
164+
* Go (optional, to build the binary locally)
164165

165166
To build a software package, run: `$ make <os>`
166167
where `<os>` is the target OS. The following values are allowed:
167168
* `amazon` for Amazon Linux 1
168169
* `amazon2` for Amazon Linux 2
169170
* `centos7` for CentOS7/RHEL7
171+
* `centos8` for CentOS8/RHEL8
170172
* `ubuntu-xenial` for Ubuntu 16.04
171173
* `ubuntu-bionic` for Ubuntu 18.04
172174
* `ubuntu-focal` for Ubuntu 20.04
173175
* `ubuntu-groovy` for Ubuntu 20.10
174176

177+
by default the nginx-asg-sync binary will be built locally, to build it inside a Docker container add the `TARGET` parameter to `make`, for example:
178+
179+
`$ make ubuntu-focal TARGET=container`
180+
175181
If you run make without any arguments, it will build software packages for all supported OSes.
176182

177183
## Support

build/Dockerfile

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
ARG CONTAINER_VERSION=ubuntu:groovy
2-
ARG GOLANG_CONTAINER=golang:1.15
2+
ARG OS_TYPE=deb_based
3+
ARG PACKAGE_VERSION=0.4-1
34

4-
FROM $GOLANG_CONTAINER AS builder
5+
FROM golang:1.15-alpine AS builder
56
WORKDIR /go/src/github.com/nginxinc/nginx-asg-sync/cmd/sync
67
COPY . /go/src/github.com/nginxinc/nginx-asg-sync/
78

8-
RUN go build -o /nginx-asg-sync
9+
RUN CGO_ENABLED=0 GOFLAGS='-mod=vendor' go build -installsuffix cgo -o /nginx-asg-sync
910

1011
#---------------------------------------------------------------------------------------------
1112

@@ -14,8 +15,6 @@ FROM ${CONTAINER_VERSION} as rpm_based
1415
RUN yum install -y rpmdevtools
1516
ADD build/package/builders/rpm_based/build.sh /
1617

17-
COPY --from=builder /nginx-asg-sync /nginx-asg-sync
18-
1918
ENTRYPOINT ["/build.sh"]
2019

2120
#---------------------------------------------------------------------------------------------
@@ -25,11 +24,22 @@ FROM ${CONTAINER_VERSION} as deb_based
2524
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install debhelper dh-systemd -y
2625
ADD build/package/builders/deb_based/build.sh /
2726

28-
COPY --from=builder /nginx-asg-sync /nginx-asg-sync
29-
3027
ARG PACKAGE_VERSION
31-
ARG OS_VERSION
28+
ARG CONTAINER_VERSION
3229
ENV PACKAGE_VERSION=${PACKAGE_VERSION}
33-
ENV OS_VERSION=${OS_VERSION}
30+
ENV CONTAINER_VERSION=${CONTAINER_VERSION}
3431

3532
ENTRYPOINT ["/build.sh"]
33+
34+
#---------------------------------------------------------------------------------------------
35+
36+
FROM ${OS_TYPE} as container
37+
38+
COPY --from=builder /nginx-asg-sync /nginx-asg-sync
39+
40+
41+
#---------------------------------------------------------------------------------------------
42+
43+
FROM ${OS_TYPE} as local
44+
45+
COPY nginx-asg-sync /nginx-asg-sync

build/package/builders/deb_based/build.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/bin/bash
22

3-
# package_name=nginx-asg-sync-${PACKAGE_VERSION}
3+
package_name=nginx-asg-sync-${PACKAGE_VERSION}
44

55
export SYSTEMD='--with=systemd'
6-
mkdir -p ~/nginx-asg-sync/
7-
cp -r /debian ~/nginx-asg-sync/
8-
cd ~/nginx-asg-sync/
6+
export OS_VERSION=${CONTAINER_VERSION#*:}
7+
8+
mkdir -p ~/${package_name}/
9+
cp -r /debian ~/${package_name}/
10+
cd ~/${package_name}/
911
sed -i "s/%%CODENAME%%/${OS_VERSION}/g" debian/changelog
1012
rm debian/nginx-asg-sync.upstart
1113
dpkg-buildpackage -us -uc

cmd/sync/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import (
1515
nginx "github.com/nginxinc/nginx-plus-go-client/client"
1616
)
1717

18-
var configFile = flag.String("config_path", "/etc/nginx/config.yaml", "Path to the config file")
19-
var logFile = flag.String("log_path", "", "Path to the log file. If the file doesn't exist, it will be created")
20-
var version = "0.4-1"
18+
var (
19+
configFile = flag.String("config_path", "/etc/nginx/config.yaml", "Path to the config file")
20+
logFile = flag.String("log_path", "", "Path to the log file. If the file doesn't exist, it will be created")
21+
version = "0.4-1"
22+
)
2123

2224
const connTimeoutInSecs = 10
2325

@@ -63,7 +65,6 @@ func main() {
6365

6466
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}
6567
nginxClient, err := nginx.NewNginxClient(httpClient, commonConfig.APIEndpoint)
66-
6768
if err != nil {
6869
log.Printf("Couldn't create NGINX client: %v", err)
6970
os.Exit(10)
@@ -102,7 +103,6 @@ func main() {
102103
for {
103104
for _, upstream := range upstreams {
104105
ips, err := cloudProviderClient.GetPrivateIPsForScalingGroup(upstream.ScalingGroup)
105-
106106
if err != nil {
107107
log.Printf("Couldn't get the IP addresses for %v: %v", upstream.ScalingGroup, err)
108108
continue

0 commit comments

Comments
 (0)