Skip to content

Commit 490ae27

Browse files
authored
Merge pull request #176 from dannawang0221/add-object-mutator
Add mutator functions to allow modify the Kubernetes resources used b…
2 parents e47c5d2 + c13ed5b commit 490ae27

File tree

2 files changed

+283
-81
lines changed

2 files changed

+283
-81
lines changed

populator-machinery/controller.go

+37-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ const (
7878
reasonPopulateOperationStartSuccess = "PopulateOperationStartSuccess"
7979
reasonPopulateOperationFailed = "PopulateOperationFailed"
8080
reasonPopulateOperationFinished = "PopulateOperationFinished"
81-
reasonPVCCreationError = "PopulatorPVCPrimeCreationError"
81+
reasonPVCPrimeCreationError = "PopulatorPVCPrimeCreationError"
82+
reasonPVCPrimeMutatorError = "reasonPVCPrimeMutatorError"
8283
reasonWaitForDataPopulationFinished = "PopulatorWaitForDataPopulationFinished"
8384
reasonStorageClassCreationError = "PopulatorStorageClassCreationError"
8485
reasonDataSourceNotFound = "PopulatorDataSourceNotFound"
@@ -116,6 +117,7 @@ type controller struct {
116117
referenceGrantSynced cache.InformerSynced
117118
podConfig *PodConfig
118119
providerFunctionConfig *ProviderFunctionConfig
120+
mutatorConfig *MutatorConfig
119121
crossNamespace bool
120122
providerMetricManager *ProviderMetricManager
121123
}
@@ -140,6 +142,9 @@ type VolumePopulatorConfig struct {
140142
// ProviderFunctionConfig is the configuration for invoking provider functions. Either PodConfig or ProviderFunctionConfig should
141143
// be specified. PodConfig and ProviderFunctionConfig can't be provided at the same time
142144
ProviderFunctionConfig *ProviderFunctionConfig
145+
// MutatorConfig is the configuration for invoking mutator functions. You can specify your own mutator functions to modify the
146+
// Kubernetes resources used for volume population
147+
MutatorConfig *MutatorConfig
143148
// ProviderMetricManager is the manager for provider specific metric handling
144149
ProviderMetricManager *ProviderMetricManager
145150
// CrossNamespace indicates if the populator supports data sources located in namespaces different than the PVC's namespace.
@@ -189,6 +194,19 @@ type PopulatorParams struct {
189194
Recorder record.EventRecorder
190195
}
191196

197+
type MutatorConfig struct {
198+
// PvcPrimeMutator is the mutator function for pvcPrime. The function gets called to modify the PVC object before pvcPrime gets created.
199+
PvcPrimeMutator func(PvcPrimeMutatorParams) (*corev1.PersistentVolumeClaim, error)
200+
}
201+
202+
// PvcPrimeMutatorParams includes the parameters passing to the PvcPrimeMutator function
203+
type PvcPrimeMutatorParams struct {
204+
// PvcPrime is the temporary PVC created by volume populator
205+
PvcPrime *corev1.PersistentVolumeClaim
206+
// StorageClass is the original StorageClass Pvc refer to
207+
StorageClass *storagev1.StorageClass
208+
}
209+
192210
func RunController(masterURL, kubeconfig, imageName, httpEndpoint, metricsPath, namespace, prefix string,
193211
gk schema.GroupKind, gvr schema.GroupVersionResource, mountPath, devicePath string,
194212
populatorArgs func(bool, *unstructured.Unstructured) ([]string, error),
@@ -292,6 +310,7 @@ func RunControllerWithConfig(vpcfg VolumePopulatorConfig) {
292310
referenceGrantSynced: referenceGrants.Informer().HasSynced,
293311
podConfig: vpcfg.PodConfig,
294312
providerFunctionConfig: vpcfg.ProviderFunctionConfig,
313+
mutatorConfig: vpcfg.MutatorConfig,
295314
crossNamespace: vpcfg.CrossNamespace,
296315
providerMetricManager: vpcfg.ProviderMetricManager,
297316
}
@@ -701,9 +720,24 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
701720
annSelectedNode: nodeName,
702721
}
703722
}
723+
if c.mutatorConfig != nil && c.mutatorConfig.PvcPrimeMutator != nil {
724+
mp := PvcPrimeMutatorParams{
725+
PvcPrime: pvcPrime,
726+
StorageClass: storageClass,
727+
}
728+
pvcPrime, err = c.mutatorConfig.PvcPrimeMutator(mp)
729+
if err != nil {
730+
c.recorder.Eventf(pvc, corev1.EventTypeWarning, reasonPVCPrimeMutatorError, "Failed to mutate populator pvcPrime: %s", err)
731+
return err
732+
}
733+
if pvcPrime == nil {
734+
c.recorder.Eventf(pvc, corev1.EventTypeWarning, reasonPVCPrimeMutatorError, "pvcPrime must not be nil")
735+
return fmt.Errorf("pvcPrime must not be nil")
736+
}
737+
}
704738
pvcPrime, err = c.kubeClient.CoreV1().PersistentVolumeClaims(c.populatorNamespace).Create(ctx, pvcPrime, metav1.CreateOptions{})
705739
if err != nil {
706-
c.recorder.Eventf(pvc, corev1.EventTypeWarning, reasonPVCCreationError, "Failed to create populator PVC prime: %s", err)
740+
c.recorder.Eventf(pvc, corev1.EventTypeWarning, reasonPVCPrimeCreationError, "Failed to create populator pvcPrime: %s", err)
707741
return err
708742
}
709743
}
@@ -734,7 +768,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
734768
if c.providerFunctionConfig.PopulateFn != nil {
735769

736770
if "" == pvcPrime.Spec.VolumeName {
737-
// We'll get called again later when the pvc prime gets bounded
771+
// We'll get called again later when the pvcPrime gets bounded
738772
return nil
739773
}
740774
pv, err := params.KubeClient.CoreV1().PersistentVolumes().Get(ctx, pvcPrime.Spec.VolumeName, metav1.GetOptions{})

0 commit comments

Comments
 (0)