Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recommendation rule controller #427

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/craned/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func initControllers(ctx context.Context, mgr ctrl.Manager, opts *options.Option
klog.Exit(err, "unable to create controller", "controller", "AnalyticsController")
}

if err := (&recommendation.Controller{
if err := (&recommendation.RecommendationController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
RestMapper: mgr.GetRESTMapper(),
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/go-echarts/go-echarts/v2 v2.2.4
github.com/gocrane/api v0.6.1-0.20220719082537-b7bdeaf2fb17
github.com/gocrane/api v0.6.1-0.20220721081535-2cf15fc58bf3
github.com/google/cadvisor v0.39.2
github.com/mjibson/go-dsp v0.0.0-20180508042940-11479a337f12
github.com/prometheus/client_golang v1.11.0
Expand Down Expand Up @@ -181,7 +181,6 @@ require (
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/tools v0.1.8 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/protobuf v1.27.1
)

replace (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ github.com/gobwas/ws v1.1.0-rc.5 h1:QOAag7FoBaBYYHRqzqkhhd8fq5RTubvI4v3Ft/gDVVQ=
github.com/gobwas/ws v1.1.0-rc.5/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL0=
github.com/gocrane/api v0.5.1-0.20220706040335-eaadbb4b99ed h1:aARCU+Hs1ZKTqJFJT/4/or/iGR6qYwMcG99CGmBFJpg=
github.com/gocrane/api v0.5.1-0.20220706040335-eaadbb4b99ed/go.mod h1:GxI+t9AW8+NsHkz2JkPBIJN//9eLUjTZl1ScYAbXMbk=
github.com/gocrane/api v0.6.1-0.20220719082537-b7bdeaf2fb17 h1:b/DZZFrJ5g3TtUb9DNhzavKrXPi/fSFJoXWwR4cCZiE=
github.com/gocrane/api v0.6.1-0.20220719082537-b7bdeaf2fb17/go.mod h1:GxI+t9AW8+NsHkz2JkPBIJN//9eLUjTZl1ScYAbXMbk=
github.com/gocrane/api v0.6.1-0.20220721081535-2cf15fc58bf3 h1:cKnH9KoQzzBg3/bzYUfRZAhFW7rndswc+TB0rj+uzng=
github.com/gocrane/api v0.6.1-0.20220721081535-2cf15fc58bf3/go.mod h1:GxI+t9AW8+NsHkz2JkPBIJN//9eLUjTZl1ScYAbXMbk=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/analytics/analytics_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,10 @@ func objRefKey(kind, apiVersion, namespace, name, recommendType string) string {
return fmt.Sprintf("%s#%s#%s#%s#%s", kind, apiVersion, namespace, name, recommendType)
}

func match(labelSelector metav1.LabelSelector, matchLabels map[string]string) bool {
func match(labelSelector *metav1.LabelSelector, matchLabels map[string]string) bool {
if labelSelector == nil {
return true
}
for k, v := range labelSelector.MatchLabels {
if matchLabels[k] != v {
return false
Expand Down
22 changes: 11 additions & 11 deletions pkg/controller/analytics/analytics_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ func TestMatch(t *testing.T) {
tests := []struct {
description string
matchLabels map[string]string
labelSelector metav1.LabelSelector
labelSelector *metav1.LabelSelector
expect bool
}{
{
description: "match labels",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: unmatchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{},
},
Expand All @@ -31,7 +31,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key2 exists",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key2",
Expand All @@ -44,7 +44,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key1 exists",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key1",
Expand All @@ -57,7 +57,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key1 doesNotExists",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key1",
Expand All @@ -70,7 +70,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key2 doesNotExists",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key2",
Expand All @@ -83,7 +83,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key2 in",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key2",
Expand All @@ -96,7 +96,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key1 in value1",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key1",
Expand All @@ -109,7 +109,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key1 in value2",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key1",
Expand All @@ -122,7 +122,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key1 notIn value1",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key1",
Expand All @@ -135,7 +135,7 @@ func TestMatch(t *testing.T) {
{
description: "match expression key1 notIn Value2",
matchLabels: matchLabels,
labelSelector: metav1.LabelSelector{
labelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "key1",
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/recommendation/recommendation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/gocrane/crane/pkg/providers"
)

// Controller is responsible for reconcile Recommendation
type Controller struct {
// RecommendationController is responsible for reconcile Recommendation
type RecommendationController struct {
client.Client
ConfigSet *analysisv1alph1.ConfigSet
Scheme *runtime.Scheme
Expand All @@ -33,7 +33,7 @@ type Controller struct {
Provider providers.History
}

func (c *Controller) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
func (c *RecommendationController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
klog.V(4).Infof("Got Recommendation %s", req.NamespacedName)

recommendation := &analysisv1alph1.Recommendation{}
Expand Down Expand Up @@ -73,7 +73,7 @@ func (c *Controller) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return ctrl.Result{}, nil
}

func (c *Controller) UpdateStatus(ctx context.Context, recommendation *analysisv1alph1.Recommendation, newStatus *analysisv1alph1.RecommendationStatus) {
func (c *RecommendationController) UpdateStatus(ctx context.Context, recommendation *analysisv1alph1.Recommendation, newStatus *analysisv1alph1.RecommendationStatus) {
if !equality.Semantic.DeepEqual(&recommendation.Status, newStatus) {
recommendation.Status = *newStatus
timeNow := metav1.Now()
Expand All @@ -90,7 +90,7 @@ func (c *Controller) UpdateStatus(ctx context.Context, recommendation *analysisv
}
}

func (c *Controller) SetupWithManager(mgr ctrl.Manager) error {
func (c *RecommendationController) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&analysisv1alph1.Recommendation{}).
Complete(c)
Expand Down
Loading