Skip to content

Commit

Permalink
restructure vault client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
daanvinken committed Nov 17, 2023
1 parent b35c595 commit d951c49
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions examples/sample/user.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions internal/clients/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions internal/controller/cephuser/cephuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)),
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit d951c49

Please sign in to comment.