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

feat: add api of config migrate, export and import #1893

Merged
merged 15 commits into from
Jun 8, 2021
48 changes: 48 additions & 0 deletions api/internal/core/migrate/conflict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 migrate

import (
"context"

"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/log"
)

func isConflicted(ctx context.Context, new *DataSet) (bool, *DataSet) {
isConflict := false
conflictedData := newDataSet()
store.RangeStore(func(key store.HubKey, s *store.GenericStore) bool {
new.rangeData(key, func(i int, obj interface{}) bool {
// Only check key of store conflict for now.
// TODO: Maybe check name of some entiries.
_, err := s.CreateCheck(obj)
if err != nil {
isConflict = true
err = conflictedData.Add(obj)
if err != nil {
fregie marked this conversation as resolved.
Show resolved Hide resolved
log.Errorf("Add obj to conflict list failed:%s", err)
return true
}
}
return true
})
return true
})
return isConflict, conflictedData
}
127 changes: 127 additions & 0 deletions api/internal/core/migrate/dataset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 migrate

import (
"errors"

"github.com/apisix/manager-api/internal/core/entity"
"github.com/apisix/manager-api/internal/core/store"
)

type DataSet struct {
Counsumers []*entity.Consumer
Routes []*entity.Route
Services []*entity.Service
SSLs []*entity.SSL
Upstreams []*entity.Upstream
Scripts []*entity.Script
GlobalPlugins []*entity.GlobalPlugins
PluginConfigs []*entity.PluginConfig
}

func newDataSet() *DataSet {
return &DataSet{
Counsumers: make([]*entity.Consumer, 0),
Routes: make([]*entity.Route, 0),
Services: make([]*entity.Service, 0),
SSLs: make([]*entity.SSL, 0),
Upstreams: make([]*entity.Upstream, 0),
Scripts: make([]*entity.Script, 0),
GlobalPlugins: make([]*entity.GlobalPlugins, 0),
PluginConfigs: make([]*entity.PluginConfig, 0),
}
}

func (a *DataSet) rangeData(key store.HubKey, f func(int, interface{}) bool) {
switch key {
case store.HubKeyConsumer:
for i, v := range a.Counsumers {
if !f(i, v) {
break
}
}
case store.HubKeyRoute:
for i, v := range a.Routes {
if !f(i, v) {
break
}
}
case store.HubKeyService:
for i, v := range a.Services {
if !f(i, v) {
break
}
}
case store.HubKeySsl:
for i, v := range a.SSLs {
if !f(i, v) {
break
}
}
case store.HubKeyUpstream:
for i, v := range a.Upstreams {
if !f(i, v) {
break
}
}
case store.HubKeyScript:
for i, v := range a.Scripts {
if !f(i, v) {
break
}
}
case store.HubKeyGlobalRule:
for i, v := range a.GlobalPlugins {
if !f(i, v) {
break
}
}
case store.HubKeyPluginConfig:
for i, v := range a.PluginConfigs {
if !f(i, v) {
break
}
}
}
}

func (a *DataSet) Add(obj interface{}) error {
var err error = nil
switch obj := obj.(type) {
case *entity.Consumer:
a.Counsumers = append(a.Counsumers, obj)
case *entity.Route:
a.Routes = append(a.Routes, obj)
case *entity.Service:
a.Services = append(a.Services, obj)
case *entity.SSL:
a.SSLs = append(a.SSLs, obj)
case *entity.Upstream:
a.Upstreams = append(a.Upstreams, obj)
case *entity.Script:
a.Scripts = append(a.Scripts, obj)
case *entity.GlobalPlugins:
a.GlobalPlugins = append(a.GlobalPlugins, obj)
case *entity.PluginConfig:
a.PluginConfigs = append(a.PluginConfigs, obj)
default:
err = errors.New("Unknown type of obj")
}
return err
}
99 changes: 99 additions & 0 deletions api/internal/core/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 migrate

import (
"context"
"encoding/json"
"errors"

"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/log"
)

var (
ErrConflict = errors.New("conflict")
starsz marked this conversation as resolved.
Show resolved Hide resolved
)

func Export(ctx context.Context) ([]byte, error) {
exportData := newDataSet()
store.RangeStore(func(key store.HubKey, s *store.GenericStore) bool {
s.Range(ctx, func(_ string, obj interface{}) bool {
err := exportData.Add(obj)
if err != nil {
fregie marked this conversation as resolved.
Show resolved Hide resolved
log.Errorf("Add obj to export list failed:%s", err)
return true
}
return true
})
return true
})

data, err := json.Marshal(exportData)
if err != nil {
return nil, err
}

return data, nil
}

type ConflictMode int

const (
ModeReturn ConflictMode = iota
ModeOverwrite
ModeSkip
)

func Import(ctx context.Context, data []byte, mode ConflictMode) (*DataSet, error) {
importData := newDataSet()
err := json.Unmarshal(data, &importData)
if err != nil {
return nil, err
}
conflict, conflictData := isConflicted(ctx, importData)
if conflict && mode == ModeReturn {
imjoey marked this conversation as resolved.
Show resolved Hide resolved
return conflictData, ErrConflict
}
store.RangeStore(func(key store.HubKey, s *store.GenericStore) bool {
importData.rangeData(key, func(i int, obj interface{}) bool {
_, e := s.CreateCheck(obj)
if e != nil {
switch mode {
case ModeSkip:
return true
case ModeOverwrite:
_, e := s.Update(ctx, obj, true)
if e != nil {
err = e
return false
}
}
} else {
_, e := s.Create(ctx, obj)
if err != nil {
err = e
return false
}
}
return true
})
return true
})
return nil, err
}
6 changes: 6 additions & 0 deletions api/internal/core/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ func (s *GenericStore) List(_ context.Context, input ListInput) (*ListOutput, er
return output, nil
}

func (s *GenericStore) Range(_ context.Context, f func(key string, obj interface{}) bool) {
s.cache.Range(func(key, value interface{}) bool {
return f(key.(string), value)
})
}

func (s *GenericStore) ingestValidate(obj interface{}) (err error) {
if s.opt.Validator != nil {
if err := s.opt.Validator.Validate(obj); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions api/internal/core/store/storehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func GetStore(key HubKey) *GenericStore {
panic(fmt.Sprintf("no store with key: %s", key))
}

func RangeStore(f func(key HubKey, store *GenericStore) bool) {
for k, s := range storeHub {
if k != "" && s != nil {
if !f(k, s) {
break
}
}
}
}

func InitStores() error {
err := InitStore(HubKeyConsumer, GenericStoreOption{
BasePath: conf.ETCDConfig.Prefix + "/consumers",
Expand Down
Loading