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

合并ocgi/general-pod-autoscaler到bk-bcs/bcs-k8s/bcs-general-pod-autoscaler #960

Merged
merged 16 commits into from
Sep 1, 2021
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
25 changes: 25 additions & 0 deletions bcs-k8s/bcs-general-pod-autoscaler/.github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Go

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15

- name: Build
run: make build

- name: Test
run: make test
138 changes: 138 additions & 0 deletions bcs-k8s/bcs-general-pod-autoscaler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# OSX leaves these everywhere on SMB shares
._*

# OSX trash
.DS_Store

# Eclipse files
.classpath
.project
.settings/**

# Files generated by JetBrains IDEs, e.g. IntelliJ IDEA
.idea/
*.iml

# Makefile temporary files
Makefile.tmp

# Vscode files
.vscode

# This is where the result of the go build goes
/output*/
/_output*/
/_output

# Emacs save files
*~
\#*\#
.\#*

# Vim-related files
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist

# cscope-related files
cscope.*

# Go test binaries
*.test
/hack/.test-cmd-auth

# JUnit test output from ginkgo e2e tests
/junit*.xml

# Mercurial files
**/.hg
**/.hg*

# Vagrant
.vagrant
network_closure.sh

# Local cluster env variables
/cluster/env.sh

# Compiled binaries in third_party
/third_party/pkg

# Also ignore etcd installed by hack/install-etcd.sh
/third_party/etcd*
/default.etcd

# User cluster configs
.kubeconfig

.tags*

# Version file for dockerized build
.dockerized-kube-version-defs

# Web UI
/www/master/node_modules/
/www/master/npm-debug.log
/www/master/shared/config/development.json

# Karma output
/www/test_out

# precommit temporary directories created by ./hack/verify-generated-docs.sh and ./hack/lib/util.sh
/_tmp/
/doc_tmp/

# Test artifacts produced by Jenkins jobs
/_artifacts/

# Go dependencies installed on Jenkins
/_gopath/

# Config directories created by gcloud and gsutil on Jenkins
/.config/gcloud*/
/.gsutil/

# CoreOS stuff
/cluster/libvirt-coreos/coreos_*.img

# Juju Stuff
/cluster/juju/charms/*
/cluster/juju/bundles/local.yaml

# Downloaded Kubernetes binary release
/kubernetes/

# direnv .envrc files
.envrc

# Downloaded kubernetes binary release tar ball
kubernetes.tar.gz

# generated files in any directory
# TODO(thockin): uncomment this when we stop committing the generated files.
#zz_generated.*
zz_generated.openapi.go

# make-related metadata
/.make/
# Just in time generated data in the source, should never be commited
/test/e2e/generated/bindata.go

# This file used by some vendor repos (e.g. github.com/go-openapi/...) to store secret variables and should not be ignored
!\.drone\.sec

# Godeps workspace
/Godeps/_workspace

/bazel-*
*.pyc

# used for the code generators only
/vendor/

# ignore binary
/bin/

./idea/
51 changes: 51 additions & 0 deletions bcs-k8s/bcs-general-pod-autoscaler/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# you need to fill in the image url yourself
REGISTRY_NAME=docker.io/xxx
GIT_COMMIT=$(shell git rev-parse "HEAD^{commit}")
VERSION=$(shell git describe --tags --abbrev=14 "${GIT_COMMIT}^{commit}" --always)
BUILD_TIME=$(shell TZ=Asia/Shanghai date +%FT%T%z)
VERSION_KEY=github.com/Tencent/bk-bcs/bcs-k8s/bcs-general-pod-autoscaler/pkg/version.Version
COMMIT_KEY=github.com/Tencent/bk-bcs/bcs-k8s/bcs-general-pod-autoscaler/pkg/version.Commit

CMDS=build
all: test build

build: vet fmt build-gpa

build-gpa:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-X '$(VERSION_KEY)=$(VERSION)' -X '$(COMMIT_KEY)=$(GIT_COMMIT)'" -o ./bin/gpa ./cmd/gpa

container: build
docker build -t $(REGISTRY_NAME)/gpa:$(VERSION) -f $(shell if [ -e ./cmd/gpa/Dockerfile ]; then echo ./cmd/gpa/Dockerfile; else echo Dockerfile; fi) --label revision=$(REV) .

push: container
docker push $(REGISTRY_NAME)/gpa:$(VERSION)

test:
go test ./pkg/...

autogen:
go mod vendor
bash hack/update-codegen.sh

vet:
go vet ./pkg/...

fmt:
go fmt ./pkg/...

clean:
rm -r ./bin
49 changes: 49 additions & 0 deletions bcs-k8s/bcs-general-pod-autoscaler/cmd/gpa/app/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under,
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

package app

import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/Tencent/bk-bcs/bcs-k8s/bcs-general-pod-autoscaler/pkg/apis/config/v1alpha1"
)

// 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}
}
if obj.GeneralPodAutoscalerUpscaleForbiddenWindow == zero {
obj.GeneralPodAutoscalerUpscaleForbiddenWindow = metav1.Duration{Duration: 3 * time.Minute}
}
if obj.GeneralPodAutoscalerDownscaleStabilizationWindow == zero {
obj.GeneralPodAutoscalerDownscaleStabilizationWindow = metav1.Duration{Duration: 5 * time.Minute}
}
if obj.GeneralPodAutoscalerCPUInitializationPeriod == zero {
obj.GeneralPodAutoscalerCPUInitializationPeriod = metav1.Duration{Duration: 5 * time.Minute}
}
if obj.GeneralPodAutoscalerInitialReadinessDelay == zero {
obj.GeneralPodAutoscalerInitialReadinessDelay = metav1.Duration{Duration: 30 * time.Second}
}
if obj.GeneralPodAutoscalerDownscaleForbiddenWindow == zero {
obj.GeneralPodAutoscalerDownscaleForbiddenWindow = metav1.Duration{Duration: 5 * time.Minute}
}
if obj.GeneralPodAutoscalerTolerance == 0 {
obj.GeneralPodAutoscalerTolerance = 0.1
}
}
121 changes: 121 additions & 0 deletions bcs-k8s/bcs-general-pod-autoscaler/cmd/gpa/app/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under,
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

package app

import (
"time"

"github.com/spf13/pflag"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"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
QPS int
Burst int
Resync time.Duration
ElectionName string
ElectionNamespace string
ElectionResourceLock string
*v1alpha1.GPAControllerConfiguration
}

// NewServerRunOptions new server run options
func NewServerRunOptions() *RunOptions {
options := &RunOptions{GPAControllerConfiguration: &v1alpha1.GPAControllerConfiguration{}}
options.addKubeFlags()
options.addElectionFlags()
options.addGPAFlags()
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.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", "bcs-system", "election namespace.")
pflag.StringVar(&s.ElectionResourceLock,
"election-resource-lock",
"leases",
"election resource type, support endoints, leases, configmaps and so on.")
}

// 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.")
}

// NewConfig new config
func (s *RunOptions) NewConfig() (*rest.Config, error) {
var (
config *rest.Config
err error
)
config, err = rest.InClusterConfig()
if err != nil {
config, err = clientcmd.BuildConfigFromFlags(s.MasterUrl, s.KubeconfigPath)
if err != nil {
return nil, err
}
}
config.Burst = s.Burst
config.QPS = float32(s.QPS)
return config, nil
}
Loading