Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate customizations to eris #4462

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions v2/api/apimanagement/customizations/product_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/rotisserie/eris"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/conversion"

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

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

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

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

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

return next(ctx, log, resolver, armClient, obj)
Expand Down
10 changes: 5 additions & 5 deletions v2/api/apimanagement/customizations/subscription_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

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

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

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

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

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

var resp armapimanagement.SubscriptionClientListSecretsResponse
resp, err = subClient.ListSecrets(ctx, id.ResourceGroupName, parentName, typedObj.AzureName(), nil)
if err != nil {
return nil, errors.Wrapf(err, "failed listing secrets")
return nil, eris.Wrapf(err, "failed listing secrets")
}

s = resp.SubscriptionKeysContract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/rotisserie/eris"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/conversion"

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

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

var pager *runtime.Pager[armappconfiguration.ConfigurationStoresClientListKeysResponse]
Expand All @@ -90,7 +90,7 @@ func (ext *ConfigurationStoreExtension) ExportKubernetesSecrets(
for pager.More() {
resp, err = pager.NextPage(ctx)
if err != nil {
return nil, errors.Wrapf(err, "failed to retreive response")
return nil, eris.Wrapf(err, "failed to retreive response")
}
addSecretsToMap(resp.Value, keys)
}
Expand Down
8 changes: 4 additions & 4 deletions v2/api/cache/customizations/redis_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

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

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

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

var resp armredis.ClientListKeysResponse
resp, err = redisClient.ListKeys(ctx, id.ResourceGroupName, typedObj.AzureName(), nil)
if err != nil {
return nil, errors.Wrapf(err, "failed listing keys")
return nil, eris.Wrapf(err, "failed listing keys")
}
accessKeys = resp.AccessKeys
}
Expand Down
44 changes: 22 additions & 22 deletions v2/api/compute/customizations/vmss_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/rotisserie/eris"
"sigs.k8s.io/controller-runtime/pkg/conversion"

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

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

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

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

azureExtensions, err := getRawChildCollection(raw, rawChildCollectionPath...)
if err != nil {
return nil, errors.Wrapf(err, "failed to get VMSS Extensions")
return nil, eris.Wrapf(err, "failed to get VMSS Extensions")
}

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

err = setChildCollection(armObj.Spec(), azureExtensions, childCollectionPathARM...)
if err != nil {
return nil, errors.Wrapf(err, "failed to set VMSS Extensions")
return nil, eris.Wrapf(err, "failed to set VMSS Extensions")
}

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

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

// Here be dragons
parentValue := reflect.ValueOf(parent)
parentValue = reflect.Indirect(parentValue)
if !parentValue.IsValid() {
return reflect.Value{}, errors.Errorf("cannot assign to nil parent")
return reflect.Value{}, eris.Errorf("cannot assign to nil parent")
}

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

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

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

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

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

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

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

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

err = json.Unmarshal(resourceJSON, embeddedResource.Interface())
if err != nil {
return errors.Wrap(err, "failed to unmarshal resource JSON")
return eris.Wrap(err, "failed to unmarshal resource JSON")
}

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

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

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

nameField := natValue.FieldByName("Name")
if !nameField.IsValid() {
return nameField, errors.Errorf("couldn't find name field")
return nameField, eris.Errorf("couldn't find name field")
}

nameField = reflect.Indirect(nameField)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

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

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

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

// 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:
Expand All @@ -82,7 +82,7 @@ func (ext *ManagedClusterExtension) ExportKubernetesSecrets(
var resp armcontainerservice.ManagedClustersClientListClusterAdminCredentialsResponse
resp, err = mcClient.ListClusterAdminCredentials(ctx, id.ResourceGroupName, typedObj.AzureName(), nil)
if err != nil {
return nil, errors.Wrapf(err, "failed listing admin credentials")
return nil, eris.Wrapf(err, "failed listing admin credentials")
}
if len(resp.CredentialResults.Kubeconfigs) > 0 {
// It's awkward that we're ignoring the other possible responses here, but that's what the AZ CLI does too:
Expand All @@ -96,7 +96,7 @@ func (ext *ManagedClusterExtension) ExportKubernetesSecrets(
var resp armcontainerservice.ManagedClustersClientListClusterUserCredentialsResponse
resp, err = mcClient.ListClusterUserCredentials(ctx, id.ResourceGroupName, typedObj.AzureName(), nil)
if err != nil {
return nil, errors.Wrapf(err, "failed listing admin credentials")
return nil, eris.Wrapf(err, "failed listing admin credentials")
}
if len(resp.CredentialResults.Kubeconfigs) > 0 {
// It's awkward that we're ignoring the other possible responses here, but that's what the AZ CLI does too:
Expand Down Expand Up @@ -179,7 +179,7 @@ func (ext *ManagedClusterExtension) PreReconcileCheck(
managedCluster, ok := obj.(*containerservice.ManagedCluster)
if !ok {
return extensions.PreReconcileCheckResult{},
errors.Errorf("cannot run on unknown resource type %T, expected *containerservice.ManagedCluster", obj)
eris.Errorf("cannot run on unknown resource type %T, expected *containerservice.ManagedCluster", obj)
}

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