Skip to content

Commit

Permalink
feat: implement scale for proxy deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln committed Mar 12, 2022
1 parent b4f96f8 commit c792ff1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
8 changes: 8 additions & 0 deletions api/v1alpha1/proxydeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.selector
//+kubebuilder:printcolumn:name="Ready",type="boolean",JSONPath=".status.conditions[?(@.type==\"Ready\")].status"
//+kubebuilder:printcolumn:name="Replicas",type="date",JSONPath=".status.replicas"
//+kubebuilder:printcolumn:name="Available Replicas",type="date",JSONPath=".status.availableReplicas"
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
//+kubebuilder:resource:shortName={"spd"},categories=all
Expand Down Expand Up @@ -169,6 +171,12 @@ type ProxyDeploymentStatus struct {

// Number of available replicas in Proxy Deployment.
AvailableReplicas int32 `json:"availableReplicas"`

// Number of unavailable replicas in Proxy Deployment.
UnavailableReplicas int32 `json:"unavailableReplicas"`

// Pod label selector.
Selector string `json:"selector"`
}

func (s *ProxyDeploymentStatus) SetCondition(condition ProxyDeploymentStatusCondition, status metav1.ConditionStatus, reason string, message string) {
Expand Down
16 changes: 16 additions & 0 deletions config/crd/bases/shulkermc.io_proxydeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ spec:
- jsonPath: .status.conditions[?(@.type=="Ready")].status
name: Ready
type: boolean
- jsonPath: .status.replicas
name: Replicas
type: date
- jsonPath: .status.availableReplicas
name: Available Replicas
type: date
Expand Down Expand Up @@ -1213,15 +1216,28 @@ spec:
description: Number of total replicas in Proxy Deployment.
format: int32
type: integer
selector:
description: Pod label selector.
type: string
unavailableReplicas:
description: Number of unavailable replicas in Proxy Deployment.
format: int32
type: integer
required:
- availableReplicas
- conditions
- replicas
- selector
- unavailableReplicas
type: object
type: object
served: true
storage: true
subresources:
scale:
labelSelectorPath: .status.selector
specReplicasPath: .spec.replicas
statusReplicasPath: .status.replicas
status: {}
status:
acceptedNames:
Expand Down
7 changes: 7 additions & 0 deletions controllers/proxydeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,15 @@ func (r *ProxyDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

if err == nil {
selector, err := metav1.LabelSelectorAsSelector(resourceBuilder.GetPodSelector())
if err != nil {
return ctrl.Result{}, err
}

proxyDeployment.Status.Replicas = int32(deployment.Status.Replicas)
proxyDeployment.Status.AvailableReplicas = int32(deployment.Status.AvailableReplicas)
proxyDeployment.Status.UnavailableReplicas = int32(deployment.Status.UnavailableReplicas)
proxyDeployment.Status.Selector = selector.String()

for _, condition := range deployment.Status.Conditions {
if condition.Type == appsv1.DeploymentAvailable {
Expand Down
7 changes: 7 additions & 0 deletions internal/resource/proxy/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resource
import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
shulkermciov1alpha1 "shulkermc.io/m/v2/api/v1alpha1"
common "shulkermc.io/m/v2/internal/resource"
Expand Down Expand Up @@ -51,6 +52,12 @@ func (b *ProxyDeploymentResourceBuilder) getRoleBindingName() string {
return fmt.Sprintf("%s-proxy-%s", b.getResourcePrefix(), b.Instance.Name)
}

func (b *ProxyDeploymentResourceBuilder) GetPodSelector() *metav1.LabelSelector {
return &metav1.LabelSelector{
MatchLabels: b.getLabels(),
}
}

func (b *ProxyDeploymentResourceBuilder) getLabels() map[string]string {
labels := map[string]string{
"app.kubernetes.io/name": b.Instance.Name,
Expand Down
4 changes: 1 addition & 3 deletions internal/resource/proxy/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ func (b *ProxyDeploymentDeploymentBuilder) Update(object client.Object) error {

deployment.Spec = appsv1.DeploymentSpec{
Replicas: &b.Instance.Spec.Replicas,
Selector: &metav1.LabelSelector{
MatchLabels: b.getLabels(),
},
Selector: b.GetPodSelector(),
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: b.getLabels(),
Expand Down

0 comments on commit c792ff1

Please sign in to comment.