Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use non-public port for metrics endpoint #627

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.55.2
version: v1.58.2

test:
name: Unit Test
Expand All @@ -33,23 +33,35 @@ jobs:
- name: Run Tests
run: go test -cover `go list ./...`

test-swagger:
name: Test Swagger
verify-code-generation:
name: Verify Code Generation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: Verify Code Generation
run: |
make verify-generate

report-swagger-changes:
name: Report Changes In Swagger Spec
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: git fetch --no-tags --no-recurse-submodules --depth=1 origin master:master
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: Install dependencies
run: go mod download
# - name: Install dependencies
# run: go mod download
- name: Install Swagger
run: go install github.com/go-swagger/go-swagger/cmd/swagger@v0.30.5
- name: Generate Swagger
run: swagger generate spec -o ./swagger.json --scan-models --exclude-deps
- name: Validate no changes
run: diff swagger.json ./swaggerui/html/swagger.json
run: go install github.com/go-swagger/go-swagger/cmd/swagger@v0.31.0
# - name: Generate Swagger
# run: swagger generate spec -o ./swagger.json --scan-models --exclude-deps
# - name: Validate no changes
# run: diff swagger.json ./swaggerui/html/swagger.json
- name: Check breaking changes
if: always()
id: breaking
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21-alpine3.19 as builder
FROM golang:1.22-alpine3.19 as builder
ENV GO111MODULE=on

RUN apk update && \
Expand Down
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,23 @@ docker-push: $(addsuffix -push,$(IMAGES))
.PHONY: deploy
deploy: $(addsuffix -image,$(IMAGES)) $(addsuffix -push,$(IMAGES))

.PHONY: generate
generate: mocks swagger

.PHONY: verify-generate
verify-generate: generate
git diff --exit-code

HAS_SWAGGER := $(shell command -v swagger;)
HAS_GOLANGCI_LINT := $(shell command -v golangci-lint;)
HAS_MOCKGEN := $(shell command -v mockgen;)

bootstrap:
ifndef HAS_SWAGGER
go install github.com/go-swagger/go-swagger/cmd/swagger@v0.30.5
go install github.com/go-swagger/go-swagger/cmd/swagger@v0.31.0
endif
ifndef HAS_GOLANGCI_LINT
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.58.2
endif
ifndef HAS_MOCKGEN
go install github.com/golang/mock/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions api/jobs/job_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

