-
Notifications
You must be signed in to change notification settings - Fork 90
/
scaleway.go
186 lines (158 loc) · 5.9 KB
/
scaleway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package scaleway
import (
"encoding"
"fmt"
"reflect"
"time"
"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/component/common/config"
"github.com/grafana/alloy/internal/component/discovery"
"github.com/grafana/alloy/internal/featuregate"
"github.com/grafana/alloy/syntax/alloytypes"
prom_config "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
prom_discovery "github.com/prometheus/prometheus/discovery/scaleway"
"github.com/scaleway/scaleway-sdk-go/scw"
"gopkg.in/yaml.v2"
)
func init() {
component.Register(component.Registration{
Name: "discovery.scaleway",
Stability: featuregate.StabilityGenerallyAvailable,
Args: Arguments{},
Exports: discovery.Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
},
})
}
type Arguments struct {
Project string `alloy:"project_id,attr"`
Role Role `alloy:"role,attr"`
APIURL string `alloy:"api_url,attr,optional"`
Zone string `alloy:"zone,attr,optional"`
AccessKey string `alloy:"access_key,attr"`
SecretKey alloytypes.Secret `alloy:"secret_key,attr,optional"`
SecretKeyFile string `alloy:"secret_key_file,attr,optional"`
NameFilter string `alloy:"name_filter,attr,optional"`
TagsFilter []string `alloy:"tags_filter,attr,optional"`
RefreshInterval time.Duration `alloy:"refresh_interval,attr,optional"`
Port int `alloy:"port,attr,optional"`
ProxyConfig *config.ProxyConfig `alloy:",squash"`
TLSConfig config.TLSConfig `alloy:"tls_config,block,optional"`
FollowRedirects bool `alloy:"follow_redirects,attr,optional"`
EnableHTTP2 bool `alloy:"enable_http2,attr,optional"`
}
var DefaultArguments = Arguments{
APIURL: "https://api.scaleway.com",
Zone: scw.ZoneFrPar1.String(),
RefreshInterval: 60 * time.Second,
Port: 80,
FollowRedirects: true,
EnableHTTP2: true,
}
// SetToDefault implements syntax.Defaulter.
func (args *Arguments) SetToDefault() {
*args = DefaultArguments
}
// Validate implements syntax.Validator.
func (args *Arguments) Validate() error {
if args.Project == "" {
return fmt.Errorf("project_id must not be empty")
}
if args.SecretKey == "" && args.SecretKeyFile == "" {
return fmt.Errorf("exactly one of secret_key or secret_key_file must be configured")
} else if args.SecretKey != "" && args.SecretKeyFile != "" {
return fmt.Errorf("exactly one of secret_key or secret_key_file must be configured")
}
if args.AccessKey == "" {
return fmt.Errorf("access_key must not be empty")
}
if err := args.ProxyConfig.Validate(); err != nil {
return err
}
// Test UnmarshalYAML against the upstream type which has custom validations.
//
// TODO(rfratto): decouple upstream validation into a separate method so this
// can be called directly.
err := (&prom_discovery.SDConfig{}).UnmarshalYAML(func(i interface{}) error {
// Here, i is an internal type (*scaleway.plain) that we can't reference or
// use.
//
// Given what we know of Prometheus SD patterns, we can do an unsafe cast
// to the public type and set it. See scaleway_tests.go for tests to ensure
// this assumption doesn't break.
//
// This will no longer be necessary once we can call a Validate method
// instead of UnmarshalYAML.
ptr := (*prom_discovery.SDConfig)(reflect.ValueOf(i).UnsafePointer())
*ptr = *args.Convert()
return nil
})
return err
}
func (args *Arguments) Convert() *prom_discovery.SDConfig {
out := &prom_discovery.SDConfig{
Project: args.Project,
APIURL: args.APIURL,
Zone: args.Zone,
AccessKey: args.AccessKey,
SecretKey: prom_config.Secret(args.SecretKey),
SecretKeyFile: args.SecretKeyFile,
NameFilter: args.NameFilter,
TagsFilter: args.TagsFilter,
HTTPClientConfig: prom_config.HTTPClientConfig{
ProxyConfig: args.ProxyConfig.Convert(),
TLSConfig: *args.TLSConfig.Convert(),
FollowRedirects: args.FollowRedirects,
EnableHTTP2: args.EnableHTTP2,
},
RefreshInterval: model.Duration(args.RefreshInterval),
Port: args.Port,
// Role uses an internal type, preventing us from setting it explicitly.
// This means we must use YAML unmarshaling to set it.
//
// TODO(rfratto): expose the role type upstream to avoid needing YAML
// unmarshaling.
}
if err := yaml.Unmarshal([]byte(args.Role), &out.Role); err != nil {
// This should never happen; we know that our role is valid at this point.
panic(err)
}
return out
}
// Role is the role of the target within the Scaleway Ecosystem.
type Role string
const (
// RoleBaremetal represents a Scaleway Elements Baremetal server.
RoleBaremetal Role = "baremetal"
// RoleInstance represents a Scaleway Elements Instance virtual server.
RoleInstance Role = "instance"
)
var (
_ encoding.TextMarshaler = Role("")
_ encoding.TextUnmarshaler = (*Role)(nil)
)
// MarshalText implements encoding.TextMarshaler, returning the raw bytes of
// the Role.
func (r Role) MarshalText() (text []byte, err error) {
return []byte(r), nil
}
// UnmarshalText implements encoding.TextUnmarshaler. UnmarshalText returns an
// error if the text is not recognized as a valid Role.
func (r *Role) UnmarshalText(text []byte) error {
switch Role(text) {
case RoleBaremetal, RoleInstance:
*r = Role(text)
return nil
default:
return fmt.Errorf("invalid role %q", text)
}
}
// New returns a new instance of a discovery.scaleway component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
newArgs := args.(Arguments)
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger)
})
}