Skip to content

Commit f433984

Browse files
Simplify ConfigurationStore extensions to only work with hub version
Co-authored-by: theunrepentantgeek <[email protected]>
1 parent 4717eeb commit f433984

File tree

1 file changed

+35
-176
lines changed

1 file changed

+35
-176
lines changed

v2/api/appconfiguration/customizations/configuration_store_extensions.go

Lines changed: 35 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import (
1717
v1 "k8s.io/api/core/v1"
1818
"sigs.k8s.io/controller-runtime/pkg/conversion"
1919

20-
storage20220501 "github.com/Azure/azure-service-operator/v2/api/appconfiguration/v1api20220501/storage"
21-
storage20240601 "github.com/Azure/azure-service-operator/v2/api/appconfiguration/v1api20240601/storage"
20+
storage "github.com/Azure/azure-service-operator/v2/api/appconfiguration/v1api20240601/storage"
2221
"github.com/Azure/azure-service-operator/v2/internal/genericarmclient"
2322
"github.com/Azure/azure-service-operator/v2/internal/set"
2423
"github.com/Azure/azure-service-operator/v2/internal/util/to"
@@ -50,73 +49,54 @@ func (ext *ConfigurationStoreExtension) ExportKubernetesSecrets(
5049
armClient *genericarmclient.GenericClient,
5150
log logr.Logger,
5251
) (*genruntime.KubernetesSecretExportResult, error) {
53-
// Check which version we're dealing with and handle accordingly
54-
if typedObj20220501, ok := obj.(*storage20220501.ConfigurationStore); ok {
55-
return ext.exportKubernetesSecrets20220501(ctx, typedObj20220501, additionalSecrets, armClient, log)
56-
}
57-
if typedObj20240601, ok := obj.(*storage20240601.ConfigurationStore); ok {
58-
return ext.exportKubernetesSecrets20240601(ctx, typedObj20240601, additionalSecrets, armClient, log)
52+
// This has to be the current hub storage version. It will need to be updated
53+
// if the hub storage version changes.
54+
typedObj, ok := obj.(*storage.ConfigurationStore)
55+
if !ok {
56+
return nil, eris.Errorf("cannot run on unknown resource type %T, expected *appconfiguration.ConfigurationStore", obj)
5957
}
6058

61-
return nil, eris.Errorf("cannot run on unknown resource type %T", obj)
62-
}
59+
// Type assert that we are the hub type. This will fail to compile if
60+
// the hub type has been changed but this extension has not
61+
var _ conversion.Hub = typedObj
6362

64-
func (ext *ConfigurationStoreExtension) exportKubernetesSecrets20220501(
65-
ctx context.Context,
66-
obj *storage20220501.ConfigurationStore,
67-
additionalSecrets set.Set[string],
68-
armClient *genericarmclient.GenericClient,
69-
log logr.Logger,
70-
) (*genruntime.KubernetesSecretExportResult, error) {
71-
primarySecrets := secretsSpecified20220501(obj)
63+
primarySecrets := secretsSpecified(typedObj)
7264
requestedSecrets := set.Union(primarySecrets, additionalSecrets)
7365
if len(requestedSecrets) == 0 {
7466
log.V(Debug).Info("No secrets retrieval to perform as operatorSpec is empty")
7567
return nil, nil
7668
}
7769

78-
keys, err := ext.getAPIKeys(ctx, obj, armClient, log)
70+
id, err := genruntime.GetAndParseResourceID(typedObj)
7971
if err != nil {
8072
return nil, err
8173
}
8274

83-
secretSlice, err := secretsToWrite20220501(obj, keys)
84-
if err != nil {
85-
return nil, err
86-
}
87-
88-
resolvedSecrets := makeResolvedSecretsMap(keys)
89-
90-
return &genruntime.KubernetesSecretExportResult{
91-
Objs: secrets.SliceToClientObjectSlice(secretSlice),
92-
RawSecrets: secrets.SelectSecrets(additionalSecrets, resolvedSecrets),
93-
}, nil
94-
}
95-
96-
func (ext *ConfigurationStoreExtension) exportKubernetesSecrets20240601(
97-
ctx context.Context,
98-
obj *storage20240601.ConfigurationStore,
99-
additionalSecrets set.Set[string],
100-
armClient *genericarmclient.GenericClient,
101-
log logr.Logger,
102-
) (*genruntime.KubernetesSecretExportResult, error) {
103-
// Type assert that we are the hub type. This will fail to compile if
104-
// the hub type has been changed but this extension has not
105-
var _ conversion.Hub = obj
106-
107-
primarySecrets := secretsSpecified20240601(obj)
108-
requestedSecrets := set.Union(primarySecrets, additionalSecrets)
109-
if len(requestedSecrets) == 0 {
110-
log.V(Debug).Info("No secrets retrieval to perform as operatorSpec is empty")
111-
return nil, nil
112-
}
75+
keys := make(map[string]armappconfiguration.APIKey)
76+
// Only bother calling ListKeys if there are secrets to retrieve
77+
if len(requestedSecrets) > 0 {
78+
subscription := id.SubscriptionID
79+
// Using armClient.ClientOptions() here ensures we share the same HTTP connection, so this is not opening a new
80+
// connection each time through
81+
var confClient *armappconfiguration.ConfigurationStoresClient
82+
confClient, err = armappconfiguration.NewConfigurationStoresClient(subscription, armClient.Creds(), armClient.ClientOptions())
83+
if err != nil {
84+
return nil, eris.Wrapf(err, "failed to create new ConfigurationStoresClient")
85+
}
11386

114-
keys, err := ext.getAPIKeys(ctx, obj, armClient, log)
115-
if err != nil {
116-
return nil, err
87+
var pager *runtime.Pager[armappconfiguration.ConfigurationStoresClientListKeysResponse]
88+
var resp armappconfiguration.ConfigurationStoresClientListKeysResponse
89+
pager = confClient.NewListKeysPager(id.ResourceGroupName, typedObj.AzureName(), nil)
90+
for pager.More() {
91+
resp, err = pager.NextPage(ctx)
92+
if err != nil {
93+
return nil, eris.Wrapf(err, "failed to retreive response")
94+
}
95+
addSecretsToMap(resp.Value, keys)
96+
}
11797
}
11898

119-
secretSlice, err := secretsToWrite20240601(obj, keys)
99+
secretSlice, err := secretsToWrite(typedObj, keys)
120100
if err != nil {
121101
return nil, err
122102
}
@@ -129,90 +109,7 @@ func (ext *ConfigurationStoreExtension) exportKubernetesSecrets20240601(
129109
}, nil
130110
}
131111

132-
func (ext *ConfigurationStoreExtension) getAPIKeys(
133-
ctx context.Context,
134-
obj genruntime.ARMMetaObject,
135-
armClient *genericarmclient.GenericClient,
136-
log logr.Logger,
137-
) (map[string]armappconfiguration.APIKey, error) {
138-
id, err := genruntime.GetAndParseResourceID(obj)
139-
if err != nil {
140-
return nil, err
141-
}
142-
143-
keys := make(map[string]armappconfiguration.APIKey)
144-
subscription := id.SubscriptionID
145-
// Using armClient.ClientOptions() here ensures we share the same HTTP connection, so this is not opening a new
146-
// connection each time through
147-
var confClient *armappconfiguration.ConfigurationStoresClient
148-
confClient, err = armappconfiguration.NewConfigurationStoresClient(subscription, armClient.Creds(), armClient.ClientOptions())
149-
if err != nil {
150-
return nil, eris.Wrapf(err, "failed to create new ConfigurationStoresClient")
151-
}
152-
153-
var pager *runtime.Pager[armappconfiguration.ConfigurationStoresClientListKeysResponse]
154-
var resp armappconfiguration.ConfigurationStoresClientListKeysResponse
155-
156-
pager = confClient.NewListKeysPager(id.ResourceGroupName, obj.AzureName(), nil)
157-
for pager.More() {
158-
resp, err = pager.NextPage(ctx)
159-
if err != nil {
160-
return nil, eris.Wrapf(err, "failed to retreive response")
161-
}
162-
addSecretsToMap(resp.Value, keys)
163-
}
164-
165-
return keys, nil
166-
}
167-
168-
func secretsSpecified20220501(obj *storage20220501.ConfigurationStore) set.Set[string] {
169-
if obj.Spec.OperatorSpec == nil || obj.Spec.OperatorSpec.Secrets == nil {
170-
return nil
171-
}
172-
173-
secrets := obj.Spec.OperatorSpec.Secrets
174-
175-
result := make(set.Set[string])
176-
if secrets.PrimaryKeyID != nil {
177-
result.Add(primaryKeyID)
178-
}
179-
if secrets.SecondaryKeyID != nil {
180-
result.Add(secondaryKeyID)
181-
}
182-
if secrets.PrimaryReadOnlyKeyID != nil {
183-
result.Add(primaryReadOnlyKeyID)
184-
}
185-
if secrets.SecondaryReadOnlyKeyID != nil {
186-
result.Add(secondaryReadOnlyKeyID)
187-
}
188-
if secrets.PrimaryKey != nil {
189-
result.Add(primaryKey)
190-
}
191-
if secrets.SecondaryKey != nil {
192-
result.Add(secondaryKey)
193-
}
194-
if secrets.PrimaryReadOnlyKey != nil {
195-
result.Add(primaryReadOnlyKey)
196-
}
197-
if secrets.SecondaryReadOnlyKey != nil {
198-
result.Add(secondaryReadOnlyKey)
199-
}
200-
if secrets.PrimaryConnectionString != nil {
201-
result.Add(primaryConnectionString)
202-
}
203-
if secrets.SecondaryConnectionString != nil {
204-
result.Add(secondaryConnectionString)
205-
}
206-
if secrets.PrimaryReadOnlyConnectionString != nil {
207-
result.Add(primaryReadOnlyConnectionString)
208-
}
209-
if secrets.SecondaryReadOnlyConnectionString != nil {
210-
result.Add(secondaryReadOnlyConnectionString)
211-
}
212-
return result
213-
}
214-
215-
func secretsSpecified20240601(obj *storage20240601.ConfigurationStore) set.Set[string] {
112+
func secretsSpecified(obj *storage.ConfigurationStore) set.Set[string] {
216113
if obj.Spec.OperatorSpec == nil || obj.Spec.OperatorSpec.Secrets == nil {
217114
return nil
218115
}
@@ -268,45 +165,7 @@ func addSecretsToMap(keys []*armappconfiguration.APIKey, result map[string]armap
268165
}
269166
}
270167

271-
func secretsToWrite20220501(obj *storage20220501.ConfigurationStore, keys map[string]armappconfiguration.APIKey) ([]*v1.Secret, error) {
272-
operatorSpecSecrets := obj.Spec.OperatorSpec.Secrets
273-
if operatorSpecSecrets == nil {
274-
return nil, nil
275-
}
276-
277-
collector := secrets.NewCollector(obj.Namespace)
278-
primary, ok := keys["Primary"]
279-
if ok {
280-
collector.AddValue(operatorSpecSecrets.PrimaryConnectionString, to.Value(primary.ConnectionString))
281-
collector.AddValue(operatorSpecSecrets.PrimaryKeyID, to.Value(primary.ID))
282-
collector.AddValue(operatorSpecSecrets.PrimaryKey, to.Value(primary.Value))
283-
}
284-
285-
primaryReadOnly, ok := keys["Primary Read Only"]
286-
if ok {
287-
collector.AddValue(operatorSpecSecrets.PrimaryReadOnlyConnectionString, to.Value(primaryReadOnly.ConnectionString))
288-
collector.AddValue(operatorSpecSecrets.PrimaryReadOnlyKeyID, to.Value(primaryReadOnly.ID))
289-
collector.AddValue(operatorSpecSecrets.PrimaryReadOnlyKey, to.Value(primaryReadOnly.Value))
290-
}
291-
292-
secondary, ok := keys["Secondary"]
293-
if ok {
294-
collector.AddValue(operatorSpecSecrets.SecondaryConnectionString, to.Value(secondary.ConnectionString))
295-
collector.AddValue(operatorSpecSecrets.SecondaryKeyID, to.Value(secondary.ID))
296-
collector.AddValue(operatorSpecSecrets.SecondaryKey, to.Value(secondary.Value))
297-
}
298-
299-
secondaryReadOnly, ok := keys["Secondary Read Only"]
300-
if ok {
301-
collector.AddValue(operatorSpecSecrets.SecondaryReadOnlyConnectionString, to.Value(secondaryReadOnly.ConnectionString))
302-
collector.AddValue(operatorSpecSecrets.SecondaryReadOnlyKeyID, to.Value(secondaryReadOnly.ID))
303-
collector.AddValue(operatorSpecSecrets.SecondaryReadOnlyKey, to.Value(secondaryReadOnly.Value))
304-
}
305-
306-
return collector.Values()
307-
}
308-
309-
func secretsToWrite20240601(obj *storage20240601.ConfigurationStore, keys map[string]armappconfiguration.APIKey) ([]*v1.Secret, error) {
168+
func secretsToWrite(obj *storage.ConfigurationStore, keys map[string]armappconfiguration.APIKey) ([]*v1.Secret, error) {
310169
operatorSpecSecrets := obj.Spec.OperatorSpec.Secrets
311170
if operatorSpecSecrets == nil {
312171
return nil, nil

0 commit comments

Comments
 (0)