Skip to content
Open
Changes from 1 commit
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
43 changes: 40 additions & 3 deletions pkg/modifier/csi_modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ import (

"github.com/kubernetes-csi/external-resizer/pkg/csi"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
)

const (
// annotations set by the external-provisioner when a modify secret is configured
modifySecretNameAnn = "volume.kubernetes.io/controller-modify-secret-name"
modifySecretNamespaceAnn = "volume.kubernetes.io/controller-modify-secret-namespace"
)

var ModifyNotSupportErr = errors.New("CSI driver does not support controller modify")

func NewModifierFromClient(
Expand Down Expand Up @@ -86,18 +93,48 @@ func (r *csiModifier) Modify(pv *v1.PersistentVolume, mutableParameters map[stri
return errors.New("empty volume handle")
}

var secrets map[string]string
secrets, err := r.getModifyCredentials(source.ControllerExpandSecretRef, pv.Annotations)
if err != nil {
return err
}

ctx, cancel := timeoutCtx(r.timeout)

defer cancel()
err := r.client.Modify(ctx, volumeID, secrets, mutableParameters)

err = r.client.Modify(ctx, volumeID, secrets, mutableParameters)
if err != nil {
return err
}

return nil
}

// getModifyCredentials fetches the credential from the secret referenced in the annotations. When missing,
// the default secretRef (CSIPersistentVolumeSource.ControllerExpandSecretRef) is used.
func (r *csiModifier) getModifyCredentials(secretRef *v1.SecretReference, annotations map[string]string) (map[string]string, error) {
secretName := annotations[modifySecretNameAnn]
secretNamespace := annotations[modifySecretNamespaceAnn]
if secretNamespace == "" || secretName == "" {
if secretRef == nil {
return nil, nil
}

secretName = secretRef.Name
secretNamespace = secretRef.Namespace
}

secret, err := r.k8sClient.CoreV1().Secrets(secretNamespace).Get(context.TODO(), secretName, metav1.GetOptions{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we reuse the context from the caller please?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a context.TODO() in the top-level modify() call of the ModifyController and passing it all the way down.

Let me know if this wasn't what you were expecting, and I'll adjust it accordingly.

if err != nil {
return nil, fmt.Errorf("error getting secret %s in namespace %s: %v", secretName, secretNamespace, err)
}

credentials := map[string]string{}
for key, value := range secret.Data {
credentials[key] = string(value)
}
return credentials, nil
}

func supportsControllerModify(client csi.Client, timeout time.Duration) (bool, error) {
ctx, cancel := timeoutCtx(timeout)
defer cancel()
Expand Down