Skip to content

Commit 1b89788

Browse files
Migrate customizations to eris (#4462)
* Update customizations * fixup customizations * Update handwritten type
1 parent d214ba0 commit 1b89788

File tree

33 files changed

+211
-204
lines changed

33 files changed

+211
-204
lines changed

v2/api/apimanagement/customizations/product_extensions.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement"
1010
"github.com/go-logr/logr"
11-
"github.com/pkg/errors"
11+
"github.com/rotisserie/eris"
1212
ctrl "sigs.k8s.io/controller-runtime"
1313
"sigs.k8s.io/controller-runtime/pkg/conversion"
1414

@@ -32,7 +32,7 @@ func (extension *ProductExtension) Delete(
3232
) (ctrl.Result, error) {
3333
typedObj, ok := obj.(*storage.Product)
3434
if !ok {
35-
return ctrl.Result{}, errors.Errorf("cannot run on unknown resource type %T, expected *apiManagement.Product", obj)
35+
return ctrl.Result{}, eris.Errorf("cannot run on unknown resource type %T, expected *apiManagement.Product", obj)
3636
}
3737

3838
// Type assert that we are the hub type. This will fail to compile if
@@ -42,19 +42,19 @@ func (extension *ProductExtension) Delete(
4242
productName := typedObj.GetName()
4343
id, err := genruntime.GetAndParseResourceID(typedObj)
4444
if err != nil {
45-
return ctrl.Result{}, errors.Wrapf(err, "failed to get the ARM ResourceId for %s", productName)
45+
return ctrl.Result{}, eris.Wrapf(err, "failed to get the ARM ResourceId for %s", productName)
4646
}
4747

4848
if id.Parent == nil {
49-
return ctrl.Result{}, errors.Wrapf(err, ". APIM Product had no parent ID: %s", id.String())
49+
return ctrl.Result{}, eris.Wrapf(err, ". APIM Product had no parent ID: %s", id.String())
5050
}
5151
parentName := id.Parent.Name
5252

5353
// Using armClient.ClientOptions() here ensures we share the same HTTP connection, so this is not opening a new
5454
// connection each time through
5555
clientFactory, err := armapimanagement.NewClientFactory(id.SubscriptionID, armClient.Creds(), armClient.ClientOptions())
5656
if err != nil {
57-
return ctrl.Result{}, errors.Wrapf(err, "failed to create new apimClient")
57+
return ctrl.Result{}, eris.Wrapf(err, "failed to create new apimClient")
5858
}
5959

6060
// This is a synchronous operation
@@ -68,7 +68,7 @@ func (extension *ProductExtension) Delete(
6868
DeleteSubscriptions: to.Ptr(true),
6969
})
7070
if err != nil {
71-
return ctrl.Result{}, errors.Wrapf(err, "failed to delete product %q", productName)
71+
return ctrl.Result{}, eris.Wrapf(err, "failed to delete product %q", productName)
7272
}
7373

7474
return next(ctx, log, resolver, armClient, obj)

v2/api/apimanagement/customizations/subscription_extensions.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement"
1414
"github.com/go-logr/logr"
15-
"github.com/pkg/errors"
15+
"github.com/rotisserie/eris"
1616
v1 "k8s.io/api/core/v1"
1717
"sigs.k8s.io/controller-runtime/pkg/conversion"
1818

@@ -42,7 +42,7 @@ func (ext *SubscriptionExtension) ExportKubernetesSecrets(
4242
// if the hub storage version changes.
4343
typedObj, ok := obj.(*apimanagement.Subscription)
4444
if !ok {
45-
return nil, errors.Errorf("cannot run on unknown resource type %T, expected *apimanagement.Subscription", obj)
45+
return nil, eris.Errorf("cannot run on unknown resource type %T, expected *apimanagement.Subscription", obj)
4646
}
4747

4848
// Type assert that we are the hub type. This will fail to compile if
@@ -62,7 +62,7 @@ func (ext *SubscriptionExtension) ExportKubernetesSecrets(
6262
}
6363

6464
if id.Parent == nil {
65-
return nil, errors.Errorf("APIM subscription had no parent ID: %s", id.String())
65+
return nil, eris.Errorf("APIM subscription had no parent ID: %s", id.String())
6666
}
6767
parentName := id.Parent.Name
6868

@@ -75,13 +75,13 @@ func (ext *SubscriptionExtension) ExportKubernetesSecrets(
7575
var subClient *armapimanagement.SubscriptionClient
7676
subClient, err = armapimanagement.NewSubscriptionClient(subscription, armClient.Creds(), armClient.ClientOptions())
7777
if err != nil {
78-
return nil, errors.Wrapf(err, "failed to create new SubscriptionClient")
78+
return nil, eris.Wrapf(err, "failed to create new SubscriptionClient")
7979
}
8080

8181
var resp armapimanagement.SubscriptionClientListSecretsResponse
8282
resp, err = subClient.ListSecrets(ctx, id.ResourceGroupName, parentName, typedObj.AzureName(), nil)
8383
if err != nil {
84-
return nil, errors.Wrapf(err, "failed listing secrets")
84+
return nil, eris.Wrapf(err, "failed listing secrets")
8585
}
8686

8787
s = resp.SubscriptionKeysContract

v2/api/appconfiguration/customizations/configuration_store_extensions.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
1414
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration"
1515
"github.com/go-logr/logr"
16-
"github.com/pkg/errors"
16+
"github.com/rotisserie/eris"
1717
v1 "k8s.io/api/core/v1"
1818
"sigs.k8s.io/controller-runtime/pkg/conversion"
1919

@@ -53,7 +53,7 @@ func (ext *ConfigurationStoreExtension) ExportKubernetesSecrets(
5353
// if the hub storage version changes.
5454
typedObj, ok := obj.(*storage.ConfigurationStore)
5555
if !ok {
56-
return nil, errors.Errorf("cannot run on unknown resource type %T, expected *appconfiguration.ConfigurationStore", obj)
56+
return nil, eris.Errorf("cannot run on unknown resource type %T, expected *appconfiguration.ConfigurationStore", obj)
5757
}
5858

5959
// Type assert that we are the hub type. This will fail to compile if
@@ -81,7 +81,7 @@ func (ext *ConfigurationStoreExtension) ExportKubernetesSecrets(
8181
var confClient *armappconfiguration.ConfigurationStoresClient
8282
confClient, err = armappconfiguration.NewConfigurationStoresClient(subscription, armClient.Creds(), armClient.ClientOptions())
8383
if err != nil {
84-
return nil, errors.Wrapf(err, "failed to create new ConfigurationStoresClient")
84+
return nil, eris.Wrapf(err, "failed to create new ConfigurationStoresClient")
8585
}
8686

8787
var pager *runtime.Pager[armappconfiguration.ConfigurationStoresClientListKeysResponse]
@@ -90,7 +90,7 @@ func (ext *ConfigurationStoreExtension) ExportKubernetesSecrets(
9090
for pager.More() {
9191
resp, err = pager.NextPage(ctx)
9292
if err != nil {
93-
return nil, errors.Wrapf(err, "failed to retreive response")
93+
return nil, eris.Wrapf(err, "failed to retreive response")
9494
}
9595
addSecretsToMap(resp.Value, keys)
9696
}

v2/api/cache/customizations/redis_extensions.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/redis/armredis"
1515
"github.com/go-logr/logr"
16-
"github.com/pkg/errors"
16+
"github.com/rotisserie/eris"
1717
v1 "k8s.io/api/core/v1"
1818
"sigs.k8s.io/controller-runtime/pkg/conversion"
1919

@@ -43,7 +43,7 @@ func (ext *RedisExtension) ExportKubernetesSecrets(
4343
// if the hub storage version changes.
4444
typedObj, ok := obj.(*redis.Redis)
4545
if !ok {
46-
return nil, errors.Errorf("cannot run on unknown resource type %T, expected *redis.Redis", obj)
46+
return nil, eris.Errorf("cannot run on unknown resource type %T, expected *redis.Redis", obj)
4747
}
4848

4949
// Type assert that we are the hub type. This will fail to compile if
@@ -71,13 +71,13 @@ func (ext *RedisExtension) ExportKubernetesSecrets(
7171
var redisClient *armredis.Client
7272
redisClient, err = armredis.NewClient(subscription, armClient.Creds(), armClient.ClientOptions())
7373
if err != nil {
74-
return nil, errors.Wrapf(err, "failed to create new new RedisClient")
74+
return nil, eris.Wrapf(err, "failed to create new new RedisClient")
7575
}
7676

7777
var resp armredis.ClientListKeysResponse
7878
resp, err = redisClient.ListKeys(ctx, id.ResourceGroupName, typedObj.AzureName(), nil)
7979
if err != nil {
80-
return nil, errors.Wrapf(err, "failed listing keys")
80+
return nil, eris.Wrapf(err, "failed listing keys")
8181
}
8282
accessKeys = resp.AccessKeys
8383
}

v2/api/compute/customizations/vmss_extensions.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
1717
"github.com/go-logr/logr"
18-
"github.com/pkg/errors"
18+
"github.com/rotisserie/eris"
1919
"sigs.k8s.io/controller-runtime/pkg/conversion"
2020

2121
compute "github.com/Azure/azure-service-operator/v2/api/compute/v1api20220301/storage"
@@ -70,7 +70,7 @@ func (e *VirtualMachineScaleSetExtension) ModifyARMResource(
7070
) (genruntime.ARMResource, error) {
7171
typedObj, ok := obj.(*compute.VirtualMachineScaleSet)
7272
if !ok {
73-
return nil, errors.Errorf("cannot run on unknown resource type %T, expected *compute.VirtualMachineScaleSet", obj)
73+
return nil, eris.Errorf("cannot run on unknown resource type %T, expected *compute.VirtualMachineScaleSet", obj)
7474
}
7575

7676
// Type assert that we are the hub type. This will fail to compile if
@@ -85,7 +85,7 @@ func (e *VirtualMachineScaleSetExtension) ModifyARMResource(
8585

8686
apiVersion, err := genruntime.GetAPIVersion(typedObj, kubeClient.Scheme())
8787
if err != nil {
88-
return nil, errors.Wrapf(err, "error getting api version for resource %s while getting status", obj.GetName())
88+
return nil, eris.Wrapf(err, "error getting api version for resource %s while getting status", obj.GetName())
8989
}
9090

9191
// Get the raw resource
@@ -94,15 +94,15 @@ func (e *VirtualMachineScaleSetExtension) ModifyARMResource(
9494
if err != nil {
9595
// If the error is NotFound, the resource we're trying to Create doesn't exist and so no modification is needed
9696
var responseError *azcore.ResponseError
97-
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound {
97+
if eris.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound {
9898
return armObj, nil
9999
}
100-
return nil, errors.Wrapf(err, "getting resource with ID: %q", resourceID)
100+
return nil, eris.Wrapf(err, "getting resource with ID: %q", resourceID)
101101
}
102102

103103
azureExtensions, err := getRawChildCollection(raw, rawChildCollectionPath...)
104104
if err != nil {
105-
return nil, errors.Wrapf(err, "failed to get VMSS Extensions")
105+
return nil, eris.Wrapf(err, "failed to get VMSS Extensions")
106106
}
107107

108108
// If the child collection is not defined, We return the arm object as is here.
@@ -115,7 +115,7 @@ func (e *VirtualMachineScaleSetExtension) ModifyARMResource(
115115

116116
err = setChildCollection(armObj.Spec(), azureExtensions, childCollectionPathARM...)
117117
if err != nil {
118-
return nil, errors.Wrapf(err, "failed to set VMSS Extensions")
118+
return nil, eris.Wrapf(err, "failed to set VMSS Extensions")
119119
}
120120

121121
return armObj, nil
@@ -130,7 +130,7 @@ func getExactParentRecursively(parentValue reflect.Value, childFieldName []strin
130130
fieldName := childFieldName[i]
131131
field := parentValue.FieldByName(fieldName)
132132
if !field.IsValid() {
133-
return reflect.Value{}, errors.Errorf("couldn't find %s field", fieldName)
133+
return reflect.Value{}, eris.Errorf("couldn't find %s field", fieldName)
134134
}
135135

136136
propertiesValue := reflect.Indirect(field)
@@ -147,15 +147,15 @@ func getExactParentRecursively(parentValue reflect.Value, childFieldName []strin
147147
func getChildCollectionField(parent any, fieldPath []string) (ret reflect.Value, err error) {
148148
defer func() {
149149
if x := recover(); x != nil {
150-
err = errors.Errorf("caught panic: %s", x)
150+
err = eris.Errorf("caught panic: %s", x)
151151
}
152152
}()
153153

154154
// Here be dragons
155155
parentValue := reflect.ValueOf(parent)
156156
parentValue = reflect.Indirect(parentValue)
157157
if !parentValue.IsValid() {
158-
return reflect.Value{}, errors.Errorf("cannot assign to nil parent")
158+
return reflect.Value{}, eris.Errorf("cannot assign to nil parent")
159159
}
160160

161161
exactParent, err := getExactParentRecursively(parentValue, fieldPath, 0)
@@ -166,11 +166,11 @@ func getChildCollectionField(parent any, fieldPath []string) (ret reflect.Value,
166166
childFieldName := fieldPath[len(fieldPath)-1]
167167
childField := exactParent.FieldByName(childFieldName)
168168
if !childField.IsValid() {
169-
return reflect.Value{}, errors.Errorf("couldn't find %q field", fieldPath)
169+
return reflect.Value{}, eris.Errorf("couldn't find %q field", fieldPath)
170170
}
171171

172172
if childField.Type().Kind() != reflect.Slice {
173-
return reflect.Value{}, errors.Errorf("%q field was not of kind Slice", fieldPath)
173+
return reflect.Value{}, eris.Errorf("%q field was not of kind Slice", fieldPath)
174174
}
175175

176176
return childField, nil
@@ -192,7 +192,7 @@ func getRawExactParentRecursively(parent map[string]any, fieldSlice []string, i
192192

193193
propsMap, ok := props.(map[string]any)
194194
if !ok {
195-
return nil, errors.Errorf("%s field wasn't a map", prop)
195+
return nil, eris.Errorf("%s field wasn't a map", prop)
196196
}
197197

198198
return getRawExactParentRecursively(propsMap, fieldSlice, i+1)
@@ -211,12 +211,12 @@ func getRawChildCollection(parent map[string]any, fieldSlice ...string) ([]any,
211211
childFieldName := fieldSlice[len(fieldSlice)-1]
212212
childField, ok := exactParent[childFieldName]
213213
if !ok {
214-
return nil, errors.Errorf("couldn't find %q field", fieldSlice)
214+
return nil, eris.Errorf("couldn't find %q field", fieldSlice)
215215
}
216216

217217
childSlice, ok := childField.([]any)
218218
if !ok {
219-
return nil, errors.Errorf("%q field wasn't a slice", fieldSlice)
219+
return nil, eris.Errorf("%q field wasn't a slice", fieldSlice)
220220
}
221221

222222
return childSlice, nil
@@ -225,7 +225,7 @@ func getRawChildCollection(parent map[string]any, fieldSlice ...string) ([]any,
225225
func setChildCollection(parent genruntime.ARMResourceSpec, childCollectionFromAzure []any, childFieldPath ...string) (err error) {
226226
defer func() {
227227
if x := recover(); x != nil {
228-
err = errors.Errorf("caught panic: %s", x)
228+
err = eris.Errorf("caught panic: %s", x)
229229
}
230230
}()
231231

@@ -256,12 +256,12 @@ func setChildCollection(parent genruntime.ARMResourceSpec, childCollectionFromAz
256256
func fuzzySetResource(resource any, embeddedResource reflect.Value) error {
257257
resourceJSON, err := json.Marshal(resource)
258258
if err != nil {
259-
return errors.Wrap(err, "failed to marshal resource JSON")
259+
return eris.Wrap(err, "failed to marshal resource JSON")
260260
}
261261

262262
err = json.Unmarshal(resourceJSON, embeddedResource.Interface())
263263
if err != nil {
264-
return errors.Wrap(err, "failed to unmarshal resource JSON")
264+
return eris.Wrap(err, "failed to unmarshal resource JSON")
265265
}
266266

267267
// TODO: Can't do a trivial fuzzyEqualityComparison here because we don't know which fields are readonly
@@ -288,7 +288,7 @@ func mergeExtensions(extensionField reflect.Value, azureExtensionsSlice reflect.
288288
azureExtension := azureExtensionsSlice.Index(i)
289289
newExtensionName, err := getNameField(azureExtension)
290290
if err != nil {
291-
return reflect.Value{}, errors.Wrapf(err, "failed to get name for new extension")
291+
return reflect.Value{}, eris.Wrapf(err, "failed to get name for new extension")
292292
}
293293
foundExistingExtension := false
294294

@@ -297,7 +297,7 @@ func mergeExtensions(extensionField reflect.Value, azureExtensionsSlice reflect.
297297
var existingName reflect.Value
298298
existingName, err = getNameField(existingExtension)
299299
if err != nil {
300-
return reflect.Value{}, errors.Wrapf(err, "failed to get name for existing extension")
300+
return reflect.Value{}, eris.Wrapf(err, "failed to get name for existing extension")
301301
}
302302

303303
if existingName.String() == newExtensionName.String() {
@@ -317,13 +317,13 @@ func mergeExtensions(extensionField reflect.Value, azureExtensionsSlice reflect.
317317
func getNameField(natValue reflect.Value) (ret reflect.Value, err error) {
318318
defer func() {
319319
if x := recover(); x != nil {
320-
err = errors.Errorf("caught panic: %s", x)
320+
err = eris.Errorf("caught panic: %s", x)
321321
}
322322
}()
323323

324324
nameField := natValue.FieldByName("Name")
325325
if !nameField.IsValid() {
326-
return nameField, errors.Errorf("couldn't find name field")
326+
return nameField, eris.Errorf("couldn't find name field")
327327
}
328328

329329
nameField = reflect.Indirect(nameField)

v2/api/containerservice/customizations/managed_cluster_extensions.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice"
1616
"github.com/go-logr/logr"
17-
"github.com/pkg/errors"
17+
"github.com/rotisserie/eris"
1818
v1 "k8s.io/api/core/v1"
1919
"sigs.k8s.io/controller-runtime/pkg/conversion"
2020

@@ -46,7 +46,7 @@ func (ext *ManagedClusterExtension) ExportKubernetesSecrets(
4646
// if the hub storage version changes.
4747
typedObj, ok := obj.(*containerservice.ManagedCluster)
4848
if !ok {
49-
return nil, errors.Errorf("cannot run on unknown resource type %T, expected *containerservice.ManagedCluster", obj)
49+
return nil, eris.Errorf("cannot run on unknown resource type %T, expected *containerservice.ManagedCluster", obj)
5050
}
5151

5252
// Type assert that we are the hub type. This will fail to compile if
@@ -72,7 +72,7 @@ func (ext *ManagedClusterExtension) ExportKubernetesSecrets(
7272
var mcClient *armcontainerservice.ManagedClustersClient
7373
mcClient, err = armcontainerservice.NewManagedClustersClient(subscription, armClient.Creds(), armClient.ClientOptions())
7474
if err != nil {
75-
return nil, errors.Wrapf(err, "failed to create new ManagedClustersClient")
75+
return nil, eris.Wrapf(err, "failed to create new ManagedClustersClient")
7676
}
7777

7878
// TODO: In the future we may need variants of these secret properties that configure usage of the public FQDN rather than the private one, see:
@@ -82,7 +82,7 @@ func (ext *ManagedClusterExtension) ExportKubernetesSecrets(
8282
var resp armcontainerservice.ManagedClustersClientListClusterAdminCredentialsResponse
8383
resp, err = mcClient.ListClusterAdminCredentials(ctx, id.ResourceGroupName, typedObj.AzureName(), nil)
8484
if err != nil {
85-
return nil, errors.Wrapf(err, "failed listing admin credentials")
85+
return nil, eris.Wrapf(err, "failed listing admin credentials")
8686
}
8787
if len(resp.CredentialResults.Kubeconfigs) > 0 {
8888
// It's awkward that we're ignoring the other possible responses here, but that's what the AZ CLI does too:
@@ -96,7 +96,7 @@ func (ext *ManagedClusterExtension) ExportKubernetesSecrets(
9696
var resp armcontainerservice.ManagedClustersClientListClusterUserCredentialsResponse
9797
resp, err = mcClient.ListClusterUserCredentials(ctx, id.ResourceGroupName, typedObj.AzureName(), nil)
9898
if err != nil {
99-
return nil, errors.Wrapf(err, "failed listing admin credentials")
99+
return nil, eris.Wrapf(err, "failed listing admin credentials")
100100
}
101101
if len(resp.CredentialResults.Kubeconfigs) > 0 {
102102
// It's awkward that we're ignoring the other possible responses here, but that's what the AZ CLI does too:
@@ -179,7 +179,7 @@ func (ext *ManagedClusterExtension) PreReconcileCheck(
179179
managedCluster, ok := obj.(*containerservice.ManagedCluster)
180180
if !ok {
181181
return extensions.PreReconcileCheckResult{},
182-
errors.Errorf("cannot run on unknown resource type %T, expected *containerservice.ManagedCluster", obj)
182+
eris.Errorf("cannot run on unknown resource type %T, expected *containerservice.ManagedCluster", obj)
183183
}
184184

185185
// Type assert that we are the hub type. This will fail to compile if

0 commit comments

Comments
 (0)