|
| 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 | +// |
| 21 | + |
| 22 | +package v1alpha |
| 23 | + |
| 24 | +import ( |
| 25 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 26 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 27 | +) |
| 28 | + |
| 29 | +// MetricsAuthenticationSpec contains spec for authentication with arangodb |
| 30 | +type MetricsAuthenticationSpec struct { |
| 31 | + // JWTTokenSecretName contains the name of the JWT kubernetes secret used for authentication |
| 32 | + JWTTokenSecretName *string `json:"jwtTokenSecretName,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +// MetricsSpec contains spec for arangodb exporter |
| 36 | +type MetricsSpec struct { |
| 37 | + Enabled *bool `json:"enabled,omitempty"` |
| 38 | + Image *string `json:"image,omitempty"` |
| 39 | + Authentication MetricsAuthenticationSpec `json:"authentication,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +// IsEnabled returns whether metrics are enabled or not |
| 43 | +func (s *MetricsSpec) IsEnabled() bool { |
| 44 | + return util.BoolOrDefault(s.Enabled, false) |
| 45 | +} |
| 46 | + |
| 47 | +// HasImage returns whether a image was specified or not |
| 48 | +func (s *MetricsSpec) HasImage() bool { |
| 49 | + return s.Image != nil |
| 50 | +} |
| 51 | + |
| 52 | +// GetImage returns the Image or empty string |
| 53 | +func (s *MetricsSpec) GetImage() string { |
| 54 | + return util.StringOrDefault(s.Image) |
| 55 | +} |
| 56 | + |
| 57 | +// SetDefaults sets default values |
| 58 | +func (s *MetricsSpec) SetDefaults(defaultTokenName string, isAuthenticated bool) { |
| 59 | + if s.Enabled == nil { |
| 60 | + s.Enabled = util.NewBool(false) |
| 61 | + } |
| 62 | + if s.GetJWTTokenSecretName() == "" { |
| 63 | + s.Authentication.JWTTokenSecretName = util.NewString(defaultTokenName) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// GetJWTTokenSecretName returns the token secret name or empty string |
| 68 | +func (s *MetricsSpec) GetJWTTokenSecretName() string { |
| 69 | + return util.StringOrDefault(s.Authentication.JWTTokenSecretName) |
| 70 | +} |
| 71 | + |
| 72 | +// HasJWTTokenSecretName returns true if a secret name was specified |
| 73 | +func (s *MetricsSpec) HasJWTTokenSecretName() bool { |
| 74 | + return s.Authentication.JWTTokenSecretName != nil |
| 75 | +} |
| 76 | + |
| 77 | +// SetDefaultsFrom fills unspecified fields with a value from given source spec. |
| 78 | +func (s *MetricsSpec) SetDefaultsFrom(source MetricsSpec) { |
| 79 | + if s.Enabled == nil { |
| 80 | + s.Enabled = util.NewBoolOrNil(source.Enabled) |
| 81 | + } |
| 82 | + if s.Image == nil { |
| 83 | + s.Image = util.NewStringOrNil(source.Image) |
| 84 | + } |
| 85 | + if s.Authentication.JWTTokenSecretName == nil { |
| 86 | + s.Authentication.JWTTokenSecretName = util.NewStringOrNil(source.Authentication.JWTTokenSecretName) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Validate the given spec |
| 91 | +func (s *MetricsSpec) Validate() error { |
| 92 | + |
| 93 | + if s.HasJWTTokenSecretName() { |
| 94 | + if err := k8sutil.ValidateResourceName(s.GetJWTTokenSecretName()); err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// ResetImmutableFields replaces all immutable fields in the given target with values from the source spec. |
| 103 | +func (s MetricsSpec) ResetImmutableFields(fieldPrefix string, target *MetricsSpec) []string { |
| 104 | + return nil |
| 105 | +} |
0 commit comments