Skip to content

Commit

Permalink
add: the container resource phases in tortoise status (#164)
Browse files Browse the repository at this point in the history
* add: the container resource phases in tortoise status

* modify tests
  • Loading branch information
sanposhiho authored Oct 11, 2023
1 parent 03b5e44 commit 3b0d352
Show file tree
Hide file tree
Showing 36 changed files with 371 additions and 108 deletions.
12 changes: 6 additions & 6 deletions api/autoscaling/v2/horizontalpodautoscaler_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ func mutateTest(before, after, torotise string) {
err = yaml.NewYAMLOrJSONDecoder(bytes.NewReader(y), 4096).Decode(tor)
status := tor.Status
Expect(err).NotTo(HaveOccurred())
err = k8sClient.Create(ctx, tor)
err = k8sClient.Create(ctx, tor.DeepCopy())
Expect(err).NotTo(HaveOccurred())
defer func() {
err = k8sClient.Delete(ctx, tor)
Expect(err).NotTo(HaveOccurred())
}()

err = k8sClient.Get(ctx, types.NamespacedName{Name: tor.GetName(), Namespace: tor.GetNamespace()}, tor)
Expect(err).NotTo(HaveOccurred())
tor.Status = status
//nolint:errcheck
k8sClient.Status().Update(ctx, tor)
err = k8sClient.Status().Update(ctx, tor)
Expect(err).NotTo(HaveOccurred())

y, err = os.ReadFile(before)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -87,9 +90,6 @@ func mutateTest(before, after, torotise string) {

var _ = Describe("v2.HPA Webhook", func() {
Context("mutating", func() {
It("nothing to do", func() {
mutateTest(filepath.Join("testdata", "mutating", "nothing-to-do", "before.yaml"), filepath.Join("testdata", "mutating", "nothing-to-do", "after.yaml"), filepath.Join("testdata", "mutating", "nothing-to-do", "tortoise.yaml"))
})
It("HPA is mutated based on the recommendation", func() {
mutateTest(filepath.Join("testdata", "mutating", "mutate-by-recommendations", "before.yaml"), filepath.Join("testdata", "mutating", "mutate-by-recommendations", "after.yaml"), filepath.Join("testdata", "mutating", "mutate-by-recommendations", "tortoise.yaml"))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ spec:
memory: Vertical
status:
tortoisePhase: Working
containerResourcePhases:
- containerName: "nginx"
resourcePhases:
cpu: Working
memory: Working
- containerName: "istio-proxy"
resourcePhases:
cpu: Working
memory: Working
targets:
deployment: sample
horizontalPodAutoscaler: sample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ spec:
memory: Vertical
status:
tortoisePhase: Working
containerResourcePhases:
- containerName: "nginx"
resourcePhases:
cpu: Working
memory: Working
- containerName: "istio-proxy"
resourcePhases:
cpu: Working
memory: Working
targets:
deployment: sample
horizontalPodAutoscaler: sample2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ spec:
memory: Vertical
status:
tortoisePhase: Working
containerResourcePhases:
- containerName: "nginx"
resourcePhases:
cpu: Working
memory: Working
- containerName: "istio-proxy"
resourcePhases:
cpu: Working
memory: Working
targets:
deployment: sample
horizontalPodAutoscaler: sample
Expand Down
27 changes: 0 additions & 27 deletions api/autoscaling/v2/testdata/mutating/nothing-to-do/after.yaml

This file was deleted.

27 changes: 0 additions & 27 deletions api/autoscaling/v2/testdata/mutating/nothing-to-do/before.yaml

This file was deleted.

22 changes: 0 additions & 22 deletions api/autoscaling/v2/testdata/mutating/nothing-to-do/tortoise.yaml

This file was deleted.

61 changes: 38 additions & 23 deletions api/v1beta1/tortoise_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type TargetRefs struct {
// It should be the same as the target of HPA.
ScaleTargetRef CrossVersionObjectReference `json:"scaleTargetRef" protobuf:"bytes,1,name=scaleTargetRef"`
// HorizontalPodAutoscalerName is the name of the target HPA.
// The target of this HPA should be the same as the DeploymentName above.
// The target of this HPA should be the same as the ScaleTargetRef above.
// The target HPA should have the ContainerResource type metric or the external metric refers to the container resource utilization.
// Please check out the document for more detail: https://github.com/mercari/tortoise/blob/master/docs/horizontal.md#supported-metrics-in-hpa
//
Expand All @@ -143,12 +143,45 @@ type CrossVersionObjectReference struct {

// TortoiseStatus defines the observed state of Tortoise
type TortoiseStatus struct {
TortoisePhase TortoisePhase `json:"tortoisePhase" protobuf:"bytes,1,name=tortoisePhase"`
Conditions Conditions `json:"conditions" protobuf:"bytes,2,name=conditions"`
Recommendations Recommendations `json:"recommendations" protobuf:"bytes,3,name=recommendations"`
Targets TargetsStatus `json:"targets" protobuf:"bytes,4,name=targets"`
TortoisePhase TortoisePhase `json:"tortoisePhase" protobuf:"bytes,1,name=tortoisePhase"`
Conditions Conditions `json:"conditions" protobuf:"bytes,2,name=conditions"`
Recommendations Recommendations `json:"recommendations" protobuf:"bytes,3,name=recommendations"`
Targets TargetsStatus `json:"targets" protobuf:"bytes,4,name=targets"`
ContainerResourcePhases []ContainerResourcePhases `json:"containerResourcePhases" protobuf:"bytes,5,name=containerResourcePhases"`
}

type ContainerResourcePhases struct {
// ContainerName is the name of target container.
ContainerName string `json:"containerName" protobuf:"bytes,1,name=containerName"`
// ResourcePhases is the phase of each resource of this container.
ResourceePhases map[v1.ResourceName]ContainerResourcePhase `json:"resourcePhases" protobuf:"bytes,2,name=resourcePhases"`
}

type ContainerResourcePhase string

const (
ContainerResourcePhaseGatheringData ContainerResourcePhase = "GatheringData"
ContainerResourcePhaseWorking ContainerResourcePhase = "Working"
)

type TortoisePhase string

const (
// TortoisePhaseInitializing means tortoise is just created and initializing some components (HPA and VPAs),
// and wait for those components to be ready.
TortoisePhaseInitializing TortoisePhase = "Initializing"
// TortoisePhaseGatheringData means tortoise is now gathering data and cannot make the accurate recommendations.
TortoisePhaseGatheringData TortoisePhase = "GatheringData"
// TortoisePhaseWorking means tortoise is making the recommendations,
// and applying the recommendation values.
TortoisePhaseWorking TortoisePhase = "Working"
// TortoisePhaseEmergency means tortoise is in the emergency mode.
TortoisePhaseEmergency TortoisePhase = "Emergency"
// TortoisePhaseBackToNormal means tortoise was in the emergency mode, and now it's coming back to the normal operation.
// During TortoisePhaseBackToNormal, the number of replicas of workloads are gradually reduced to the usual value.
TortoisePhaseBackToNormal TortoisePhase = "BackToNormal"
)

type TargetsStatus struct {
// +optional
HorizontalPodAutoscaler string `json:"horizontalPodAutoscaler" protobuf:"bytes,1,opt,name=horizontalPodAutoscaler"`
Expand Down Expand Up @@ -223,24 +256,6 @@ type ReplicasRecommendation struct {
UpdatedAt metav1.Time `json:"updatedAt,omitempty" protobuf:"bytes,6,opt,name=updatedAt"`
}

type TortoisePhase string

const (
// TortoisePhaseInitializing means tortoise is just created and initializing some components (HPA and VPAs),
// and wait for those components to be ready.
TortoisePhaseInitializing TortoisePhase = "Initializing"
// TortoisePhaseGatheringData means tortoise is now gathering data and cannot make the accurate recommendations.
TortoisePhaseGatheringData TortoisePhase = "GatheringData"
// TortoisePhaseWorking means tortoise is making the recommendations,
// and applying the recommendation values.
TortoisePhaseWorking TortoisePhase = "Working"
// TortoisePhaseEmergency means tortoise is in the emergency mode.
TortoisePhaseEmergency TortoisePhase = "Emergency"
// TortoisePhaseBackToNormal means tortoise was in the emergency mode, and now it's coming back to the normal operation.
// During TortoisePhaseBackToNormal, the number of replicas of workloads are gradually reduced to the usual value.
TortoisePhaseBackToNormal TortoisePhase = "BackToNormal"
)

type HPATargetUtilizationRecommendationPerContainer struct {
// ContainerName is the name of target container.
ContainerName string `json:"containerName" protobuf:"bytes,1,name=containerName"`
Expand Down
29 changes: 29 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

20 changes: 19 additions & 1 deletion config/crd/bases/autoscaling.mercari.com_tortoises.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ spec:
properties:
horizontalPodAutoscalerName:
description: "HorizontalPodAutoscalerName is the name of the target
HPA. The target of this HPA should be the same as the DeploymentName
HPA. The target of this HPA should be the same as the ScaleTargetRef
above. The target HPA should have the ContainerResource type
metric or the external metric refers to the container resource
utilization. Please check out the document for more detail:
Expand Down Expand Up @@ -585,6 +585,23 @@ spec:
- type
x-kubernetes-list-type: map
type: object
containerResourcePhases:
items:
properties:
containerName:
description: ContainerName is the name of target container.
type: string
resourcePhases:
additionalProperties:
type: string
description: ResourcePhases is the phase of each resource of
this container.
type: object
required:
- containerName
- resourcePhases
type: object
type: array
recommendations:
properties:
horizontal:
Expand Down Expand Up @@ -737,6 +754,7 @@ spec:
type: string
required:
- conditions
- containerResourcePhases
- recommendations
- targets
- tortoisePhase
Expand Down
9 changes: 9 additions & 0 deletions controllers/testdata/deletion-no-delete/before/tortoise.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ status:
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
containerResourcePhases:
- containerName: "nginx"
resourcePhases:
cpu: Working
memory: Working
- containerName: "istio-proxy"
resourcePhases:
cpu: Working
memory: Working
9 changes: 9 additions & 0 deletions controllers/testdata/deletion-policy-all/before/tortoise.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ status:
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
containerResourcePhases:
- containerName: "nginx"
resourcePhases:
cpu: Working
memory: Working
- containerName: "istio-proxy"
resourcePhases:
cpu: Working
memory: Working
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,12 @@ status:
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
containerResourcePhases:
- containerName: "nginx"
resourcePhases:
cpu: Working
memory: Working
- containerName: "istio-proxy"
resourcePhases:
cpu: Working
memory: Working
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,12 @@ status:
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
containerResourcePhases:
- containerName: "nginx"
resourcePhases:
cpu: Working
memory: Working
- containerName: "istio-proxy"
resourcePhases:
cpu: Working
memory: Working
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,12 @@ status:
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
containerResourcePhases:
- containerName: "nginx"
resourcePhases:
cpu: Working
memory: Working
- containerName: "istio-proxy"
resourcePhases:
cpu: Working
memory: Working
Loading

0 comments on commit 3b0d352

Please sign in to comment.