-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support for multiple types of workloads #15529
Open
chenzhiguo
wants to merge
2
commits into
knative:main
Choose a base branch
from
chenzhiguo:pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2019 The Knative 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. | ||
*/ | ||
|
||
package autoscaling | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/tools/cache" | ||
autoscalingv1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" | ||
) | ||
|
||
// ScalerType classified by Workload type | ||
type ScalerType string | ||
|
||
func ScalerTypeFactoryByWR(wr WorkloadResource) ScalerType { | ||
return ScalerType(fmt.Sprintf("ApiVersion:%s/Kind:%s", wr.ApiVersion, wr.Kind)) | ||
} | ||
|
||
func ScalerTypeFactory(or v1.ObjectReference) ScalerType { | ||
return ScalerType(fmt.Sprintf("ApiVersion:%s/Kind:%s", or.APIVersion, or.Kind)) | ||
} | ||
|
||
type Scaler interface { | ||
ApplyScale(ctx context.Context, pa *autoscalingv1alpha1.PodAutoscaler, desiredScale int32) error | ||
} | ||
|
||
// ScalerFactory the Scaler factory to automatically generate Scaler objects. | ||
type ScalerFactory func(ctx context.Context, bs BaseScaler) (Scaler, error) | ||
|
||
// ScalerFactories is a factory holder for any ScalerType | ||
var ScalerFactories = make(map[ScalerType]ScalerFactory) | ||
|
||
// ScalerTypes is a scaler instance holder for any ScalerType | ||
var ScalerTypes = make(map[ScalerType]Scaler) | ||
|
||
// RegisterScalerType Inject the Scaler factory to automatically generate Scaler objects. | ||
func RegisterScalerType(key ScalerType, factory ScalerFactory) bool { | ||
if _, ok := ScalerFactories[key]; ok { | ||
return ok | ||
} else { | ||
ScalerFactories[key] = factory | ||
return ok | ||
} | ||
} | ||
|
||
type BaseScaler interface { | ||
// GetDynamicClient Get the default dynamic client | ||
GetDynamicClient() dynamic.Interface | ||
|
||
// GetListerFactory Get the ListerFactory function related to the corresponding workload. | ||
GetListerFactory() func(schema.GroupVersionResource) (cache.GenericLister, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ package kpa | |
import ( | ||
"context" | ||
"fmt" | ||
"k8s.io/client-go/dynamic" | ||
"knative.dev/serving/pkg/reconciler/autoscaling" | ||
"net/http" | ||
"time" | ||
|
||
|
@@ -43,7 +45,6 @@ import ( | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
|
@@ -372,5 +373,30 @@ func (ks *scaler) scale(ctx context.Context, pa *autoscalingv1alpha1.PodAutoscal | |
} | ||
|
||
logger.Infof("Scaling from %d to %d", currentScale, desiredScale) | ||
return desiredScale, ks.applyScale(ctx, pa, desiredScale, ps) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems you still have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it can be delete. |
||
|
||
// Operate different types of workloads. | ||
scaleType := autoscaling.ScalerTypeFactory(pa.Spec.ScaleTargetRef) | ||
if scaler, ok := autoscaling.ScalerTypes[scaleType]; ok { | ||
err = scaler.ApplyScale(ctx, pa, desiredScale) | ||
} else { | ||
if sff, ok := autoscaling.ScalerFactories[scaleType]; ok { | ||
s, err := sff(ctx, ks) | ||
if err != nil { | ||
return desiredScale, err | ||
} | ||
autoscaling.ScalerTypes[scaleType] = s | ||
return desiredScale, s.ApplyScale(ctx, pa, desiredScale) | ||
} | ||
} | ||
return desiredScale, err | ||
} | ||
|
||
// GetDynamicClient Get the default dynamic client | ||
func (ks *scaler) GetDynamicClient() dynamic.Interface { | ||
return ks.dynamicClient | ||
} | ||
|
||
// GetListerFactory Get the ListerFactory function related to the corresponding workload. | ||
func (ks *scaler) GetListerFactory() func(schema.GroupVersionResource) (cache.GenericLister, error) { | ||
return ks.listerFactory | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright 2019 The Knative 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. | ||
*/ | ||
|
||
package autoscaling | ||
|
||
// WorkloadResource Define a type of workload | ||
type WorkloadResource struct { | ||
Kind string | ||
ApiVersion string | ||
} | ||
|
||
var ( | ||
DEPLOYMENT = WorkloadResource{ | ||
Kind: "Deployment", | ||
ApiVersion: "apps/v1", | ||
} | ||
// Add others... | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2019 The Knative 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. | ||
*/ | ||
|
||
package workload | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"knative.dev/pkg/apis/duck" | ||
autoscalingv1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" | ||
"knative.dev/serving/pkg/reconciler/autoscaling" | ||
"knative.dev/serving/pkg/resources" | ||
) | ||
|
||
type DeploymentScaler struct { | ||
ctx context.Context | ||
bs autoscaling.BaseScaler | ||
} | ||
|
||
var _ autoscaling.Scaler = &DeploymentScaler{} | ||
|
||
func init() { | ||
autoscaling.RegisterScalerType(autoscaling.ScalerTypeFactoryByWR(autoscaling.DEPLOYMENT), NewDeploymentScaler) | ||
} | ||
|
||
func NewDeploymentScaler(ctx context.Context, bs autoscaling.BaseScaler) (autoscaling.Scaler, error) { | ||
return &DeploymentScaler{ctx: ctx, bs: bs}, nil | ||
} | ||
|
||
func (a *DeploymentScaler) ApplyScale(ctx context.Context, pa *autoscalingv1alpha1.PodAutoscaler, desiredScale int32) error { | ||
ps, err := resources.GetScaleResource(pa.Namespace, pa.Spec.ScaleTargetRef, a.bs.GetListerFactory()) | ||
if err != nil { | ||
return fmt.Errorf("failed to get scale target %v: %w", pa.Spec.ScaleTargetRef, err) | ||
} | ||
gvr, _, err := resources.ScaleResourceArguments(pa.Spec.ScaleTargetRef) | ||
if err != nil { | ||
return err | ||
} | ||
psNew := ps.DeepCopy() | ||
psNew.Spec.Replicas = &desiredScale | ||
patch, err := duck.CreatePatch(ps, psNew) | ||
if err != nil { | ||
return err | ||
} | ||
patchBytes, err := patch.MarshalJSON() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = a.bs.GetDynamicClient().Resource(*gvr).Namespace(pa.Namespace).Patch(ctx, ps.Name, types.JSONPatchType, | ||
patchBytes, metav1.PatchOptions{}) | ||
return err | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a new line in between.