Skip to content

Commit

Permalink
chore(doc): Add documentation to PDB trait parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Nov 17, 2020
1 parent ccda232 commit 7e71966
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions pkg/trait/pdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,27 @@ package trait

import (
"fmt"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"

"k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
)

// The Pdb trait allows to configure the PodDisruptionBudget resource.
// The PDB trait allows to configure the PodDisruptionBudget resource for the Integration pods.
//
// +camel-k:trait=pdb
type pdbTrait struct {
BaseTrait `property:",squash"`
BaseTrait `property:",squash"`
// The number of pods for the Integration that must still be available after an eviction.
// It can be either an absolute number or a percentage.
// Only one of `min-available` and `max-unavailable` can be specified.
MinAvailable string `property:"min-available" json:"minAvailable,omitempty"`
// The number of pods for the Integration that can be unavailable after an eviction.
// It can be either an absolute number or a percentage (default `1` if `min-available` is also not set).
// Only one of `max-unavailable` and `min-available` can be specified.
MaxUnavailable string `property:"max-unavailable" json:"maxUnavailable,omitempty"`
MinAvailable string `property:"min-available" json:"minAvailable,omitempty"`
}

func newPdbTrait() Trait {
Expand Down Expand Up @@ -65,45 +73,39 @@ func (t *pdbTrait) Configure(e *Environment) (bool, error) {
}

func (t *pdbTrait) Apply(e *Environment) error {
if pdb, err := t.generatePodDisruptionBudget(e); err == nil {
e.Resources.Add(pdb)
} else {
return err
}
return nil
}

func (t *pdbTrait) generatePodDisruptionBudget(e *Environment) (*v1beta1.PodDisruptionBudget, error) {
if t.MaxUnavailable == "" && t.MinAvailable == "" {
t.MaxUnavailable = "1"
}

integration := e.Integration
spec := v1beta1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
v1.IntegrationLabel: integration.Name,
pdb := t.podDisruptionBudgetFor(e.Integration)
e.Resources.Add(pdb)

return nil
}

func (t *pdbTrait) podDisruptionBudgetFor(integration *v1.Integration) *v1beta1.PodDisruptionBudget {
pdb := &v1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: integration.Name,
Namespace: integration.Namespace,
Labels: integration.Labels,
},
Spec: v1beta1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
v1.IntegrationLabel: integration.Name,
},
},
},
}

var min, max intstr.IntOrString

if t.MaxUnavailable != "" {
max = intstr.Parse(t.MaxUnavailable)
spec.MaxUnavailable = &max

max := intstr.Parse(t.MaxUnavailable)
pdb.Spec.MaxUnavailable = &max
} else {
min = intstr.Parse(t.MinAvailable)
spec.MinAvailable = &min
min := intstr.Parse(t.MinAvailable)
pdb.Spec.MinAvailable = &min
}

return &v1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: integration.Name,
Namespace: integration.Namespace,
Labels: integration.Labels,
},
Spec: spec,
}, nil
return pdb
}

0 comments on commit 7e71966

Please sign in to comment.