Skip to content

Commit

Permalink
Update Longhorn related PV/PVC annnotations for new CSI provisioner
Browse files Browse the repository at this point in the history
1. The new external CSI provisioner will use driver name as its
provisioner name. Previously, the provisioner name is different
from driver name, which is `rancher.io/longhorn` and does not
follow the driver naming schema. Hence we cannot deploy a
compatible provisioner and a related CSI driver with old provisioner
name to handle the existing volumes.

2. The external CSI provisioner uses the annotations of PVC/PV to
find the matching CSI driver. Hence we can updating the annotations
for Longhorn related PVC/PV so that the new provisioner is able to
verify and handle the existing volumes.

Longhorn longhorn#347

Signed-off-by: Shuo Wu <shuo@rancher.com>
  • Loading branch information
Shuo Wu committed Nov 5, 2019
1 parent 93ae2ab commit 4462d48
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func deployCSIDriver(kubeClient *clientset.Clientset, lhClient *lhclientset.Clie
return err
}

if err := upgradeLonghornRelatedComponents(kubeClient, namespace); err != nil {
return err
}

csiDriverObjectDeployment := csi.NewCSIDriverObject()
if err := csiDriverObjectDeployment.Deploy(kubeClient); err != nil {
return err
Expand Down
48 changes: 48 additions & 0 deletions app/upgrader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app

import (
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"

"github.com/longhorn/longhorn-manager/types"
)

const (
PVCAnnotationCSIProvisioner = "volume.beta.kubernetes.io/storage-provisioner"
PVAnnotationCSIProvisioner = "pv.kubernetes.io/provisioned-by"
)

func upgradeLonghornRelatedComponents(kubeClient *clientset.Clientset, namespace string) error {
logrus.Infof("Upgrading Longhorn related components for CSI v1.1.0")

pvList, err := kubeClient.CoreV1().PersistentVolumes().List(metav1.ListOptions{})
if err != nil {
return err
}
for _, pv := range pvList.Items {
if v, exist := pv.Annotations[PVAnnotationCSIProvisioner]; exist {
if v == types.DeprecatedProvisionerName {
pv.Annotations[PVAnnotationCSIProvisioner] = types.LonghornDriverName
_, err := kubeClient.CoreV1().PersistentVolumes().Update(&pv)
if err != nil {
return err
}
}

pvc, err := kubeClient.CoreV1().PersistentVolumeClaims(pv.Spec.ClaimRef.Namespace).Get(pv.Spec.ClaimRef.Name, metav1.GetOptions{})
if err != nil {
return err
}
if v, exist := pvc.Annotations[PVCAnnotationCSIProvisioner]; exist && v == types.DeprecatedProvisionerName {
pvc.Annotations[PVCAnnotationCSIProvisioner] = types.LonghornDriverName
_, err := kubeClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Update(pvc)
if err != nil {
return err
}
}
}
}

return nil
}
2 changes: 2 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const (
LonghornLabelVolume = "longhornvolume"

LonghornDriverName = "driver.longhorn.io"

DeprecatedProvisionerName = "rancher.io/longhorn"
)

const (
Expand Down

0 comments on commit 4462d48

Please # to comment.