containerized-data-importer/pkg/controller/datavolume/external-population-controller.go
Shelly Kagan 5f85c423e1
Integration of Data volume using CDI populators (#2722)
* move cleanup out of dv deletion

It seemed off to call cleanup in the prepare function
just because we don't call cleanup unless the dv is deleting.
Instead we check in the clenup function itself if it should be
done: in this 2 specific cases in case of deletion and in case
the dv succeeded.
The cleanup will be used in future commit also for population cleanup
which we also want to happen not only on deletion.

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Use populator if csi storage class exists

Add new datavolume phase PendingPopulation to
indicate wffc when using populators, this new
phase will be used in kubevirt in order to know
that there is no need for dummy pod to pass wffc phase
and that the population will occur once creating the vm.

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Update population targetPVC with pvc prime annotations

The annotations will be used to update dv that uses the
populators.

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Adjust UT with new behavior

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* updates after review

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Fix import populator report progress

The import pod should be taken from pvcprime

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Prevent requeue upload dv when failing to find progress report pod

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Remove size inflation in populators

The populators are handling existing PVCs.
The PVC already has a defined requested size,
inflating the PVC' with fsoverhead will only be
on the PVC' spec and will not reflect on the target
PVC, this seems undesired.
Instead if the populators is using by PVC that the
datavolume controller created the inflation will happen
there if needed.

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Adjust functional tests to handle dvs using populators

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Fix clone test

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* add shouldUpdateProgress variable to know if need to update progress

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* Change update of annotation from denied list to allowed list

Instead if checking if the annotation on pvcPrime is not desired
go over desired list and if the annotation exists add it.

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* fix removing annotations from pv when rebinding

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* More fixes and UT

Signed-off-by: Shelly Kagan <skagan@redhat.com>

* a bit more updates and UTs

Signed-off-by: Shelly Kagan <skagan@redhat.com>

---------

Signed-off-by: Shelly Kagan <skagan@redhat.com>
2023-06-14 03:16:53 +02:00

226 lines
9.3 KiB
Go

/*
Copyright 2022 The CDI Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package datavolume
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
cc "kubevirt.io/containerized-data-importer/pkg/controller/common"
featuregates "kubevirt.io/containerized-data-importer/pkg/feature-gates"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
)
const (
// ExternalPopulationSucceeded provides a const to indicate that the external population of the PVC has succeeded (reason)
ExternalPopulationSucceeded = "ExternalPopulationSucceeded"
// MessageExternalPopulationSucceeded provides a const to indicate that the external population of the PVC has succeeded (message)
MessageExternalPopulationSucceeded = "PVC %s successfully populated by %s"
// NoAnyVolumeDataSource provides a const to indicate that the AnyVolumeDataSource feature gate is not enabled (reason)
NoAnyVolumeDataSource = "NoAnyVolumeDataSource"
// MessageNoAnyVolumeDataSource provides a const to indicate that the AnyVolumeDataSource feature gate is not enabled (message)
MessageNoAnyVolumeDataSource = "AnyVolumeDataSource feature gate is not enabled: External population not supported"
// NoCSIDriverForExternalPopulation provides a const to indicate that no CSI drivers were found for external population (reason)
NoCSIDriverForExternalPopulation = "NoCSIDriverForExternalPopulation"
// MessageNoCSIDriverForExternalPopulation provides a const to indicate that no CSI drivers were found for external population (message)
MessageNoCSIDriverForExternalPopulation = "No CSI drivers were found: External population not supported"
populatorControllerName = "datavolume-external-population-controller"
)
// PopulatorReconciler members
type PopulatorReconciler struct {
ReconcilerBase
}
// NewPopulatorController creates a new instance of the datavolume external population controller
func NewPopulatorController(ctx context.Context, mgr manager.Manager, log logr.Logger, installerLabels map[string]string) (controller.Controller, error) {
client := mgr.GetClient()
reconciler := &PopulatorReconciler{
ReconcilerBase: ReconcilerBase{
client: client,
scheme: mgr.GetScheme(),
log: log.WithName(populatorControllerName),
recorder: mgr.GetEventRecorderFor(populatorControllerName),
featureGates: featuregates.NewFeatureGates(client),
installerLabels: installerLabels,
shouldUpdateProgress: false,
},
}
datavolumeController, err := controller.New(populatorControllerName, mgr, controller.Options{Reconciler: reconciler})
if err != nil {
return nil, err
}
if err := addDataVolumeControllerCommonWatches(mgr, datavolumeController, dataVolumePopulator); err != nil {
return nil, err
}
return datavolumeController, nil
}
// Reconcile loop for externally populated DataVolumes
func (r *PopulatorReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
return r.reconcile(ctx, req, r)
}
// If dataSourceRef is set (external populator), use an empty spec.Source field
func (r *PopulatorReconciler) prepare(syncState *dvSyncState) error {
if !dvUsesVolumePopulator(syncState.dv) {
return errors.Errorf("undefined population source")
}
// TODO - let's revisit this
syncState.dv.Spec.Source = &cdiv1.DataVolumeSource{}
syncState.dvMutated.Spec.Source = &cdiv1.DataVolumeSource{}
return nil
}
// checkPopulationRequirements returns true if the PVC meets the requirements to be populated
func (r *PopulatorReconciler) checkPopulationRequirements(pvc *corev1.PersistentVolumeClaim, dv *cdiv1.DataVolume, event *Event) (bool, error) {
csiDriverAvailable, err := storageClassCSIDriverExists(r.client, r.log, pvc.Spec.StorageClassName)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
// Non-snapshot population will only work with CSI drivers
if !isSnapshotPopulation(pvc) && !csiDriverAvailable {
r.recorder.Event(pvc, corev1.EventTypeWarning, NoCSIDriverForExternalPopulation, MessageNoCSIDriverForExternalPopulation)
event.reason = NoCSIDriverForExternalPopulation
event.message = MessageNoCSIDriverForExternalPopulation
event.eventType = corev1.EventTypeWarning
return false, nil
}
return r.checkAnyVolumeDataSource(pvc, dv, event), nil
}
// checkAnyVolumeDataSource determines if the AnyVolumeDataSource feature gate is required/enabled
func (r *PopulatorReconciler) checkAnyVolumeDataSource(pvc *corev1.PersistentVolumeClaim, dv *cdiv1.DataVolume, event *Event) bool {
// We don't need the AnyVolumeDataSource feature gate if
// the PVC is externally populated by a PVC or Snapshot.
nonPopulator := isPvcPopulation(pvc) || isSnapshotPopulation(pvc)
// If the AnyVolumeDataSource feature gate is disabled, Kubernetes drops
// the contents of the dataSourceRef field. We can then determine if the
// feature is enabled or not by checking that field after creating the PVC.
enabled := pvc.Spec.DataSourceRef != nil
populationSupported := enabled || nonPopulator
if !populationSupported {
r.recorder.Event(pvc, corev1.EventTypeWarning, NoAnyVolumeDataSource, MessageNoAnyVolumeDataSource)
event.reason = NoAnyVolumeDataSource
event.message = MessageNoAnyVolumeDataSource
event.eventType = corev1.EventTypeWarning
} else {
event.reason = ExternalPopulationSucceeded
event.message = fmt.Sprintf(MessageExternalPopulationSucceeded, pvc.Name, getPopulatorName(pvc))
event.eventType = corev1.EventTypeNormal
}
return populationSupported
}
// getPopulatorName returns the name of the passed PVC's populator
func getPopulatorName(pvc *corev1.PersistentVolumeClaim) string {
var populatorName string
if pvc.Spec.DataSourceRef != nil {
populatorName = pvc.Spec.DataSourceRef.Name
} else {
populatorName = pvc.Spec.DataSource.Name
}
return populatorName
}
// dvUsesVolumePopulator returns true if the datavolume's PVC is meant to be externally populated
func dvUsesVolumePopulator(dv *cdiv1.DataVolume) bool {
return isDataSourcePopulated(dv) || isDataSourceRefPopulated(dv)
}
// isDataSourcePopulated returns true if the DataVolume has a populated DataSource field
func isDataSourcePopulated(dv *cdiv1.DataVolume) bool {
return (dv.Spec.PVC != nil && dv.Spec.PVC.DataSource != nil) ||
(dv.Spec.Storage != nil && dv.Spec.Storage.DataSource != nil)
}
// isDataSourceRefPopulated returns true if the DataVolume has a populated DataSourceRef field
func isDataSourceRefPopulated(dv *cdiv1.DataVolume) bool {
return (dv.Spec.PVC != nil && dv.Spec.PVC.DataSourceRef != nil) ||
(dv.Spec.Storage != nil && dv.Spec.Storage.DataSourceRef != nil)
}
// isPvcPopulation returns true if a PVC's population source is of PVC kind
func isPvcPopulation(pvc *corev1.PersistentVolumeClaim) bool {
return (pvc.Spec.DataSource != nil && pvc.Spec.DataSource.Kind == "PersistentVolumeClaim") ||
(pvc.Spec.DataSourceRef != nil && pvc.Spec.DataSourceRef.Kind == "PersistentVolumeClaim")
}
// isSnapshotPopulation returns true if a PVC's population source is of Snapshot kind
func isSnapshotPopulation(pvc *corev1.PersistentVolumeClaim) bool {
return (pvc.Spec.DataSource != nil && pvc.Spec.DataSource.Kind == "VolumeSnapshot") ||
(pvc.Spec.DataSourceRef != nil && pvc.Spec.DataSourceRef.Kind == "VolumeSnapshot")
}
// Generic controller functions
func (r *PopulatorReconciler) updateAnnotations(dataVolume *cdiv1.DataVolume, pvc *corev1.PersistentVolumeClaim) error {
if !dvUsesVolumePopulator(dataVolume) {
return errors.Errorf("undefined population source")
}
pvc.Annotations[cc.AnnExternalPopulation] = "true"
return nil
}
func (r *PopulatorReconciler) sync(log logr.Logger, req reconcile.Request) (dvSyncResult, error) {
syncState, err := r.syncExternalPopulation(log, req)
if err == nil {
err = r.syncUpdate(log, &syncState)
}
return syncState.dvSyncResult, err
}
func (r *PopulatorReconciler) syncExternalPopulation(log logr.Logger, req reconcile.Request) (dvSyncState, error) {
syncState, syncErr := r.syncCommon(log, req, nil, r.prepare)
if syncErr != nil || syncState.result != nil {
return syncState, syncErr
}
if err := r.handlePvcCreation(log, &syncState, r.updateAnnotations); err != nil {
syncErr = err
}
return syncState, syncErr
}
func (r *PopulatorReconciler) updateStatusPhase(pvc *corev1.PersistentVolumeClaim, dataVolumeCopy *cdiv1.DataVolume, event *Event) error {
// * Population by Snapshots doesn't have additional requirements.
// * Population by PVC requires CSI drivers.
// * Population by external populators requires both CSI drivers and the AnyVolumeDataSource feature gate.
if supported, err := r.checkPopulationRequirements(pvc, dataVolumeCopy, event); err != nil {
return err
} else if supported {
dataVolumeCopy.Status.Phase = cdiv1.Succeeded
}
return nil
}