Skip to content

Commit

Permalink
add recommendation rule controller
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhu committed Jul 21, 2022
1 parent 85695c4 commit 0c11a9a
Show file tree
Hide file tree
Showing 9 changed files with 500 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/craned/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,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.0
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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ github.com/gocrane/api v0.5.1-0.20220706040335-eaadbb4b99ed h1:aARCU+Hs1ZKTqJFJT
github.com/gocrane/api v0.5.1-0.20220706040335-eaadbb4b99ed/go.mod h1:GxI+t9AW8+NsHkz2JkPBIJN//9eLUjTZl1ScYAbXMbk=
github.com/gocrane/api v0.6.0 h1:ENhtkD1P9pm+65W9gky19+J2X2gIwOMX7ySrR4GyEek=
github.com/gocrane/api v0.6.0/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.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
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 @@ -488,7 +488,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

0 comments on commit 0c11a9a

Please sign in to comment.