Skip to content

Commit 66a618a

Browse files
authored
feat(plugins): add support for latest version, fix version preference for duplicate plugins (#2158)
* chore(plugins): remove unused functions * chore(plugins): rename test functions * chore(plugins): add test for SomeVersionOf * chore(plugins): add test for HasExactVersionOf * chore(plugins): add test for HasNewerVersionOf * chore(plugins): extend TestPluginListString to cover string generation * chore(plugins): refactor TestPluginListSanitize * chore(plugins): extend TestPluginListSanitize to cover incorrect versions * feat(plugins): add support for latest version * chore(plugins): deprecate unused Hash method * fix(plugins): correctly update plugin version and add tests * chore(grafana): deprecate unused PLUGINS_HASH env * chore(plugins): update docs * feat(plugins): add String method to GrafanaPlugin * feat(plugins): add HasValidVersion and HasInvalidVersion to GrafanaPlugin
1 parent 56279f5 commit 66a618a

File tree

7 files changed

+518
-71
lines changed

7 files changed

+518
-71
lines changed

api/v1beta1/grafana_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import (
2727
"sigs.k8s.io/controller-runtime/pkg/client"
2828
)
2929

30+
const (
31+
PluginVersionLatest string = "latest"
32+
)
33+
3034
type OperatorStageName string
3135

3236
type OperatorStageStatus string

api/v1beta1/plugin_list.go

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package v1beta1
22

33
import (
4-
"crypto/sha256"
54
"fmt"
6-
"io"
75
"sort"
86
"strings"
97

@@ -15,26 +13,37 @@ type GrafanaPlugin struct {
1513
Version string `json:"version"`
1614
}
1715

18-
type PluginList []GrafanaPlugin
16+
func (p GrafanaPlugin) HasValidVersion() bool {
17+
if p.Version == PluginVersionLatest {
18+
return true
19+
}
1920

20-
type PluginMap map[string]PluginList
21+
_, err := semver.Parse(p.Version)
2122

22-
func (l PluginList) Hash() string {
23-
sb := strings.Builder{}
24-
for _, plugin := range l {
25-
sb.WriteString(plugin.Name)
26-
sb.WriteString(plugin.Version)
23+
return err == nil
24+
}
25+
26+
func (p GrafanaPlugin) HasInvalidVersion() bool {
27+
return !p.HasValidVersion()
28+
}
29+
30+
func (p GrafanaPlugin) String() string {
31+
if p.Version == PluginVersionLatest {
32+
return p.Name
2733
}
2834

29-
hash := sha256.New()
30-
io.WriteString(hash, sb.String()) //nolint
31-
return fmt.Sprintf("%x", hash.Sum(nil))
35+
return fmt.Sprintf("%s %s", p.Name, p.Version)
3236
}
3337

38+
type PluginList []GrafanaPlugin
39+
40+
type PluginMap map[string]PluginList
41+
3442
func (l PluginList) String() string {
3543
plugins := make(sort.StringSlice, 0, len(l))
44+
3645
for _, plugin := range l {
37-
plugins = append(plugins, fmt.Sprintf("%s %s", plugin.Name, plugin.Version))
46+
plugins = append(plugins, plugin.String())
3847
}
3948

4049
sort.Sort(plugins)
@@ -44,9 +53,9 @@ func (l PluginList) String() string {
4453

4554
// Update update plugin version
4655
func (l PluginList) Update(plugin *GrafanaPlugin) {
47-
for _, installedPlugin := range l {
56+
for i, installedPlugin := range l {
4857
if installedPlugin.Name == plugin.Name {
49-
installedPlugin.Version = plugin.Version
58+
l[i].Version = plugin.Version
5059
break
5160
}
5261
}
@@ -57,8 +66,7 @@ func (l PluginList) Sanitize() PluginList {
5766
var sanitized PluginList
5867

5968
for _, plugin := range l {
60-
_, err := semver.Parse(plugin.Version)
61-
if err != nil {
69+
if plugin.HasInvalidVersion() {
6270
continue
6371
}
6472

@@ -81,17 +89,6 @@ func (l PluginList) HasSomeVersionOf(plugin *GrafanaPlugin) bool {
8189
return false
8290
}
8391

84-
// GetInstalledVersionOf gets the plugin from the list regardless of the version
85-
func (l PluginList) GetInstalledVersionOf(plugin *GrafanaPlugin) *GrafanaPlugin {
86-
for _, listedPlugin := range l {
87-
if listedPlugin.Name == plugin.Name {
88-
return &listedPlugin
89-
}
90-
}
91-
92-
return nil
93-
}
94-
9592
// HasExactVersionOf returns true if the list contains the same plugin in the same version
9693
func (l PluginList) HasExactVersionOf(plugin *GrafanaPlugin) bool {
9794
for _, listedPlugin := range l {
@@ -110,6 +107,14 @@ func (l PluginList) HasNewerVersionOf(plugin *GrafanaPlugin) (bool, error) {
110107
continue
111108
}
112109

110+
if listedPlugin.Version == PluginVersionLatest && plugin.Version != PluginVersionLatest {
111+
return true, nil
112+
}
113+
114+
if plugin.Version == PluginVersionLatest {
115+
return false, nil
116+
}
117+
113118
listedVersion, err := semver.Parse(listedPlugin.Version)
114119
if err != nil {
115120
return false, err
@@ -127,16 +132,3 @@ func (l PluginList) HasNewerVersionOf(plugin *GrafanaPlugin) (bool, error) {
127132

128133
return false, nil
129134
}
130-
131-
// VersionsOf returns the number of different versions of a given plugin in the list
132-
func (l PluginList) VersionsOf(plugin *GrafanaPlugin) int {
133-
i := 0
134-
135-
for _, listedPlugin := range l {
136-
if listedPlugin.Name == plugin.Name {
137-
i++
138-
}
139-
}
140-
141-
return i
142-
}

0 commit comments

Comments
 (0)