Skip to content

Commit

Permalink
feat: cli plugin adopt the block index repository
Browse files Browse the repository at this point in the history
  • Loading branch information
fengluodb committed Jun 22, 2023
1 parent 93a5db2 commit f55c86b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 35 deletions.
3 changes: 2 additions & 1 deletion controllers/apps/configuration/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"reflect"

cfgutil "github.com/apecloud/kubeblocks/internal/configuration/util"
"github.com/go-logr/logr"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -33,6 +32,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

cfgutil "github.com/apecloud/kubeblocks/internal/configuration/util"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
cfgcore "github.com/apecloud/kubeblocks/internal/configuration"
cfgcm "github.com/apecloud/kubeblocks/internal/configuration/config_manager"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strings"
"time"

"github.com/apecloud/kubeblocks/internal/generics"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -37,6 +36,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

"github.com/apecloud/kubeblocks/internal/generics"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
cfgcore "github.com/apecloud/kubeblocks/internal/configuration"
cfgcm "github.com/apecloud/kubeblocks/internal/configuration/config_manager"
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ require (
github.com/russross/blackfriday/v2 v2.1.0
github.com/sahilm/fuzzy v0.1.0
github.com/sethvargo/go-password v0.2.0
github.com/shirou/gopsutil v3.20.11+incompatible
github.com/shirou/gopsutil/v3 v3.23.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cast v1.5.0
Expand Down Expand Up @@ -125,7 +124,6 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20221026131551-cf6655e29de4 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/ahmetalpbalkan/go-cursor v0.0.0-20131010032410-8136607ea412 // indirect
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ github.com/Shopify/sarama v1.30.0 h1:TOZL6r37xJBDEMLx4yjB77jxbZYXPaDow08TSK6vIL0
github.com/Shopify/sarama v1.30.0/go.mod h1:zujlQQx1kzHsh4jfV1USnptCQrHAEZ2Hk8fTKCulPVs=
github.com/Shopify/toxiproxy/v2 v2.1.6-0.20210914104332-15ea381dcdae h1:ePgznFqEG1v3AjMklnK8H7BSc++FDSo7xfK9K7Af+0Y=
github.com/Shopify/toxiproxy/v2 v2.1.6-0.20210914104332-15ea381dcdae/go.mod h1:/cvHQkZ1fst0EmZnA5dFtiQdWCNCFYzb+uE2vqVgvx0=
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 h1:5sXbqlSomvdjlRbWyNqkPsJ3Fg+tQZCbgeX1VGljbQY=
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/StudioSol/set v1.0.0 h1:G27J71la+Da08WidabBkoRrvPLTa4cdCn0RjvyJ5WKQ=
github.com/StudioSol/set v1.0.0/go.mod h1:hIUNZPo6rEGF43RlPXHq7Fjmf+HkVJBqAjtK7Z9LoIU=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
Expand Down Expand Up @@ -1532,8 +1530,6 @@ github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty4DZO54I4FGqIpto=
github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil/v3 v3.23.1 h1:a9KKO+kGLKEvcPIs4W62v0nu3sciVDOOOPUD0Hz7z/4=
github.com/shirou/gopsutil/v3 v3.23.1/go.mod h1:NN6mnm5/0k8jw4cBfCnJtr5L7ErOTg18tMNpgFkn0hA=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
Expand Down
11 changes: 9 additions & 2 deletions internal/cli/cmd/plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ func (p *Paths) IndexPath(name string) string {
return filepath.Join(p.IndexBase(), name)
}

func (p *Paths) IndexPluginsPath(name string) string {
return filepath.Join(p.IndexPath(name), "plugins")
func (p *Paths) IndexPluginsPath(name string) []string {
result := make([]string, 0)
if _, err := os.Stat(filepath.Join(p.IndexPath(name), "plugins")); err == nil {
result = append(result, filepath.Join(p.IndexPath(name), "plugins"))
}
if _, err := os.Stat(filepath.Join(p.IndexPath(name), "krew-plugins")); err == nil {
result = append(result, filepath.Join(p.IndexPath(name), "krew-plugins"))
}
return result
}

func (p *Paths) InstallReceiptsPath() string {
Expand Down
57 changes: 34 additions & 23 deletions internal/cli/cmd/plugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,18 @@ func NewPaths(base string) *Paths {
return &Paths{base: base, tmp: os.TempDir()}
}

func LoadPluginByName(pluginsDir, pluginName string) (Plugin, error) {
klog.V(4).Infof("Reading plugin %q from %s", pluginName, pluginsDir)
return ReadPluginFromFile(filepath.Join(pluginsDir, pluginName+ManifestExtension))
// LoadPluginByName loads plugin from index repository
func LoadPluginByName(pluginsDirs []string, pluginName string) (Plugin, error) {
var plugin Plugin
var err error
for _, p := range pluginsDirs {
plugin, err = ReadPluginFromFile(filepath.Join(p, pluginName+ManifestExtension))
if errors.Is(err, os.ErrNotExist) {
continue
}
break
}
return plugin, err
}

func ReadPluginFromFile(path string) (Plugin, error) {
Expand Down Expand Up @@ -256,29 +265,31 @@ func findPluginManifestFiles(indexDir string) ([]string, error) {
}

// LoadPluginListFromFS will parse and retrieve all plugin files.
func LoadPluginListFromFS(indexDir string) ([]Plugin, error) {
indexDir, err := filepath.EvalSymlinks(indexDir)
if err != nil {
return nil, err
}

files, err := findPluginManifestFiles(indexDir)
if err != nil {
return nil, errors.Wrap(err, "failed to scan plugins in index directory")
}
klog.V(4).Infof("found %d plugins in dir %s", len(files), indexDir)
func LoadPluginListFromFS(pluginDirs []string) ([]Plugin, error) {
list := make([]Plugin, 0)
for _, pluginDir := range pluginDirs {
pluginDir, err := filepath.EvalSymlinks(pluginDir)
if err != nil {
return nil, err
}

list := make([]Plugin, 0, len(files))
for _, file := range files {
pluginName := strings.TrimSuffix(file, filepath.Ext(file))
p, err := LoadPluginByName(indexDir, pluginName)
files, err := findPluginManifestFiles(pluginDir)
if err != nil {
// loading the index repository shouldn't fail because of one plugin
// if loading the plugin fails, log the error and continue
klog.Errorf("failed to read or parse plugin manifest %q: %v", pluginName, err)
continue
return nil, errors.Wrap(err, "failed to scan plugins in index directory")
}
klog.V(4).Infof("found %d plugins in dir %s", len(files), pluginDir)

for _, file := range files {
pluginName := strings.TrimSuffix(file, filepath.Ext(file))
p, err := LoadPluginByName([]string{pluginDir}, pluginName)
if err != nil {
// loading the index repository shouldn't fail because of one plugin
// if loading the plugin fails, log the error and continue
klog.Errorf("failed to read or parse plugin manifest %q: %v", pluginName, err)
continue
}
list = append(list, p)
}
list = append(list, p)
}
return list, nil
}
4 changes: 2 additions & 2 deletions internal/controllerutil/volumesnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func (c *VolumeSnapshotCompatClient) CheckResourceExists(key client.ObjectKey, o
}

func convertObjectBetweenAPIVersion[T1 any, T2 any](from T1, to T2) error {
fromJsonBytes, err := json.Marshal(from)
fromJSONBytes, err := json.Marshal(from)
if err != nil {
return err
}
if err = json.Unmarshal(fromJsonBytes, to); err != nil {
if err = json.Unmarshal(fromJSONBytes, to); err != nil {
return err
}
return nil
Expand Down

0 comments on commit f55c86b

Please sign in to comment.