Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1036 from StrongMonkey/fix-persist-volume
Browse files Browse the repository at this point in the history
Fix persistentvolume failing to update issue
  • Loading branch information
StrongMonkey authored May 5, 2020
2 parents 3e1ac30 + 29ddb3b commit d4c5f27
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion modules/service/controllers/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ import (
corev1controller "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/generic"
"github.com/rancher/wrangler/pkg/objectset"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

const (
ownerreferenceGVK = "objectset.rio.cattle.io/owner-gvk"
ownerreferenceName = "objectset.rio.cattle.io/owner-name"
ownerreferenceNamespace = "objectset.rio.cattle.io/owner-namespace"
)

func Register(ctx context.Context, rContext *types.Context) error {

sh := &serviceHandler{
namespace: rContext.Namespace,
publicDomainCache: rContext.Admin.Admin().V1().PublicDomain().Cache(),
clusterDomainCache: rContext.Admin.Admin().V1().ClusterDomain().Cache(),
pvcCache: rContext.Core.Core().V1().PersistentVolumeClaim().Cache(),
configmaps: rContext.Core.Core().V1().ConfigMap(),
}

Expand Down Expand Up @@ -53,6 +62,7 @@ type serviceHandler struct {

clusterDomainCache adminv1.ClusterDomainCache
publicDomainCache adminv1.PublicDomainCache
pvcCache corev1controller.PersistentVolumeClaimCache
configmaps corev1controller.ConfigMapClient
}

Expand All @@ -70,7 +80,25 @@ func (s *serviceHandler) populate(service *riov1.Service, status riov1.ServiceSt
return nil, status, err
}

return os.All(), status, nil
// filter all persistentvolume and don't add persistentvolume that already existed
result := objectset.NewObjectSet()
for _, object := range os.All() {
switch object.(type) {
case *v1.PersistentVolumeClaim:
pvc, err := s.pvcCache.Get(object.(*v1.PersistentVolumeClaim).Namespace, object.(*v1.PersistentVolumeClaim).Name)
if errors.IsNotFound(err) || (err == nil && isPvcOwnedByService(pvc, service)) {
result.Add(object)
}
default:
result.Add(object)
}
}

return result.All(), status, nil
}

func isPvcOwnedByService(pvc *v1.PersistentVolumeClaim, service *riov1.Service) bool {
return pvc.Annotations[ownerreferenceGVK] == "rio.cattle.io/v1, Kind=Service" && pvc.Annotations[ownerreferenceName] == service.Name && pvc.Annotations[ownerreferenceNamespace] == service.Namespace
}

func (s *serviceHandler) ensureFeatures(service *riov1.Service) error {
Expand Down

0 comments on commit d4c5f27

Please sign in to comment.