Skip to content

Commit

Permalink
upgrade: update all the instance manager labels to use checksum of im…
Browse files Browse the repository at this point in the history
…age names when upgrading Longhorn manager from v1.0.0 to v1.0.1

Longhorn manager in v1.0.1 uses checksum of image name for instance
manager labels. We need to update the labels for all existing instance
managers so that the new Longhorn manager can correctly find them

Longhorn longhorn#1323

Signed-off-by: Phan Le <phan.le@rancher.com>
  • Loading branch information
PhanLe1010 authored and yasker committed Jul 6, 2020
1 parent 56534b7 commit dcb88e9
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
17 changes: 17 additions & 0 deletions upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
lhclientset "github.com/longhorn/longhorn-manager/k8s/pkg/client/clientset/versioned"

"github.com/longhorn/longhorn-manager/upgrade/v070to080"
"github.com/longhorn/longhorn-manager/upgrade/v100to101"
"github.com/longhorn/longhorn-manager/upgrade/v1alpha1"
)

Expand Down Expand Up @@ -105,6 +106,9 @@ func upgrade(currentNodeID, namespace string, config *restclient.Config, lhClien
if err = doAPIVersionUpgrade(namespace, config, lhClient); err != nil {
return
}
if err = doPodsUpgrade(namespace, lhClient, kubeClient); err != nil {
return
}
if err = doCRDUpgrade(namespace, lhClient); err != nil {
return
}
Expand Down Expand Up @@ -207,6 +211,9 @@ func doCRDUpgrade(namespace string, lhClient *lhclientset.Clientset) (err error)
if err := v070to080.UpgradeCRDs(namespace, lhClient); err != nil {
return err
}
if err := v100to101.UpgradeCRDs(namespace, lhClient); err != nil {
return err
}
return nil
}

Expand All @@ -219,3 +226,13 @@ func upgradeLocalNode() (err error) {
}
return nil
}

func doPodsUpgrade(namespace string, lhClient *lhclientset.Clientset, kubeClient *clientset.Clientset) (err error) {
defer func() {
err = errors.Wrap(err, "upgrade Pods failed")
}()
if err = v100to101.UpgradeInstanceManagerPods(namespace, lhClient, kubeClient); err != nil {
return err
}
return nil
}
114 changes: 114 additions & 0 deletions upgrade/v100to101/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package v100to101

import (
"github.com/pkg/errors"

v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"

longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta1"
lhclientset "github.com/longhorn/longhorn-manager/k8s/pkg/client/clientset/versioned"
"github.com/longhorn/longhorn-manager/types"
)

// This upgrade is needed because we changed from using full image name (in v1.0.0)
// to use the checksum of image name (in v1.0.1) for instance manager labels.
// Therefore, we need to update all existing instance manager labels so that
// the updated Longhorn manager can correctly find them.
// Link to the original issue: https://github.com/longhorn/longhorn/issues/1323

const (
upgradeLogPrefix = "upgrade from v1.0.0 to v1.0.1: "
)

func UpgradeInstanceManagerPods(namespace string, lhClient *lhclientset.Clientset, kubeClient *clientset.Clientset) (err error) {
defer func() {
err = errors.Wrapf(err, upgradeLogPrefix+"upgrade instance manager pods failed")
}()

imList, err := lhClient.LonghornV1beta1().InstanceManagers(namespace).List(metav1.ListOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return errors.Wrapf(err, upgradeLogPrefix+"failed to list all existing instance managers during the instance managers pods upgrade")
}

for _, im := range imList.Items {
if im.Spec.Image == "" {
continue
}
imPodsList, err := kubeClient.CoreV1().Pods(namespace).List(metav1.ListOptions{
FieldSelector: "metadata.name=" + im.Name,
})
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return errors.Wrapf(err, upgradeLogPrefix+"failed to find pod for instance manager %v during the instance managers pods upgrade", im.Name)
}
for _, pod := range imPodsList.Items {
if err = upgradeInstanceMangerPodLabel(&pod, &im, kubeClient, namespace); err != nil {
return err
}
}
}
return nil
}

func upgradeInstanceMangerPodLabel(pod *v1.Pod, im *longhorn.InstanceManager, kubeClient *clientset.Clientset, namespace string) (err error) {
metadata, err := meta.Accessor(pod)
if err != nil {
return err
}
metadata.SetLabels(types.GetInstanceManagerLabels(im.Spec.NodeID, im.Spec.Image, im.Spec.Type))
if pod, err = kubeClient.CoreV1().Pods(namespace).Update(pod); err != nil {
return errors.Wrapf(err, upgradeLogPrefix+"failed to update the spec for instance manager pod %v during the instance managers upgrade", pod.Name)
}
return nil
}

func UpgradeCRDs(namespace string, lhClient *lhclientset.Clientset) error {
if err := doInstanceManagerUpgrade(namespace, lhClient); err != nil {
return err
}
return nil
}

func doInstanceManagerUpgrade(namespace string, lhClient *lhclientset.Clientset) (err error) {
defer func() {
err = errors.Wrapf(err, upgradeLogPrefix+"upgrade instance manager failed")
}()

imList, err := lhClient.LonghornV1beta1().InstanceManagers(namespace).List(metav1.ListOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return errors.Wrapf(err, upgradeLogPrefix+"failed to list all existing instance managers during the instance managers upgrade")
}

for _, im := range imList.Items {
if im.Spec.Image != "" {
if err := upgradeInstanceManagersLabels(&im, lhClient, namespace); err != nil {
return err
}
}
}
return nil
}

func upgradeInstanceManagersLabels(im *longhorn.InstanceManager, lhClient *lhclientset.Clientset, namespace string) (err error) {
metadata, err := meta.Accessor(im)
if err != nil {
return err
}
metadata.SetLabels(types.GetInstanceManagerLabels(im.Spec.NodeID, im.Spec.Image, im.Spec.Type))
if im, err = lhClient.LonghornV1beta1().InstanceManagers(namespace).Update(im); err != nil {
return errors.Wrapf(err, upgradeLogPrefix+"failed to update the spec for instance manager %v during the instance managers upgrade", im.Name)
}
return nil
}

0 comments on commit dcb88e9

Please sign in to comment.