-
Notifications
You must be signed in to change notification settings - Fork 376
/
recommendation.go
153 lines (128 loc) · 4.96 KB
/
recommendation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package recommendation
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
patchtypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"sigs.k8s.io/controller-runtime/pkg/client"
analysisapi "github.com/gocrane/api/analysis/v1alpha1"
"github.com/gocrane/crane/pkg/recommendation/recommender"
"github.com/gocrane/crane/pkg/server/config"
"github.com/gocrane/crane/pkg/server/ginwrapper"
"github.com/gocrane/crane/pkg/utils"
)
type Handler struct {
client client.Client
dynamicClient dynamic.Interface
discoveryClient discovery.DiscoveryInterface
}
func NewRecommendationHandler(config *config.Config) *Handler {
dynamicClient := dynamic.NewForConfigOrDie(config.KubeConfig)
discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(config.KubeConfig)
return &Handler{
client: config.Client,
dynamicClient: dynamicClient,
discoveryClient: discoveryClient,
}
}
// ListRecommendations list the recommendations in cluster.
func (h *Handler) ListRecommendations(c *gin.Context) {
recommendList := &analysisapi.RecommendationList{}
err := h.client.List(context.TODO(), recommendList)
if err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
ginwrapper.WriteResponse(c, nil, recommendList)
}
// ListRecommendationRules list the recommendationRules in cluster.
func (h *Handler) ListRecommendationRules(c *gin.Context) {
recommendationRuleList := &analysisapi.RecommendationRuleList{}
err := h.client.List(context.TODO(), recommendationRuleList)
if err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
ginwrapper.WriteResponse(c, nil, recommendationRuleList)
}
// CreateRecommendationRule create a recommendationRules from request.
func (h *Handler) CreateRecommendationRule(c *gin.Context) {
recommendationRule := &analysisapi.RecommendationRule{}
if err := c.ShouldBindJSON(&recommendationRule); err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
err := h.client.Create(context.TODO(), recommendationRule)
if err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
ginwrapper.WriteResponse(c, nil, nil)
}
// UpdateRecommendationRule update a recommendationRules from request.
func (h *Handler) UpdateRecommendationRule(c *gin.Context) {
recommendationRule := &analysisapi.RecommendationRule{}
if err := c.ShouldBindJSON(&recommendationRule); err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
if c.Param("recommendationRuleName") != recommendationRule.Name {
ginwrapper.WriteResponse(c, fmt.Errorf("RecommendationRule name is unexpect"), nil)
return
}
recommendationRuleExist := &analysisapi.RecommendationRule{}
if err := h.client.Get(context.TODO(), types.NamespacedName{Name: recommendationRule.Name}, recommendationRuleExist); err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
recommendationRuleExist.Spec = recommendationRule.Spec
err := h.client.Update(context.TODO(), recommendationRuleExist)
if err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
ginwrapper.WriteResponse(c, nil, nil)
}
// AdoptRecommendation adopt a recommendation from request.
func (h *Handler) AdoptRecommendation(c *gin.Context) {
recommendationExist := &analysisapi.Recommendation{}
if err := h.client.Get(context.TODO(), types.NamespacedName{Namespace: c.Param("namespace"), Name: c.Param("recommendationName")}, recommendationExist); err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
if string(recommendationExist.Spec.Type) == recommender.ReplicasRecommender ||
string(recommendationExist.Spec.Type) == recommender.ResourceRecommender {
gvr, err := utils.GetGroupVersionResource(h.discoveryClient, recommendationExist.Spec.TargetRef.APIVersion, recommendationExist.Spec.TargetRef.Kind)
if err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
_, err = h.dynamicClient.Resource(*gvr).Namespace(recommendationExist.Spec.TargetRef.Namespace).Patch(context.TODO(), recommendationExist.Spec.TargetRef.Name, patchtypes.StrategicMergePatchType, []byte(recommendationExist.Status.RecommendedInfo), metav1.PatchOptions{})
if err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
ginwrapper.WriteResponse(c, nil, nil)
} else {
ginwrapper.WriteResponse(c, fmt.Errorf("Recommendation type %s is not supported for adoption ", string(recommendationExist.Spec.Type)), nil)
return
}
}
// DeleteRecommendationRule delete a recommendationRules from request.
func (h *Handler) DeleteRecommendationRule(c *gin.Context) {
recommendationRuleExist := &analysisapi.RecommendationRule{}
if err := h.client.Get(context.TODO(), types.NamespacedName{Name: c.Param("recommendationRuleName")}, recommendationRuleExist); err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
err := h.client.Delete(context.TODO(), recommendationRuleExist)
if err != nil {
ginwrapper.WriteResponse(c, err, nil)
return
}
ginwrapper.WriteResponse(c, nil, nil)
}