Merge pull request #6039 from stonezdj/refact_5996

Refactor capacity
This commit is contained in:
Steven Zou 2018-10-24 10:50:11 +08:00 committed by GitHub
commit 3b76a960e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 11 additions and 158 deletions

View File

@ -100,6 +100,7 @@ services:
- /data/secretkey:/etc/core/key:z
- /data/ca_download/:/etc/core/ca/:z
- /data/psc/:/etc/core/token/:z
- /data/:/data/:z
networks:
- harbor
dns_search: .

View File

@ -1,37 +0,0 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"net/http"
"github.com/goharbor/harbor/src/adminserver/systeminfo/imagestorage"
"github.com/goharbor/harbor/src/common/utils/log"
)
// Capacity handles /api/systeminfo/capacity and returns system capacity
func Capacity(w http.ResponseWriter, r *http.Request) {
capacity, err := imagestorage.GlobalDriver.Cap()
if err != nil {
log.Errorf("failed to get capacity: %v", err)
handleInternalServerError(w)
return
}
if err = writeJSON(w, capacity); err != nil {
log.Errorf("failed to write response: %v", err)
return
}
}

View File

@ -1,73 +0,0 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/goharbor/harbor/src/adminserver/systeminfo/imagestorage"
"github.com/stretchr/testify/assert"
)
type fakeImageStorageDriver struct {
capacity *imagestorage.Capacity
err error
}
func (f *fakeImageStorageDriver) Name() string {
return "fake"
}
func (f *fakeImageStorageDriver) Cap() (*imagestorage.Capacity, error) {
return f.capacity, f.err
}
func TestCapacity(t *testing.T) {
cases := []struct {
driver imagestorage.Driver
responseCode int
capacity *imagestorage.Capacity
}{
{&fakeImageStorageDriver{nil, errors.New("error")}, http.StatusInternalServerError, nil},
{&fakeImageStorageDriver{&imagestorage.Capacity{100, 90}, nil}, http.StatusOK, &imagestorage.Capacity{100, 90}},
}
req, err := http.NewRequest("", "", nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
for _, c := range cases {
imagestorage.GlobalDriver = c.driver
w := httptest.NewRecorder()
Capacity(w, req)
assert.Equal(t, c.responseCode, w.Code, "unexpected response code")
if c.responseCode == http.StatusOK {
b, err := ioutil.ReadAll(w.Body)
if err != nil {
t.Fatalf("failed to read from response body: %v", err)
}
capacity := &imagestorage.Capacity{}
if err = json.Unmarshal(b, capacity); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
assert.Equal(t, c.capacity, capacity)
}
}
}

View File

@ -17,10 +17,10 @@ package client
import (
"strings"
"github.com/goharbor/harbor/src/adminserver/systeminfo/imagestorage"
"github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/common/http/modifier/auth"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/core/systeminfo/imagestorage"
)
// Client defines methods that an Adminserver client should implement

View File

@ -69,13 +69,3 @@ func TestResetCfgs(t *testing.T) {
return
}
}
func TestCapacity(t *testing.T) {
capacity, err := c.Capacity()
if !assert.Nil(t, err, "unexpected error") {
return
}
assert.Equal(t, uint64(100), capacity.Total)
assert.Equal(t, uint64(90), capacity.Free)
}

View File

@ -26,7 +26,6 @@ func newRouter() http.Handler {
r.HandleFunc("/api/configurations", api.UpdateCfgs).Methods("PUT")
r.HandleFunc("/api/configs", api.ListCfgs).Methods("GET")
r.HandleFunc("/api/configurations/reset", api.ResetCfgs).Methods("POST")
r.HandleFunc("/api/systeminfo/capacity", api.Capacity).Methods("GET")
r.HandleFunc("/api/ping", api.Ping).Methods("GET")
return r
}

View File

