From f55c86b3799157dbb5580b6342e2b4f326b36946 Mon Sep 17 00:00:00 2001 From: fengluodb Date: Thu, 22 Jun 2023 19:47:07 +0800 Subject: [PATCH] feat: cli plugin adopt the block index repository --- controllers/apps/configuration/config_util.go | 3 +- .../reconfigurerequest_controller.go | 3 +- go.mod | 2 - go.sum | 4 -- internal/cli/cmd/plugin/types.go | 11 +++- internal/cli/cmd/plugin/utils.go | 57 +++++++++++-------- internal/controllerutil/volumesnapshot.go | 4 +- 7 files changed, 49 insertions(+), 35 deletions(-) diff --git a/controllers/apps/configuration/config_util.go b/controllers/apps/configuration/config_util.go index f3482751675..a33a9bb4eec 100644 --- a/controllers/apps/configuration/config_util.go +++ b/controllers/apps/configuration/config_util.go @@ -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" @@ -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" diff --git a/controllers/apps/configuration/reconfigurerequest_controller.go b/controllers/apps/configuration/reconfigurerequest_controller.go index b34e36d0b70..ccc7de0d69c 100644 --- a/controllers/apps/configuration/reconfigurerequest_controller.go +++ b/controllers/apps/configuration/reconfigurerequest_controller.go @@ -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" @@ -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" diff --git a/go.mod b/go.mod index c0797878832..10833cf30c2 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 4a3db2d8ff1..e093220e8e0 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/internal/cli/cmd/plugin/types.go b/internal/cli/cmd/plugin/types.go index af7918fade6..e05767dc8e4 100644 --- a/internal/cli/cmd/plugin/types.go +++ b/internal/cli/cmd/plugin/types.go @@ -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 { diff --git a/internal/cli/cmd/plugin/utils.go b/internal/cli/cmd/plugin/utils.go index 9cb2987886b..f066d9b8e18 100644 --- a/internal/cli/cmd/plugin/utils.go +++ b/internal/cli/cmd/plugin/utils.go @@ -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) { @@ -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 } diff --git a/internal/controllerutil/volumesnapshot.go b/internal/controllerutil/volumesnapshot.go index 56fec086573..87c930573c3 100644 --- a/internal/controllerutil/volumesnapshot.go +++ b/internal/controllerutil/volumesnapshot.go @@ -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