Skip to content

Commit

Permalink
Merge pull request OpenFunction#476 from tpiperatgod/issue-474
Browse files Browse the repository at this point in the history
feat: Upgrade KEDA to v2.10.1 and HPA(autoscaling) api version to v2
  • Loading branch information
benjaminhuo committed Aug 10, 2023
2 parents bf10845 + 9081aec commit 41e6747
Show file tree
Hide file tree
Showing 45 changed files with 41,866 additions and 966 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,28 @@ jobs:
GO111MODULE: "on"
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.18.x
go-version: 1.19

- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Check License Header
uses: apache/skywalking-eyes/header@501a28d2fb4a9b962661987e50cf0219631b32ff

- name: Install kubebuilder-3.1.0
- name: Install kubebuilder-3.11.1
run: |
curl -L -o kubebuilder "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v3.1.0/kubebuilder_linux_amd64"
curl -L -o kubebuilder "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v3.11.1/kubebuilder_linux_amd64"
chmod +x kubebuilder && mv kubebuilder /usr/local/bin/
- name: Run basic test
run: make test

Expand All @@ -77,26 +78,29 @@ jobs:

e2e_test:
runs-on: ubuntu-latest
timeout-minutes: 30
timeout-minutes: 60
name: E2E Tests
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.18.x
go-version: 1.19

- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Create kind cluster
uses: container-tools/kind-action@v1
with:
version: v0.20.0
node_image: kindest/node:v1.26.6

- name: Create busybox pod
run: |
Expand Down Expand Up @@ -141,7 +145,7 @@ jobs:
- name: Install OpenFunction
run: |
kubectl apply -f config/bundle.yaml
kubectl apply --server-side=true -f config/bundle.yaml
kubectl apply -f config/strategy/build-strategy.yaml
kubectl patch deployments.apps -n openfunction openfunction-controller-manager -p "{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"openfunction\",\"image\":\"kind-registry:5000/openfunction/openfunction:latest\",\"resources\":null}]}}}}"
kubectl rollout status -n openfunction deployment openfunction-controller-manager --timeout=1m
Expand Down
39 changes: 23 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ IMG ?= openfunction/openfunction:$(VERSION)
IMG_DEV ?= openfunctiondev/openfunction:$(VERSION)
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
#CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
CRD_OPTIONS ?= "crd:preserveUnknownFields=false,maxDescLen=0"
CRD_OPTIONS ?= "crd"

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.26.0

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -55,9 +58,9 @@ help: ## Display this help.

##@ Development

manifests: generate fmt vet controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
manifests: kustomize generate fmt vet controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role paths="./..." output:crd:artifacts:config=config/crd/bases
kubectl kustomize config/default | sed -e '/creationTimestamp: null/d' | sed -e 's/openfunction-system/openfunction/g' | sed -e 's/openfunction\:latest/openfunction\:$(VERSION)/g' | sed -e 's/app.kubernetes.io\/version\: latest/app.kubernetes.io\/version\: $(VERSION)/g' > config/bundle.yaml
$(KUSTOMIZE) build config/default | sed -e '/creationTimestamp: null/d' | sed -e 's/openfunction-system/openfunction/g' | sed -e 's/openfunction\:latest/openfunction\:$(VERSION)/g' | sed -e 's/app.kubernetes.io\/version\: latest/app.kubernetes.io\/version\: $(VERSION)/g' > config/bundle.yaml
cat config/configmap/openfunction-config.yaml >> config/bundle.yaml

generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
Expand All @@ -77,11 +80,15 @@ goimports:
vet: ## Run go vet against code.
go vet ./...

ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./pkg/... ./controllers/... -coverprofile cover.out
.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(shell pwd)/bin -p path)" go test ./... -coverprofile cover.out

ENVTEST = $(shell pwd)/bin/setup-envtest
.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST):
test -s $(shell pwd)/bin/setup-envtest || GOBIN=$(shell pwd)/bin go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

verify: verify-crds

Expand Down Expand Up @@ -116,20 +123,20 @@ docker-push-dev: ## Push dev docker image with the openfunction.
##@ Deployment

install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl create -f -
$(KUSTOMIZE) build config/crd | kubectl apply --server-side=true --force-conflicts -f -

uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl delete -f -

deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl apply -f -
$(KUSTOMIZE) build config/default | kubectl apply --server-side=true --force-conflicts -f -

undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -

dev:
kubectl kustomize config/default | sed -e '/creationTimestamp/d' | sed -e 's/openfunction-system/openfunction/g' | sed -e 's/openfunction\/openfunction/openfunctiondev\/openfunction/g' > config/bundle.yaml
kubectl kustomize config/samples/ | sed -e 's/openfunction\/sample-go-func/openfunctiondev\/sample-go-func/g' > config/samples/function-sample-dev.yaml
dev: kustomize
$(KUSTOMIZE) config/default | sed -e '/creationTimestamp/d' | sed -e 's/openfunction-system/openfunction/g' | sed -e 's/openfunction\/openfunction/openfunctiondev\/openfunction/g' > config/bundle.yaml
$(KUSTOMIZE) config/samples/ | sed -e 's/openfunction\/sample-go-func/openfunctiondev\/sample-go-func/g' > config/samples/function-sample-dev.yaml

clean:
git checkout config/bundle.yaml
Expand All @@ -155,15 +162,15 @@ e2e-events: skywalking-e2e yq

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.11.1)

KUSTOMIZE = $(shell pwd)/bin/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v4@v4.5.2)
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v4@v4.5.7)

