/
client_builder.go
52 lines (41 loc) 路 1.5 KB
/
client_builder.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
package cluster
import (
"context"
"github.com/pkg/errors"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
)
// Client is a kubernetes API client.
type Client interface {
Get(ctx context.Context, name, namespace string, obj kubernetes.Object) error
}
// ConfigClientProcessor updates a Config retrieving objects from
// the API server through a client.
type ConfigClientProcessor func(ctx context.Context, client Client, c *Config) error
// ConfigClientBuilder allows to register processors to build a Config
// using a cluster client, retrieving the api objects from the API server.
type ConfigClientBuilder struct {
processors []ConfigClientProcessor
}
// NewConfigClientBuilder builds a new ConfigClientBuilder with
// no processors registered.
func NewConfigClientBuilder() *ConfigClientBuilder {
return &ConfigClientBuilder{}
}
// Register stores processors to be used during Build.
func (b *ConfigClientBuilder) Register(processors ...ConfigClientProcessor) *ConfigClientBuilder {
b.processors = append(b.processors, processors...)
return b
}
// Build constructs a Config for a cluster using the registered processors.
func (b *ConfigClientBuilder) Build(ctx context.Context, client Client, cluster *anywherev1.Cluster) (*Config, error) {
c := &Config{
Cluster: cluster,
}
for _, p := range b.processors {
if err := p(ctx, client, c); err != nil {
return nil, errors.Wrap(err, "building Config from a cluster client")
}
}
return c, nil
}