Merge pull request #12430 from kofj/preheat_and_healthcheck

Preheat and healthcheck
This commit is contained in:
Steven Zou 2020-07-10 15:17:34 +08:00 committed by GitHub
commit 1dfc93c3f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 0 deletions

View File

@ -667,6 +667,33 @@ paths:
$ref: '#/responses/404'
'500':
$ref: '#/responses/500'
/p2p/preheat/instances/ping:
post:
summary: Ping status of a instance.
description: |
This endpoint checks status of a instance, the instance can be given by ID or Endpoint URL (together with credential)
operationId: PingInstances
parameters:
- $ref: '#/parameters/requestId'
- name: instance
in: body
description: The JSON object of instance.
required: true
schema:
$ref: '#/definitions/Instance'
tags:
- preheat
responses:
'200':
$ref: '#/responses/200'
'400':
$ref: '#/responses/400'
'401':
$ref: '#/responses/401'
'404':
description: Instance not found (when instance is provided by ID).
'500':
$ref: '#/responses/500'
/p2p/preheat/instances:
get:
summary: List P2P provider instances
@ -934,6 +961,35 @@ paths:
$ref: '#/responses/409'
'500':
$ref: '#/responses/500'
post:
summary: Manual preheat
description: Manual preheat
tags:
- preheat
operationId: ManualPreheat
parameters:
- $ref: '#/parameters/requestId'
- $ref: '#/parameters/projectName'
- $ref: '#/parameters/preheatPolicyName'
- name: policy
in: body
description: The policy schema info
required: true
schema:
$ref: '#/definitions/PreheatPolicy'
responses:
'201':
$ref: '#/responses/201'
'400':
$ref: '#/responses/400'
'401':
$ref: '#/responses/401'
'404':
$ref: '#/responses/404'
'403':
$ref: '#/responses/403'
'500':
$ref: '#/responses/500'
delete:
summary: Delete a preheat policy
description: Delete a preheat policy

View File

@ -12,6 +12,7 @@ import (
"github.com/go-openapi/strfmt"
preheatCtl "github.com/goharbor/harbor/src/controller/p2p/preheat"
projectCtl "github.com/goharbor/harbor/src/controller/project"
liberrors "github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/models/policy"
instanceModel "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider"
@ -24,6 +25,7 @@ func newPreheatAPI() *preheatAPI {
return &preheatAPI{
preheatCtl: preheatCtl.Ctl,
projectCtl: projectCtl.Ctl,
enforcer: preheatCtl.Enf,
}
}
@ -36,6 +38,7 @@ type preheatAPI struct {
BaseAPI
preheatCtl preheatCtl.Controller
projectCtl projectCtl.Controller
enforcer preheatCtl.Enforcer
}
func (api *preheatAPI) Prepare(ctx context.Context, operation string, params interface{}) middleware.Responder {
@ -253,6 +256,62 @@ func (api *preheatAPI) ListPolicies(ctx context.Context, params operation.ListPo
WithLink(api.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String())
}
// ManualPreheat is manual preheat
func (api *preheatAPI) ManualPreheat(ctx context.Context, params operation.ManualPreheatParams) middleware.Responder {
project, err := api.projectCtl.GetByName(ctx, params.ProjectName)
if err != nil {
return api.SendError(ctx, err)
}
policy, err := api.preheatCtl.GetPolicyByName(ctx, project.ProjectID, params.PreheatPolicyName)
if err != nil {
return api.SendError(ctx, err)
}
_, err = api.enforcer.EnforcePolicy(ctx, policy.ID)
if err != nil {
return api.SendError(ctx, err)
}
// TODO: build execution URL
var location = ""
return operation.NewManualPreheatCreated().WithLocation(location)
}
func (api *preheatAPI) PingInstances(ctx context.Context, params operation.PingInstancesParams) middleware.Responder {
var instance *instanceModel.Instance
var err error
if params.Instance.ID > 0 {
// by ID
instance, err = api.preheatCtl.GetInstance(ctx, params.Instance.ID)
if liberrors.IsNotFoundErr(err) {
return operation.NewPingInstancesNotFound()
}
if err != nil {
api.SendError(ctx, err)
}
} else {
// by endpoint URL
if params.Instance.Endpoint == "" {
return operation.NewPingInstancesBadRequest()
}
instance, err = convertParamInstanceToModelInstance(params.Instance)
if err != nil {
api.SendError(ctx, err)
}
}
err = api.preheatCtl.CheckHealth(ctx, instance)
if err != nil {
api.SendError(ctx, err)
}
return operation.NewPingInstancesOK()
}
// convertPolicyToPayload converts model policy to swagger model
func convertPolicyToPayload(policy *policy.Schema) (*models.PreheatPolicy, error) {
if policy == nil {