@ -20,7 +20,6 @@ import (
"github.com/goharbor/harbor/src/adminserver/handlers"
syscfg "github.com/goharbor/harbor/src/adminserver/systemcfg"
sysinfo "github.com/goharbor/harbor/src/adminserver/systeminfo"
"github.com/goharbor/harbor/src/common/utils/log"
)
@ -47,8 +46,6 @@ func main() {
}
log.Info("system initialization completed")
sysinfo.Init()
port := os.Getenv("PORT")
if len(port) == 0 {
port = "80"

View File

@ -19,7 +19,6 @@ import (
"net/http"
"net/http/httptest"
"github.com/goharbor/harbor/src/adminserver/systeminfo/imagestorage"
"github.com/goharbor/harbor/src/common"
)
@ -122,36 +121,9 @@ func NewAdminserver(config map[string]interface{}) (*httptest.Server, error) {
}),
})
capacityHandler, err := NewCapacityHandle()
if err != nil {
return nil, err
}
m = append(m, &RequestHandlerMapping{
Method: "GET",
Pattern: "/api/systeminfo/capacity",
Handler: capacityHandler,
})
return NewServer(m...), nil
}
// NewCapacityHandle ...
func NewCapacityHandle() (func(http.ResponseWriter, *http.Request), error) {
capacity := imagestorage.Capacity{
Total: 100,
Free: 90,
}
b, err := json.Marshal(capacity)
if err != nil {
return nil, err
}
resp := &Response{
StatusCode: http.StatusOK,
Body: b,
}
return Handler(resp), nil
}
// GetDefaultConfigMap returns the defailt config map for easier modification.
func GetDefaultConfigMap() map[string]interface{} {
return adminServerDefaultConfig

View File

@ -29,6 +29,8 @@ import (
"github.com/goharbor/harbor/src/common/utils/clair"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/core/systeminfo"
"github.com/goharbor/harbor/src/core/systeminfo/imagestorage"
)
// SystemInfoAPI handle requests for getting system info /api/systeminfo
@ -120,7 +122,8 @@ func (sia *SystemInfoAPI) validate() {
func (sia *SystemInfoAPI) GetVolumeInfo() {
sia.validate()
capacity, err := config.AdminserverClient.Capacity()
systeminfo.Init()
capacity, err := imagestorage.GlobalDriver.Cap()
if err != nil {
log.Errorf("failed to get capacity: %v", err)
sia.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))

View File

@ -112,6 +112,7 @@ func initRouters() {
beego.Router("/api/internal/syncregistry", &api.InternalAPI{}, "post:SyncRegistry")
beego.Router("/api/internal/renameadmin", &api.InternalAPI{}, "post:RenameAdmin")
beego.Router("/api/internal/configurations", &api.ConfigAPI{}, "get:GetInternalConfig")
// external service that hosted on harbor process:
beego.Router("/service/notifications", &registry.NotificationHandler{})

View File

@ -19,8 +19,8 @@ import (
"reflect"
"syscall"
storage "github.com/goharbor/harbor/src/adminserver/systeminfo/imagestorage"
"github.com/goharbor/harbor/src/common/utils/log"
storage "github.com/goharbor/harbor/src/core/systeminfo/imagestorage"
)
const (

View File

@ -17,7 +17,7 @@ package filesystem
import (
"testing"
storage "github.com/goharbor/harbor/src/adminserver/systeminfo/imagestorage"
storage "github.com/goharbor/harbor/src/core/systeminfo/imagestorage"
"github.com/stretchr/testify/assert"
)

View File

@ -17,8 +17,8 @@ package systeminfo
import (
"os"
"github.com/goharbor/harbor/src/adminserver/systeminfo/imagestorage"
"github.com/goharbor/harbor/src/adminserver/systeminfo/imagestorage/filesystem"
"github.com/goharbor/harbor/src/core/systeminfo/imagestorage"
"github.com/goharbor/harbor/src/core/systeminfo/imagestorage/filesystem"
)
// Init image storage driver