@@ -23,19 +23,53 @@ import (
2323 "k8s.io/klog/v2"
2424)
2525
26+ const useStarCountThreshold = 10
27+
28+ type packageKey struct {
29+ Name string
30+ Repository string
31+ }
32+
2633// FindBestArtifactHubMatch takes the helm releases found in the cluster and attempts to match those to a package in artifacthub
2734func 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
3039 for _ , p := range ahubPackages {
31- var score float32
3240 if p .Name != clusterRelease .Chart .Metadata .Name {
3341 continue
3442 }
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+ }
3670 if score > highScore {
3771 highScore = score
38- highScorePackage = p
72+ highScorePackage = packagesByName [ k ]
3973 }
4074 }
4175 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)
122156 klog .V (10 ).Infof ("+1.5 score for %s, preferred repo (ahub package repo %s)" , release .Chart .Metadata .Name , pkg .Repository .Name )
123157 ret += 1.5
124158 }
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 )
126160 return ret
127161}
128162
0 commit comments