Skip to content

Commit

Permalink
Merge pull request gocrane#796 from qmhu/ana-conversion
Browse files Browse the repository at this point in the history
Support v0.5 version compatiablilty
  • Loading branch information
qmhu committed May 18, 2023
2 parents e5bcb70 + dde6ace commit 9efc321
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 34 deletions.
7 changes: 6 additions & 1 deletion cmd/craned/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ func Run(ctx context.Context, opts *options.Options) error {
return err
}

if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
klog.ErrorS(err, "failed to add health check endpoint")
return err
}

if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
klog.ErrorS(err, "failed to add health check endpoint")
return err
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/controller/analytics/analytics_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,11 @@ func setReadyCondition(status *analysisv1alph1.AnalyticsStatus, conditionStatus
func ConvertToRecommendationRule(analytics *analysisv1alph1.Analytics) *analysisv1alph1.RecommendationRule {
recommendationRule := &analysisv1alph1.RecommendationRule{}
recommendationRule.Name = analytics.Name
if recommendationRule.Annotations == nil {
recommendationRule.Annotations = map[string]string{}
}
recommendationRule.Annotations[known.AnalyticsConversionAnnotation] = string(analytics.UID)
recommendationRule.Spec.ResourceSelectors = analytics.Spec.ResourceSelectors
// todo: make sure the conversion is right after recommendation refactor
recommendationRule.Spec.Recommenders = []analysisv1alph1.Recommender{{Name: string(analytics.Spec.Type)}}
if analytics.Namespace == known.CraneSystemNamespace {
recommendationRule.Spec.NamespaceSelector.Any = true
Expand All @@ -560,6 +563,11 @@ func ConvertToRecommendationRule(analytics *analysisv1alph1.Analytics) *analysis
recommendationRule.Spec.RunInterval = (time.Duration(*analytics.Spec.CompletionStrategy.PeriodSeconds) * time.Second).String()
}

recommendationRule.Status = analysisv1alph1.RecommendationRuleStatus{
LastUpdateTime: analytics.Status.LastUpdateTime,
Recommendations: analytics.Status.Recommendations,
}

return recommendationRule
}

Expand All @@ -572,8 +580,10 @@ func UpsertRecommendationRule(recommendationRule *analysisv1alph1.Recommendation
return err
}

if !reflect.DeepEqual(recommendationRule.Spec, recommendationRuleExist.Spec) {
if !reflect.DeepEqual(recommendationRule.Spec, recommendationRuleExist.Spec) ||
reflect.DeepEqual(recommendationRule.Annotations, recommendationRuleExist.Annotations) {
recommendationRuleExist.Spec = recommendationRule.Spec
recommendationRuleExist.Annotations = recommendationRule.Annotations
return client.Update(context.TODO(), recommendationRuleExist)
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/recommendation/recommendation_rule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ func (c *RecommendationRuleController) doReconcile(ctx context.Context, recommen
opts := []client.ListOption{
client.MatchingLabels(map[string]string{known.RecommendationRuleUidLabel: string(recommendationRule.UID)}),
}
if convert, uid := IsConvertFromAnalytics(recommendationRule); convert {
opts = []client.ListOption{
client.MatchingLabels(map[string]string{known.AnalyticsUidLabel: uid}),
}
}

err = c.Client.List(ctx, &currRecommendations, opts...)
if err != nil {
c.Recorder.Event(recommendationRule, corev1.EventTypeWarning, "FailedSelectResource", err.Error())
Expand Down Expand Up @@ -380,6 +386,9 @@ func CreateRecommendationObject(recommendationRule *analysisv1alph1.Recommendati
if id.Namespace != "" {
namespace = id.Namespace
}
if convert, _ := IsConvertFromAnalytics(recommendationRule); convert {
namespace = known.CraneSystemNamespace
}

recommendation := &analysisv1alph1.Recommendation{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -398,6 +407,9 @@ func CreateRecommendationObject(recommendationRule *analysisv1alph1.Recommendati
labels := map[string]string{}
labels[known.RecommendationRuleNameLabel] = recommendationRule.Name
labels[known.RecommendationRuleUidLabel] = string(recommendationRule.UID)
if convert, uid := IsConvertFromAnalytics(recommendationRule); convert {
labels[known.AnalyticsUidLabel] = uid
}
labels[known.RecommendationRuleRecommenderLabel] = recommenderName
labels[known.RecommendationRuleTargetKindLabel] = target.Kind
labels[known.RecommendationRuleTargetVersionLabel] = target.GroupVersionKind().Version
Expand Down Expand Up @@ -485,3 +497,13 @@ func executeMission(ctx context.Context, wg *sync.WaitGroup, recommenderMgr reco
mission.APIVersion = recommendation.APIVersion
}
}

func IsConvertFromAnalytics(recommendationRule *analysisv1alph1.RecommendationRule) (bool, string) {
if recommendationRule.Annotations != nil {
if uid, exist := recommendationRule.Annotations[known.AnalyticsConversionAnnotation]; exist {
return true, uid
}
}

return false, ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *RecommendationTriggerController) Reconcile(ctx context.Context, req ctr

recommendationRuleRef := utils.GetRecommendationRuleOwnerReference(recommendation)
if recommendationRuleRef == nil {
klog.Warningf("cannot found referred recommendation rule %s/%s", recommendation.Namespace, recommendationRuleRef.Name)
klog.Warning("cannot found referred recommendation rule")
return ctrl.Result{}, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/known/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
ReplicasRecommendationValueAnnotation = "analysis.crane.io/replicas-recommendation"
ResourceRecommendationValueAnnotation = "analysis.crane.io/resource-recommendation"
RunNumberAnnotation = "analysis.crane.io/run-number"
AnalyticsConversionAnnotation = "analysis.crane.io/analytics-conversion"
)

const (
Expand Down
64 changes: 34 additions & 30 deletions pkg/metrics/metric_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (

"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/api/meta"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/scale"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

autoscalingapi "github.com/gocrane/api/autoscaling/v1alpha1"
predictionapi "github.com/gocrane/api/prediction/v1alpha1"

"github.com/gocrane/crane/pkg/features"
. "github.com/gocrane/crane/pkg/metricprovider"
)

Expand Down Expand Up @@ -79,42 +81,44 @@ func (c *CraneMetricCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (c *CraneMetricCollector) Collect(ch chan<- prometheus.Metric) {
var ehpaList autoscalingapi.EffectiveHorizontalPodAutoscalerList
err := c.List(context.TODO(), &ehpaList)
if err != nil {
klog.Errorf("Failed to list ehpa: %v", err)
}
var predictionMetrics []PredictionMetric
for _, ehpa := range ehpaList.Items {
namespace := ehpa.Namespace
if ehpa.Spec.Prediction != nil {
var tsp predictionapi.TimeSeriesPrediction
tspName := "ehpa-" + ehpa.Name
if utilfeature.DefaultFeatureGate.Enabled(features.CraneAutoscaling) {
var ehpaList autoscalingapi.EffectiveHorizontalPodAutoscalerList
err := c.List(context.TODO(), &ehpaList)
if err != nil {
klog.Errorf("Failed to list ehpa: %v", err)
}
var predictionMetrics []PredictionMetric
for _, ehpa := range ehpaList.Items {
namespace := ehpa.Namespace
if ehpa.Spec.Prediction != nil {
var tsp predictionapi.TimeSeriesPrediction
tspName := "ehpa-" + ehpa.Name

err := c.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: tspName}, &tsp)
if err != nil {
klog.Errorf("Failed to get tsp: %v", err)
return
}
metricListTsp := c.getMetricsTsp(&tsp)
for _, metric := range metricListTsp {
if MetricContains(predictionMetrics, metric) {
continue
err := c.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: tspName}, &tsp)
if err != nil {
klog.Errorf("Failed to get tsp: %v", err)
return
}
metricListTsp := c.getMetricsTsp(&tsp)
for _, metric := range metricListTsp {
if MetricContains(predictionMetrics, metric) {
continue
}

ch <- prometheus.NewMetricWithTimestamp(metric.Timestamp, prometheus.MustNewConstMetric(metric.Desc, prometheus.GaugeValue, metric.MetricValue, metric.TargetKind, metric.TargetName, metric.TargetNamespace, metric.ResourceIdentifier, metric.Algorithm))
predictionMetrics = append(predictionMetrics, metric)
ch <- prometheus.NewMetricWithTimestamp(metric.Timestamp, prometheus.MustNewConstMetric(metric.Desc, prometheus.GaugeValue, metric.MetricValue, metric.TargetKind, metric.TargetName, metric.TargetNamespace, metric.ResourceIdentifier, metric.Algorithm))
predictionMetrics = append(predictionMetrics, metric)
}
}
}

if ehpa.Spec.Crons != nil {
metricCron, err := c.getMetricsCron(&ehpa)
if err != nil {
klog.Errorf("Failed to get metricCron: %v", err)
return
}
if ehpa.Spec.Crons != nil {
metricCron, err := c.getMetricsCron(&ehpa)
if err != nil {
klog.Errorf("Failed to get metricCron: %v", err)
return
}

ch <- metricCron
ch <- metricCron
}
}
}
}
Expand Down

0 comments on commit 9efc321

Please sign in to comment.