Skip to content

Commit e54da95

Browse files
committed
Merge branch 'main' into add-nginx-reload-complete-check
2 parents 7b86299 + 32cff72 commit e54da95

File tree

16 files changed

+361
-179
lines changed

16 files changed

+361
-179
lines changed

.github/workflows/ci.yml

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,27 @@ permissions:
2020

2121
env:
2222
NFPM_VERSION: 'v2.35.3'
23-
GOPROXY: "https://${{ secrets.ARTIFACTORY_USER }}:${{ secrets.ARTIFACTORY_TOKEN }}@azr.artifactory.f5net.com/artifactory/api/go/f5-nginx-go-dev"
23+
GOPROXY: "direct"
2424

2525
jobs:
26+
proxy-sanity-check:
27+
name: Proxy Sanity Check
28+
runs-on: ubuntu-22.04
29+
if: ${{ !github.event.pull_request.head.repo.fork }}
30+
env:
31+
GOPROXY: "https://${{ secrets.ARTIFACTORY_USER }}:${{ secrets.ARTIFACTORY_TOKEN }}@azr.artifactory.f5net.com/artifactory/api/go/f5-nginx-go-dev"
32+
steps:
33+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
34+
with:
35+
fetch-tags: 'true'
36+
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
37+
with:
38+
go-version-file: 'go.mod'
39+
cache: false
40+
- name: run goproxy-sanity-check
41+
run: |
42+
make build
43+
2644
lint:
2745
name: Lint
2846
runs-on: ubuntu-22.04
@@ -36,9 +54,12 @@ jobs:
3654
run: make install-tools
3755
- name: run lint
3856
run: make lint
57+
3958
unit-test:
4059
name: Unit Tests
4160
runs-on: ubuntu-22.04
61+
permissions:
62+
contents: write
4263
steps:
4364
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
4465
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
@@ -52,9 +73,10 @@ jobs:
5273
with:
5374
config: ./.testcoverage.yaml
5475
## when token is not specified (value '') this feature is turned off
55-
git-token: ${{ github.ref_name == 'v3' && secrets.GITHUB_TOKEN || '' }}
76+
git-token: ${{ github.ref_name == 'main' && secrets.GITHUB_TOKEN || '' }}
5677
## name of orphaned branch where badges are stored
5778
git-branch: badges
79+
5880
race-condition-test:
5981
name: Unit tests with race condition detection
6082
runs-on: ubuntu-22.04
@@ -66,11 +88,14 @@ jobs:
6688
cache: false
6789
- name: Run unit tests with race condition detection
6890
run: make race-condition-test
91+
6992
build-unsigned-snapshot:
7093
name: Build Unsigned Snapshot
7194
runs-on: ubuntu-22.04
7295
steps:
7396
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
97+
with:
98+
fetch-tags: 'true'
7499
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
75100
with:
76101
go-version-file: 'go.mod'
@@ -232,15 +257,17 @@ jobs:
232257
benchmark-data-dir-path: ""
233258
# Set auto-push to false since GitHub API token is not given
234259
auto-push: false
235-
alert-threshold: '125%'
260+
alert-threshold: '150%'
236261
gh-pages-branch: "benchmark-results"
237262
fail-on-alert: true
238263
- name: Push benchmark result
239-
if: ${{ success() && github.ref_name == 'v3'}}
264+
if: ${{ success() && github.ref_name == 'main'}}
240265
run: git push 'https://github-actions:${{ secrets.GITHUB_TOKEN }}@github.com/nginx/agent.git' benchmark-results:benchmark-results
241266
load-tests:
242267
name: Load Tests
243268
if: ${{ !github.event.pull_request.head.repo.fork && !startsWith(github.ref_name, 'dependabot/') }}
269+
permissions:
270+
contents: write
244271
runs-on: ubuntu-22.04
245272
needs: build-unsigned-snapshot
246273

@@ -303,10 +330,10 @@ jobs:
303330
benchmark-data-dir-path: ""
304331
# Set auto-push to false since GitHub API token is not given
305332
auto-push: false
306-
alert-threshold: '175%'
333+
alert-threshold: '150%'
307334
gh-pages-branch: "benchmark-results"
308335
fail-on-alert: true
309336

310337
- name: Push load test result
311-
if: ${{ success() && github.ref_name == 'v3'}}
338+
if: ${{ success() && github.ref_name == 'main' }}
312339
run: git push 'https://github-actions:${{ secrets.GITHUB_TOKEN }}@github.com/nginx/agent.git' benchmark-results:benchmark-results

.github/workflows/release-branch.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ on:
4242

4343
env:
4444
NFPM_VERSION: 'v2.35.3'
45-
GOPROXY: "https://${{ secrets.ARTIFACTORY_USER }}:${{ secrets.ARTIFACTORY_TOKEN }}@azr.artifactory.f5net.com/artifactory/api/go/f5-nginx-go-local-approved-dependency"
45+
GOPROXY: "https://${{ secrets.ARTIFACTORY_USER }}:${{ secrets.ARTIFACTORY_TOKEN }}@azr.artifactory.f5net.com/artifactory/api/go/f5-nginx-go-dev"
4646

