From 4be1e54c6835b2e03e1d336c9a907eef7dd13949 Mon Sep 17 00:00:00 2001 From: mmelko Date: Thu, 22 Oct 2020 13:49:16 +0200 Subject: [PATCH] WIP Add pod disruption budget for every integration --- deploy/resources.go | 2 +- pkg/trait/pdb.go | 97 +++++++++++++++++++++++++++++++++++++ pkg/trait/trait_register.go | 1 + 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 pkg/trait/pdb.go diff --git a/deploy/resources.go b/deploy/resources.go index 162979f9f4..c70a548ad0 100644 --- a/deploy/resources.go +++ b/deploy/resources.go @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Code generated by vfsgen; DO NOT EDIT. +// Code generated by vfsgen; DO NOT EDIT package deploy diff --git a/pkg/trait/pdb.go b/pkg/trait/pdb.go new file mode 100644 index 0000000000..1db07e9f75 --- /dev/null +++ b/pkg/trait/pdb.go @@ -0,0 +1,97 @@ +/* +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 ( + 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=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 + } + + return e.IntegrationInPhase( + v1.IntegrationPhaseDeploying, + v1.IntegrationPhaseRunning, + ), nil +} + +func (t *pdbTrait) Apply(e *Environment) error { + pdbName := "pdb-" + e.Integration.Name + "-it" + t.L.Info("PodDisruptionBudget for the integration", e.Integration.Name, "not found") + pdb := t.generatePodDisruptionBudget(pdbName, e.Integration) + e.Resources.Add(pdb) + + return nil +} + +func (t *pdbTrait) generatePodDisruptionBudget(pdbName string, integration *v1.Integration) *v1beta1.PodDisruptionBudget { + spec := v1beta1.PodDisruptionBudgetSpec{ + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "camel.apache.org/integration": integration.Name, + }, + }, + } + + var min, max intstr.IntOrString + + if t.MinAvailable == "" && t.MaxUnavailable == "" { + //set the default + max = intstr.Parse("1") + spec.MaxUnavailable = &max + } else if t.MinAvailable != "" { + // sets to maxUnavailable to nil because only one parameter is allowed to be set + min = intstr.Parse(t.MinAvailable) + spec.MinAvailable = &min + spec.MaxUnavailable = nil + t.MaxUnavailable = "" + } else { + //set maxAvailable + max = intstr.Parse(t.MaxUnavailable) + spec.MaxUnavailable = &max + } + + return &v1beta1.PodDisruptionBudget{ + ObjectMeta: metav1.ObjectMeta{ + Name: pdbName, + Namespace: integration.Namespace, + Labels: integration.Labels, + }, + Spec: spec, + } +} diff --git a/pkg/trait/trait_register.go b/pkg/trait/trait_register.go index 30c4356e5e..ef171c74f5 100644 --- a/pkg/trait/trait_register.go +++ b/pkg/trait/trait_register.go @@ -46,4 +46,5 @@ func init() { AddToTraits(newIstioTrait) AddToTraits(newIngressTrait) AddToTraits(newOwnerTrait) + AddToTraits(newPdbTrait) }