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

Release radix-api use resources #684

Merged
merged 3 commits into from
Oct 2, 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
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"REQUIRE_APP_CONFIGURATION_ITEM": "true",
"REQUIRE_APP_AD_GROUPS": "true",
"RADIX_ENVIRONMENT":"qa",
"PROMETHEUS_URL":"http://localhost:9091",
"RADIX_APP":"radix-api",
"LOG_LEVEL":"info",
"LOG_PRETTY":"true"
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ build: $(BINS)
mocks: bootstrap
mockgen -source ./api/buildstatus/models/buildstatus.go -destination ./api/test/mock/buildstatus_mock.go -package mock
mockgen -source ./api/deployments/deployment_handler.go -destination ./api/deployments/mock/deployment_handler_mock.go -package mock
mockgen -source ./api/metrics/prometheus_handler.go -destination ./api/metrics/mock/prometheus_handler_mock.go -package mock
mockgen -source ./api/metrics/prometheus_client.go -destination ./api/metrics/mock/prometheus_client_mock.go -package mock
mockgen -source ./api/environments/job_handler.go -destination ./api/environments/mock/job_handler_mock.go -package mock
mockgen -source ./api/environments/environment_handler.go -destination ./api/environments/mock/environment_handler_mock.go -package mock
mockgen -source ./api/utils/tlsvalidation/interface.go -destination ./api/utils/tlsvalidation/mock/tls_secret_validator_mock.go -package mock
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ The following env vars are needed. Useful default values in brackets.
- `RADIX_CONTAINER_REGISTRY` - (`radixdev.azurecr.io`)
- `PIPELINE_IMG_TAG` - (`master-latest`)
- `TEKTON_IMG_TAG` - (`release-latest`)
- `PROMETHEUS_URL` - `http://localhost:9091` use this to get Prometheus metrics running the following command (the local port 9090 is used by the API server `/metrics` endpoint, in-cluster URL is http://prometheus-operator-prometheus.monitor.svc.cluster.local:9090):
```
kubectl -n monitor port-forward svc/prometheus-operator-prometheus 9091:9090
```

You also probably want to start with the argument `--useOutClusterClient=false`. When `useOutClusterClient` is `false`, several debugging settings are enabled:
* a service principal with superpowers is used to authorize the requests, and the client's `Authorization` bearer token is ignored.
* the Radix API will connect to the currently-configured `kubectl` context and ignore `K8S_API_HOST`.
* the server CORS settings are modified to accept the `X-Requested-With` header in incoming requests. This is necessary to allow direct requests from web browser while e.g. debugging [radix-web-console](https://github.com/equinor/radix-web-console).
* verbose debugging output from CORS rule evaluation is logged to console.


If you are using VSCode, there is a convenient launch configuration in `.vscode`.

#### Validate code
Expand Down
73 changes: 72 additions & 1 deletion api/applications/applications_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

applicationModels "github.com/equinor/radix-api/api/applications/models"
"github.com/equinor/radix-api/api/metrics"
"github.com/equinor/radix-api/models"
"github.com/gorilla/mux"
)
Expand All @@ -19,17 +20,19 @@ type applicationController struct {
*models.DefaultController
hasAccessToRR
applicationHandlerFactory ApplicationHandlerFactory
prometheusHandler metrics.PrometheusHandler
}

// NewApplicationController Constructor
func NewApplicationController(hasAccessTo hasAccessToRR, applicationHandlerFactory ApplicationHandlerFactory) models.Controller {
func NewApplicationController(hasAccessTo hasAccessToRR, applicationHandlerFactory ApplicationHandlerFactory, prometheusHandler metrics.PrometheusHandler) models.Controller {
if hasAccessTo == nil {
hasAccessTo = hasAccess
}

return &applicationController{
hasAccessToRR: hasAccessTo,
applicationHandlerFactory: applicationHandlerFactory,
prometheusHandler: prometheusHandler,
}
}

Expand Down Expand Up @@ -133,6 +136,11 @@ func (ac *applicationController) GetRoutes() models.Routes {
Method: "POST",
HandlerFunc: ac.RegenerateDeployKeyHandler,
},
models.Route{
Path: appPath + "/resources",
Method: "GET",
HandlerFunc: ac.GetUsedResources,
},
}

return routes
Expand Down Expand Up @@ -992,3 +1000,66 @@ func (ac *applicationController) TriggerPipelinePromote(accounts models.Accounts

ac.JSONResponse(w, r, &jobSummary)
}

// GetUsedResources Gets used resources for the application
func (ac *applicationController) GetUsedResources(accounts models.Accounts, w http.ResponseWriter, r *http.Request) {
// swagger:operation GET /applications/{appName}/resources application getResources
// ---
// summary: Gets used resources for the application
// parameters:
// - name: appName
// in: path
// description: Name of the application
// type: string
// required: true
// - name: environment
// in: query
// description: Name of the application environment
// type: string
// required: false
// - name: component
// in: query
// description: Name of the application component in an environment
// type: string
// required: false
// - name: duration
// in: query
// description: Duration of the period, default is 30d (30 days). Example 10m, 1h, 2d, 3w, where m-minutes, h-hours, d-days, w-weeks
// type: string
// required: false
// - name: since
// in: query
// description: End time-point of the period in the past, default is now. Example 10m, 1h, 2d, 3w, where m-minutes, h-hours, d-days, w-weeks
// type: string
// required: false
// - name: Impersonate-User
// in: header
// description: Works only with custom setup of cluster. Allow impersonation of test users (Required if Impersonate-Group is set)
// type: string
// required: false
// - name: Impersonate-Group
// in: header
// description: Works only with custom setup of cluster. Allow impersonation of a comma-seperated list of test groups (Required if Impersonate-User is set)
// type: string
// required: false
// responses:
// "200":
// description: Successful trigger pipeline
// schema:
// "$ref": "#/definitions/UsedResources"
// "404":
// description: "Not found"
appName := mux.Vars(r)["appName"]
envName := r.FormValue("environment")
componentName := r.FormValue("component")
duration := r.FormValue("duration")
since := r.FormValue("since")

usedResources, err := ac.prometheusHandler.GetUsedResources(r.Context(), accounts.UserAccount.RadixClient, appName, envName, componentName, duration, since)
if err != nil {
ac.ErrorResponse(w, r, err)
return
}

ac.JSONResponse(w, r, &usedResources)
}
Loading
Loading