-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add deletion secret as annotation to content #165
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -523,7 +523,7 @@ func (ctrl *csiSnapshotController) checkandBindSnapshotContent(snapshot *crdv1.V | |
return newContent, nil | ||
} | ||
|
||
func (ctrl *csiSnapshotController) getCreateSnapshotInput(snapshot *crdv1.VolumeSnapshot) (*crdv1.VolumeSnapshotClass, *v1.PersistentVolume, string, map[string]string, error) { | ||
func (ctrl *csiSnapshotController) getCreateSnapshotInput(snapshot *crdv1.VolumeSnapshot) (*crdv1.VolumeSnapshotClass, *v1.PersistentVolume, string, *v1.SecretReference, error) { | ||
className := snapshot.Spec.VolumeSnapshotClassName | ||
klog.V(5).Infof("getCreateSnapshotInput [%s]: VolumeSnapshotClassName [%s]", snapshot.Name, *className) | ||
var class *crdv1.VolumeSnapshotClass | ||
|
@@ -553,12 +553,8 @@ func (ctrl *csiSnapshotController) getCreateSnapshotInput(snapshot *crdv1.Volume | |
if err != nil { | ||
return nil, nil, "", nil, err | ||
} | ||
snapshotterCredentials, err := getCredentials(ctrl.client, snapshotterSecretRef) | ||
if err != nil { | ||
return nil, nil, "", nil, err | ||
} | ||
|
||
return class, volume, contentName, snapshotterCredentials, nil | ||
return class, volume, contentName, snapshotterSecretRef, nil | ||
} | ||
|
||
func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatusOperation(snapshot *crdv1.VolumeSnapshot, content *crdv1.VolumeSnapshotContent) (*crdv1.VolumeSnapshot, error) { | ||
|
@@ -580,10 +576,14 @@ func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatusOperation(sn | |
driverName, snapshotID = content.Spec.CSI.Driver, content.Spec.CSI.SnapshotHandle | ||
} | ||
} else { | ||
class, volume, _, snapshotterCredentials, err := ctrl.getCreateSnapshotInput(snapshot) | ||
class, volume, _, snapshotterSecretRef, err := ctrl.getCreateSnapshotInput(snapshot) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get input parameters to create snapshot %s: %q", snapshot.Name, err) | ||
} | ||
snapshotterCredentials, err := getCredentials(ctrl.client, snapshotterSecretRef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
driverName, snapshotID, creationTime, size, readyToUse, err = ctrl.handler.CreateSnapshot(snapshot, volume, class.Parameters, snapshotterCredentials) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we calling CreateSnapshot here? Shouldn't the snapshot already exist at this point? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to check the status of snapshot if uploading is needed after the snapshot is cut. We call CreateSnapshot for dynamic creation case because ListSnapshots is not required and CreateSnapshot is idempotent so it will just return status of the same snapshot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sounds non-ideal. Usually Create operations have lower QPS limits than Get/List There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot rely on Get/List because ListSnapshots is optional and many drivers don't support that. We just fix a bug recently to check that capability. Note that for static binding, we will call GetSnapshotStatus (which call ListSnapshots CSI function) because CreateSnapshot is not invoked for static binding. Even in that case, we can only use that if ListSnapshots is supported by the driver. Since it is pre-provisioned, we just assume the snapshot is valid and ready if driver didn't implement ListSnapshots. For dynamic creation, we can't assume the snapshot is ready. We have to check the status. Note that in the CSI spec, we specified that "calling CreateSnapshot repeatedly is the preferred way to check if the processing is complete".
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think using CreateSnapshot when we really want GetSnapshot is ideal for the following reasons:
Anyway, this is not strictly related to this particular PR, I think we should open a issue to track this as a future enhancement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, sounds like we need a different issue to track this for further discussion. Will need to bring this up to CSI community too if we don't want to suggest CO to call CreateSnapshot. Many drivers couldn't support ListSnapshots so the only way to find out the status is to call CreateSnapshot. I remember @jingxu97 strongly suggested to use CreateSnapshot so that we can get gRPC code when this was implemented at that time. Initially we used ListSnapshots to find status for all, but I was asked to use CreateSnapshot when CSI went GA (I submitted that PR right after the Shanghai KubeCon last year: #61). Anyway let's discuss it in a different issue. |
||
if err != nil { | ||
klog.Errorf("checkandUpdateBoundSnapshotStatusOperation: failed to call create snapshot to check whether the snapshot is ready to use %q", err) | ||
|
@@ -627,11 +627,16 @@ func (ctrl *csiSnapshotController) createSnapshotOperation(snapshot *crdv1.Volum | |
return nil, err | ||
} | ||
|
||
class, volume, contentName, snapshotterCredentials, err := ctrl.getCreateSnapshotInput(snapshot) | ||
class, volume, contentName, snapshotterSecretRef, err := ctrl.getCreateSnapshotInput(snapshot) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get input parameters to create snapshot %s: %q", snapshot.Name, err) | ||
} | ||
|
||
snapshotterCredentials, err := getCredentials(ctrl.client, snapshotterSecretRef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
driverName, snapshotID, creationTime, size, readyToUse, err := ctrl.handler.CreateSnapshot(snapshot, volume, class.Parameters, snapshotterCredentials) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to take snapshot of the volume, %s: %q", volume.Name, err) | ||
|
@@ -687,6 +692,16 @@ func (ctrl *csiSnapshotController) createSnapshotOperation(snapshot *crdv1.Volum | |
DeletionPolicy: class.DeletionPolicy, | ||
}, | ||
} | ||
|
||
// Set AnnDeletionSecretRefName and AnnDeletionSecretRefNamespace | ||
if snapshotterSecretRef != nil { | ||
klog.V(5).Infof("createSnapshotOperation: set annotation [%s] on content [%s].", AnnDeletionSecretRefName, snapshotContent.Name) | ||
metav1.SetMetaDataAnnotation(&snapshotContent.ObjectMeta, AnnDeletionSecretRefName, snapshotterSecretRef.Name) | ||
|
||
klog.V(5).Infof("syncContent: set annotation [%s] on content [%s].", AnnDeletionSecretRefNamespace, snapshotContent.Name) | ||
metav1.SetMetaDataAnnotation(&snapshotContent.ObjectMeta, AnnDeletionSecretRefNamespace, snapshotterSecretRef.Namespace) | ||
} | ||
|
||
klog.V(3).Infof("volume snapshot content %v", snapshotContent) | ||
// Try to create the VolumeSnapshotContent object several times | ||
for i := 0; i < ctrl.createSnapshotContentRetryCount; i++ { | ||
|
@@ -736,23 +751,30 @@ func (ctrl *csiSnapshotController) deleteSnapshotContentOperation(content *crdv1 | |
|
||
// get secrets if VolumeSnapshotClass specifies it | ||
var snapshotterCredentials map[string]string | ||
snapshotClassName := content.Spec.VolumeSnapshotClassName | ||
if snapshotClassName != nil { | ||
if snapshotClass, err := ctrl.classLister.Get(*snapshotClassName); err == nil { | ||
// Resolve snapshotting secret credentials. | ||
// No VolumeSnapshot is provided when resolving delete secret names, since the VolumeSnapshot may or may not exist at delete time. | ||
snapshotterSecretRef, err := getSecretReference(snapshotClass.Parameters, content.Name, nil) | ||
if err != nil { | ||
return err | ||
} | ||
snapshotterCredentials, err = getCredentials(ctrl.client, snapshotterSecretRef) | ||
if err != nil { | ||
return err | ||
} | ||
var err error | ||
|
||
// Check if annotation exists | ||
if metav1.HasAnnotation(content.ObjectMeta, AnnDeletionSecretRefName) && metav1.HasAnnotation(content.ObjectMeta, AnnDeletionSecretRefNamespace) { | ||
annDeletionSecretName := content.Annotations[AnnDeletionSecretRefName] | ||
annDeletionSecretNamespace := content.Annotations[AnnDeletionSecretRefNamespace] | ||
|
||
snapshotterSecretRef := &v1.SecretReference{} | ||
|
||
if annDeletionSecretName == "" || annDeletionSecretNamespace == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unit test for this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated unit test 1-4 to use |
||
return fmt.Errorf("cannot delete snapshot %#v, err: secret name or namespace not specified", content.Name) | ||
} | ||
|
||
snapshotterSecretRef.Name = annDeletionSecretName | ||
snapshotterSecretRef.Namespace = annDeletionSecretNamespace | ||
|
||
snapshotterCredentials, err = getCredentials(ctrl.client, snapshotterSecretRef) | ||
if err != nil { | ||
// Continue with deletion, as the secret may have already been deleted. | ||
klog.Errorf("Failed to get credentials for snapshot %s: %s", content.Name, err.Error()) | ||
} | ||
} | ||
|
||
err := ctrl.handler.DeleteSnapshot(content, snapshotterCredentials) | ||
err = ctrl.handler.DeleteSnapshot(content, snapshotterCredentials) | ||
if err != nil { | ||
ctrl.eventRecorder.Event(content, v1.EventTypeWarning, "SnapshotDeleteError", "Failed to delete snapshot") | ||
return fmt.Errorf("failed to delete snapshot %#v, err: %v", content.Name, err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually need to create the secret API object somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is on line 1280.