Skip to content

Commit

Permalink
Set Disk resource status to unknown when ndm pod is shutdown (#66)
Browse files Browse the repository at this point in the history
This PR contains cleaning of resources when ndm pod
deleted gracefully. This will make resources state
unknown which are attached with that node when we
shutdown that node/pod. This will make sure that there are no 
stale disk resources as nodes are added/removed to cluster. 

Changes committed:
modified:   cmd/controller/controller.go
modified:   cmd/controller/diskstore.go
modified:   cmd/controller/diskstore_test.go

Signed-off-by: Shovan Maity <[email protected]>
  • Loading branch information
Shovan Maity authored and kmova committed Aug 8, 2018
1 parent 6ff78d9 commit 6ec98e3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
NDMHostKey = "kubernetes.io/hostname" // NDMHostKey is host name label prefix.
NDMActive = "Active" // NDMActive is constant for active resource status.
NDMInactive = "Inactive" // NDMInactive is constant for inactive resource status.
NDMUnknown = "Unknown" // NDMUnknown is constant for resource unknown satus.
)

// ControllerBroadcastChannel is used to send a copy of controller object to each probe.
Expand Down Expand Up @@ -153,6 +154,10 @@ func (c *Controller) Broadcast() {
func (c *Controller) run(threadiness int, stopCh <-chan struct{}) error {
glog.Info("started the controller")
<-stopCh
glog.Info("changing the state to unknown before shutting down.")
// Changing the state to unknown before shutting down. Similar as when one pod is
// running and you stopped kubelet it will make pod status unknown.
c.MarkDiskStatusToUnknown()
glog.Info("shutting down the controller")
return nil
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/controller/diskstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,20 @@ func (c *Controller) PushDiskResource(oldDr *apis.Disk, diskDetails *DiskInfo) {
}
c.CreateDisk(diskApi)
}

// MarkDiskStatusToUnknown makes state of all resources owned by node unknown
// This will call as a cleanup process before shutting down.
func (c *Controller) MarkDiskStatusToUnknown() {
listDR, err := c.ListDiskResource()
if err != nil {
glog.Error(err)
return
}
for _, item := range listDR.Items {
drCopy := item.DeepCopy()
drCopy.Status.State = NDMUnknown
if dr, err := c.Clientset.OpenebsV1alpha1().Disks().Update(drCopy); err == nil {
glog.Error("updated disk object : ", dr.ObjectMeta.Name)
}
}
}
31 changes: 31 additions & 0 deletions cmd/controller/diskstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,34 @@ func TestDeactivateStaleDiskResource(t *testing.T) {
})
}
}

func TestMarkDiskStatusToUnknown(t *testing.T) {
fakeNdmClient := ndmFakeClientset.NewSimpleClientset()
fakeKubeClient := fake.NewSimpleClientset()
fakeController := &Controller{
HostName: fakeHostName,
KubeClientset: fakeKubeClient,
Clientset: fakeNdmClient,
}
dr := mockEmptyDiskCr()
dr.ObjectMeta.Labels[NDMHostKey] = fakeController.HostName
fakeController.CreateDisk(dr)

fakeController.MarkDiskStatusToUnknown()
dr.Status.State = NDMUnknown
cdr, err := fakeController.Clientset.OpenebsV1alpha1().Disks().Get(fakeDiskUid, metav1.GetOptions{})
tests := map[string]struct {
actualDisk apis.Disk
actualError error
expectedDisk apis.Disk
expectedError error
}{
"DeactivateOwnedDiskResource should make all present resources state inactive": {actualDisk: *cdr, actualError: err, expectedDisk: dr, expectedError: nil},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expectedDisk, test.actualDisk)
assert.Equal(t, test.expectedError, test.actualError)
})
}
}

0 comments on commit 6ec98e3

Please sign in to comment.