Skip to content

Commit

Permalink
fix mutating webhook to initialize all container's autoscaling policy (
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Apr 8, 2023
1 parent bfb0036 commit ea06bf9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Image URL to use all building/pushing image targets
IMG ?= ghcr.io/mercari/tortoise:v0.0.4
IMG ?= ghcr.io/mercari/tortoise:v0.0.6
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.24.2

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ Tortoise, you cannot get it from the breeder.
Tortoise, you need to get it from GitHub instead.

```shell
# install all needed resources to the cluster.
# Install CRDs into the K8s cluster specified in ~/.kube/config.
make install
# Deploy controller to the K8s cluster specified in ~/.kube/config.
make deploy
```

Tortoise, you don't need a rearing cage, but need VPA in your Kubernetes cluster before installing it.

## Documentations

(Note that the feature described in the doc may not have been implemented yet.)

- [Concept](./docs/concept.md): describes a brief overview of tortoise.
- [Horizontal scaling](./docs/horizontal.md): describes how the Tortoise does the horizontal autoscaling.
- [Vertical scaling](./docs/vertical.md): describes how the Tortoise does the vertical autoscaling.
- [The emergency mode](./docs/emergency.md): describes the emergency mode.
- [Flag configurations for admin](./docs/flag-configuration.md): describes how the cluster admin can configure the global behavior via flags
- [Technically details](./docs/internal.md): describes the technically details of Tortoise. (mostly for the contributors)

## API definition
Expand Down
28 changes: 28 additions & 0 deletions api/v1alpha1/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package v1alpha1

import (
"context"
"fmt"

v1 "k8s.io/api/apps/v1"

"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/controller-runtime/pkg/client"
)

type service struct {
c client.Client
}

func New(c client.Client) *service {
return &service{c: c}
}

func (c *service) GetDeploymentOnTortoise(ctx context.Context, tortoise *Tortoise) (*v1.Deployment, error) {
d := &v1.Deployment{}
if err := c.c.Get(ctx, types.NamespacedName{Namespace: tortoise.Namespace, Name: tortoise.Spec.TargetRefs.DeploymentName}, d); err != nil {
return nil, fmt.Errorf("failed to get deployment on tortoise: %w", err)
}
return d, nil
}
3 changes: 2 additions & 1 deletion api/v1alpha1/tortoise_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type TortoiseSpec struct {
// +optional
UpdateMode UpdateMode `json:"updateMode,omitempty" protobuf:"bytes,2,opt,name=updateMode"`
// ResourcePolicy contains the policy how each resource is updated.
ResourcePolicy []ContainerResourcePolicy `json:"resourcePolicy" protobuf:"bytes,3,name=resourcePolicy"`
// +optional
ResourcePolicy []ContainerResourcePolicy `json:"resourcePolicy,omitempty" protobuf:"bytes,3,opt,name=resourcePolicy"`
// FeatureGates allows to list the alpha feature names.
// +optional
FeatureGates []string `json:"featureGates,omitempty" protobuf:"bytes,4,opt,name=featureGates"`
Expand Down
25 changes: 25 additions & 0 deletions api/v1alpha1/tortoise_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SOFTWARE.
package v1alpha1

import (
"context"
"errors"
"fmt"
v1 "k8s.io/api/core/v1"
Expand All @@ -39,8 +40,10 @@ import (

// log is for logging in this package.
var tortoiselog = logf.Log.WithName("tortoise-resource")
var DeploymentService *service

func (r *Tortoise) SetupWebhookWithManager(mgr ctrl.Manager) error {
DeploymentService = New(mgr.GetClient())
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
Expand Down Expand Up @@ -69,6 +72,28 @@ func (r *Tortoise) Default() {
r.Spec.UpdateMode = UpdateModeOff
}

d, err := DeploymentService.GetDeploymentOnTortoise(context.Background(), r)
if err != nil {
tortoiselog.Error(err, "failed to get deployment")
}

if len(d.Spec.Template.Spec.Containers) != len(r.Spec.ResourcePolicy) {
for _, c := range d.Spec.Template.Spec.Containers {
policyExist := false
for _, p := range r.Spec.ResourcePolicy {
if c.Name == p.ContainerName {
policyExist = true
break
}
}
if !policyExist {
r.Spec.ResourcePolicy = append(r.Spec.ResourcePolicy, ContainerResourcePolicy{
ContainerName: c.Name,
})
}
}
}

for i := range r.Spec.ResourcePolicy {
_, ok := r.Spec.ResourcePolicy[i].AutoscalingPolicy[v1.ResourceCPU]
if !ok {
Expand Down

0 comments on commit ea06bf9

Please sign in to comment.