feat: add plugin existence check before installation

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
pull/31404/head
Suleiman Dibirov 9 months ago
parent 99cd196435
commit 327dfeab38

@ -91,7 +91,12 @@ func (i *HTTPInstaller) Install() error {
return fmt.Errorf("failed to extract plugin metadata from tarball: %w", err)
}
filename := fmt.Sprintf("%s-%s.tgz", metadata.Name, metadata.Version)
tarballPath := helmpath.DataPath("plugins", filename)
pluginsPath := helmpath.DataPath("plugins")
foundPlugins, _ := plugin.FindPlugins([]string{pluginsPath}, plugin.Descriptor{Name: metadata.Name})
if len(foundPlugins) > 0 {
return fmt.Errorf("plugin %q already exists at %q", metadata.Name, foundPlugins[0].Dir())
}
tarballPath := filepath.Join(pluginsPath, filename)
if err := os.MkdirAll(filepath.Dir(tarballPath), 0755); err != nil {
return fmt.Errorf("failed to create plugins directory: %w", err)
}

@ -97,6 +97,20 @@ func (i *LocalInstaller) installFromDirectory() error {
if !isPlugin(i.Source) {
return ErrMissingMetadata
}
// Load plugin to get metadata and check for existing plugin with same name
p, err := plugin.LoadDir(i.Source)
if err != nil {
return fmt.Errorf("failed to load plugin: %w", err)
}
metadata := p.Metadata()
pluginsPath := helmpath.DataPath("plugins")
foundPlugins, _ := plugin.FindPlugins([]string{pluginsPath}, plugin.Descriptor{Name: metadata.Name})
if len(foundPlugins) > 0 {
return fmt.Errorf("plugin %q already exists at %q", metadata.Name, foundPlugins[0].Dir())
}
slog.Debug("symlinking", "source", i.Source, "path", i.Path())
return os.Symlink(i.Source, i.Path())
}
@ -116,7 +130,12 @@ func (i *LocalInstaller) installFromArchive() error {
return fmt.Errorf("failed to extract plugin metadata from tarball: %w", err)
}
filename := fmt.Sprintf("%s-%s.tgz", metadata.Name, metadata.Version)
tarballPath := helmpath.DataPath("plugins", filename)
pluginsPath := helmpath.DataPath("plugins")
foundPlugins, _ := plugin.FindPlugins([]string{pluginsPath}, plugin.Descriptor{Name: metadata.Name})
if len(foundPlugins) > 0 {
return fmt.Errorf("plugin %q already exists at %q", metadata.Name, foundPlugins[0].Dir())
}
tarballPath := filepath.Join(pluginsPath, filename)
if err := os.MkdirAll(filepath.Dir(tarballPath), 0755); err != nil {
return fmt.Errorf("failed to create plugins directory: %w", err)
}

@ -101,8 +101,12 @@ func (i *OCIInstaller) Install() error {
return fmt.Errorf("failed to extract plugin metadata from tarball: %w", err)
}
filename := fmt.Sprintf("%s-%s.tgz", metadata.Name, metadata.Version)
tarballPath := helmpath.DataPath("plugins", filename)
pluginsPath := helmpath.DataPath("plugins")
foundPlugins, _ := plugin.FindPlugins([]string{pluginsPath}, plugin.Descriptor{Name: metadata.Name})
if len(foundPlugins) > 0 {
return fmt.Errorf("plugin %q already exists at %q", metadata.Name, foundPlugins[0].Dir())
}
tarballPath := filepath.Join(pluginsPath, filename)
if err := os.MkdirAll(filepath.Dir(tarballPath), 0755); err != nil {
return fmt.Errorf("failed to create plugins directory: %w", err)
}

@ -26,6 +26,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/Masterminds/vcs"
"helm.sh/helm/v4/internal/plugin"
"helm.sh/helm/v4/internal/plugin/cache"
"helm.sh/helm/v4/internal/third_party/dep/fs"
"helm.sh/helm/v4/pkg/helmpath"
@ -91,6 +92,19 @@ func (i *VCSInstaller) Install() error {
return ErrMissingMetadata
}
// Load plugin to get metadata and check for existing plugin with same name
p, err := plugin.LoadDir(i.Repo.LocalPath())
if err != nil {
return fmt.Errorf("failed to load plugin: %w", err)
}
metadata := p.Metadata()
// Check if a plugin with the same name already exists
foundPlugins, _ := plugin.FindPlugins([]string{i.PluginsDirectory}, plugin.Descriptor{Name: metadata.Name})
if len(foundPlugins) > 0 {
return fmt.Errorf("plugin %q already exists at %q", metadata.Name, foundPlugins[0].Dir())
}
slog.Debug("copying files", "source", i.Repo.LocalPath(), "destination", i.Path())
return fs.CopyDir(i.Repo.LocalPath(), i.Path())
}

Loading…
Cancel
Save