|
| 1 | +// Copyright 2025 SolarWinds Worldwide, LLC. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package swok8sdiscovery |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "regexp" |
| 21 | + "strings" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/solarwinds/solarwinds-otel-collector-contrib/internal/k8sconfig" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + k8s "k8s.io/client-go/kubernetes" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + defaultInterval time.Duration = time.Minute * 5 |
| 32 | +) |
| 33 | + |
| 34 | +type Config struct { |
| 35 | + k8sconfig.APIConfig `mapstructure:",squash"` |
| 36 | + |
| 37 | + Interval time.Duration `mapstructure:"interval"` |
| 38 | + |
| 39 | + Reporter string `mapstructure:"reporter"` |
| 40 | + |
| 41 | + Database *DatabaseDiscoveryConfig `mapstructure:"database"` |
| 42 | + |
| 43 | + // For mocking purposes only. |
| 44 | + makeClient func() (k8s.Interface, error) |
| 45 | +} |
| 46 | + |
| 47 | +type DatabaseDiscoveryConfig struct { |
| 48 | + ImageRules []*ImageRule `mapstructure:"image_rules"` |
| 49 | + DomainRules []*DomainRule `mapstructure:"domain_rules"` |
| 50 | +} |
| 51 | + |
| 52 | +type ImageRule struct { |
| 53 | + DatabaseType string `mapstructure:"database_type"` |
| 54 | + // regular expressions patterns to match against container images |
| 55 | + Patterns []string `mapstructure:"patterns"` |
| 56 | + PatternsCompiled []*regexp.Regexp `mapstructure:"-"` // compiled from Patterns during validation |
| 57 | + |
| 58 | + // default port for database communitation if not specified elsewhere |
| 59 | + DefaultPort int32 `mapstructure:"default_port"` |
| 60 | +} |
| 61 | + |
| 62 | +type DomainRule struct { |
| 63 | + DatabaseType string `mapstructure:"database_type"` |
| 64 | + // communication endpoint must match at least one of these patterns |
| 65 | + Patterns []string `mapstructure:"patterns"` |
| 66 | + PatternsCompiled []*regexp.Regexp `mapstructure:"-"` // compiled from Patterns during validation |
| 67 | + |
| 68 | + // in case more DomainRules match, this one will be preferred to be found in service name or endpoint self |
| 69 | + DomainHints []string `mapstructure:"domain_hints"` |
| 70 | +} |
| 71 | + |
| 72 | +func (c *Config) Validate() error { |
| 73 | + if err := c.APIConfig.Validate(); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if c.Interval == 0 { |
| 78 | + c.Interval = defaultInterval |
| 79 | + } |
| 80 | + |
| 81 | + // validate that rules doesn't have databaseType empty |
| 82 | + if c.Database != nil { |
| 83 | + if err := ValidateDatabaseDiscovery(c.Database); err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func ValidateDatabaseDiscovery(databaseDiscovery *DatabaseDiscoveryConfig) error { |
| 92 | + for i := range databaseDiscovery.ImageRules { |
| 93 | + r := databaseDiscovery.ImageRules[i] |
| 94 | + if r.DatabaseType == "" { |
| 95 | + return errors.New("database_type must be specified for all image_rules") |
| 96 | + } |
| 97 | + r.DatabaseType = strings.ToLower(r.DatabaseType) |
| 98 | + |
| 99 | + if len(r.Patterns) == 0 { |
| 100 | + return errors.New("at least one match pattern must be specified for all image_rules") |
| 101 | + } |
| 102 | + |
| 103 | + r.PatternsCompiled = make([]*regexp.Regexp, len(r.Patterns)) |
| 104 | + for j, pattern := range r.Patterns { |
| 105 | + compiled, err := regexp.Compile(pattern) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + r.PatternsCompiled[j] = compiled |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + for i := range databaseDiscovery.DomainRules { |
| 114 | + r := databaseDiscovery.DomainRules[i] |
| 115 | + if r.DatabaseType == "" { |
| 116 | + return errors.New("database_type must be specified for all domain_rules") |
| 117 | + } |
| 118 | + r.DatabaseType = strings.ToLower(r.DatabaseType) |
| 119 | + if len(r.Patterns) == 0 { |
| 120 | + return errors.New("at least one match pattern must be specified for all domain_rules") |
| 121 | + } |
| 122 | + |
| 123 | + r.PatternsCompiled = make([]*regexp.Regexp, len(r.Patterns)) |
| 124 | + for j, pattern := range r.Patterns { |
| 125 | + compiled, err := regexp.Compile(pattern) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + r.PatternsCompiled[j] = compiled |
| 130 | + } |
| 131 | + |
| 132 | + for j, hint := range r.DomainHints { |
| 133 | + r.DomainHints[j] = strings.ToLower(hint) |
| 134 | + } |
| 135 | + } |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func (c *Config) getClient() (k8s.Interface, error) { |
| 140 | + if c.makeClient != nil { |
| 141 | + return c.makeClient() |
| 142 | + } |
| 143 | + return k8sconfig.MakeClient(c.APIConfig) |
| 144 | +} |
| 145 | + |
| 146 | +// listPods lists all pods across all namespaces using the typed client. |
| 147 | +func (c *Config) listPods(ctx context.Context, client k8s.Interface) ([]corev1.Pod, error) { |
| 148 | + pl, err := client.CoreV1().Pods(metav1.NamespaceAll).List(ctx, metav1.ListOptions{}) |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + return pl.Items, nil |
| 153 | +} |
| 154 | + |
| 155 | +// listServices lists all services across all namespaces using the typed client. |
| 156 | +func (c *Config) listServices(ctx context.Context, client k8s.Interface) ([]corev1.Service, error) { |
| 157 | + sl, err := client.CoreV1().Services(metav1.NamespaceAll).List(ctx, metav1.ListOptions{}) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + return sl.Items, nil |
| 162 | +} |
0 commit comments