const (
Expand Down Expand Up @@ -268,7 +268,7 @@ func getLastReadyCondition(conditions []apis.Condition) *apis.Condition {
return nil
}

func sortPipelineTaskStatusConditionsDesc(conditions []apis.Condition) v1.Conditions {
func sortPipelineTaskStatusConditionsDesc(conditions []apis.Condition) duckv1.Conditions {
sort.Slice(conditions, func(i, j int) bool {
if conditions[i].LastTransitionTime.Inner.IsZero() || conditions[j].LastTransitionTime.Inner.IsZero() {
return false
Expand Down
9 changes: 2 additions & 7 deletions api/router/server.go → api/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/equinor/radix-api/models"
"github.com/equinor/radix-api/swaggerui"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/cors"
"github.com/rs/zerolog/log"
"github.com/urfave/negroni/v3"
Expand All @@ -25,8 +24,8 @@ const (
swaggerUIPath = "/swaggerui"
)

// NewServer Constructor function
func NewServer(clusterName string, kubeUtil utils.KubeUtil, controllers ...models.Controller) http.Handler {
// NewAPIHandler Constructor function
func NewAPIHandler(clusterName string, kubeUtil utils.KubeUtil, controllers ...models.Controller) http.Handler {
router := mux.NewRouter().StrictSlash(true)

initializeSwaggerUI(router)
Expand All @@ -47,10 +46,6 @@ func NewServer(clusterName string, kubeUtil utils.KubeUtil, controllers ...model
negroni.Wrap(router),
))

serveMux.Handle("/metrics", negroni.New(
negroni.Wrap(promhttp.Handler()),
))

rec := negroni.NewRecovery()
rec.PrintStack = false

Expand Down
25 changes: 25 additions & 0 deletions api/router/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package router

import (
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni/v3"
)

// NewMetricsHandler Constructor function
func NewMetricsHandler() http.Handler {
serveMux := http.NewServeMux()
serveMux.Handle("GET /metrics", promhttp.Handler())

rec := negroni.NewRecovery()
rec.PrintStack = false
n := negroni.New(
rec,
setZerologLogger(zerologLoggerWithRequestId),
zerologRequestLogger(),
)
n.UseHandler(serveMux)

return n
}
4 changes: 2 additions & 2 deletions api/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (tu *Utils) ExecuteUnAuthorizedRequest(method, endpoint string) <-chan *htt
go func() {
rr := httptest.NewRecorder()
defer close(response)
router.NewServer("anyClusterName", NewKubeUtilMock(tu.kubeClient, tu.radixClient, tu.secretProviderClient, tu.certClient), tu.controllers...).ServeHTTP(rr, req)
router.NewAPIHandler("anyClusterName", NewKubeUtilMock(tu.kubeClient, tu.radixClient, tu.secretProviderClient, tu.certClient), tu.controllers...).ServeHTTP(rr, req)
response <- rr
}()

Expand All @@ -83,7 +83,7 @@ func (tu *Utils) ExecuteRequestWithParameters(method, endpoint string, parameter
go func() {
rr := httptest.NewRecorder()
defer close(response)
router.NewServer("anyClusterName", NewKubeUtilMock(tu.kubeClient, tu.radixClient, tu.secretProviderClient, tu.certClient), tu.controllers...).ServeHTTP(rr, req)
router.NewAPIHandler("anyClusterName", NewKubeUtilMock(tu.kubeClient, tu.radixClient, tu.secretProviderClient, tu.certClient), tu.controllers...).ServeHTTP(rr, req)
response <- rr
}()

Expand Down
73 changes: 37 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
module github.com/equinor/radix-api

go 1.21
go 1.22.0

toolchain go1.22.1

require (
github.com/cert-manager/cert-manager v1.14.2
github.com/cert-manager/cert-manager v1.14.5
github.com/equinor/radix-common v1.9.2
github.com/equinor/radix-job-scheduler v1.9.1
github.com/equinor/radix-operator v1.51.2
github.com/evanphx/json-patch/v5 v5.7.0
github.com/equinor/radix-operator v1.52.1
github.com/evanphx/json-patch/v5 v5.9.0
github.com/felixge/httpsnoop v1.0.4
github.com/go-swagger/go-swagger v0.30.5
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/golang/mock v1.6.0
github.com/gorilla/mux v1.8.1
github.com/marstr/guid v1.1.0
github.com/mitchellh/mapstructure v1.5.0
github.com/prometheus-operator/prometheus-operator/pkg/client v0.70.0
github.com/prometheus/client_golang v1.18.0
github.com/rs/cors v1.10.1
github.com/prometheus-operator/prometheus-operator/pkg/client v0.74.0
github.com/prometheus/client_golang v1.19.1
github.com/rs/cors v1.11.0
github.com/rs/xid v1.5.0
github.com/rs/zerolog v1.32.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
github.com/tektoncd/pipeline v0.55.0
github.com/urfave/negroni/v3 v3.0.0
golang.org/x/sync v0.5.0
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v0.29.0
github.com/urfave/negroni/v3 v3.1.0
golang.org/x/sync v0.7.0
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
knative.dev/pkg v0.0.0-20231219072704-d513e487961e
sigs.k8s.io/secrets-store-csi-driver v1.4.0
)
Expand All @@ -44,24 +45,25 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/elnormous/contenttype v1.0.4 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/cel-go v0.18.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-containerregistry v0.16.1 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand All @@ -73,17 +75,16 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.70.0 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.74.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/statsd_exporter v0.22.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
Expand All @@ -96,30 +97,30 @@ require (
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/api v0.154.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.29.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20240103051144-eec4567ac022 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
sigs.k8s.io/controller-runtime v0.16.3 // indirect
k8s.io/apiextensions-apiserver v0.29.5 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240322212309-b815d8309940 // indirect
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 // indirect
sigs.k8s.io/controller-runtime v0.17.2 // indirect
sigs.k8s.io/gateway-api v1.0.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
Expand Down
Loading
Loading