Skip to content

Commit 77ead51

Browse files
authored
Merge pull request #544 from nixpanic/ControllerModifyVolume/secrets
Use credentials when calling ControllerModifyVolume
2 parents 34546a9 + 9c8a388 commit 77ead51

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

pkg/modifier/csi_modifier.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ import (
2424

2525
"github.com/kubernetes-csi/external-resizer/pkg/csi"
2626
v1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/client-go/informers"
2829
"k8s.io/client-go/kubernetes"
2930
)
3031

32+
const (
33+
// annotations set by the external-provisioner when a modify secret is configured
34+
modifySecretNameAnn = "volume.kubernetes.io/controller-modify-secret-name"
35+
modifySecretNamespaceAnn = "volume.kubernetes.io/controller-modify-secret-namespace"
36+
)
37+
3138
var ModifyNotSupportErr = errors.New("CSI driver does not support controller modify")
3239

3340
func NewModifierFromClient(
@@ -69,7 +76,7 @@ func (r *csiModifier) Name() string {
6976
return r.name
7077
}
7178

72-
func (r *csiModifier) Modify(pv *v1.PersistentVolume, mutableParameters map[string]string) error {
79+
func (r *csiModifier) Modify(ctx context.Context, pv *v1.PersistentVolume, mutableParameters map[string]string) error {
7380

7481
var volumeID string
7582
var source *v1.CSIPersistentVolumeSource
@@ -86,18 +93,48 @@ func (r *csiModifier) Modify(pv *v1.PersistentVolume, mutableParameters map[stri
8693
return errors.New("empty volume handle")
8794
}
8895

89-
var secrets map[string]string
96+
secrets, err := r.getModifyCredentials(ctx, source.ControllerExpandSecretRef, pv.Annotations)
97+
if err != nil {
98+
return err
99+
}
90100

91101
ctx, cancel := timeoutCtx(r.timeout)
92-
93102
defer cancel()
94-
err := r.client.Modify(ctx, volumeID, secrets, mutableParameters)
103+
104+
err = r.client.Modify(ctx, volumeID, secrets, mutableParameters)
95105
if err != nil {
96106
return err
97107
}
108+
98109
return nil
99110
}
100111

112+
// getModifyCredentials fetches the credential from the secret referenced in the annotations. When missing,
113+
// the default secretRef (CSIPersistentVolumeSource.ControllerExpandSecretRef) is used.
114+
func (r *csiModifier) getModifyCredentials(ctx context.Context, secretRef *v1.SecretReference, annotations map[string]string) (map[string]string, error) {
115+
secretName := annotations[modifySecretNameAnn]
116+
secretNamespace := annotations[modifySecretNamespaceAnn]
117+
if secretNamespace == "" || secretName == "" {
118+
if secretRef == nil {
119+
return nil, nil
120+
}
121+
122+
secretName = secretRef.Name
123+
secretNamespace = secretRef.Namespace
124+
}
125+
126+
secret, err := r.k8sClient.CoreV1().Secrets(secretNamespace).Get(ctx, secretName, metav1.GetOptions{})
127+
if err != nil {
128+
return nil, fmt.Errorf("error getting secret %s in namespace %s: %v", secretName, secretNamespace, err)
129+
}
130+
131+
credentials := map[string]string{}
132+
for key, value := range secret.Data {
133+
credentials[key] = string(value)
134+
}
135+
return credentials, nil
136+
}
137+
101138
func supportsControllerModify(client csi.Client, timeout time.Duration) (bool, error) {
102139
ctx, cancel := timeoutCtx(timeout)
103140
defer cancel()

pkg/modifier/modifier.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package modifier
1818

1919
import (
20+
"context"
21+
2022
v1 "k8s.io/api/core/v1"
2123
)
2224

@@ -25,5 +27,5 @@ type Modifier interface {
2527
// Name returns the modifier's name.
2628
Name() string
2729
// Modify executes the modify operation of this PVC.
28-
Modify(pv *v1.PersistentVolume, mutableParameters map[string]string) error
30+
Modify(ctx context.Context, pv *v1.PersistentVolume, mutableParameters map[string]string) error
2931
}

pkg/modifycontroller/modify_volume.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package modifycontroller
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"maps"
2223
"slices"
@@ -73,6 +74,7 @@ func (ctrl *modifyController) modify(pvc *v1.PersistentVolumeClaim, pv *v1.Persi
7374
return pvc, pv, err, false
7475
}
7576

77+
ctx := context.TODO()
7678
inUncertainState := false
7779
if inProgress {
7880
_, inUncertainState = ctrl.uncertainPVCs.Load(pvcKey)
@@ -89,10 +91,10 @@ func (ctrl *modifyController) modify(pvc *v1.PersistentVolumeClaim, pv *v1.Persi
8991
if err != nil {
9092
return pvc, pv, err, false
9193
}
92-
return ctrl.controllerModifyVolumeWithTarget(pvc, pv, vac)
94+
return ctrl.controllerModifyVolumeWithTarget(ctx, pvc, pv, vac)
9395
}
9496

95-
return ctrl.validateVACAndModifyVolumeWithTarget(pvc, pv)
97+
return ctrl.validateVACAndModifyVolumeWithTarget(ctx, pvc, pv)
9698
}
9799

98100
func (ctrl *modifyController) rolledBack(pvc *v1.PersistentVolumeClaim) (*v1.PersistentVolumeClaim, error) {
@@ -123,6 +125,7 @@ func (ctrl *modifyController) getTargetVAC(pvc *v1.PersistentVolumeClaim, vacNam
123125
// func validateVACAndModifyVolumeWithTarget validate the VAC. The function sets pvc.Status.ModifyVolumeStatus
124126
// to Pending if VAC does not exist and proceeds to trigger ModifyVolume if VAC exists
125127
func (ctrl *modifyController) validateVACAndModifyVolumeWithTarget(
128+
ctx context.Context,
126129
pvc *v1.PersistentVolumeClaim,
127130
pv *v1.PersistentVolume) (*v1.PersistentVolumeClaim, *v1.PersistentVolume, error, bool) {
128131

@@ -141,18 +144,19 @@ func (ctrl *modifyController) validateVACAndModifyVolumeWithTarget(
141144
// Record an event to indicate that external resizer is modifying this volume.
142145
ctrl.eventRecorder.Event(pvc, v1.EventTypeNormal, util.VolumeModify,
143146
fmt.Sprintf("external resizer is modifying volume %s with vac %s", pvc.Name, vac.Name))
144-
return ctrl.controllerModifyVolumeWithTarget(pvc, pv, vac)
147+
return ctrl.controllerModifyVolumeWithTarget(ctx, pvc, pv, vac)
145148
}
146149

147150
// func controllerModifyVolumeWithTarget trigger the CSI ControllerModifyVolume API call
148151
// and handle both success and error scenarios
149152
func (ctrl *modifyController) controllerModifyVolumeWithTarget(
153+
ctx context.Context,
150154
pvc *v1.PersistentVolumeClaim,
151155
pv *v1.PersistentVolume,
152156
vacObj *storagev1.VolumeAttributesClass,
153157
) (*v1.PersistentVolumeClaim, *v1.PersistentVolume, error, bool) {
154158
var err error
155-
pvc, pv, err = ctrl.callModifyVolumeOnPlugin(pvc, pv, vacObj)
159+
pvc, pv, err = ctrl.callModifyVolumeOnPlugin(ctx, pvc, pv, vacObj)
156160
if err == nil {
157161
klog.V(4).Infof("Update volumeAttributesClass of PV %q to %s succeeded", pv.Name, vacObj.Name)
158162
// Record an event to indicate that modify operation is successful.
@@ -195,6 +199,7 @@ func (ctrl *modifyController) controllerModifyVolumeWithTarget(
195199
}
196200

197201
func (ctrl *modifyController) callModifyVolumeOnPlugin(
202+
ctx context.Context,
198203
pvc *v1.PersistentVolumeClaim,
199204
pv *v1.PersistentVolume,
200205
vac *storagev1.VolumeAttributesClass) (*v1.PersistentVolumeClaim, *v1.PersistentVolume, error) {
@@ -209,7 +214,7 @@ func (ctrl *modifyController) callModifyVolumeOnPlugin(
209214
parameters[pvcNamespaceKey] = pvc.GetNamespace()
210215
parameters[pvNameKey] = pv.GetName()
211216
}
212-
err := ctrl.modifier.Modify(pv, parameters)
217+
err := ctrl.modifier.Modify(ctx, pv, parameters)
213218

214219
if err != nil {
215220
return pvc, pv, err

0 commit comments

Comments
 (0)