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

Create PodDisruptionBudget for every integration #1787

Merged
merged 1 commit into from
Nov 12, 2020
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
11 changes: 11 additions & 0 deletions deploy/operator-role-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ rules:
- patch
- update
- watch
- apiGroups:
- policy
resources:
- poddisruptionbudgets
verbs:
- create
- delete
- get
- update
- list
- patch
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand Down
11 changes: 11 additions & 0 deletions deploy/operator-role-olm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ rules:
- patch
- update
- watch
- apiGroups:
- policy
resources:
- poddisruptionbudgets
verbs:
- create
- delete
- get
- update
astefanutti marked this conversation as resolved.
Show resolved Hide resolved
- patch
- list
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand Down
11 changes: 11 additions & 0 deletions deploy/operator-role-openshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ rules:
- patch
- update
- watch
- apiGroups:
- policy
resources:
- poddisruptionbudgets
verbs:
- create
- delete
- get
- update
- list
- patch
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand Down
12 changes: 6 additions & 6 deletions deploy/resources.go

Large diffs are not rendered by default.

109 changes: 109 additions & 0 deletions pkg/trait/pdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

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"
)

// The Pdb trait allows to configure the PodDisruptionBudget resource.
//
// +camel-k:trait=pdb
type pdbTrait struct {
BaseTrait `property:",squash"`
MaxUnavailable string `property:"max-unavailable" json:"maxUnavailable,omitempty"`
MinAvailable string `property:"min-available" json:"minAvailable,omitempty"`
}

func newPdbTrait() Trait {
return &pdbTrait{
BaseTrait: NewBaseTrait("pdb", 900),
}
}

func (t *pdbTrait) Configure(e *Environment) (bool, error) {
if t.Enabled == nil || !*t.Enabled {
return false, nil
}

strategy, err := e.DetermineControllerStrategy()
if err != nil {
return false, fmt.Errorf("unable to determine the controller stratedy")
}

if strategy == ControllerStrategyCronJob {
return false, fmt.Errorf("poddisruptionbudget isn't supported with cron-job controller strategy")
}

if t.MaxUnavailable != "" && t.MinAvailable != "" {
return false, fmt.Errorf("both minAvailable and maxUnavailable can't be set simultaneously")
}

return e.IntegrationInPhase(
astefanutti marked this conversation as resolved.
Show resolved Hide resolved
v1.IntegrationPhaseDeploying,
v1.IntegrationPhaseRunning,
), nil
}

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 == "" {
astefanutti marked this conversation as resolved.
Show resolved Hide resolved
t.MaxUnavailable = "1"
}

integration := e.Integration
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

} else {
min = intstr.Parse(t.MinAvailable)
spec.MinAvailable = &min
}

return &v1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: integration.Name,
Namespace: integration.Namespace,
Labels: integration.Labels,
},
Spec: spec,
}, nil
}
122 changes: 122 additions & 0 deletions pkg/trait/pdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package trait

import (
"testing"

"github.com/stretchr/testify/assert"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

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

func TestConfigurePdbTraitDoesSucceed(t *testing.T) {
pdbTrait, environment, _ := createPdbTest()
configured, err := pdbTrait.Configure(environment)

assert.True(t, configured)
assert.Nil(t, err)
}

func TestConfigurePdbTraitDoesNotSucceed(t *testing.T) {
pdbTrait, environment, _ := createPdbTest()

pdbTrait.MinAvailable = "1"
pdbTrait.MaxUnavailable = "2"
configured, err := pdbTrait.Configure(environment)
assert.NotNil(t, err)
assert.False(t, configured)
}
func TestPdbIsCreatedWithoutParametersEnabled(t *testing.T) {
pdbTrait, environment, _ := createPdbTest()

pdb := pdbCreatedCheck(pdbTrait, environment, t)
assert.Equal(t, int32(1), pdb.Spec.MaxUnavailable.IntVal)
}

func TestPdbIsCreatedWithMaxUnavailable(t *testing.T) {
pdbTrait, environment, _ := createPdbTest()
pdbTrait.MaxUnavailable = "1"

pdb := pdbCreatedCheck(pdbTrait, environment, t)
assert.Equal(t, int32(1), pdb.Spec.MaxUnavailable.IntVal)
}

func TestPdbIsCreatedWithMinAvailable(t *testing.T) {
pdbTrait, environment, _ := createPdbTest()
pdbTrait.MinAvailable = "2"

pdb := pdbCreatedCheck(pdbTrait, environment, t)
assert.Equal(t, int32(2), pdb.Spec.MinAvailable.IntVal)
}

func pdbCreatedCheck(pdbTrait *pdbTrait, environment *Environment, t *testing.T) *v1beta1.PodDisruptionBudget {
err := pdbTrait.Apply(environment)
assert.Nil(t, err)
pdb := findPdb(environment.Resources)

assert.NotNil(t, pdb)
assert.Equal(t, environment.Integration.Name, pdb.Name)
assert.Equal(t, environment.Integration.Namespace, pdb.Namespace)
assert.Equal(t, environment.Integration.Labels, pdb.Labels)
return pdb
}

func findPdb(resources *kubernetes.Collection) *v1beta1.PodDisruptionBudget {
for _, a := range resources.Items() {
if _, ok := a.(*v1beta1.PodDisruptionBudget); ok {
return a.(*v1beta1.PodDisruptionBudget)
}
}
return nil
}

func createPdbTest() (*pdbTrait, *Environment, *appsv1.Deployment) {
trait := newPdbTrait().(*pdbTrait)
enabled := true
trait.Enabled = &enabled

deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "integration-name",
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{},
},
}

environment := &Environment{
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "integration-name",
},
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseDeploying,
},
},
Resources: kubernetes.NewCollection(deployment),
}

return trait, environment, deployment
}
1 change: 1 addition & 0 deletions pkg/trait/trait_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ func init() {
AddToTraits(newIstioTrait)
AddToTraits(newIngressTrait)
AddToTraits(newOwnerTrait)
AddToTraits(newPdbTrait)
}