From d951c49071339a7ddea467f4d6e49a7c80021b84 Mon Sep 17 00:00:00 2001 From: daanvi Date: Fri, 17 Nov 2023 14:10:53 -0600 Subject: [PATCH] restructure vault client creation --- examples/sample/user.yaml | 6 +++--- internal/clients/vault/vault.go | 8 ++++++++ internal/controller/cephuser/cephuser.go | 17 +++++++---------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/examples/sample/user.yaml b/examples/sample/user.yaml index 25f5214..45297fa 100644 --- a/examples/sample/user.yaml +++ b/examples/sample/user.yaml @@ -1,12 +1,12 @@ apiVersion: ceph.radosgw.crossplane.io/v1alpha1 kind: CephUser metadata: - name: my-ceph-user-y + name: my-ceph-user-z spec: deletionPolicy: Delete forProvider: - displayedName: my-ceph-user-y - uid: myuser-y + displayedName: my-ceph-user-z + uid: myuser-z userQuotaMaxBuckets: 5 userQuotaMaxObjects: 1000 userQuotaMaxSizeKB: 204800 diff --git a/internal/clients/vault/vault.go b/internal/clients/vault/vault.go index 1f818ab..13fa940 100644 --- a/internal/clients/vault/vault.go +++ b/internal/clients/vault/vault.go @@ -58,6 +58,14 @@ func NewVaultClientForCephAdmins() (*vault.Client, error) { return NewVaultClient(vaultConfig) } +func NewVaultClientWithPanic(config v1alpha1.VaultConfig) *vault.Client { + client, err := NewVaultClient(config) + if err != nil { + panic(err) + } + return client +} + func WriteSecretsToVault(client *vault.Client, vaultConfig v1alpha1.VaultConfig, key *string, data *map[string]interface{}) error { if vaultConfig.KVVersion == "1" { err := client.KVv1(vaultConfig.MountPath).Put(context.TODO(), *key, *data) diff --git a/internal/controller/cephuser/cephuser.go b/internal/controller/cephuser/cephuser.go index 63d2df2..d106758 100644 --- a/internal/controller/cephuser/cephuser.go +++ b/internal/controller/cephuser/cephuser.go @@ -29,8 +29,9 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/daanvinken/provider-radosgw/apis/ceph/v1alpha1" apisv1alpha1 "github.com/daanvinken/provider-radosgw/apis/v1alpha1" + pc_v1alpha1 "github.com/daanvinken/provider-radosgw/apis/v1alpha1" "github.com/daanvinken/provider-radosgw/internal/clients/radosgw" - vault "github.com/daanvinken/provider-radosgw/internal/clients/vault" + "github.com/daanvinken/provider-radosgw/internal/clients/vault" vault_sdk "github.com/hashicorp/vault/api" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/types" @@ -81,6 +82,7 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { kube: mgr.GetClient(), usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &apisv1alpha1.ProviderConfigUsage{}), newRadosgwClientFn: radosgw.NewRadosgwClient, + newVaultClientFn: vault.NewVaultClientWithPanic, vaultAdminClient: vaultAdminClient, log: o.Logger.WithValues("controller", name)}), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -102,9 +104,9 @@ type connector struct { kube client.Client usage resource.Tracker newRadosgwClientFn func(host string, credentials radosgw.Credentials) *radosgw_admin.API - //newVaultClient func(host string, credentials radosgw.Credentials) *radosgw_admin.API - log logging.Logger - vaultAdminClient *vault_sdk.Client + newVaultClientFn func(config pc_v1alpha1.VaultConfig) *vault_sdk.Client + log logging.Logger + vaultAdminClient *vault_sdk.Client } // Connect typically produces an ExternalClient by: @@ -130,11 +132,6 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E return nil, errors.Wrap(err, errGetPC) } - vaultClient, err := vault.NewVaultClient(pc.Spec.CredentialsVault) - if err != nil { - return nil, errors.Wrap(err, errVaultClientCreate) - } - radosgwCredentials, err := GetAdminCredentials(c.vaultAdminClient, pc) if err != nil { return nil, errors.Wrap(err, errFetchSecretAdmin) @@ -144,7 +141,7 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E rgwClient: c.newRadosgwClientFn(pc.Spec.HostName, radosgwCredentials), kubeClient: c.kube, log: c.log, - vaultClient: vaultClient, + vaultClient: c.newVaultClientFn(pc.Spec.CredentialsVault), }, err }