Files
kubevela/pkg/addon/registry.go
qiaozp 9317bb1cab Refactor: addon cache mechanism and code architecture (#2956)
* Refactor: fix addon cache and code

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>

* basic trim

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* Fix list OSS bucket addon's meta

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* rename listAddonMeta func

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix enable

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* rename and trim cache func call

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* remove same source code, use Registry to implement Source interface. Keep the compatibility of DeployTo fields.

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* complete github reader

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix read from github, fix test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* reviewable

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* header

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* rename function, restore test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* try CI

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* sort out functions name. add detail test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* filter directory without metadata.yaml in oss

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* add GitHub reader unit test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* clean up

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* reviewable

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* header

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* add cache arg

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* change field name

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* build swagger

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* some json tag, revert cache logic

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

Co-authored-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
2021-12-21 09:31:37 +08:00

190 lines
5.3 KiB
Go

/*
Copyright 2021 The KubeVela 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 addon
import (
"context"
"encoding/json"
"fmt"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
velatypes "github.com/oam-dev/kubevela/apis/types"
)
const registryConfigMapName = "vela-addon-registry"
const registriesKey = "registries"
// Registry represent a addon registry model
type Registry struct {
Name string `json:"name"`
Git *GitAddonSource `json:"git,omitempty"`
OSS *OSSAddonSource `json:"oss,omitempty"`
}
// RegistryDataStore CRUD addon registry data in configmap
type RegistryDataStore interface {
ListRegistries(context.Context) ([]Registry, error)
AddRegistry(context.Context, Registry) error
DeleteRegistry(context.Context, string) error
UpdateRegistry(context.Context, Registry) error
GetRegistry(context.Context, string) (Registry, error)
}
// NewRegistryDataStore get RegistryDataStore operation interface
func NewRegistryDataStore(cli client.Client) RegistryDataStore {
return registryImpl{cli}
}
type registryImpl struct {
client client.Client
}
func (r registryImpl) ListRegistries(ctx context.Context) ([]Registry, error) {
cm := &v1.ConfigMap{}
if err := r.client.Get(ctx, types.NamespacedName{Namespace: velatypes.DefaultKubeVelaNS, Name: registryConfigMapName}, cm); err != nil {
return nil, err
}
if _, ok := cm.Data[registriesKey]; !ok {
return nil, NewAddonError("Error addon registry configmap registry-key not exist")
}
registries := map[string]Registry{}
if err := json.Unmarshal([]byte(cm.Data[registriesKey]), &registries); err != nil {
return nil, err
}
var res []Registry
for _, registry := range registries {
res = append(res, registry)
}
return res, nil
}
func (r registryImpl) AddRegistry(ctx context.Context, registry Registry) error {
cm := &v1.ConfigMap{}
if err := r.client.Get(ctx, types.NamespacedName{Namespace: velatypes.DefaultKubeVelaNS, Name: registryConfigMapName}, cm); err != nil {
if apierrors.IsNotFound(err) {
b, err := json.Marshal(map[string]Registry{
registry.Name: registry,
})
if err != nil {
return err
}
cm = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: registryConfigMapName,
Namespace: velatypes.DefaultKubeVelaNS,
},
Data: map[string]string{
registriesKey: string(b),
},
}
if err := r.client.Create(ctx, cm); err != nil {
return err
}
return nil
}
return err
}
registries := map[string]Registry{}
if err := json.Unmarshal([]byte(cm.Data[registriesKey]), &registries); err != nil {
return err
}
registries[registry.Name] = registry
b, err := json.Marshal(registries)
if err != nil {
return err
}
cm.Data = map[string]string{
registriesKey: string(b),
}
if err := r.client.Update(ctx, cm); err != nil {
return err
}
return nil
}
func (r registryImpl) DeleteRegistry(ctx context.Context, name string) error {
cm := &v1.ConfigMap{}
if err := r.client.Get(ctx, types.NamespacedName{Namespace: velatypes.DefaultKubeVelaNS, Name: registryConfigMapName}, cm); err != nil {
return err
}
registries := map[string]Registry{}
if err := json.Unmarshal([]byte(cm.Data[registriesKey]), &registries); err != nil {
return err
}
delete(registries, name)
b, err := json.Marshal(registries)
if err != nil {
return err
}
cm.Data = map[string]string{
registriesKey: string(b),
}
if err := r.client.Update(ctx, cm); err != nil {
return err
}
return nil
}
func (r registryImpl) UpdateRegistry(ctx context.Context, registry Registry) error {
cm := &v1.ConfigMap{}
if err := r.client.Get(ctx, types.NamespacedName{Namespace: velatypes.DefaultKubeVelaNS, Name: registryConfigMapName}, cm); err != nil {
return err
}
registries := map[string]Registry{}
if err := json.Unmarshal([]byte(cm.Data[registriesKey]), &registries); err != nil {
return err
}
if _, ok := registries[registry.Name]; !ok {
return fmt.Errorf("addon registry %s not exist", registry.Name)
}
registries[registry.Name] = registry
b, err := json.Marshal(registries)
if err != nil {
return err
}
cm.Data = map[string]string{
registriesKey: string(b),
}
if err := r.client.Update(ctx, cm); err != nil {
return err
}
return nil
}
func (r registryImpl) GetRegistry(ctx context.Context, name string) (Registry, error) {
var res Registry
cm := &v1.ConfigMap{}
if err := r.client.Get(ctx, types.NamespacedName{Namespace: velatypes.DefaultKubeVelaNS, Name: registryConfigMapName}, cm); err != nil {
return res, err
}
registries := map[string]Registry{}
if err := json.Unmarshal([]byte(cm.Data[registriesKey]), &registries); err != nil {
return res, err
}
var notExist bool
if res, notExist = registries[name]; !notExist {
return res, fmt.Errorf("registry name %s not found", name)
}
return res, nil
}