|
4 | 4 | package cfgardenobserver // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/cfgardenobserver"
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "errors" |
| 8 | + "fmt" |
7 | 9 | "time"
|
8 | 10 | )
|
9 | 11 |
|
10 | 12 | // Config defines configuration for CF Garden observer.
|
11 | 13 | type Config struct {
|
12 |
| - // The URL of the CF Garden api. Default is "unix:///var/vcap/data/garden/garden.sock" |
13 |
| - Endpoint string `mapstructure:"endpoint"` |
| 14 | + // CloudFoundry API Configuration |
| 15 | + CloudFoundry CfConfig `mapstructure:"cloud_foundry"` |
| 16 | + |
| 17 | + // Garden API Configuration |
| 18 | + Garden GardenConfig `mapstructure:"garden"` |
14 | 19 |
|
15 | 20 | // RefreshInterval determines the frequency at which the observer
|
16 | 21 | // needs to poll for collecting information about new processes.
|
| 22 | + // Default: "1m" |
17 | 23 | RefreshInterval time.Duration `mapstructure:"refresh_interval"`
|
| 24 | + |
| 25 | + // The time to wait before resyncing app information on cached containers |
| 26 | + // using the CloudFoundry API. |
| 27 | + // Default: "5m" |
| 28 | + CacheSyncInterval time.Duration `mapstructure:"cache_sync_interval"` |
| 29 | + |
| 30 | + // Determines whether or not Application labels get added to the Endpoint labels. |
| 31 | + // This requires cloud_foundry to be configured, such that API calls can be made |
| 32 | + // Default: false |
| 33 | + IncludeAppLabels bool `mapstructure:"include_app_labels"` |
| 34 | +} |
| 35 | + |
| 36 | +// Validate overrides the embedded noop validation so that load config can trigger |
| 37 | +// our own validation logic. |
| 38 | +func (config *Config) Validate() error { |
| 39 | + if !config.IncludeAppLabels { |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + c := config.CloudFoundry |
| 44 | + if c.Endpoint == "" { |
| 45 | + return errors.New("CloudFoundry.Endpoint must be specified when IncludeAppLabels is set to true") |
| 46 | + } |
| 47 | + if c.Auth.Type == "" { |
| 48 | + return errors.New("CloudFoundry.Auth.Type must be specified when IncludeAppLabels is set to true") |
| 49 | + } |
| 50 | + |
| 51 | + switch c.Auth.Type { |
| 52 | + case authTypeUserPass: |
| 53 | + if c.Auth.Username == "" { |
| 54 | + return fieldError(authTypeUserPass, "username") |
| 55 | + } |
| 56 | + if c.Auth.Password == "" { |
| 57 | + return fieldError(authTypeUserPass, "password") |
| 58 | + } |
| 59 | + case authTypeClientCredentials: |
| 60 | + if c.Auth.ClientID == "" { |
| 61 | + return fieldError(authTypeClientCredentials, "client_id") |
| 62 | + } |
| 63 | + if c.Auth.ClientSecret == "" { |
| 64 | + return fieldError(authTypeClientCredentials, "client_secret") |
| 65 | + } |
| 66 | + case authTypeToken: |
| 67 | + if c.Auth.AccessToken == "" { |
| 68 | + return fieldError(authTypeToken, "access_token") |
| 69 | + } |
| 70 | + if c.Auth.RefreshToken == "" { |
| 71 | + return fieldError(authTypeToken, "refresh_token") |
| 72 | + } |
| 73 | + default: |
| 74 | + return fmt.Errorf("configuration option `auth_type` must be set to one of the following values: [user_pass, client_credentials, token]. Specified value: %s", c.Auth.Type) |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func fieldError(authType authType, param string) error { |
| 81 | + return fmt.Errorf("%s is required when using auth_type: %s", param, authType) |
| 82 | +} |
| 83 | + |
| 84 | +type GardenConfig struct { |
| 85 | + // The URL of the CF Garden api. Default is "/var/vcap/data/garden/garden.sock" |
| 86 | + Endpoint string `mapstructure:"endpoint"` |
18 | 87 | }
|
| 88 | + |
| 89 | +type CfConfig struct { |
| 90 | + // The URL of the CloudFoundry API |
| 91 | + Endpoint string `mapstructure:"endpoint"` |
| 92 | + |
| 93 | + // Authentication details |
| 94 | + Auth CfAuth `mapstructure:"auth"` |
| 95 | +} |
| 96 | + |
| 97 | +type CfAuth struct { |
| 98 | + // Authentication method, there are 3 options |
| 99 | + Type authType `mapstructure:"type"` |
| 100 | + |
| 101 | + // Used for user_pass authentication method |
| 102 | + Username string `mapstructure:"username"` |
| 103 | + Password string `mapstructure:"password"` |
| 104 | + |
| 105 | + // Used for token authentication method |
| 106 | + AccessToken string `mapstructure:"access_token"` |
| 107 | + RefreshToken string `mapstructure:"refresh_token"` |
| 108 | + |
| 109 | + // Used for client_credentials authentication method |
| 110 | + ClientID string `mapstructure:"client_id"` |
| 111 | + ClientSecret string `mapstructure:"client_secret"` |
| 112 | +} |
| 113 | + |
| 114 | +// authType describes the type of authentication to use for the CloudFoundry API |
| 115 | +type authType string |
| 116 | + |
| 117 | +const ( |
| 118 | + // authTypeClientCredentials uses a client ID and client secret to authenticate |
| 119 | + authTypeClientCredentials authType = "client_credentials" |
| 120 | + // authTypeUserPass uses username and password to authenticate |
| 121 | + authTypeUserPass authType = "user_pass" |
| 122 | + // authTypeToken uses access token and refresh token to authenticate |
| 123 | + authTypeToken authType = "token" |
| 124 | +) |
0 commit comments