Skip to content

Commit

Permalink
Fix PDB and VS not deleted (#584)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request!  Here are some tips for you:

1. Run unit tests and ensure that they are passing
2. If your change introduces any API changes, make sure to update the
e2e tests
3. Make sure documentation is updated for your PR!

-->
# Description
<!-- Briefly describe the motivation for the change. Please include
illustrations where appropriate. -->

This PR solves two bugs:

1. Previous PDB not deleted when redeployed
a. The previous implementation only used Apply to create a new
revision's PDB without deleting the previous revision's PDB.
b. This PR uses Patch instead of Apply so it will only use one PDB to
every revision
2. Virtual service not deleted when undeployed
a. This issue happens due to condition logic that would only delete VS
with revision > 1. Removing this condition solve the issue.
b. This condition was created to maintain backward compatibility to VS
without revision, but the implementation is faulty.

# Tests
<!-- Besides the existing / updated automated tests, what specific
scenarios should be tested? Consider the backward compatibility of the
changes, whether corner cases are covered, etc. Please describe the
tests and check the ones that have been completed. Eg:
- [x] Deploying new and existing standard models
- [ ] Deploying PyFunc models
-->

# Checklist
- [ ] Added PR label
- [ ] Added unit test, integration, and/or e2e tests
- [x] Tested locally
- [ ] Updated documentation
- [ ] Update Swagger spec if the PR introduce API changes
- [ ] Regenerated Golang and Python client if the PR introduces API
changes

# Release Notes
<!--
Does this PR introduce a user-facing change?
If no, just write "NONE" in the release-note block below.
If yes, a release note is required. Enter your extended release note in
the block below.
If the PR requires additional action from users switching to the new
release, include the string "action required".

For more information about release notes, see kubernetes' guide here:
http://git.k8s.io/community/contributors/guide/release-notes.md
-->

```release-note

```
  • Loading branch information
ariefrahmansyah authored May 21, 2024
1 parent 7506c07 commit a57445b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 38 deletions.
20 changes: 12 additions & 8 deletions api/cluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ func (c *controller) Deploy(ctx context.Context, modelService *models.Service) (
}

if c.deploymentConfig.PodDisruptionBudget.Enabled {
pdbs := createPodDisruptionBudgets(modelService, c.deploymentConfig.PodDisruptionBudget)
// Create / update pdb
pdbs := generatePDBSpecs(modelService, c.deploymentConfig.PodDisruptionBudget)
if err := c.deployPodDisruptionBudgets(ctx, pdbs); err != nil {
log.Errorf("unable to create pdb: %v", err)
return nil, errors.Wrapf(err, fmt.Sprintf("%v", ErrUnableToCreatePDB))
Expand Down Expand Up @@ -320,19 +321,22 @@ func (c *controller) Delete(ctx context.Context, modelService *models.Service) (
}

if c.deploymentConfig.PodDisruptionBudget.Enabled {
pdbs := createPodDisruptionBudgets(modelService, c.deploymentConfig.PodDisruptionBudget)
pdbs := generatePDBSpecs(modelService, c.deploymentConfig.PodDisruptionBudget)
if err := c.deletePodDisruptionBudgets(ctx, pdbs); err != nil {
log.Errorf("unable to delete pdb %v", err)
return nil, ErrUnableToDeletePDB
}
}

if modelService.RevisionID > 1 {
vsName := fmt.Sprintf("%s-%s-%s", modelService.ModelName, modelService.ModelVersion, models.VirtualServiceComponentType)
if err := c.deleteVirtualService(ctx, vsName, modelService.Namespace); err != nil {
log.Errorf("unable to delete virtual service %v", err)
return nil, ErrUnableToDeleteVirtualService
}
vsCfg, err := NewVirtualService(modelService, "")
if err != nil {
log.Errorf("unable to initialize virtual service builder: %v", err)
return nil, errors.Wrapf(err, fmt.Sprintf("%v", ErrUnableToDeleteVirtualService))
}

if err := c.deleteVirtualService(ctx, vsCfg); err != nil {
log.Errorf("unable to delete virtual service %v", err)
return nil, ErrUnableToDeleteVirtualService
}

return modelService, nil
Expand Down
46 changes: 31 additions & 15 deletions api/cluster/pdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package cluster

import (
"context"
"encoding/json"
"fmt"
"math"

policyv1 "k8s.io/api/policy/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
metav1cfg "k8s.io/client-go/applyconfigurations/meta/v1"
policyv1cfg "k8s.io/client-go/applyconfigurations/policy/v1"

"github.com/caraml-dev/merlin/config"
"github.com/caraml-dev/merlin/models"
Expand All @@ -29,39 +30,50 @@ func NewPodDisruptionBudget(modelService *models.Service, componentType string,
labels["serving.kserve.io/inferenceservice"] = modelService.Name

return &PodDisruptionBudget{
Name: fmt.Sprintf("%s-%s-%s", modelService.Name, componentType, models.PDBComponentType),
Name: fmt.Sprintf("%s-%s-%s-%s", modelService.ModelName, modelService.ModelVersion, componentType, models.PDBComponentType),
Namespace: modelService.Namespace,
Labels: labels,
MaxUnavailablePercentage: pdbConfig.MaxUnavailablePercentage,
MinAvailablePercentage: pdbConfig.MinAvailablePercentage,
}
}

func (cfg PodDisruptionBudget) BuildPDBSpec() (*policyv1cfg.PodDisruptionBudgetSpecApplyConfiguration, error) {
func (cfg PodDisruptionBudget) BuildPDBSpec() (*policyv1.PodDisruptionBudget, error) {
if cfg.MaxUnavailablePercentage == nil && cfg.MinAvailablePercentage == nil {
return nil, fmt.Errorf("one of maxUnavailable and minAvailable must be specified")
}

pdbSpec := &policyv1cfg.PodDisruptionBudgetSpecApplyConfiguration{
Selector: &metav1cfg.LabelSelectorApplyConfiguration{
MatchLabels: cfg.Labels,
pdb := &policyv1.PodDisruptionBudget{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy/v1",
Kind: "PodDisruptionBudget",
},
ObjectMeta: metav1.ObjectMeta{
Name: cfg.Name,
Namespace: cfg.Namespace,
Labels: cfg.Labels,
},
Spec: policyv1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: cfg.Labels,
},
},
}

// 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.FromString(fmt.Sprintf("%d%%", *cfg.MinAvailablePercentage))
pdbSpec.MinAvailable = &minAvailable
pdb.Spec.MinAvailable = &minAvailable
} else if cfg.MaxUnavailablePercentage != nil {
maxUnavailable := intstr.FromString(fmt.Sprintf("%d%%", *cfg.MaxUnavailablePercentage))
pdbSpec.MaxUnavailable = &maxUnavailable
pdb.Spec.MaxUnavailable = &maxUnavailable
}

return pdbSpec, nil
return pdb, nil
}

func createPodDisruptionBudgets(modelService *models.Service, pdbConfig config.PodDisruptionBudgetConfig) []*PodDisruptionBudget {
func generatePDBSpecs(modelService *models.Service, pdbConfig config.PodDisruptionBudgetConfig) []*PodDisruptionBudget {
pdbs := []*PodDisruptionBudget{}

// Only create PDB if: ceil(minReplica * minAvailablePercent) < minReplica
Expand Down Expand Up @@ -113,11 +125,15 @@ func (c *controller) deployPodDisruptionBudget(ctx context.Context, pdb *PodDisr
return err
}

pdbCfg := policyv1cfg.PodDisruptionBudget(pdb.Name, pdb.Namespace)
pdbCfg.WithLabels(pdb.Labels)
pdbCfg.WithSpec(pdbSpec)
pdbJSON, err := json.Marshal(pdbSpec)
if err != nil {
return err
}

forceEnabled := true

_, err = c.policyClient.PodDisruptionBudgets(pdb.Namespace).Apply(ctx, pdbCfg, metav1.ApplyOptions{FieldManager: "application/apply-patch"})
_, err = c.policyClient.PodDisruptionBudgets(pdb.Namespace).
Patch(ctx, pdb.Name, types.ApplyPatchType, pdbJSON, metav1.PatchOptions{FieldManager: "application/apply-patch", Force: &forceEnabled})
if err != nil {
return err
}
Expand Down
47 changes: 34 additions & 13 deletions api/cluster/pdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"testing"

"github.com/stretchr/testify/assert"
policyv1 "k8s.io/api/policy/v1"
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"

"github.com/caraml-dev/merlin/config"
"github.com/caraml-dev/merlin/models"
Expand All @@ -32,7 +31,7 @@ func TestPodDisruptionBudget_BuildPDBSpec(t *testing.T) {
tests := []struct {
name string
fields fields
want *policyv1cfg.PodDisruptionBudgetSpecApplyConfiguration
want *policyv1.PodDisruptionBudget
wantErr bool
}{
{
Expand All @@ -44,10 +43,21 @@ func TestPodDisruptionBudget_BuildPDBSpec(t *testing.T) {
MaxUnavailablePercentage: nil,
MinAvailablePercentage: &defaultInt,
},
want: &policyv1cfg.PodDisruptionBudgetSpecApplyConfiguration{
MinAvailable: &defaultIntOrString,
Selector: &metav1cfg.LabelSelectorApplyConfiguration{
MatchLabels: defaultLabels,
want: &policyv1.PodDisruptionBudget{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy/v1",
Kind: "PodDisruptionBudget",
},
ObjectMeta: metav1.ObjectMeta{
Name: "sklearn-sample-s-1-model-pdb",
Namespace: "pdb-test",
Labels: defaultLabels,
},
Spec: policyv1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: defaultLabels,
},
MinAvailable: &defaultIntOrString,
},
},
wantErr: false,
Expand All @@ -61,10 +71,21 @@ func TestPodDisruptionBudget_BuildPDBSpec(t *testing.T) {
MaxUnavailablePercentage: &defaultInt,
MinAvailablePercentage: &defaultInt,
},
want: &policyv1cfg.PodDisruptionBudgetSpecApplyConfiguration{
MinAvailable: &defaultIntOrString,
Selector: &metav1cfg.LabelSelectorApplyConfiguration{
MatchLabels: defaultLabels,
want: &policyv1.PodDisruptionBudget{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy/v1",
Kind: "PodDisruptionBudget",
},
ObjectMeta: metav1.ObjectMeta{
Name: "sklearn-sample-s-1-model-pdb",
Namespace: "pdb-test",
Labels: defaultLabels,
},
Spec: policyv1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: defaultLabels,
},
MinAvailable: &defaultIntOrString,
},
},
wantErr: false,
Expand Down Expand Up @@ -103,7 +124,7 @@ func TestPodDisruptionBudget_BuildPDBSpec(t *testing.T) {
}
}

func TestCreatePodDisruptionBudgets(t *testing.T) {
func Test_generatePDBSpecs(t *testing.T) {
err := models.InitKubernetesLabeller("gojek.com/", "dev")
assert.Nil(t, err)

Expand Down Expand Up @@ -322,7 +343,7 @@ func TestCreatePodDisruptionBudgets(t *testing.T) {

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
pdbs := createPodDisruptionBudgets(tt.modelService, tt.pdbConfig)
pdbs := generatePDBSpecs(tt.modelService, tt.pdbConfig)
assert.Equal(t, tt.expected, pdbs)
})
}
Expand Down
4 changes: 2 additions & 2 deletions api/cluster/virtual_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,6 @@ func (c *controller) deployVirtualService(ctx context.Context, vsCfg *VirtualSer
Patch(ctx, vsCfg.Name, types.ApplyPatchType, vsJSON, metav1.PatchOptions{FieldManager: "application/apply-patch", Force: &forceEnabled})
}

func (c *controller) deleteVirtualService(ctx context.Context, name, namespace string) error {
return c.istioClient.VirtualServices(namespace).Delete(ctx, name, metav1.DeleteOptions{})
func (c *controller) deleteVirtualService(ctx context.Context, vsCfg *VirtualService) error {
return c.istioClient.VirtualServices(vsCfg.Namespace).Delete(ctx, vsCfg.Name, metav1.DeleteOptions{})
}

0 comments on commit a57445b

Please sign in to comment.