From f3c417efa5c456959238334fcf517c3c1ebb78f6 Mon Sep 17 00:00:00 2001 From: Paul Cioanca Date: Wed, 18 Jun 2025 23:09:37 +0300 Subject: [PATCH 1/3] feat(ec2_sd_configs): introduce optional pagination for AWS API calls --- lib/promscrape/discovery/ec2/api.go | 18 ++++++++++-------- lib/promscrape/discovery/ec2/ec2.go | 7 ++++--- lib/promscrape/discovery/ec2/instance.go | 9 +++++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/promscrape/discovery/ec2/api.go b/lib/promscrape/discovery/ec2/api.go index abae3b152ea87..f2c59645c80fa 100644 --- a/lib/promscrape/discovery/ec2/api.go +++ b/lib/promscrape/discovery/ec2/api.go @@ -8,10 +8,11 @@ import ( ) type apiConfig struct { - awsConfig *awsapi.Config - instanceFilters []awsapi.Filter - azFilters []awsapi.Filter - port int + awsConfig *awsapi.Config + instanceFilters []awsapi.Filter + azFilters []awsapi.Filter + port int + maxResultsPerPage int // A map from AZ name to AZ id. azMap map[string]string @@ -42,10 +43,11 @@ func newAPIConfig(sdc *SDConfig) (*apiConfig, error) { return nil, err } cfg := &apiConfig{ - awsConfig: awsCfg, - instanceFilters: sdc.InstanceFilters, - azFilters: sdc.AZFilters, - port: port, + awsConfig: awsCfg, + instanceFilters: sdc.InstanceFilters, + azFilters: sdc.AZFilters, + port: port, + maxResultsPerPage: sdc.MaxResultsPerPage, } return cfg, nil } diff --git a/lib/promscrape/discovery/ec2/ec2.go b/lib/promscrape/discovery/ec2/ec2.go index 8ba36bf0b59d8..1606bd5275210 100644 --- a/lib/promscrape/discovery/ec2/ec2.go +++ b/lib/promscrape/discovery/ec2/ec2.go @@ -29,9 +29,10 @@ type SDConfig struct { RoleARN string `yaml:"role_arn,omitempty"` // RefreshInterval time.Duration `yaml:"refresh_interval"` // refresh_interval is obtained from `-promscrape.ec2SDCheckInterval` command-line option. - Port *int `yaml:"port,omitempty"` - InstanceFilters []awsapi.Filter `yaml:"filters,omitempty"` - AZFilters []awsapi.Filter `yaml:"az_filters,omitempty"` + Port *int `yaml:"port,omitempty"` + InstanceFilters []awsapi.Filter `yaml:"filters,omitempty"` + AZFilters []awsapi.Filter `yaml:"az_filters,omitempty"` + MaxResultsPerPage int `yaml:"max_results_per_page,omitempty"` } // GetLabels returns ec2 labels according to sdc. diff --git a/lib/promscrape/discovery/ec2/instance.go b/lib/promscrape/discovery/ec2/instance.go index acd8674cc99eb..0c9b29c6c2b53 100644 --- a/lib/promscrape/discovery/ec2/instance.go +++ b/lib/promscrape/discovery/ec2/instance.go @@ -32,6 +32,15 @@ func getReservations(cfg *apiConfig) ([]Reservation, error) { var rs []Reservation pageToken := "" instanceFilters := awsapi.GetFiltersQueryString(cfg.instanceFilters) + + if cfg.maxResultsPerPage > 0 { + if len(instanceFilters) == 0 { + instanceFilters = fmt.Sprintf("MaxResults=%d", cfg.maxResultsPerPage) + } else { + instanceFilters += fmt.Sprintf("&MaxResults=%d", cfg.maxResultsPerPage) + } + } + for { data, err := cfg.awsConfig.GetEC2APIResponse("DescribeInstances", instanceFilters, pageToken) if err != nil { From c1aa88936780ed23a074a425d8f5446c4c52fa80 Mon Sep 17 00:00:00 2001 From: Paul Cioanca Date: Wed, 18 Jun 2025 23:26:06 +0300 Subject: [PATCH 2/3] chore: build via GH workflow --- .github/workflows/build-docker-image.yml | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .github/workflows/build-docker-image.yml diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml new file mode 100644 index 0000000000000..1246a301a8770 --- /dev/null +++ b/.github/workflows/build-docker-image.yml @@ -0,0 +1,37 @@ +name: Build and Copy + +on: + workflow_dispatch: + +jobs: + build-and-copy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build Docker image + run: | + PKG_TAG=1.119.0-ec2-pagination make package-vmagent-amd64 + + - name: Get image ID + id: image_id + run: | + IMAGE_ID=$(docker images --format "{{.ID}}" | head -n 1) + echo "image_id=$IMAGE_ID" >> $GITHUB_OUTPUT + + - name: Create container + run: docker create --name temp_vmagent ${{ steps.image_id.outputs.image_id }} + + - name: Copy /vmagent-prod from container + run: docker cp temp_vmagent:/vmagent-prod ./vmagent-prod + + - name: Remove container + run: docker rm temp_vmagent + + - name: List copied file + run: ls -l ./vmagent-prod \ No newline at end of file From d7ad22f9e1c7d23f391cfc95a0bf46e8156960ea Mon Sep 17 00:00:00 2001 From: Paul Cioanca Date: Wed, 18 Jun 2025 23:27:57 +0300 Subject: [PATCH 3/3] chore: more triggers --- .github/workflows/build-docker-image.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml index 1246a301a8770..73b8383c99474 100644 --- a/.github/workflows/build-docker-image.yml +++ b/.github/workflows/build-docker-image.yml @@ -2,6 +2,14 @@ name: Build and Copy on: workflow_dispatch: + pull_request: + branches: + - cluster + - master + paths: + - '**.go' + - '**/Dockerfile*' + - '**/Makefile' jobs: build-and-copy: