Skip to content

Commit

Permalink
Make Resolver generic in value parameter too (#3742)
Browse files Browse the repository at this point in the history
This is needed to support resolving a map[string]string for collections
of secrets.
  • Loading branch information
matthchr authored Jan 26, 2024
1 parent 162cfdf commit d1997b7
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 40 deletions.
4 changes: 2 additions & 2 deletions v2/internal/genericarmclient/generic_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Test_NewResourceGroup(t *testing.T) {
resourceGroup := testContext.NewTestResourceGroup()
resolved := genruntime.ConvertToARMResolvedDetails{
Name: resourceGroup.Name,
ResolvedReferences: genruntime.MakeResolved[genruntime.ResourceReference](nil),
ResolvedReferences: genruntime.MakeResolved[genruntime.ResourceReference, string](nil),
}
spec, err := resourceGroup.Spec.ConvertToARM(resolved)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -103,7 +103,7 @@ func Test_NewResourceGroup_Error(t *testing.T) {

resolved := genruntime.ConvertToARMResolvedDetails{
Name: rgName,
ResolvedReferences: genruntime.MakeResolved[genruntime.ResourceReference](nil),
ResolvedReferences: genruntime.MakeResolved[genruntime.ResourceReference, string](nil),
}
spec, err := resourceGroup.Spec.ConvertToARM(resolved)
g.Expect(err).ToNot(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion v2/internal/reconcilers/azuresql/local_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (u *localUser) Exists(ctx context.Context) (bool, error) {
return exists, nil
}

func (u *localUser) connectToDB(ctx context.Context, secrets genruntime.Resolved[genruntime.SecretReference]) (*sql.DB, error) {
func (u *localUser) connectToDB(ctx context.Context, secrets genruntime.Resolved[genruntime.SecretReference, string]) (*sql.DB, error) {
details, err := getOwnerDetails(ctx, u.resourceResolver, u.user)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion v2/internal/reconcilers/mysql/local_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (u *localUser) Exists(ctx context.Context) (bool, error) {
return exists, nil
}

func (u *localUser) connectToDB(ctx context.Context, secrets genruntime.Resolved[genruntime.SecretReference]) (*sql.DB, error) {
func (u *localUser) connectToDB(ctx context.Context, secrets genruntime.Resolved[genruntime.SecretReference, string]) (*sql.DB, error) {
serverFQDN, err := getServerFQDN(ctx, u.resourceResolver, u.user)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (r *PostgreSQLUserReconciler) UpdateStatus(ctx context.Context, log logr.Lo
return nil
}

func (r *PostgreSQLUserReconciler) connectToDB(ctx context.Context, _ logr.Logger, user *asopostgresql.User, secrets genruntime.Resolved[genruntime.SecretReference]) (*sql.DB, error) {
func (r *PostgreSQLUserReconciler) connectToDB(ctx context.Context, _ logr.Logger, user *asopostgresql.User, secrets genruntime.Resolved[genruntime.SecretReference, string]) (*sql.DB, error) {
// Get the owner - at this point it must exist
ownerDetails, err := r.ResourceResolver.ResolveOwner(ctx, user)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions v2/internal/resolver/config_map_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package resolver
import (
"context"

"github.com/Azure/azure-service-operator/v2/pkg/genruntime/core"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -17,12 +16,13 @@ import (
"github.com/Azure/azure-service-operator/v2/internal/set"
"github.com/Azure/azure-service-operator/v2/internal/util/kubeclient"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/core"
)

// ConfigMapResolver is a configmap resolver
type ConfigMapResolver interface {
ResolveConfigMapReference(ctx context.Context, ref genruntime.NamespacedConfigMapReference) (string, error)
ResolveConfigMapReferences(ctx context.Context, refs set.Set[genruntime.NamespacedConfigMapReference]) (genruntime.Resolved[genruntime.ConfigMapReference], error)
ResolveConfigMapReferences(ctx context.Context, refs set.Set[genruntime.NamespacedConfigMapReference]) (genruntime.Resolved[genruntime.ConfigMapReference, string], error)
}

// kubeConfigMapResolver resolves Kubernetes config maps
Expand Down Expand Up @@ -66,16 +66,16 @@ func (r *kubeConfigMapResolver) ResolveConfigMapReference(ctx context.Context, r
}

// ResolveConfigMapReferences resolves all provided configmap references
func (r *kubeConfigMapResolver) ResolveConfigMapReferences(ctx context.Context, refs set.Set[genruntime.NamespacedConfigMapReference]) (genruntime.Resolved[genruntime.ConfigMapReference], error) {
func (r *kubeConfigMapResolver) ResolveConfigMapReferences(ctx context.Context, refs set.Set[genruntime.NamespacedConfigMapReference]) (genruntime.Resolved[genruntime.ConfigMapReference, string], error) {
result := make(map[genruntime.ConfigMapReference]string, len(refs))

for ref := range refs {
value, err := r.ResolveConfigMapReference(ctx, ref)
if err != nil {
return genruntime.MakeResolved[genruntime.ConfigMapReference](nil), err
return genruntime.MakeResolved[genruntime.ConfigMapReference, string](nil), err
}
result[ref.ConfigMapReference] = value
}

return genruntime.MakeResolved[genruntime.ConfigMapReference](result), nil
return genruntime.MakeResolved[genruntime.ConfigMapReference, string](result), nil
}
28 changes: 14 additions & 14 deletions v2/internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,25 @@ func (r *Resolver) ResolveReferenceToARMID(ctx context.Context, ref genruntime.N
}

// ResolveReferencesToARMIDs resolves all provided references to their ARM IDs.
func (r *Resolver) ResolveReferencesToARMIDs(ctx context.Context, refs map[genruntime.NamespacedResourceReference]struct{}) (genruntime.Resolved[genruntime.ResourceReference], error) {
func (r *Resolver) ResolveReferencesToARMIDs(ctx context.Context, refs map[genruntime.NamespacedResourceReference]struct{}) (genruntime.Resolved[genruntime.ResourceReference, string], error) {
result := make(map[genruntime.ResourceReference]string, len(refs))

for ref := range refs {
armID, err := r.ResolveReferenceToARMID(ctx, ref)
if err != nil {
return genruntime.MakeResolved[genruntime.ResourceReference](nil), err
return genruntime.MakeResolved[genruntime.ResourceReference, string](nil), err
}
result[ref.ResourceReference] = armID
}

return genruntime.MakeResolved[genruntime.ResourceReference](result), nil
return genruntime.MakeResolved[genruntime.ResourceReference, string](result), nil
}

// ResolveResourceReferences resolves every reference found on the specified genruntime.ARMMetaObject to its corresponding ARM ID.
func (r *Resolver) ResolveResourceReferences(ctx context.Context, metaObject genruntime.ARMMetaObject) (genruntime.Resolved[genruntime.ResourceReference], error) {
func (r *Resolver) ResolveResourceReferences(ctx context.Context, metaObject genruntime.ARMMetaObject) (genruntime.Resolved[genruntime.ResourceReference, string], error) {
refs, err := reflecthelpers.FindResourceReferences(metaObject)
if err != nil {
return genruntime.Resolved[genruntime.ResourceReference]{}, errors.Wrapf(err, "finding references on %q", metaObject.GetName())
return genruntime.Resolved[genruntime.ResourceReference, string]{}, errors.Wrapf(err, "finding references on %q", metaObject.GetName())
}

// Include the namespace
Expand All @@ -121,7 +121,7 @@ func (r *Resolver) ResolveResourceReferences(ctx context.Context, metaObject gen
// resolve them
resolvedRefs, err := r.ResolveReferencesToARMIDs(ctx, namespacedRefs)
if err != nil {
return genruntime.Resolved[genruntime.ResourceReference]{}, errors.Wrapf(err, "failed resolving ARM IDs for references")
return genruntime.Resolved[genruntime.ResourceReference, string]{}, errors.Wrapf(err, "failed resolving ARM IDs for references")
}

return resolvedRefs, nil
Expand Down Expand Up @@ -288,15 +288,15 @@ func (r *Resolver) findGVK(ref genruntime.NamespacedResourceReference) (schema.G
func (r *Resolver) ResolveSecretReferences(
ctx context.Context,
refs set.Set[genruntime.NamespacedSecretReference],
) (genruntime.Resolved[genruntime.SecretReference], error) {
) (genruntime.Resolved[genruntime.SecretReference, string], error) {
return r.kubeSecretResolver.ResolveSecretReferences(ctx, refs)
}

// ResolveResourceSecretReferences resolves all of the specified genruntime.MetaObject's secret references.
func (r *Resolver) ResolveResourceSecretReferences(ctx context.Context, metaObject genruntime.MetaObject) (genruntime.Resolved[genruntime.SecretReference], error) {
func (r *Resolver) ResolveResourceSecretReferences(ctx context.Context, metaObject genruntime.MetaObject) (genruntime.Resolved[genruntime.SecretReference, string], error) {
refs, err := reflecthelpers.FindSecretReferences(metaObject)
if err != nil {
return genruntime.Resolved[genruntime.SecretReference]{}, errors.Wrapf(err, "finding secrets on %q", metaObject.GetName())
return genruntime.Resolved[genruntime.SecretReference, string]{}, errors.Wrapf(err, "finding secrets on %q", metaObject.GetName())
}

// Include the namespace
Expand All @@ -308,7 +308,7 @@ func (r *Resolver) ResolveResourceSecretReferences(ctx context.Context, metaObje
// resolve them
resolvedSecrets, err := r.ResolveSecretReferences(ctx, namespacedSecretRefs)
if err != nil {
return genruntime.Resolved[genruntime.SecretReference]{}, errors.Wrapf(err, "failed resolving secret references")
return genruntime.Resolved[genruntime.SecretReference, string]{}, errors.Wrapf(err, "failed resolving secret references")
}

return resolvedSecrets, nil
Expand All @@ -318,15 +318,15 @@ func (r *Resolver) ResolveResourceSecretReferences(ctx context.Context, metaObje
func (r *Resolver) ResolveConfigMapReferences(
ctx context.Context,
refs set.Set[genruntime.NamespacedConfigMapReference],
) (genruntime.Resolved[genruntime.ConfigMapReference], error) {
) (genruntime.Resolved[genruntime.ConfigMapReference, string], error) {
return r.kubeConfigMapResolver.ResolveConfigMapReferences(ctx, refs)
}

// ResolveResourceConfigMapReferences resolves the specified genruntime.MetaObject's configmap references.
func (r *Resolver) ResolveResourceConfigMapReferences(ctx context.Context, metaObject genruntime.MetaObject) (genruntime.Resolved[genruntime.ConfigMapReference], error) {
func (r *Resolver) ResolveResourceConfigMapReferences(ctx context.Context, metaObject genruntime.MetaObject) (genruntime.Resolved[genruntime.ConfigMapReference, string], error) {
refs, err := reflecthelpers.FindConfigMapReferences(metaObject)
if err != nil {
return genruntime.Resolved[genruntime.ConfigMapReference]{}, errors.Wrapf(err, "finding config maps on %q", metaObject.GetName())
return genruntime.Resolved[genruntime.ConfigMapReference, string]{}, errors.Wrapf(err, "finding config maps on %q", metaObject.GetName())
}

// Include the namespace
Expand All @@ -338,7 +338,7 @@ func (r *Resolver) ResolveResourceConfigMapReferences(ctx context.Context, metaO
// resolve them
resolvedConfigMaps, err := r.ResolveConfigMapReferences(ctx, namespacedConfigMapReferences)
if err != nil {
return genruntime.Resolved[genruntime.ConfigMapReference]{}, errors.Wrapf(err, "failed resolving config map references")
return genruntime.Resolved[genruntime.ConfigMapReference, string]{}, errors.Wrapf(err, "failed resolving config map references")
}

return resolvedConfigMaps, nil
Expand Down
8 changes: 4 additions & 4 deletions v2/internal/resolver/secret_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// SecretResolver is a secret resolver
type SecretResolver interface {
ResolveSecretReference(ctx context.Context, ref genruntime.NamespacedSecretReference) (string, error)
ResolveSecretReferences(ctx context.Context, refs set.Set[genruntime.NamespacedSecretReference]) (genruntime.Resolved[genruntime.SecretReference], error)
ResolveSecretReferences(ctx context.Context, refs set.Set[genruntime.NamespacedSecretReference]) (genruntime.Resolved[genruntime.SecretReference, string], error)
}

// kubeSecretResolver resolves Kubernetes secrets
Expand Down Expand Up @@ -68,16 +68,16 @@ func (r *kubeSecretResolver) ResolveSecretReference(ctx context.Context, ref gen
}

// ResolveSecretReferences resolves all provided secret references
func (r *kubeSecretResolver) ResolveSecretReferences(ctx context.Context, refs set.Set[genruntime.NamespacedSecretReference]) (genruntime.Resolved[genruntime.SecretReference], error) {
func (r *kubeSecretResolver) ResolveSecretReferences(ctx context.Context, refs set.Set[genruntime.NamespacedSecretReference]) (genruntime.Resolved[genruntime.SecretReference, string], error) {
result := make(map[genruntime.SecretReference]string, len(refs))

for ref := range refs {
value, err := r.ResolveSecretReference(ctx, ref)
if err != nil {
return genruntime.MakeResolved[genruntime.SecretReference](nil), err
return genruntime.MakeResolved[genruntime.SecretReference, string](nil), err
}
result[ref.SecretReference] = value
}

return genruntime.MakeResolved[genruntime.SecretReference](result), nil
return genruntime.MakeResolved[genruntime.SecretReference, string](result), nil
}
8 changes: 5 additions & 3 deletions v2/pkg/genruntime/arm_transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ type ConvertToARMResolvedDetails struct {
// TODO: We might be able to remove this in favor of using AzureName() everywhere in the future
Name string

// TODO: We could collapse all of these Resolved[X, string] into a single Resolved[any, string], if we wanted to...

// ResolvedReferences is a set of references which have been resolved to their ARM IDs.
ResolvedReferences Resolved[ResourceReference]
ResolvedReferences Resolved[ResourceReference, string]

// ResolvedSecrets is a set of secret references which have been resolved to the corresponding
// secret value.
ResolvedSecrets Resolved[SecretReference]
ResolvedSecrets Resolved[SecretReference, string]

// ResolvedConfigMaps is a set of config map references which have been resolved to the corresponding
// config map value.
ResolvedConfigMaps Resolved[ConfigMapReference]
ResolvedConfigMaps Resolved[ConfigMapReference, string]
}

type ToARMConverter interface {
Expand Down
2 changes: 1 addition & 1 deletion v2/pkg/genruntime/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func ValidateConfigMapDestinations(destinations []*ConfigMapDestination) (admiss
}

// LookupOptionalConfigMapReferenceValue looks up a ConfigMapReference if it's not nil, or else returns the provided value
func LookupOptionalConfigMapReferenceValue(resolved Resolved[ConfigMapReference], ref *ConfigMapReference, value *string) (string, error) {
func LookupOptionalConfigMapReferenceValue(resolved Resolved[ConfigMapReference, string], ref *ConfigMapReference, value *string) (string, error) {
if ref == nil && value == nil {
return "", errors.Errorf("ref and value are both nil")
}
Expand Down
18 changes: 10 additions & 8 deletions v2/pkg/genruntime/resolved.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,34 @@ type reference interface {
}

// Resolved is a set of references which have been resolved for a particular resource.
type Resolved[T reference] struct {
type Resolved[T reference, V any] struct {
// resolved is a map of T to value.
resolved map[T]string
resolved map[T]V
}

// MakeResolved creates a Resolved
func MakeResolved[T reference](resolvedMap map[T]string) Resolved[T] {
return Resolved[T]{
func MakeResolved[T reference, V any](resolvedMap map[T]V) Resolved[T, V] {
return Resolved[T, V]{
resolved: resolvedMap,
}
}

// Lookup looks up the value for the given reference. If it cannot be found, an error is returned.
func (r Resolved[T]) Lookup(ref T) (string, error) {
func (r Resolved[T, V]) Lookup(ref T) (V, error) {
result, ok := r.resolved[ref]
if !ok {
return "", errors.Errorf("couldn't find resolved %T %s", ref, ref.String())
var ret V
return ret, errors.Errorf("couldn't find resolved %T %s", ref, ref.String())
}
return result, nil
}

// LookupFromPtr looks up the value for the given reference. If the reference is nil, an error is returned.
// If the value cannot be found, an error is returned
func (r Resolved[T]) LookupFromPtr(ref *T) (string, error) {
func (r Resolved[T, V]) LookupFromPtr(ref *T) (V, error) {
if ref == nil {
return "", errors.Errorf("cannot look up secret from nil SecretReference")
var ret V
return ret, errors.Errorf("cannot look up secret from nil SecretReference")
}

return r.Lookup(*ref)
Expand Down

0 comments on commit d1997b7

Please sign in to comment.