|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | +) |
| 7 | + |
| 8 | +// IntentionsServicesConfig configures regexp/list of names of services. |
| 9 | +type IntentionsServicesConfig struct { |
| 10 | + // Regexp configures the source services to monitor by matching on the service name. |
| 11 | + // Either Regexp or Names must be configured, not both. |
| 12 | + Regexp *string `mapstructure:"regexp"` |
| 13 | + |
| 14 | + // Names configures the services to monitor by listing the service name. |
| 15 | + // Either Regexp or Names must be configured, not both. |
| 16 | + Names []string `mapstructure:"names"` |
| 17 | +} |
| 18 | + |
| 19 | +// Copy returns a deep copy of IntentionsServicesConfig |
| 20 | +func (c *IntentionsServicesConfig) Copy() *IntentionsServicesConfig { |
| 21 | + if c == nil { |
| 22 | + return nil |
| 23 | + } |
| 24 | + |
| 25 | + var o IntentionsServicesConfig |
| 26 | + o.Regexp = StringCopy(c.Regexp) |
| 27 | + |
| 28 | + if c.Names != nil { |
| 29 | + o.Names = make([]string, 0, len(c.Names)) |
| 30 | + o.Names = append(o.Names, c.Names...) |
| 31 | + } |
| 32 | + return &o |
| 33 | +} |
| 34 | + |
| 35 | +// Merge combines all values in this IntentionsServicesConfig with the values in the other |
| 36 | +// configuration, with values in the other configuration taking precedence. |
| 37 | +// Maps and slices are merged, most other values are overwritten. Complex |
| 38 | +// structs define their own merge functionality. |
| 39 | +func (c *IntentionsServicesConfig) Merge(o *IntentionsServicesConfig) *IntentionsServicesConfig { |
| 40 | + if c == nil { |
| 41 | + if o == nil { |
| 42 | + return nil |
| 43 | + } |
| 44 | + return o.Copy() |
| 45 | + } |
| 46 | + |
| 47 | + if o == nil { |
| 48 | + return c.Copy() |
| 49 | + } |
| 50 | + |
| 51 | + r := c.Copy() |
| 52 | + |
| 53 | + if o.Regexp != nil { |
| 54 | + r.Regexp = StringCopy(o.Regexp) |
| 55 | + } |
| 56 | + |
| 57 | + r.Names = mergeSlices(c.Names, o.Names) |
| 58 | + |
| 59 | + return r |
| 60 | +} |
| 61 | + |
| 62 | +// Finalize ensures there no nil pointers. |
| 63 | +// Exception: `Regexp` is never finalized and can potentially be nil. |
| 64 | +func (c *IntentionsServicesConfig) Finalize() { |
| 65 | + if c == nil { // config not required, return early |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + if c.Names == nil { |
| 70 | + c.Names = []string{} |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func (c *IntentionsServicesConfig) Validate() error { |
| 75 | + if c == nil { // config not required, return early |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + // Check that either regex or names is configured but not both |
| 80 | + namesConfigured := c.Names != nil && len(c.Names) > 0 |
| 81 | + regexConfigured := c.Regexp != nil |
| 82 | + |
| 83 | + if namesConfigured && regexConfigured { |
| 84 | + return fmt.Errorf("regexp and names fields cannot both be " + |
| 85 | + "configured. If both are needed, consider including the list of " + |
| 86 | + "names as part of the regex or creating separate tasks") |
| 87 | + } |
| 88 | + if !namesConfigured && !regexConfigured { |
| 89 | + return fmt.Errorf("either the regexp or names field must be configured") |
| 90 | + } |
| 91 | + |
| 92 | + // Validate regex |
| 93 | + if regexConfigured { |
| 94 | + if _, err := regexp.Compile(StringVal(c.Regexp)); err != nil { |
| 95 | + return fmt.Errorf("unable to compile intentions service regexp: %s", err) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Check that names does not contain empty strings |
| 100 | + if namesConfigured { |
| 101 | + for _, name := range c.Names { |
| 102 | + if name == "" { |
| 103 | + return fmt.Errorf("names field includes empty string(s). " + |
| 104 | + "intentions service names cannot be empty") |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func (c *IntentionsServicesConfig) GoString() string { |
| 113 | + if c == nil { |
| 114 | + return ": (*IntentionsServicesConfig)(nil)" |
| 115 | + } |
| 116 | + |
| 117 | + if len(c.Names) > 0 { |
| 118 | + return fmt.Sprintf( |
| 119 | + "Names:%s", |
| 120 | + c.Names, |
| 121 | + ) |
| 122 | + |
| 123 | + } else { |
| 124 | + return fmt.Sprintf( |
| 125 | + "Regexp:%s", |
| 126 | + StringVal(c.Regexp), |
| 127 | + ) |
| 128 | + |
| 129 | + } |
| 130 | +} |
0 commit comments