-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ricardo Lüders
committed
Nov 27, 2023
1 parent
ca5ff4f
commit e57a5c1
Showing
7 changed files
with
224 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package helmcharts | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/openshift/insights-operator/pkg/gatherers" | ||
"github.com/openshift/insights-operator/pkg/record" | ||
"github.com/openshift/insights-operator/pkg/utils" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
var helmChartsGathererPeriod = time.Hour * 24 | ||
|
||
type Gatherer struct { | ||
gatherKubeConfig *rest.Config | ||
gatherProtoKubeConfig *rest.Config | ||
lastProcessingTime time.Time | ||
} | ||
|
||
func New(gatherKubeConfig, gatherProtoKubeConfig *rest.Config) *Gatherer { | ||
return &Gatherer{ | ||
gatherKubeConfig: gatherKubeConfig, | ||
gatherProtoKubeConfig: gatherProtoKubeConfig, | ||
lastProcessingTime: time.Unix(0, 0), | ||
} | ||
} | ||
|
||
func (g *Gatherer) GetName() string { | ||
return "helmcharts" | ||
} | ||
|
||
func (g *Gatherer) GetGatheringFunctions(context.Context) (map[string]gatherers.GatheringClosure, error) { | ||
return map[string]gatherers.GatheringClosure{ | ||
"helm_info": { | ||
Run: func(ctx context.Context) ([]record.Record, []error) { | ||
return g.GatherHelmInfo(ctx) | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (g *Gatherer) ShouldBeProcessedNow() bool { | ||
return utils.ShouldBeProcessedNow(g.lastProcessingTime, helmChartsGathererPeriod) | ||
} | ||
|
||
func (g *Gatherer) UpdateLastProcessingTime() { | ||
g.lastProcessingTime = time.Now() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package helmcharts | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/openshift/insights-operator/pkg/gatherers" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Gatherer_Basic(t *testing.T) { | ||
gatherer := New(nil, nil) | ||
assert.Equal(t, "helmcharts", gatherer.GetName()) | ||
gatheringFunctions, err := gatherer.GetGatheringFunctions(context.TODO()) | ||
assert.NoError(t, err) | ||
assert.Greater(t, len(gatheringFunctions), 0) | ||
|
||
assert.Implements(t, (*gatherers.Interface)(nil), gatherer) | ||
assert.Implements(t, (*gatherers.CustomPeriodGatherer)(nil), gatherer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package helmcharts | ||
|
||
type HelmChartInfo struct { | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
Resources map[string]int `json:"resources"` | ||
} | ||
|
||
type HelmChartInfoList struct { | ||
Namespaces map[string][]HelmChartInfo | ||
} | ||
|
||
func newHelmChartInfoList() HelmChartInfoList { | ||
return HelmChartInfoList{ | ||
Namespaces: make(map[string][]HelmChartInfo), | ||
} | ||
} | ||
|
||
func (h *HelmChartInfoList) addItem(ns string, resourceType string, info HelmChartInfo) { | ||
if _, ok := h.Namespaces[ns]; !ok { | ||
h.Namespaces[ns] = make([]HelmChartInfo, 0) | ||
} | ||
|
||
var helmIdx int | ||
var found bool | ||
for i, n := range h.Namespaces[ns] { | ||
if n.Name == info.Name && n.Version == info.Version { | ||
helmIdx = i | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
info.Resources = map[string]int{resourceType: 1} | ||
h.Namespaces[ns] = append(h.Namespaces[ns], info) | ||
return | ||
} | ||
|
||
if h.Namespaces[ns][helmIdx].Resources == nil { | ||
h.Namespaces[ns][helmIdx].Resources = make(map[string]int) | ||
} | ||
h.Namespaces[ns][helmIdx].Resources[resourceType]++ | ||
} |
Oops, something went wrong.