Skip to content

Commit

Permalink
Merge pull request #178 from smileusd/pvname_fix
Browse files Browse the repository at this point in the history
reduce the volume name gap with external-provisioner
  • Loading branch information
k8s-ci-robot authored Nov 16, 2024
2 parents a6d1dc5 + a048b2e commit a0dd6f0
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ var (
errStopProvision = errors.New("stop provisioning")
)

type VolumeNameHook func(claim *v1.PersistentVolumeClaim) string

// ProvisionController is a controller that provisions PersistentVolumes for
// PersistentVolumeClaims.
type ProvisionController struct {
Expand Down Expand Up @@ -179,6 +181,8 @@ type ProvisionController struct {
claimsInProgress sync.Map

volumeStore VolumeStore

volumeNameHook VolumeNameHook
}

const (
Expand Down Expand Up @@ -596,6 +600,16 @@ func DeletionTimeout(timeout time.Duration) func(*ProvisionController) error {
}
}

func VolumeName(hook VolumeNameHook) func(*ProvisionController) error {
return func(c *ProvisionController) error {
if c.HasRun() {
return errRuntime
}
c.volumeNameHook = hook
return nil
}
}

// HasRun returns whether the controller has Run
func (ctrl *ProvisionController) HasRun() bool {
ctrl.hasRunLock.Lock()
Expand Down Expand Up @@ -652,6 +666,7 @@ func NewProvisionController(
addFinalizer: DefaultAddFinalizer,
hasRun: false,
hasRunLock: &sync.Mutex{},
volumeNameHook: getProvisionedVolumeNameForClaim,
}

for _, option := range options {
Expand Down Expand Up @@ -1403,7 +1418,7 @@ func (ctrl *ProvisionController) provisionClaimOperation(ctx context.Context, cl
// A previous doProvisionClaim may just have finished while we were waiting for
// the locks. Check that PV (with deterministic name) hasn't been provisioned
// yet.
pvName := ctrl.getProvisionedVolumeNameForClaim(claim)
pvName := ctrl.volumeNameHook(claim)
_, exists, err := ctrl.volumes.GetByKey(pvName)
if err == nil && exists {
// Volume has been already provisioned, nothing to do.
Expand Down Expand Up @@ -1653,7 +1668,7 @@ func getInClusterNamespace() string {

// getProvisionedVolumeNameForClaim returns PV.Name for the provisioned volume.
// The name must be unique.
func (ctrl *ProvisionController) getProvisionedVolumeNameForClaim(claim *v1.PersistentVolumeClaim) string {
func getProvisionedVolumeNameForClaim(claim *v1.PersistentVolumeClaim) string {
return "pvc-" + string(claim.UID)
}

Expand Down

0 comments on commit a0dd6f0

Please sign in to comment.