-
Notifications
You must be signed in to change notification settings - Fork 41
/
config.go
168 lines (151 loc) · 6.35 KB
/
config.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
package gcp
import (
"context"
"encoding/json"
"fmt"
"github.com/integr8ly/cloud-resource-operator/pkg/resources"
errorUtil "github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
controllerruntime "sigs.k8s.io/controller-runtime"
"time"
"github.com/integr8ly/cloud-resource-operator/internal/k8sutil"
"github.com/integr8ly/cloud-resource-operator/pkg/providers"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
DefaultConfigMapName = "cloud-resources-gcp-strategies"
defaultReconcileTime = time.Second * 30
DefaultFinalizer = "cloud-resources-operator.integreatly.org/finalizers"
defaultGcpIdentifierLength = 40
)
// DefaultConfigMapNamespace is the default namespace that Configmaps will be created in
var DefaultConfigMapNamespace, _ = k8sutil.GetWatchNamespace()
type StrategyConfig struct {
Region string `json:"region"`
ProjectID string `json:"projectID"`
CreateStrategy json.RawMessage `json:"createStrategy"`
DeleteStrategy json.RawMessage `json:"deleteStrategy"`
}
//go:generate moq -out config_moq.go . ConfigManager
type ConfigManager interface {
ReadStorageStrategy(ctx context.Context, rt providers.ResourceType, tier string) (*StrategyConfig, error)
}
type ConfigMapConfigManager struct {
configMapName string
configMapNamespace string
client client.Client
}
func NewConfigMapConfigManager(cm string, namespace string, client client.Client) *ConfigMapConfigManager {
if cm == "" {
cm = DefaultConfigMapName
}
if namespace == "" {
namespace = DefaultConfigMapNamespace
}
return &ConfigMapConfigManager{
configMapName: cm,
configMapNamespace: namespace,
client: client,
}
}
func NewDefaultConfigManager(client client.Client) *ConfigMapConfigManager {
return NewConfigMapConfigManager(DefaultConfigMapName, DefaultConfigMapNamespace, client)
}
func (cmm *ConfigMapConfigManager) ReadStorageStrategy(ctx context.Context, rt providers.ResourceType, tier string) (*StrategyConfig, error) {
stratCfg, err := cmm.getTierStrategyForProvider(ctx, string(rt), tier)
if err != nil {
return nil, errorUtil.Wrapf(err, "failed to get tier to strategy mapping for resource type %s", string(rt))
}
return stratCfg, nil
}
func (cmm *ConfigMapConfigManager) getTierStrategyForProvider(ctx context.Context, rt string, tier string) (*StrategyConfig, error) {
cm, err := resources.GetConfigMapOrDefault(ctx, cmm.client, types.NamespacedName{Name: cmm.configMapName, Namespace: cmm.configMapNamespace}, BuildDefaultConfigMap(cmm.configMapName, cmm.configMapNamespace))
if err != nil {
return nil, errorUtil.Wrapf(err, "failed to get gcp strategy config map %s in namespace %s", cmm.configMapName, cmm.configMapNamespace)
}
rawStrategyMapping := cm.Data[rt]
if rawStrategyMapping == "" {
return nil, errorUtil.New(fmt.Sprintf("gcp strategy for resource type %s is not defined", rt))
}
var strategyMapping map[string]*StrategyConfig
if err = json.Unmarshal([]byte(rawStrategyMapping), &strategyMapping); err != nil {
return nil, errorUtil.Wrapf(err, "failed to unmarshal strategy mapping for resource type %s", rt)
}
strategyConfig := strategyMapping[tier]
if strategyConfig == nil {
return nil, errorUtil.New(fmt.Sprintf("no strategy found for deployment type %s and deployment tier %s", rt, tier))
}
if strategyConfig.ProjectID == "" {
defaultProject, err := GetProjectFromStrategyOrDefault(ctx, cmm.client, strategyConfig)
if err != nil {
return nil, errorUtil.Wrap(err, "failed to get default gcp project")
}
strategyConfig.ProjectID = defaultProject
}
if strategyConfig.Region == "" {
defaultRegion, err := GetRegionFromStrategyOrDefault(ctx, cmm.client, strategyConfig)
if err != nil {
return nil, errorUtil.Wrap(err, "failed to get default gcp region")
}
strategyConfig.Region = defaultRegion
}
return strategyConfig, nil
}
func BuildDefaultConfigMap(name, namespace string) *v1.ConfigMap {
return &v1.ConfigMap{
ObjectMeta: controllerruntime.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: map[string]string{
"blobstorage": `{"development": { "region": "", "projectID": "", "createStrategy": {}, "deleteStrategy": {} }, "production": { "region": "", "projectID": "", "createStrategy": {}, "deleteStrategy": {} }}`,
"redis": `{"development": { "region": "", "projectID": "", "createStrategy": {}, "deleteStrategy": {} }, "production": { "region": "", "projectID": "", "createStrategy": {}, "deleteStrategy": {} }}`,
"postgres": `{"development": { "region": "", "projectID": "", "createStrategy": {}, "deleteStrategy": {} }, "production": { "region": "", "projectID": "", "createStrategy": {}, "deleteStrategy": {} }}`,
"_network": `{"development": { "region": "", "projectID": "", "createStrategy": {}, "deleteStrategy": {} }, "production": { "region": "", "projectID": "", "createStrategy": {}, "deleteStrategy": {} }}`,
},
}
}
func GetRegionFromStrategyOrDefault(ctx context.Context, c client.Client, strategy *StrategyConfig) (string, error) {
defaultRegion, err := getDefaultRegion(ctx, c)
if err != nil {
return "", errorUtil.Wrap(err, "failed to get default region")
}
region := strategy.Region
if region == "" {
region = defaultRegion
}
return region, nil
}
func getDefaultRegion(ctx context.Context, c client.Client) (string, error) {
region, err := resources.GetGCPRegion(ctx, c)
if err != nil {
return "", errorUtil.Wrap(err, "failed to retrieve region from cluster")
}
if region == "" {
return "", errorUtil.New("failed to retrieve region from cluster, region is not defined")
}
return region, nil
}
func GetProjectFromStrategyOrDefault(ctx context.Context, c client.Client, strategy *StrategyConfig) (string, error) {
defaultProject, err := getDefaultProject(ctx, c)
if err != nil {
return "", errorUtil.Wrap(err, "failed to get default project")
}
project := strategy.ProjectID
if project == "" {
project = defaultProject
}
return project, nil
}
func getDefaultProject(ctx context.Context, c client.Client) (string, error) {
defaultProject, err := resources.GetGCPProject(ctx, c)
if err != nil {
return "", errorUtil.Wrap(err, "failed to retrieve project from cluster")
}
if defaultProject == "" {
return "", errorUtil.New("failed to retrieve project from cluster, project ID is not defined")
}
return defaultProject, nil
}
var _ ConfigManager = (*ConfigMapConfigManager)(nil)