4747
defaults:
4848
run:
@@ -304,10 +304,10 @@ jobs:
304304
script: |
305305
const { repo, owner } = context.repo;
306306
const result = await github.rest.pulls.create({
307-
title: 'Merge ${{ github.ref_name }} back into main',
307+
title: 'Merge ${{ inputs.releaseBranch }} back into main',
308308
owner,
309309
repo,
310-
head: '${{ github.ref_name }}',
310+
head: '${{ inputs.releaseBranch }}',
311311
base: 'main',
312312
body: [
313313
'This PR is auto-generated by the release workflow.'

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ MANIFEST_DIR ?= /var/lib/nginx-agent
4848
DIRS = $(BUILD_DIR) $(TEST_BUILD_DIR) $(BUILD_DIR)/$(DOCS_DIR) $(BUILD_DIR)/$(DOCS_DIR)/$(PROTO_DIR)
4949
$(shell mkdir -p $(DIRS))
5050

51-
VERSION ?= "v3.0.0"
51+
VERSION ?= $(shell git describe --match "v[0-9]*" --abbrev=0 --tags)
52+
ifeq ($(strip $(VERSION)),)
53+
VERSION := $(shell curl https://api.github.com/repos/nginx/agent/releases/latest -s | jq .name -r)
54+
endif
5255
COMMIT = $(shell git rev-parse --short HEAD)
5356
DATE = $(shell date +%F_%H-%M-%S)
5457
LDFLAGS = "-s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)"
@@ -104,7 +107,6 @@ include Makefile.containers
104107
include Makefile.packaging
105108

106109
.PHONY: help clean no-local-changes build lint format unit-test integration-test run dev run-mock-management-grpc-server generate generate-mocks local-apk-package local-deb-package local-rpm-package
107-
108110
help: ## Show help message
109111
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-24s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
110112

Makefile.packaging

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ AZURE_PACKAGES_DIR := ./build/azure/packages
99
BINARY_PATH := $(BUILD_DIR)/$(BINARY_NAME)
1010
GPG_PUBLIC_KEY := .key
1111
PACKAGE_BUILD ?= 1
12-
PACKAGE_VERSION := $(shell git describe --match "v[0-9]*" --abbrev=0 --tags)
12+
PACKAGE_VERSION ?= $(shell echo ${VERSION} | tr -d 'v')
1313
TARBALL_NAME := $(PACKAGE_PREFIX).tar.gz
1414

1515
DEB_DISTROS ?= ubuntu-plucky-25.04 ubuntu-noble-24.04 ubuntu-jammy-22.04 ubuntu-focal-20.04 debian-bookworm-12 debian-bullseye-11
@@ -35,7 +35,7 @@ $(PACKAGES_DIR):
3535
@mkdir -p $(PACKAGES_DIR)/deb && mkdir -p $(PACKAGES_DIR)/rpm && mkdir -p $(PACKAGES_DIR)/apk
3636

3737
.PHONY: package
38-
package: $(PACKAGES_DIR) #### Create final packages for all supported distros
38+
package: gpg-key $(PACKAGES_DIR) #### Create final packages for all supported distros
3939
# Create deb packages
4040
@for arch in $(DEB_ARCHS); do \
4141
GOWORK=off CGO_ENABLED=0 GOARCH=$${arch} GOOS=linux go build -pgo=auto -ldflags=${LDFLAGS} -o $(BINARY_PATH) $(PROJECT_DIR)/$(PROJECT_FILE); \
@@ -111,7 +111,7 @@ package: $(PACKAGES_DIR) #### Create final packages for all supported distros
111111
find $(PACKAGES_DIR)/apk ;\
112112

113113
# Create tarball containing all packages
114-
cd $(PACKAGES_DIR) && tar -czvf "./$(TARBALL_NAME)" * && cd ../.. && cp "${PACKAGES_DIR}/$(TARBALL_NAME)"; \
114+
cd $(PACKAGES_DIR) && tar -czvf "./$(TARBALL_NAME)" * && cd ../..; \
115115

116116
.PHONY: gpg-key
117117
gpg-key: ## Generate GPG public key

internal/collector/otel_collector_plugin.go

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"log/slog"
12+
"net"
1213
"os"
1314
"strings"
1415
"sync"
@@ -46,11 +47,12 @@ const (
4647
type (
4748
// Collector The OTel collector plugin start an embedded OTel collector for metrics collection in the OTel format.
4849
Collector struct {
49-
service types.CollectorInterface
50-
cancel context.CancelFunc
51-
config *config.Config
52-
mu *sync.Mutex
53-
stopped bool
50+
service types.CollectorInterface
51+
config *config.Config
52+
mu *sync.Mutex
53+
cancel context.CancelFunc
54+
previousNAPSysLogServer string
55+
stopped bool
5456
}
5557
)
5658

@@ -86,10 +88,11 @@ func NewCollector(conf *config.Config) (*Collector, error) {
8688
}
8789

8890
return &Collector{
89-
config: conf,
90-
service: oTelCollector,
91-
stopped: true,
92-
mu: &sync.Mutex{},
91+
config: conf,
92+
service: oTelCollector,
93+
stopped: true,
94+
mu: &sync.Mutex{},
95+
previousNAPSysLogServer: "",
9396
}, nil
9497
}
9598

@@ -550,10 +553,12 @@ func (oc *Collector) updateNginxAppProtectTcplogReceivers(nginxConfigContext *mo
550553
oc.config.Collector.Receivers.TcplogReceivers = make(map[string]*config.TcplogReceiver)
551554
}
552555

553-
if nginxConfigContext.NAPSysLogServer != "" {
554-
if !oc.doesTcplogReceiverAlreadyExist(nginxConfigContext.NAPSysLogServer) {
556+
napSysLogServer := oc.findAvailableSyslogServers(nginxConfigContext.NAPSysLogServers)
557+
558+
if napSysLogServer != "" {
559+
if !oc.doesTcplogReceiverAlreadyExist(napSysLogServer) {
555560
oc.config.Collector.Receivers.TcplogReceivers["nginx_app_protect"] = &config.TcplogReceiver{
556-
ListenAddress: nginxConfigContext.NAPSysLogServer,
561+
ListenAddress: napSysLogServer,
557562
Operators: []config.Operator{
558563
// regex captures the priority number from the log line
559564
{
@@ -606,13 +611,13 @@ func (oc *Collector) updateNginxAppProtectTcplogReceivers(nginxConfigContext *mo
606611
}
607612
}
608613

609-
tcplogReceiverDeleted := oc.areNapReceiversDeleted(nginxConfigContext)
614+
tcplogReceiverDeleted := oc.areNapReceiversDeleted(napSysLogServer)
610615

611616
return newTcplogReceiverAdded || tcplogReceiverDeleted
612617
}
613618

614-
func (oc *Collector) areNapReceiversDeleted(nginxConfigContext *model.NginxConfigContext) bool {
615-
listenAddressesToBeDeleted := oc.configDeletedNapReceivers(nginxConfigContext)
619+
func (oc *Collector) areNapReceiversDeleted(napSysLogServer string) bool {
620+
listenAddressesToBeDeleted := oc.configDeletedNapReceivers(napSysLogServer)
616621
if len(listenAddressesToBeDeleted) != 0 {
617622
delete(oc.config.Collector.Receivers.TcplogReceivers, "nginx_app_protect")
618623
return true
@@ -621,17 +626,17 @@ func (oc *Collector) areNapReceiversDeleted(nginxConfigContext *model.NginxConfi
621626
return false
622627
}
623628

624-
func (oc *Collector) configDeletedNapReceivers(nginxConfigContext *model.NginxConfigContext) map[string]bool {
629+
func (oc *Collector) configDeletedNapReceivers(napSysLogServer string) map[string]bool {
625630
elements := make(map[string]bool)
626631

627632
for _, tcplogReceiver := range oc.config.Collector.Receivers.TcplogReceivers {
628633
elements[tcplogReceiver.ListenAddress] = true
629634
}
630635

631-
if nginxConfigContext.NAPSysLogServer != "" {
636+
if napSysLogServer != "" {
632637
addressesToDelete := make(map[string]bool)
633-
if !elements[nginxConfigContext.NAPSysLogServer] {
634-
addressesToDelete[nginxConfigContext.NAPSysLogServer] = true
638+
if !elements[napSysLogServer] {
639+
addressesToDelete[napSysLogServer] = true
635640
}
636641

637642
return addressesToDelete
@@ -675,6 +680,39 @@ func (oc *Collector) updateResourceAttributes(
675680
return actionUpdated
676681
}
677682

683+
func (oc *Collector) findAvailableSyslogServers(napSyslogServers []string) string {
684+
napSyslogServersMap := make(map[string]bool)
685+
for _, server := range napSyslogServers {
686+
napSyslogServersMap[server] = true
687+
}
688+
689+
if oc.previousNAPSysLogServer != "" {
690+
if _, ok := napSyslogServersMap[oc.previousNAPSysLogServer]; ok {
691+
return oc.previousNAPSysLogServer
692+
}
693+
}
694+
695+
for _, napSyslogServer := range napSyslogServers {
696+
ln, err := net.Listen("tcp", napSyslogServer)
697+
if err != nil {
698+
slog.Debug("NAP syslog server is not reachable", "address", napSyslogServer,
699+
"error", err)
700+
701+
continue
702+
}
703+
closeError := ln.Close()
704+
if closeError != nil {
705+
slog.Debug("Failed to close syslog server", "address", napSyslogServer, "error", closeError)
706+
}
707+
708+
slog.Debug("Found valid NAP syslog server", "address", napSyslogServer)
709+
710+
return napSyslogServer
711+
}
712+
713+
return ""
714+
}
715+
678716
func isOSSReceiverChanged(nginxReceiver config.NginxReceiver, nginxConfigContext *model.NginxConfigContext) bool {
679717
return nginxReceiver.StubStatus.URL != nginxConfigContext.StubStatus.URL ||
680718
len(nginxReceiver.AccessLogs) != len(nginxConfigContext.AccessLogs)

0 commit comments

Comments
 (0)