diff --git a/app/driver.go b/app/driver.go index c0eecc8670..5e675e3bd6 100644 --- a/app/driver.go +++ b/app/driver.go @@ -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 diff --git a/app/upgrader.go b/app/upgrader.go new file mode 100644 index 0000000000..3fb5fac43a --- /dev/null +++ b/app/upgrader.go @@ -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 +} diff --git a/types/types.go b/types/types.go index 980fbf353e..3ab3a70729 100644 --- a/types/types.go +++ b/types/types.go @@ -43,6 +43,8 @@ const ( LonghornLabelVolume = "longhornvolume" LonghornDriverName = "driver.longhorn.io" + + DeprecatedProvisionerName = "rancher.io/longhorn" ) const (