Add preheat APIs, handlers.

1. Manual preheat.
2. Instance health check.

Signed-off-by: fanjiankong <fanjiankong@tencent.com>
This commit is contained in:
fanjiankong 2020-07-08 23:58:20 +08:00
parent 9483559d18
commit 080afbfe1b
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

@ -10,6 +10,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"
@ -22,6 +23,7 @@ func newPreheatAPI() *preheatAPI {
return &preheatAPI{
preheatCtl: preheatCtl.Ctl,
projectCtl: projectCtl.Ctl,
enforcer: preheatCtl.Enf,
}
}
@ -31,6 +33,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 {
@ -248,6 +251,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 {