Skip to content

Commit e9508a3

Browse files
authored
Merge pull request #14 from humblec/cntrl-1
lib: correct function name and error condition checks
2 parents d952802 + e7868c1 commit e9508a3

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

populator-machinery/controller.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ func RunController(masterURL, kubeconfig, imageName, namespace, prefix string,
108108
}()
109109

110110
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
111-
if nil != err {
111+
if err != nil {
112112
klog.Fatalf("Failed to create config: %v", err)
113113
}
114114

115115
kubeClient, err := kubernetes.NewForConfig(cfg)
116-
if nil != err {
116+
if err != nil {
117117
klog.Fatalf("Failed to create client: %v", err)
118118
}
119119

120120
dynClient, err := dynamic.NewForConfig(cfg)
121-
if nil != err {
121+
if err != nil {
122122
klog.Fatalf("Failed to create dynamic client: %v", err)
123123
}
124124

@@ -224,7 +224,7 @@ func RunController(masterURL, kubeconfig, imageName, namespace, prefix string,
224224
kubeInformerFactory.Start(stopCh)
225225
dynInformerFactory.Start(stopCh)
226226

227-
if err = c.run(stopCh); nil != err {
227+
if err = c.run(stopCh); err != nil {
228228
klog.Fatalf("Failed to run controller: %v", err)
229229
}
230230
}
@@ -239,29 +239,29 @@ func (c *controller) addNotification(keyToCall, objType, namespace, name string)
239239
c.mu.Lock()
240240
defer c.mu.Unlock()
241241
s := c.notifyMap[key]
242-
if nil == s {
242+
if s == nil {
243243
s = &stringSet{make(map[string]empty)}
244244
c.notifyMap[key] = s
245245
}
246246
s.set[keyToCall] = empty{}
247247
s = c.cleanupMap[keyToCall]
248-
if nil == s {
248+
if s == nil {
249249
s = &stringSet{make(map[string]empty)}
250250
c.cleanupMap[keyToCall] = s
251251
}
252252
s.set[key] = empty{}
253253
}
254254

255-
func (c *controller) cleanupNofications(keyToCall string) {
255+
func (c *controller) cleanupNotifications(keyToCall string) {
256256
c.mu.Lock()
257257
defer c.mu.Unlock()
258258
s := c.cleanupMap[keyToCall]
259-
if nil == s {
259+
if s == nil {
260260
return
261261
}
262262
for key := range s.set {
263263
t := c.notifyMap[key]
264-
if nil == t {
264+
if t == nil {
265265
continue
266266
}
267267
delete(t.set, keyToCall)
@@ -291,11 +291,11 @@ func translateObject(obj interface{}) metav1.Object {
291291

292292
func (c *controller) handleMapped(obj interface{}, objType string) {
293293
object := translateObject(obj)
294-
if nil == object {
294+
if object == nil {
295295
return
296296
}
297297
var key string
298-
if 0 == len(object.GetNamespace()) {
298+
if len(object.GetNamespace()) == 0 {
299299
key = objType + "/" + object.GetName()
300300
} else {
301301
key = objType + "/" + object.GetNamespace() + "/" + object.GetName()
@@ -312,7 +312,7 @@ func (c *controller) handleMapped(obj interface{}, objType string) {
312312
func (c *controller) handlePVC(obj interface{}) {
313313
c.handleMapped(obj, "pvc")
314314
object := translateObject(obj)
315-
if nil == object {
315+
if object == nil {
316316
return
317317
}
318318
if c.populatorNamespace != object.GetNamespace() {
@@ -366,7 +366,7 @@ func (c *controller) runWorker() {
366366
parts := strings.Split(key, "/")
367367
switch parts[0] {
368368
case "pvc":
369-
if 3 != len(parts) {
369+
if len(parts) != 3 {
370370
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))
371371
return nil
372372
}
@@ -375,7 +375,7 @@ func (c *controller) runWorker() {
375375
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))
376376
return nil
377377
}
378-
if nil != err {
378+
if err != nil {
379379
c.workqueue.AddRateLimited(key)
380380
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
381381
}
@@ -389,7 +389,7 @@ func (c *controller) runWorker() {
389389
return
390390
}
391391
err := processNextWorkItem(obj)
392-
if nil != err {
392+
if err != nil {
393393
utilruntime.HandleError(err)
394394
}
395395
}
@@ -405,7 +405,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
405405

406406
var pvc *corev1.PersistentVolumeClaim
407407
pvc, err = c.pvcLister.PersistentVolumeClaims(pvcNamespace).Get(pvcName)
408-
if nil != err {
408+
if err != nil {
409409
if errors.IsNotFound(err) {
410410
utilruntime.HandleError(fmt.Errorf("pvc '%s' in work queue no longer exists", key))
411411
return nil
@@ -414,7 +414,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
414414
}
415415

416416
dataSourceRef := pvc.Spec.DataSourceRef
417-
if nil == dataSourceRef {
417+
if dataSourceRef == nil {
418418
// Ignore PVCs without a datasource
419419
return nil
420420
}
@@ -426,7 +426,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
426426

427427
var unstructured *unstructured.Unstructured
428428
unstructured, err = c.unstLister.Namespace(pvc.Namespace).Get(dataSourceRef.Name)
429-
if nil != err {
429+
if err != nil {
430430
if !errors.IsNotFound(err) {
431431
return err
432432
}
@@ -437,12 +437,12 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
437437

438438
var waitForFirstConsumer bool
439439
var nodeName string
440-
if nil != pvc.Spec.StorageClassName {
440+
if pvc.Spec.StorageClassName != nil {
441441
storageClassName := *pvc.Spec.StorageClassName
442442

443443
var storageClass *storagev1.StorageClass
444444
storageClass, err = c.scLister.Get(storageClassName)
445-
if nil != err {
445+
if err != nil {
446446
if !errors.IsNotFound(err) {
447447
return err
448448
}
@@ -451,10 +451,10 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
451451
return nil
452452
}
453453

454-
if nil != storageClass.VolumeBindingMode && storagev1.VolumeBindingWaitForFirstConsumer == *storageClass.VolumeBindingMode {
454+
if storageClass.VolumeBindingMode != nil && storagev1.VolumeBindingWaitForFirstConsumer == *storageClass.VolumeBindingMode {
455455
waitForFirstConsumer = true
456456
nodeName = pvc.Annotations[annSelectedNode]
457-
if "" == nodeName {
457+
if nodeName == "" {
458458
// Wait for the PVC to get a node name before continuing
459459
return nil
460460
}
@@ -466,7 +466,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
466466
c.addNotification(key, "pod", c.populatorNamespace, podName)
467467
var pod *corev1.Pod
468468
pod, err = c.podLister.Pods(c.populatorNamespace).Get(podName)
469-
if nil != err {
469+
if err != nil {
470470
if !errors.IsNotFound(err) {
471471
return err
472472
}
@@ -477,7 +477,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
477477
c.addNotification(key, "pvc", c.populatorNamespace, pvcPrimeName)
478478
var pvcPrime *corev1.PersistentVolumeClaim
479479
pvcPrime, err = c.pvcLister.PersistentVolumeClaims(c.populatorNamespace).Get(pvcPrimeName)
480-
if nil != err {
480+
if err != nil {
481481
if !errors.IsNotFound(err) {
482482
return err
483483
}
@@ -490,12 +490,12 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
490490

491491
// Ensure the PVC has a finalizer on it so we can clean up the stuff we create
492492
err = c.ensureFinalizer(ctx, pvc, c.pvcFinalizer, true)
493-
if nil != err {
493+
if err != nil {
494494
return err
495495
}
496496

497497
// If the pod doesn't exist yet, create it
498-
if nil == pod {
498+
if pod == nil {
499499
var rawBlock bool
500500
if nil != pvc.Spec.VolumeMode && corev1.PersistentVolumeBlock == *pvc.Spec.VolumeMode {
501501
rawBlock = true
@@ -504,7 +504,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
504504
// Calculate the args for the populator pod
505505
var args []string
506506
args, err = c.populatorArgs(rawBlock, unstructured)
507-
if nil != err {
507+
if err != nil {
508508
return err
509509
}
510510

@@ -539,12 +539,12 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
539539
pod.Spec.NodeName = nodeName
540540
}
541541
_, err = c.kubeClient.CoreV1().Pods(c.populatorNamespace).Create(ctx, pod, metav1.CreateOptions{})
542-
if nil != err {
542+
if err != nil {
543543
return err
544544
}
545545

546546
// If PVC' doesn't exist yet, create it
547-
if nil == pvcPrime {
547+
if pvcPrime == nil {
548548
pvcPrime = &corev1.PersistentVolumeClaim{
549549
ObjectMeta: metav1.ObjectMeta{
550550
Name: pvcPrimeName,
@@ -563,7 +563,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
563563
}
564564
}
565565
_, err = c.kubeClient.CoreV1().PersistentVolumeClaims(c.populatorNamespace).Create(ctx, pvcPrime, metav1.CreateOptions{})
566-
if nil != err {
566+
if err != nil {
567567
return err
568568
}
569569
}
@@ -576,7 +576,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
576576
if corev1.PodFailed == pod.Status.Phase {
577577
// Delete failed pods so we can try again
578578
err = c.kubeClient.CoreV1().Pods(c.populatorNamespace).Delete(ctx, pod.Name, metav1.DeleteOptions{})
579-
if nil != err {
579+
if err != nil {
580580
return err
581581
}
582582
}
@@ -585,15 +585,15 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
585585
}
586586

587587
// This would be bad
588-
if nil == pvcPrime {
588+
if pvcPrime == nil {
589589
return fmt.Errorf("Failed to find PVC for populator pod")
590590
}
591591

592592
// Get PV
593593
var pv *corev1.PersistentVolume
594594
c.addNotification(key, "pv", "", pvcPrime.Spec.VolumeName)
595595
pv, err = c.kubeClient.CoreV1().PersistentVolumes().Get(ctx, pvcPrime.Spec.VolumeName, metav1.GetOptions{})
596-
if nil != err {
596+
if err != nil {
597597
if !errors.IsNotFound(err) {
598598
return err
599599
}
@@ -622,12 +622,12 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
622622
patchPv.Annotations[c.populatedFromAnno] = pvc.Namespace + "/" + dataSourceRef.Name
623623
var patchData []byte
624624
patchData, err = json.Marshal(patchPv)
625-
if nil != err {
625+
if err != nil {
626626
return err
627627
}
628628
_, err = c.kubeClient.CoreV1().PersistentVolumes().Patch(ctx, pv.Name, types.StrategicMergePatchType,
629629
patchData, metav1.PatchOptions{})
630-
if nil != err {
630+
if err != nil {
631631
return err
632632
}
633633

@@ -638,7 +638,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
638638
}
639639

640640
// Wait for the bind controller to rebind the PV
641-
if nil != pvcPrime {
641+
if pvcPrime != nil {
642642
if corev1.ClaimLost != pvcPrime.Status.Phase {
643643
return nil
644644
}
@@ -647,29 +647,29 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
647647
// *** At this point the volume population is done and we're just cleaning up ***
648648

649649
// If the pod still exists, delete it
650-
if nil != pod {
650+
if pod != nil {
651651
err = c.kubeClient.CoreV1().Pods(c.populatorNamespace).Delete(ctx, pod.Name, metav1.DeleteOptions{})
652-
if nil != err {
652+
if err != nil {
653653
return err
654654
}
655655
}
656656

657657
// If PVC' still exists, delete it
658-
if nil != pvcPrime {
658+
if pvcPrime != nil {
659659
err = c.kubeClient.CoreV1().PersistentVolumeClaims(c.populatorNamespace).Delete(ctx, pvcPrime.Name, metav1.DeleteOptions{})
660-
if nil != err {
660+
if err != nil {
661661
return err
662662
}
663663
}
664664

665665
// Make sure the PVC finalizer is gone
666666
err = c.ensureFinalizer(ctx, pvc, c.pvcFinalizer, false)
667-
if nil != err {
667+
if err != nil {
668668
return err
669669
}
670670

671671
// Clean up our internal callback maps
672-
c.cleanupNofications(key)
672+
c.cleanupNotifications(key)
673673

674674
return nil
675675
}
@@ -751,12 +751,12 @@ func (c *controller) ensureFinalizer(ctx context.Context, pvc *corev1.Persistent
751751
}
752752

753753
data, err := json.Marshal(patch)
754-
if nil != err {
754+
if err != nil {
755755
return err
756756
}
757757
_, err = c.kubeClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Patch(ctx, pvc.Name, types.JSONPatchType,
758758
data, metav1.PatchOptions{})
759-
if nil != err {
759+
if err != nil {
760760
return err
761761
}
762762

0 commit comments

Comments
 (0)