From 089e19c6eb1df03a86e63029923419318c8151d8 Mon Sep 17 00:00:00 2001 From: Bapung Date: Sun, 11 Jan 2026 17:31:28 +0800 Subject: [PATCH] add filters --- config/manager/image_pull_secret_patch.yaml | 10 +++ config/manager/kustomization.yaml | 9 ++ config/rbac/role_binding.yaml | 6 +- .../samples/gitea_v1alpha1_runnergroup.yaml | 39 +++++++- internal/gitea/client.go | 90 ++++++++++--------- 5 files changed, 107 insertions(+), 47 deletions(-) create mode 100644 config/manager/image_pull_secret_patch.yaml diff --git a/config/manager/image_pull_secret_patch.yaml b/config/manager/image_pull_secret_patch.yaml new file mode 100644 index 0000000..ed24a29 --- /dev/null +++ b/config/manager/image_pull_secret_patch.yaml @@ -0,0 +1,10 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: controller-manager + namespace: system +spec: + template: + spec: + imagePullSecrets: + - name: ghcr-secret diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index 5c5f0b8..f986a51 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -1,2 +1,11 @@ resources: - manager.yaml +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +images: +- name: controller + newName: ghcr.io/bapung/gitea-runner-operator + newTag: sha-b638d72 + +patchesStrategicMerge: +- image_pull_secret_patch.yaml diff --git a/config/rbac/role_binding.yaml b/config/rbac/role_binding.yaml index 33cbca7..976527b 100644 --- a/config/rbac/role_binding.yaml +++ b/config/rbac/role_binding.yaml @@ -10,6 +10,6 @@ roleRef: kind: ClusterRole name: manager-role subjects: -- kind: ServiceAccount - name: controller-manager - namespace: system + - kind: ServiceAccount + name: controller-manager + namespace: gitea-runner-operator-system diff --git a/config/samples/gitea_v1alpha1_runnergroup.yaml b/config/samples/gitea_v1alpha1_runnergroup.yaml index 6085a90..2ae0feb 100644 --- a/config/samples/gitea_v1alpha1_runnergroup.yaml +++ b/config/samples/gitea_v1alpha1_runnergroup.yaml @@ -1,3 +1,16 @@ +apiVersion: v1 +kind: Secret +metadata: + name: gitea-credentials + labels: + app.kubernetes.io/name: gitea-runner-operator + app.kubernetes.io/managed-by: kustomize +stringData: + # The Gitea API Token (for the Operator to poll for jobs) + auth-token: "3430680995113a33a17715bb552882d504f5cf98" + # The Runner Registration Token (for the Runner to register itself) + registration-token: "5r4lpLA9rKCZZEHyUyKHeA187DoaElcTBySITRRi" +--- apiVersion: gitea.bpg.pw/v1alpha1 kind: RunnerGroup metadata: @@ -6,4 +19,28 @@ metadata: app.kubernetes.io/managed-by: kustomize name: runnergroup-sample spec: - # TODO(user): Add fields here + # The base URL of your Gitea instance + giteaURL: "https://gitea.bpg.pw" + + # Scope of the runners (global, org, or repo) + scope: "repo" + org: "bapung" # Required if scope is 'org' or 'repo' + repo: "dummy-service-workflow" # Required if scope is 'repo' + + # Labels to identify this runner group + labels: + - "linux" + - "amd64" + + # Maximum number of runners to spawn concurrently + maxActiveRunners: 5 + + # Reference to the Secret containing the API token + authToken: + name: gitea-credentials + key: auth-token + + # Reference to the Secret containing the Registration token + registrationToken: + name: gitea-credentials + key: registration-token diff --git a/internal/gitea/client.go b/internal/gitea/client.go index 882b51e..b2516c6 100644 --- a/internal/gitea/client.go +++ b/internal/gitea/client.go @@ -153,56 +153,60 @@ func (c *HTTPClient) getQueuedRunsGlobal(ctx context.Context, giteaURL, authToke // fetchWorkflowJobs fetches workflow jobs from a given endpoint with label filtering and pagination func (c *HTTPClient) fetchWorkflowJobs(ctx context.Context, endpoint, authToken string, labels []string) (int, error) { totalCount := 0 - page := 1 - limit := 50 // Default page size + statuses := []string{"queued", "waiting", "pending"} - for { - u, err := url.Parse(endpoint) - if err != nil { - return 0, err - } - q := u.Query() - q.Set("status", "queued") - q.Set("page", fmt.Sprintf("%d", page)) - q.Set("limit", fmt.Sprintf("%d", limit)) - u.RawQuery = q.Encode() + for _, status := range statuses { + page := 1 + limit := 50 // Default page size - req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil) - if err != nil { - return 0, err - } + for { + u, err := url.Parse(endpoint) + if err != nil { + return 0, err + } + q := u.Query() + q.Set("status", status) + q.Set("page", fmt.Sprintf("%d", page)) + q.Set("limit", fmt.Sprintf("%d", limit)) + u.RawQuery = q.Encode() - req.Header.Set("Authorization", "token "+authToken) - req.Header.Set("Accept", "application/json") + req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil) + if err != nil { + return 0, err + } - resp, err := c.httpClient.Do(req) - if err != nil { - return 0, err - } + req.Header.Set("Authorization", "token "+authToken) + req.Header.Set("Accept", "application/json") - if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) + resp, err := c.httpClient.Do(req) + if err != nil { + return 0, err + } + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + resp.Body.Close() + return 0, c.handleHTTPError(resp.StatusCode, body, "fetch workflow jobs") + } + + var result ActionWorkflowJobsResponse + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + resp.Body.Close() + return 0, err + } resp.Body.Close() - return 0, c.handleHTTPError(resp.StatusCode, body, "fetch workflow jobs") + + // Filter and count matching jobs for this page + pageCount := c.filterQueuedJobs(result.Jobs, labels) + totalCount += pageCount + + // Break if we've fetched all available results + if len(result.Jobs) < limit { + break + } + + page++ } - - var result ActionWorkflowJobsResponse - if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { - resp.Body.Close() - return 0, err - } - resp.Body.Close() - - // Filter and count matching jobs for this page - pageCount := c.filterQueuedJobs(result.Jobs, labels) - totalCount += pageCount - - // Break if we've fetched all available results - if len(result.Jobs) < limit { - break - } - - page++ } return totalCount, nil