Skip to content

Commit

Permalink
Add new resources, scale with API (#92)
Browse files Browse the repository at this point in the history
Can now manage:
- replicasets
- replicationcontrollers
- statefulsets

Scaling now occurs through the scaling API,
rather than manipulating replica count on
resources.

Fix issue with match selector being incorrectly
used, did not match correctly.
  • Loading branch information
jthomperoo committed Jan 18, 2020
1 parent ac0f031 commit 6e66fe3
Show file tree
Hide file tree
Showing 26 changed files with 1,606 additions and 442 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Support for other entrypoints other than `/bin/sh`, can specify an entrypoint for the shell command method.
### Changed
- Can scale ReplicaSets, ReplicationControllers and StatefulSets alongside Deployments.
- ResourceMetrics fields have `resourceName` and `resource` rather than `deploymentName` and `deployment`. In JSON this means that only the resource name will be exposed via field `resource`.
- Uses scaling API rather than manually adjusting replica count on resource.
- Matches using match selector rather than incorrectly using resource labels and building a different selector.

## [v0.8.0] - 2019-12-17
### Added
Expand Down
32 changes: 12 additions & 20 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,12 @@ import (
"github.com/jthomperoo/custom-pod-autoscaler/config"
"github.com/jthomperoo/custom-pod-autoscaler/evaluate"
"github.com/jthomperoo/custom-pod-autoscaler/metric"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/jthomperoo/custom-pod-autoscaler/resourceclient"
)

// RunType api marks the metric gathering/evaluation as running during an API request
const RunType = "api"

type getMetricer interface {
GetMetrics(deployment *appsv1.Deployment) (*metric.ResourceMetrics, error)
}

type getEvaluationer interface {
GetEvaluation(resourceMetrics *metric.ResourceMetrics) (*evaluate.Evaluation, error)
}

// Error is an error response from the API, with the status code and an error message
type Error struct {
Message string `json:"message"`
Expand All @@ -55,9 +45,9 @@ type Error struct {
type API struct {
Router chi.Router
Config *config.Config
Clientset kubernetes.Interface
GetMetricer getMetricer
GetEvaluationer getEvaluationer
Client resourceclient.Client
GetMetricer metric.GetMetricer
GetEvaluationer evaluate.GetEvaluationer
}

// Routes sets up routing for the API
Expand All @@ -70,17 +60,18 @@ func (api *API) Routes() {
}

func (api *API) getMetrics(w http.ResponseWriter, r *http.Request) {
// Get deployments being managed
deployment, err := api.Clientset.AppsV1().Deployments(api.Config.Namespace).Get(api.Config.ScaleTargetRef.Name, metav1.GetOptions{})
// Get resource being managed
resource, err := api.Client.Get(api.Config.ScaleTargetRef.APIVersion, api.Config.ScaleTargetRef.Kind, api.Config.ScaleTargetRef.Name, api.Config.Namespace)
if err != nil {
apiError(w, &Error{
err.Error(),
http.StatusInternalServerError,
})
return
}

// Get metrics
metrics, err := api.GetMetricer.GetMetrics(deployment)
metrics, err := api.GetMetricer.GetMetrics(resource)
if err != nil {
apiError(w, &Error{
err.Error(),
Expand All @@ -101,17 +92,18 @@ func (api *API) getMetrics(w http.ResponseWriter, r *http.Request) {
}

func (api *API) getEvaluation(w http.ResponseWriter, r *http.Request) {
// Get deployments being managed
deployment, err := api.Clientset.AppsV1().Deployments(api.Config.Namespace).Get(api.Config.ScaleTargetRef.Name, metav1.GetOptions{})
// Get resource being managed
resource, err := api.Client.Get(api.Config.ScaleTargetRef.APIVersion, api.Config.ScaleTargetRef.Kind, api.Config.ScaleTargetRef.Name, api.Config.Namespace)
if err != nil {
apiError(w, &Error{
err.Error(),
http.StatusInternalServerError,
})
return
}

// Get metrics
metrics, err := api.GetMetricer.GetMetrics(deployment)
metrics, err := api.GetMetricer.GetMetrics(resource)
if err != nil {
apiError(w, &Error{
err.Error(),
Expand Down
Loading

0 comments on commit 6e66fe3

Please sign in to comment.