github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/metamanager/client/volumeattachment.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 api "k8s.io/api/storage/v1" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 10 "github.com/kubeedge/beehive/pkg/core/model" 11 "github.com/kubeedge/kubeedge/edge/pkg/common/message" 12 "github.com/kubeedge/kubeedge/edge/pkg/common/modules" 13 "github.com/kubeedge/kubeedge/edge/pkg/metamanager" 14 ) 15 16 // VolumeAttachmentsGetter is interface to get client VolumeAttachments 17 type VolumeAttachmentsGetter interface { 18 VolumeAttachments(namespace string) VolumeAttachmentsInterface 19 } 20 21 // VolumeAttachmentsInterface is interface for client VolumeAttachments 22 type VolumeAttachmentsInterface interface { 23 Create(*api.VolumeAttachment) (*api.VolumeAttachment, error) 24 Update(*api.VolumeAttachment) error 25 Delete(name string) error 26 Get(name string, options metav1.GetOptions) (*api.VolumeAttachment, error) 27 } 28 29 type volumeattachments struct { 30 namespace string 31 send SendInterface 32 } 33 34 func newVolumeAttachments(n string, s SendInterface) *volumeattachments { 35 return &volumeattachments{ 36 namespace: n, 37 send: s, 38 } 39 } 40 41 func (c *volumeattachments) Create(va *api.VolumeAttachment) (*api.VolumeAttachment, error) { 42 resource := fmt.Sprintf("%s/%s/%s", c.namespace, "volumeattachment", va.Name) 43 vaMsg := message.BuildMsg(modules.MetaGroup, "", modules.EdgedModuleName, resource, model.InsertOperation, va) 44 _, err := c.send.SendSync(vaMsg) 45 if err != nil { 46 return nil, fmt.Errorf("create VolumeAttachment failed, err: %v", err) 47 } 48 return nil, nil 49 } 50 51 func (c *volumeattachments) Update(va *api.VolumeAttachment) error { 52 return nil 53 } 54 55 func (c *volumeattachments) Delete(name string) error { 56 resource := fmt.Sprintf("%s/%s/%s", c.namespace, "volumeattachment", name) 57 vaMsg := message.BuildMsg(modules.MetaGroup, "", modules.EdgedModuleName, resource, model.DeleteOperation, nil) 58 _, err := c.send.SendSync(vaMsg) 59 if err != nil { 60 return fmt.Errorf("delete VolumeAttachment failed, err: %v", err) 61 } 62 return nil 63 } 64 65 func (c *volumeattachments) Get(name string, options metav1.GetOptions) (*api.VolumeAttachment, error) { 66 resource := fmt.Sprintf("%s/%s/%s", c.namespace, "volumeattachment", name) 67 vaMsg := message.BuildMsg(modules.MetaGroup, "", modules.EdgedModuleName, resource, model.QueryOperation, nil) 68 msg, err := c.send.SendSync(vaMsg) 69 if err != nil { 70 return nil, fmt.Errorf("get volumeattachment from metaManager failed, err: %v", err) 71 } 72 73 var content []byte 74 switch msg.Content.(type) { 75 case []byte: 76 content = msg.GetContent().([]byte) 77 default: 78 content, err = json.Marshal(msg.GetContent()) 79 if err != nil { 80 return nil, fmt.Errorf("marshal message to volumeattachment failed, err: %v", err) 81 } 82 } 83 84 if msg.GetOperation() == model.ResponseOperation && msg.GetSource() == metamanager.MetaManagerModuleName { 85 return handleVolumeAttachmentFromMetaDB(content) 86 } 87 return handleVolumeAttachmentFromMetaManager(content) 88 } 89 90 func handleVolumeAttachmentFromMetaDB(content []byte) (*api.VolumeAttachment, error) { 91 var lists []string 92 err := json.Unmarshal(content, &lists) 93 if err != nil { 94 return nil, fmt.Errorf("unmarshal message to volumeattachment list from db failed, err: %v", err) 95 } 96 97 if len(lists) != 1 { 98 return nil, fmt.Errorf("volumeattachment length from meta db is %d", len(lists)) 99 } 100 101 var va *api.VolumeAttachment 102 err = json.Unmarshal([]byte(lists[0]), &va) 103 if err != nil { 104 return nil, fmt.Errorf("unmarshal message to volumeattachment from db failed, err: %v", err) 105 } 106 return va, nil 107 } 108 109 func handleVolumeAttachmentFromMetaManager(content []byte) (*api.VolumeAttachment, error) { 110 var va *api.VolumeAttachment 111 err := json.Unmarshal(content, &va) 112 if err != nil { 113 return nil, fmt.Errorf("unmarshal message to volumeattachment failed, err: %v", err) 114 } 115 return va, nil 116 }