Skip to content

Commit

Permalink
Create pod trait that adds PodDisruptionBudget for every integration
Browse files Browse the repository at this point in the history
  • Loading branch information
mmelko committed Nov 3, 2020
1 parent cad0ed3 commit 3b29056
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 4 deletions.
9 changes: 9 additions & 0 deletions deploy/operator-role-olm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ rules:
- patch
- update
- watch
- apiGroups:
- "policy"
resources:
- poddisruptionbudgets
verbs:
- create
- delete
- get
- update
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand Down
9 changes: 9 additions & 0 deletions deploy/operator-role-openshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ rules:
- patch
- update
- watch
- apiGroups:
- "policy"
resources:
- poddisruptionbudgets
verbs:
- create
- delete
- get
- update
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand Down
8 changes: 4 additions & 4 deletions deploy/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions pkg/trait/pod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
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 PodDisruptionBudget trait is used to configure the pdb
//
// +camel-k:trait=pod
type podTrait struct {
BaseTrait `property:",squash"`
MaxUnavailable string `property:"max-unavailable" json:"maxUnavailable,omitempty"`
MinAvailable string `property:"min-available" json:"minAvailable,omitempty"`
}

func newPodTrait() Trait {
return &podTrait{
BaseTrait: NewBaseTrait("pod", 900),
}
}

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

return e.IntegrationInPhase(
v1.IntegrationPhaseDeploying,
v1.IntegrationPhaseRunning,
), nil
}

func (t *podTrait) Apply(e *Environment) error {
pdb, err := t.generatePodDisruptionBudget(e.Integration)

if pdb != nil {
e.Resources.Add(pdb)
}

return err
}

func (t *podTrait) generatePodDisruptionBudget(integration *v1.Integration) (*v1beta1.PodDisruptionBudget, error) {

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

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
}
Loading

0 comments on commit 3b29056

Please sign in to comment.