@@ -23,19 +23,53 @@ import (
23
23
"k8s.io/klog/v2"
24
24
)
25
25
26
+ const useStarCountThreshold = 10
27
+
28
+ type packageKey struct {
29
+ Name string
30
+ Repository string
31
+ }
32
+
26
33
// FindBestArtifactHubMatch takes the helm releases found in the cluster and attempts to match those to a package in artifacthub
27
34
func FindBestArtifactHubMatch (clusterRelease * release.Release , ahubPackages []ArtifactHubHelmPackage ) * output.ReleaseOutput {
28
- var highScore float32
29
- var highScorePackage ArtifactHubHelmPackage
35
+ packagesByName := map [packageKey ]ArtifactHubHelmPackage {}
36
+ packageScores := map [packageKey ]float32 {}
37
+ packageStars := map [packageKey ]int {}
38
+ var useStars bool
30
39
for _ , p := range ahubPackages {
31
- var score float32
32
40
if p .Name != clusterRelease .Chart .Metadata .Name {
33
41
continue
34
42
}
35
- score = scoreChartSimilarity (clusterRelease , p )
43
+
44
+ key := packageKey {Name : p .Name , Repository : p .Repository .Name }
45
+ packageScores [key ] = scoreChartSimilarity (clusterRelease , p )
46
+ packagesByName [key ] = p
47
+ packageStars [key ] = p .Stars
48
+
49
+ if p .Stars >= useStarCountThreshold {
50
+ useStars = true // If any package has more than 10 stars, we add a point to the highest star package
51
+ }
52
+ }
53
+
54
+ var highestStarPackageName * packageKey
55
+ var highStars int
56
+ for p , stars := range packageStars {
57
+ if stars > highStars {
58
+ highStars = stars
59
+ highestStarPackageName = & p
60
+ }
61
+ }
62
+
63
+ var highScore float32
64
+ var highScorePackage ArtifactHubHelmPackage
65
+ for k , score := range packageScores {
66
+ if useStars && highestStarPackageName != nil && k == * highestStarPackageName {
67
+ klog .V (10 ).Infof ("adding a point to the highest star package: %s:%s" , k .Repository , k .Name )
68
+ score ++ // Add a point to the highest star package
69
+ }
36
70
if score > highScore {
37
71
highScore = score
38
- highScorePackage = p
72
+ highScorePackage = packagesByName [ k ]
39
73
}
40
74
}
41
75
klog .V (10 ).Infof ("highScore for '%s': %f, highScorePackage Repo: %s" , clusterRelease .Chart .Metadata .Name , highScore , highScorePackage .Repository .Name )
@@ -122,7 +156,7 @@ func scoreChartSimilarity(release *release.Release, pkg ArtifactHubHelmPackage)
122
156
klog .V (10 ).Infof ("+1.5 score for %s, preferred repo (ahub package repo %s)" , release .Chart .Metadata .Name , pkg .Repository .Name )
123
157
ret += 1.5
124
158
}
125
- klog .V (10 ).Infof ("calculated score repo: %s, release: %s, score: %f\n \n " , pkg .Repository .Name , release .Name , ret )
159
+ klog .V (10 ).Infof ("calculated score repo: %s, release: %s, stars: %d, score: %f\n \n " , pkg .Repository .Name , release .Name , pkg . Stars , ret )
126
160
return ret
127
161
}
128
162
0 commit comments