|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package v1alpha |
| 24 | + |
| 25 | +import ( |
| 26 | + time "time" |
| 27 | + |
| 28 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 29 | + "github.com/pkg/errors" |
| 30 | +) |
| 31 | + |
| 32 | +// ChaosSpec holds configuration for the deployment chaos monkey. |
| 33 | +type ChaosSpec struct { |
| 34 | + // Enabled switches the chaos monkey for a deployment on or off. |
| 35 | + Enabled *bool `json:"enabled,omitempty"` |
| 36 | + // Interval is the time between events |
| 37 | + Interval *time.Duration `json:"interval,omitempty"` |
| 38 | + // KillPodProbability is the chance of a pod being killed during an event |
| 39 | + KillPodProbability *Percent `json:"kill-pod-probability,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +// IsEnabled returns the value of enabled. |
| 43 | +func (s ChaosSpec) IsEnabled() bool { |
| 44 | + return util.BoolOrDefault(s.Enabled) |
| 45 | +} |
| 46 | + |
| 47 | +// GetInterval returns the value of interval. |
| 48 | +func (s ChaosSpec) GetInterval() time.Duration { |
| 49 | + return util.DurationOrDefault(s.Interval) |
| 50 | +} |
| 51 | + |
| 52 | +// GetKillPodProbability returns the value of kill-pod-probability. |
| 53 | +func (s ChaosSpec) GetKillPodProbability() Percent { |
| 54 | + return PercentOrDefault(s.KillPodProbability) |
| 55 | +} |
| 56 | + |
| 57 | +// Validate the given spec |
| 58 | +func (s ChaosSpec) Validate() error { |
| 59 | + if s.IsEnabled() { |
| 60 | + if s.GetInterval() <= 0 { |
| 61 | + return maskAny(errors.Wrapf(ValidationError, "Interval must be > 0")) |
| 62 | + } |
| 63 | + if err := s.GetKillPodProbability().Validate(); err != nil { |
| 64 | + return maskAny(err) |
| 65 | + } |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +// SetDefaults fills in missing defaults |
| 71 | +func (s *ChaosSpec) SetDefaults() { |
| 72 | + if s.GetInterval() == 0 { |
| 73 | + s.Interval = util.NewDuration(time.Minute) |
| 74 | + } |
| 75 | + if s.GetKillPodProbability() == 0 { |
| 76 | + s.KillPodProbability = NewPercent(50) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// SetDefaultsFrom fills unspecified fields with a value from given source spec. |
| 81 | +func (s *ChaosSpec) SetDefaultsFrom(source ChaosSpec) { |
| 82 | + if s.Enabled == nil { |
| 83 | + s.Enabled = util.NewBoolOrNil(source.Enabled) |
| 84 | + } |
| 85 | + if s.Interval == nil { |
| 86 | + s.Interval = util.NewDurationOrNil(source.Interval) |
| 87 | + } |
| 88 | + if s.KillPodProbability == nil { |
| 89 | + s.KillPodProbability = NewPercentOrNil(source.KillPodProbability) |
| 90 | + } |
| 91 | +} |
0 commit comments