E2E = $(shell pwd)/bin/e2e
skywalking-e2e: ## Download skywalking-e2e locally if necessary.
$(call go-get-tool,$(E2E),github.com/apache/skywalking-infra-e2e/cmd/e2e@2a33478)
$(call go-get-tool,$(E2E),github.com/apache/skywalking-infra-e2e/cmd/e2e@v1.2.1-0.20230623134912-0a5b398fc966)

YQ = $(shell pwd)/bin/yq
yq: ## Download yq locally if necessary.
Expand Down
4 changes: 2 additions & 2 deletions apis/core/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ limitations under the License.
*/

// Package v1beta1 contains API Schema definitions for the core v1beta1 API group
//+kubebuilder:object:generate=true
//+groupName=core.openfunction.io
// +kubebuilder:object:generate=true
// +groupName=core.openfunction.io
package v1beta1

import (
Expand Down
2 changes: 1 addition & 1 deletion apis/core/v1beta1/serving_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package v1beta1

import (
componentsv1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1"
kedav1alpha1 "github.com/kedacore/keda/v2/api/v1alpha1"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
4 changes: 2 additions & 2 deletions apis/core/v1beta1/zz_generated.deepcopy.go

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

54 changes: 39 additions & 15 deletions apis/core/v1beta2/function_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
"regexp"
"strings"

"github.com/openfunction/pkg/constants"

shipwrightv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"k8s.io/api/autoscaling/v2beta2"
autoscalingv2 "k8s.io/api/autoscaling/v2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/openfunction/pkg/constants"
)

//var (
Expand Down Expand Up @@ -65,15 +65,15 @@ var (
v1.RestartPolicyNever: true,
}
kedaScaledJobRestartPolicesSlice = convertMapKeysToStringSlice(kedaScaledJobRestartPolices)
scalingPolicySelects = map[v2beta2.ScalingPolicySelect]bool{
v2beta2.MaxPolicySelect: true,
v2beta2.MinPolicySelect: true,
v2beta2.DisabledPolicySelect: true,
scalingPolicySelects = map[autoscalingv2.ScalingPolicySelect]bool{
autoscalingv2.MaxChangePolicySelect: true,
autoscalingv2.MinChangePolicySelect: true,
autoscalingv2.DisabledPolicySelect: true,
}
scalingPolicySelectsSlice = convertMapKeysToStringSlice(scalingPolicySelects)
HPAScalingPolicyTypes = map[v2beta2.HPAScalingPolicyType]bool{
v2beta2.PodsScalingPolicy: true,
v2beta2.PercentScalingPolicy: true,
HPAScalingPolicyTypes = map[autoscalingv2.HPAScalingPolicyType]bool{
autoscalingv2.PercentScalingPolicy: true,
autoscalingv2.PodsScalingPolicy: true,
}
HPAScalingPolicyTypesSlice = convertMapKeysToStringSlice(HPAScalingPolicyTypes)
kedaScaledJobScalingStrategies = map[string]bool{"default": true, "custom": true, "accurate": true}
Expand Down Expand Up @@ -143,7 +143,7 @@ func (r *Function) Default() {
r.HandleWorkloadRuntime()
}

func (r Function) HandleWorkloadRuntime() {
func (r *Function) HandleWorkloadRuntime() {
if r.Annotations == nil {
r.Annotations = make(map[string]string)
}
Expand Down Expand Up @@ -331,6 +331,11 @@ func (r *Function) ValidateServing() error {
return err
}
}
if scaledObject.Fallback != nil {
if err := r.ValidateKedaScaledObjectFallback(); err != nil {
return err
}
}
}
if scaleOptions.Keda.ScaledJob != nil {
scaleJob := scaleOptions.Keda.ScaledJob
Expand Down Expand Up @@ -448,6 +453,25 @@ func (r *Function) ValidateKedaScaledObjectAdvanced() error {
return nil
}

func (r *Function) ValidateKedaScaledObjectFallback() error {
fallback := r.Spec.Serving.ScaleOptions.Keda.ScaledObject.Fallback
if fallback != nil {
if fallback.FailureThreshold < 1 {
return field.Invalid(field.NewPath("spec", "serving", "scaleOptions",
"keda", "scaleObject", "fallback", "failureThreshold"),
fallback.FailureThreshold,
"must be greater than or equal to 1")
}
if fallback.Replicas < 0 {
return field.Invalid(field.NewPath("spec", "serving", "scaleOptions",
"keda", "scaleObject", "fallback", "replicas"),
fallback.Replicas,
"must be greater than or equal to 0")
}
}
return nil
}

func (r *Function) ValidateKedaScaledJobScalingStrategy() error {
strategy := r.Spec.Serving.ScaleOptions.Keda.ScaledJob.ScalingStrategy
if strategy.Strategy != "" {
Expand Down Expand Up @@ -499,10 +523,10 @@ func convertMapKeysToStringSlice(m interface{}) []string {
keys = append(keys, string(key.Interface().(shipwrightv1alpha1.BuildStrategyKind)))
case v1.RestartPolicy:
keys = append(keys, string(key.Interface().(v1.RestartPolicy)))
case v2beta2.ScalingPolicySelect:
keys = append(keys, string(key.Interface().(v2beta2.ScalingPolicySelect)))
case v2beta2.HPAScalingPolicyType:
keys = append(keys, string(key.Interface().(v2beta2.HPAScalingPolicyType)))
case autoscalingv2.ScalingPolicySelect:
keys = append(keys, string(key.Interface().(autoscalingv2.ScalingPolicySelect)))
case autoscalingv2.HPAScalingPolicyType:
keys = append(keys, string(key.Interface().(autoscalingv2.HPAScalingPolicyType)))
}
}
return keys
Expand Down
Loading

0 comments on commit 41e6747

Please sign in to comment.