|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020 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 Adam Janikowski |
| 21 | +// |
| 22 | + |
| 23 | +package v1 |
| 24 | + |
| 25 | +import ( |
| 26 | + "github.com/arangodb/kube-arangodb/pkg/apis/shared" |
| 27 | + sharedv1 "github.com/arangodb/kube-arangodb/pkg/apis/shared/v1" |
| 28 | + "github.com/pkg/errors" |
| 29 | + core "k8s.io/api/core/v1" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + ServerGroupReservedInitContainerNameLifecycle = "init-lifecycle" |
| 34 | + ServerGroupReservedInitContainerNameUUID = "uuid" |
| 35 | +) |
| 36 | + |
| 37 | +func IsReservedServerGroupInitContainerName(name string) bool { |
| 38 | + switch name { |
| 39 | + case ServerGroupReservedInitContainerNameLifecycle, ServerGroupReservedInitContainerNameUUID: |
| 40 | + return true |
| 41 | + default: |
| 42 | + return false |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func ValidateServerGroupInitContainerName(name string) error { |
| 47 | + if IsReservedServerGroupInitContainerName(name) { |
| 48 | + return errors.Errorf("InitContainer name %s is restricted", name) |
| 49 | + } |
| 50 | + |
| 51 | + return sharedv1.AsKubernetesResourceName(&name).Validate() |
| 52 | +} |
| 53 | + |
| 54 | +type ServerGroupInitContainerMode string |
| 55 | + |
| 56 | +func (s *ServerGroupInitContainerMode) Get() ServerGroupInitContainerMode { |
| 57 | + if s == nil { |
| 58 | + return ServerGroupInitContainerUpdateMode // default |
| 59 | + } |
| 60 | + |
| 61 | + return *s |
| 62 | +} |
| 63 | + |
| 64 | +func (s ServerGroupInitContainerMode) New() *ServerGroupInitContainerMode { |
| 65 | + return &s |
| 66 | +} |
| 67 | + |
| 68 | +func (s *ServerGroupInitContainerMode) Validate() error { |
| 69 | + switch v := s.Get(); v { |
| 70 | + case ServerGroupInitContainerIgnoreMode, ServerGroupInitContainerUpdateMode: |
| 71 | + return nil |
| 72 | + default: |
| 73 | + return errors.Errorf("Unknown serverGroupInitContainerMode %s", v) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +const ( |
| 78 | + // ServerGroupInitContainerIgnoreMode ignores init container changes in pod recreation flow |
| 79 | + ServerGroupInitContainerIgnoreMode ServerGroupInitContainerMode = "ignore" |
| 80 | + // ServerGroupInitContainerUpdateMode enforce update of pod if init container has been changed |
| 81 | + ServerGroupInitContainerUpdateMode ServerGroupInitContainerMode = "update" |
| 82 | +) |
| 83 | + |
| 84 | +type ServerGroupInitContainers struct { |
| 85 | + // Containers contains list of containers |
| 86 | + Containers []core.Container `json:"containers,omitempty"` |
| 87 | + |
| 88 | + // Mode keep container replace mode |
| 89 | + Mode *ServerGroupInitContainerMode `json:"mode,omitempty"` |
| 90 | +} |
| 91 | + |
| 92 | +func (s *ServerGroupInitContainers) GetMode() *ServerGroupInitContainerMode { |
| 93 | + if s == nil { |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + return s.Mode |
| 98 | +} |
| 99 | + |
| 100 | +func (s *ServerGroupInitContainers) GetContainers() []core.Container { |
| 101 | + if s == nil { |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + return s.Containers |
| 106 | +} |
| 107 | + |
| 108 | +func (s *ServerGroupInitContainers) Validate() error { |
| 109 | + if s == nil { |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + return shared.WithErrors( |
| 114 | + shared.PrefixResourceError("mode", s.Mode.Validate()), |
| 115 | + shared.PrefixResourceError("containers", s.validateInitContainers()), |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +func (s *ServerGroupInitContainers) validateInitContainers() error { |
| 120 | + for _, c := range s.Containers { |
| 121 | + if err := ValidateServerGroupInitContainerName(c.Name); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
0 commit comments