Skip to content

Commit

Permalink
fix: 代码规范化
Browse files Browse the repository at this point in the history
Signed-off-by: Super-zhw <1458070668@qq.com>
  • Loading branch information
Super-zhw committed Aug 16, 2021
1 parent a959b14 commit 65ed0ec
Show file tree
Hide file tree
Showing 23 changed files with 602 additions and 266 deletions.
5 changes: 4 additions & 1 deletion bcs-k8s/bcs-general-pod-autoscaler/cmd/gpa/app/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"github.com/Tencent/bk-bcs/bcs-k8s/bcs-general-pod-autoscaler/pkg/apis/config/v1alpha1"
)

func RecommendedDefaultGPAControllerConfiguration(obj *v1alpha1.GPAControllerConfiguration) {
// RecommendDefaultGPAControllerConfig 原方法名 RecommendedDefaultGPAControllerConfiguration
//
// RecommendDefaultGPAControllerConfig recommended default GPA controller configuration
func RecommendDefaultGPAControllerConfig(obj *v1alpha1.GPAControllerConfiguration) {
zero := metav1.Duration{}
if obj.GeneralPodAutoscalerSyncPeriod == zero {
obj.GeneralPodAutoscalerSyncPeriod = metav1.Duration{Duration: 15 * time.Second}
Expand Down
56 changes: 43 additions & 13 deletions bcs-k8s/bcs-general-pod-autoscaler/cmd/gpa/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/Tencent/bk-bcs/bcs-k8s/bcs-general-pod-autoscaler/pkg/apis/config/v1alpha1"
)

// RunOptions run options
type RunOptions struct {
KubeconfigPath string
MasterUrl string
Expand All @@ -34,45 +35,74 @@ type RunOptions struct {
*v1alpha1.GPAControllerConfiguration
}

// NewServerRunOptions new server run options
func NewServerRunOptions() *RunOptions {
options := &RunOptions{GPAControllerConfiguration: &v1alpha1.GPAControllerConfiguration{}}
options.addKubeFlags()
options.addElectionFlags()
options.addGPAFlags()
RecommendedDefaultGPAControllerConfiguration(options.GPAControllerConfiguration)
RecommendDefaultGPAControllerConfig(options.GPAControllerConfiguration)
return options
}

func (s *RunOptions) addKubeFlags() {
pflag.DurationVar(&s.Resync, "resync", 10*time.Minute, "Time to resync from apiserver.")
pflag.StringVar(&s.KubeconfigPath, "kubeconfig-path", "", "Absolute path to the kubeconfig file.")
pflag.StringVar(&s.KubeconfigPath,
"kubeconfig-path", "", "Absolute path to the kubeconfig file.")
pflag.StringVar(&s.MasterUrl, "master", "", "Master url.")
pflag.IntVar(&s.QPS, "qps", 100, "qps of auto scaler.")
pflag.IntVar(&s.Burst, "burst", 200, "burst of auto scaler.")
}

func (s *RunOptions) addElectionFlags() {
pflag.StringVar(&s.ElectionName, "election-name", "general-podautoscaler", "election name.")
pflag.StringVar(&s.ElectionNamespace, "election-namespace", "kube-system", "election namespace.")
pflag.StringVar(&s.ElectionResourceLock, "election-resource-lock", "leases", "election resource type, support endoints, leases, configmaps and so on.")
pflag.StringVar(&s.ElectionNamespace,
"election-namespace", "kube-system", "election namespace.")
pflag.StringVar(&s.ElectionResourceLock,
"election-resource-lock",
"leases",
"election resource type, support endoints, leases, configmaps and so on.")
}

// AddFlags adds flags related to GPAController for controller manager to the specified FlagSet.
// addGPAFlags addFlags adds flags related to GPAController for controller manager to the specified FlagSet
func (o *RunOptions) addGPAFlags() {
if o == nil {
return
}

pflag.DurationVar(&o.GeneralPodAutoscalerSyncPeriod.Duration, "general-pod-autoscaler-sync-period", o.GeneralPodAutoscalerSyncPeriod.Duration, "The period for syncing the number of pods in general pod autoscaler.")
pflag.DurationVar(&o.GeneralPodAutoscalerUpscaleForbiddenWindow.Duration, "general-pod-autoscaler-upscale-delay", o.GeneralPodAutoscalerUpscaleForbiddenWindow.Duration, "The period since last upscale, before another upscale can be performed in general pod autoscaler.")
pflag.DurationVar(&o.GeneralPodAutoscalerDownscaleStabilizationWindow.Duration, "general-pod-autoscaler-downscale-stabilization", o.GeneralPodAutoscalerDownscaleStabilizationWindow.Duration, "The period for which autoscaler will look backwards and not scale down below any recommendation it made during that period.")
pflag.DurationVar(&o.GeneralPodAutoscalerDownscaleForbiddenWindow.Duration, "general-pod-autoscaler-downscale-delay", o.GeneralPodAutoscalerDownscaleForbiddenWindow.Duration, "The period since last downscale, before another downscale can be performed in general pod autoscaler.")
pflag.Float64Var(&o.GeneralPodAutoscalerTolerance, "general-pod-autoscaler-tolerance", o.GeneralPodAutoscalerTolerance, "The minimum change (from 1.0) in the desired-to-actual metrics ratio for the general pod autoscaler to consider scaling.")
pflag.BoolVar(&o.GeneralPodAutoscalerUseRESTClients, "general-pod-autoscaler-use-rest-clients", o.GeneralPodAutoscalerUseRESTClients, "If set to true, causes the general pod autoscaler controller to use REST clients through the kube-aggregator, instead of using the legacy metrics client through the API server proxy. This is required for custom metrics support in the general pod autoscaler.")
pflag.DurationVar(&o.GeneralPodAutoscalerCPUInitializationPeriod.Duration, "general-pod-autoscaler-cpu-initialization-period", o.GeneralPodAutoscalerCPUInitializationPeriod.Duration, "The period after pod start when CPU samples might be skipped.")
pflag.DurationVar(&o.GeneralPodAutoscalerInitialReadinessDelay.Duration, "general-pod-autoscaler-initial-readiness-delay", o.GeneralPodAutoscalerInitialReadinessDelay.Duration, "The period after pod start during which readiness changes will be treated as initial readiness.")
pflag.DurationVar(&o.GeneralPodAutoscalerSyncPeriod.Duration,
"general-pod-autoscaler-sync-period", o.GeneralPodAutoscalerSyncPeriod.Duration,
"The period for syncing the number of pods in general pod autoscaler.")
pflag.DurationVar(&o.GeneralPodAutoscalerUpscaleForbiddenWindow.Duration,
"general-pod-autoscaler-upscale-delay", o.GeneralPodAutoscalerUpscaleForbiddenWindow.Duration,
"The period since last upscale, before another upscale can be performed in general pod autoscaler.")
pflag.DurationVar(&o.GeneralPodAutoscalerDownscaleStabilizationWindow.Duration,
"general-pod-autoscaler-downscale-stabilization",
o.GeneralPodAutoscalerDownscaleStabilizationWindow.Duration,
"The period for which autoscaler will look backwards and not scale down below any recommendation it made during that period.")
pflag.DurationVar(
&o.GeneralPodAutoscalerDownscaleForbiddenWindow.Duration,
"general-pod-autoscaler-downscale-delay",
o.GeneralPodAutoscalerDownscaleForbiddenWindow.Duration,
"The period since last downscale, before another downscale can be performed in general pod autoscaler.")
pflag.Float64Var(&o.GeneralPodAutoscalerTolerance,
"general-pod-autoscaler-tolerance", o.GeneralPodAutoscalerTolerance,
"The minimum change (from 1.0) in the desired-to-actual metrics ratio for the general pod autoscaler to consider scaling.")
pflag.BoolVar(&o.GeneralPodAutoscalerUseRESTClients,
"general-pod-autoscaler-use-rest-clients",
o.GeneralPodAutoscalerUseRESTClients,
"If set to true, causes the general pod autoscaler controller to use REST clients through the kube-aggregator, instead of using the legacy metrics client through the API server proxy. This is required for custom metrics support in the general pod autoscaler.")
pflag.DurationVar(&o.GeneralPodAutoscalerCPUInitializationPeriod.Duration,
"general-pod-autoscaler-cpu-initialization-period",
o.GeneralPodAutoscalerCPUInitializationPeriod.Duration,
"The period after pod start when CPU samples might be skipped.")
pflag.DurationVar(&o.GeneralPodAutoscalerInitialReadinessDelay.Duration,
"general-pod-autoscaler-initial-readiness-delay",
o.GeneralPodAutoscalerInitialReadinessDelay.Duration,
"The period after pod start during which readiness changes will be treated as initial readiness.")
}

// NewConfig new config
func (s *RunOptions) NewConfig() (*rest.Config, error) {
var (
config *rest.Config
Expand Down
2 changes: 1 addition & 1 deletion bcs-k8s/bcs-general-pod-autoscaler/cmd/gpa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func main() {
)
coreFactory.Start(stop)
scalerFactory.Start(stop)
ctx, cancel := context.WithCancel(context.TODO()) // TODO once Run() accepts a context, it should be used here
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

go func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ServerRunOptions struct {
AllowDescheduleCount int
}

// NewServerRunOptions New Server Run Options
func NewServerRunOptions() *ServerRunOptions {
options := &ServerRunOptions{}
options.addFlags()
Expand All @@ -51,10 +52,11 @@ func (s *ServerRunOptions) addFlags() {
pflag.BoolVar(&s.ShowVersion, "version", false, "Show version.")
}

// Validate validate
func (s *ServerRunOptions) Validate() error {
address := net.ParseIP(s.Address)
if address.To4() == nil {
return fmt.Errorf("%v is not a valid IP address\n", s.Address)
return fmt.Errorf("%v is not a valid IP address", s.Address)
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/Tencent/bk-bcs/bcs-k8s/bcs-general-pod-autoscaler/pkg/validator"
)

// Run run
func Run(s *ServerRunOptions) error {
stopCh := util.SetupSignalHandler()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type AutoScalingDrivenMode struct {
EventMode *EventMode `json:"event,omitempty" protobuf:"bytes,4,opt,name=event"`
}

// MetricMode metric mode 指标模式
type MetricMode struct {
// metrics contains the specifications for which to use to calculate the
// desired replica count (the maximum replica count across all metrics will
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (

var heapsterQueryStart = -5 * time.Minute

// HeapsterMetricsClient heapster metrics client
type HeapsterMetricsClient struct {
services v1core.ServiceInterface
podsGetter v1core.PodsGetter
Expand All @@ -48,6 +49,7 @@ type HeapsterMetricsClient struct {
heapsterPort string
}

// NewHeapsterMetricsClient New Heapster Metrics Client
func NewHeapsterMetricsClient(client clientset.Interface, namespace, scheme, service, port string) MetricsClient {
return &HeapsterMetricsClient{
services: client.CoreV1().Services(namespace),
Expand All @@ -58,6 +60,7 @@ func NewHeapsterMetricsClient(client clientset.Interface, namespace, scheme, ser
}
}

// GetResourceMetric Get Resource Metric
func (h *HeapsterMetricsClient) GetResourceMetric(resource v1.ResourceName, namespace string, selector labels.Selector, container string) (PodMetricsInfo, time.Time, error) {
metricPath := fmt.Sprintf("/apis/metrics/v1alpha1/namespaces/%s/pods", namespace)
params := map[string]string{"labelSelector": selector.String()}
Expand Down Expand Up @@ -110,6 +113,7 @@ func (h *HeapsterMetricsClient) GetResourceMetric(resource v1.ResourceName, name
return res, timestamp, nil
}

// GetRawMetric Get Raw Metric
func (h *HeapsterMetricsClient) GetRawMetric(metricName string, namespace string, selector labels.Selector, metricSelector labels.Selector) (PodMetricsInfo, time.Time, error) {
podList, err := h.podsGetter.Pods(namespace).List(metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
Expand Down Expand Up @@ -179,10 +183,16 @@ func (h *HeapsterMetricsClient) GetRawMetric(metricName string, namespace string
return res, *timestamp, nil
}

func (h *HeapsterMetricsClient) GetObjectMetric(metricName string, namespace string, objectRef *autoscaling.CrossVersionObjectReference, metricSelector labels.Selector) (int64, time.Time, error) {
// GetObjectMetric Get Object Metric
func (h *HeapsterMetricsClient) GetObjectMetric(
metricName string,
namespace string,
objectRef *autoscaling.CrossVersionObjectReference,
metricSelector labels.Selector) (int64, time.Time, error) {
return 0, time.Time{}, fmt.Errorf("object metrics are not yet supported")
}

// GetExternalMetric Get External Metric
func (h *HeapsterMetricsClient) GetExternalMetric(metricName, namespace string, selector labels.Selector) ([]int64, time.Time, error) {
return nil, time.Time{}, fmt.Errorf("external metrics aren't supported")
}
Expand Down Expand Up @@ -213,9 +223,8 @@ func collapseTimeSamples(metrics heapster.MetricResult, duration time.Duration)

if newest.FloatValue != nil {
return int64(floatSum / float64(floatSumCount) * 1000), newest.Timestamp, true
} else {
return (intSum * 1000) / int64(intSumCount), newest.Timestamp, true
}
return (intSum * 1000) / int64(intSumCount), newest.Timestamp, true
}

return 0, time.Time{}, false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"fmt"
"time"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -34,7 +34,11 @@ const (
metricServerDefaultMetricWindow = time.Minute
)

func NewRESTMetricsClient(resourceClient resourceclient.PodMetricsesGetter, customClient customclient.CustomMetricsClient, externalClient externalclient.ExternalMetricsClient) MetricsClient {
// NewRESTMetricsClient New REST Metrics Client
func NewRESTMetricsClient(
resourceClient resourceclient.PodMetricsesGetter,
customClient customclient.CustomMetricsClient,
externalClient externalclient.ExternalMetricsClient) MetricsClient {
return &restMetricsClient{
&resourceMetricsClient{resourceClient},
&customMetricsClient{customClient},
Expand Down Expand Up @@ -172,7 +176,11 @@ func (c *customMetricsClient) GetRawMetric(metricName string, namespace string,

// GetObjectMetric gets the given metric (and an associated timestamp) for the given
// object in the given namespace
func (c *customMetricsClient) GetObjectMetric(metricName string, namespace string, objectRef *autoscaling.CrossVersionObjectReference, metricSelector labels.Selector) (int64, time.Time, error) {
func (c *customMetricsClient) GetObjectMetric(
metricName string,
namespace string,
objectRef *autoscaling.CrossVersionObjectReference,
metricSelector labels.Selector) (int64, time.Time, error) {
gvk := schema.FromAPIVersionAndKind(objectRef.APIVersion, objectRef.Kind)
var metricValue *customapi.MetricValue
var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import (
// GetResourceUtilizationRatio takes in a set of metrics, a set of matching requests,
// and a target utilization percentage, and calculates the ratio of
// desired to actual utilization (returning that, the actual utilization, and the raw average value)
func GetResourceUtilizationRatio(metrics PodMetricsInfo, requests map[string]int64, targetUtilization int32) (utilizationRatio float64, currentUtilization int32, rawAverageValue int64, err error) {
func GetResourceUtilizationRatio(
metrics PodMetricsInfo,
requests map[string]int64,
targetUtilization int32) (utilizationRatio float64, currentUtilization int32, rawAverageValue int64, err error) {
metricsTotal := int64(0)
requestsTotal := int64(0)
numEntries := 0
Expand Down
Loading

0 comments on commit 65ed0ec

Please sign in to comment.