Skip to content
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

Fix PDB implementation #425

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions api/cluster/pdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type PodDisruptionBudget struct {
Labels map[string]string
MaxUnavailablePercentage *int
MinAvailablePercentage *int
Selector *metav1.LabelSelector
}

func NewPodDisruptionBudget(modelService *models.Service, componentType string, pdbConfig config.PodDisruptionBudgetConfig) *PodDisruptionBudget {
Expand All @@ -47,10 +46,10 @@ func (cfg PodDisruptionBudget) BuildPDBSpec() (*policyv1cfg.PodDisruptionBudgetS
// Since we can specify only one of maxUnavailable and minAvailable, minAvailable takes precedence
// https://kubernetes.io/docs/tasks/run-application/configure-pdb/#specifying-a-poddisruptionbudget
if cfg.MinAvailablePercentage != nil {
minAvailable := intstr.FromInt(*cfg.MinAvailablePercentage)
minAvailable := intstr.FromString(fmt.Sprintf("%d%%", *cfg.MinAvailablePercentage))
pdbSpec.MinAvailable = &minAvailable
} else if cfg.MaxUnavailablePercentage != nil {
maxUnavailable := intstr.FromInt(*cfg.MaxUnavailablePercentage)
maxUnavailable := intstr.FromString(fmt.Sprintf("%d%%", *cfg.MaxUnavailablePercentage))
pdbSpec.MaxUnavailable = &maxUnavailable
}

Expand Down Expand Up @@ -90,7 +89,7 @@ func (c *controller) deployPodDisruptionBudget(ctx context.Context, pdb *PodDisr
pdbCfg.WithLabels(pdb.Labels)
pdbCfg.WithSpec(pdbSpec)

_, err = c.policyClient.PodDisruptionBudgets(pdb.Namespace).Apply(ctx, pdbCfg, metav1.ApplyOptions{})
_, err = c.policyClient.PodDisruptionBudgets(pdb.Namespace).Apply(ctx, pdbCfg, metav1.ApplyOptions{FieldManager: "application/apply-patch"})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this in the previous PR. But what's the reason for using Apply instead of Create? https://github.com/kubernetes/client-go/blob/v0.27.3/kubernetes/typed/policy/v1/poddisruptionbudget.go#L118

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can use Apply for both Undeploy and Redeploy. Otherwise, we need to implement a function that call Update.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can use Apply for both Undeploy and Redeploy

Do you mean for both Deploy and Redeploy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yes, you're right. It's for Deploy and Redeploy.

if err != nil {
return err
}
Expand Down
100 changes: 100 additions & 0 deletions api/cluster/pdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cluster

import (
"reflect"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
metav1cfg "k8s.io/client-go/applyconfigurations/meta/v1"
policyv1cfg "k8s.io/client-go/applyconfigurations/policy/v1"
)

func TestPodDisruptionBudget_BuildPDBSpec(t *testing.T) {
defaultInt := 20
defaultIntOrString := intstr.FromString("20%")
defaultLabels := map[string]string{
"gojek.com/app": "sklearn-sample-s",
}

type fields struct {
Name string
Namespace string
Labels map[string]string
MaxUnavailablePercentage *int
MinAvailablePercentage *int
Selector *metav1.LabelSelector
}
tests := []struct {
name string
fields fields
want *policyv1cfg.PodDisruptionBudgetSpecApplyConfiguration
wantErr bool
}{
{
name: "valid: enabled with min_available",
fields: fields{
Name: "sklearn-sample-s-1-model-pdb",
Namespace: "pdb-test",
Labels: defaultLabels,
MaxUnavailablePercentage: nil,
MinAvailablePercentage: &defaultInt,
},
want: &policyv1cfg.PodDisruptionBudgetSpecApplyConfiguration{
MinAvailable: &defaultIntOrString,
Selector: &metav1cfg.LabelSelectorApplyConfiguration{
MatchLabels: defaultLabels,
},
},
wantErr: false,
},
{
name: "valid: enabled but both max_unavailable and min_available specified. will use min available",
fields: fields{
Name: "sklearn-sample-s-1-model-pdb",
Namespace: "pdb-test",
Labels: defaultLabels,
MaxUnavailablePercentage: &defaultInt,
MinAvailablePercentage: &defaultInt,
},
want: &policyv1cfg.PodDisruptionBudgetSpecApplyConfiguration{
MinAvailable: &defaultIntOrString,
Selector: &metav1cfg.LabelSelectorApplyConfiguration{
MatchLabels: defaultLabels,
},
},
wantErr: false,
},
{
name: "invalid: enabled but no max_unavailable and min_available",
fields: fields{
Name: "sklearn-sample-s-1-model-pdb",
Namespace: "pdb-test",
Labels: map[string]string{},
MaxUnavailablePercentage: nil,
MinAvailablePercentage: nil,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := PodDisruptionBudget{
Name: tt.fields.Name,
Namespace: tt.fields.Namespace,
Labels: tt.fields.Labels,
MaxUnavailablePercentage: tt.fields.MaxUnavailablePercentage,
MinAvailablePercentage: tt.fields.MinAvailablePercentage,
}
got, err := cfg.BuildPDBSpec()
if (err != nil) != tt.wantErr {
t.Errorf("PodDisruptionBudget.BuildPDBSpec() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("PodDisruptionBudget.BuildPDBSpec() = %v, want %v", got, tt.want)
}
})
}
}
6 changes: 3 additions & 3 deletions api/config/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestUnmarshalTopologySpreadConstraints(t *testing.T) {
}

func TestPDBConfig(t *testing.T) {
defaultMaxUnavailablePercentage := 20
defaultMinAvailablePercentage := 20

testCases := []struct {
desc string
Expand All @@ -110,8 +110,8 @@ func TestPDBConfig(t *testing.T) {
desc: "Success: valid pdb config",
envConfigPath: "./testdata/valid-environment-1.yaml",
expectedPDBConfig: PodDisruptionBudgetConfig{
Enabled: true,
MaxUnavailablePercentage: &defaultMaxUnavailablePercentage,
Enabled: true,
MinAvailablePercentage: &defaultMinAvailablePercentage,
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion api/config/testdata/valid-environment-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
max_memory: "8Gi"
pod_disruption_budget:
enabled: true
max_unavailable_percentage: 20
min_available_percentage: 20
queue_resource_percentage: "20"
default_prediction_job_config:
executor_replica: 3
Expand Down
Loading