From 28a382d8b249fa524366a29459136d1d402da612 Mon Sep 17 00:00:00 2001
From: Valentin Rothberg <rothberg@redhat.com>
Date: Fri, 20 Sep 2019 08:41:42 +0200
Subject: [PATCH] split acquiring the lock out of getLock

Split the logic to acquire the disk locks into a separate method
`dnsNameLock.acquire() error` to make the API more flexible and
less prone to errors.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
---
 plugins/meta/dnsname/files.go | 17 ++++++++++-------
 plugins/meta/dnsname/main.go  |  9 +++++++++
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/plugins/meta/dnsname/files.go b/plugins/meta/dnsname/files.go
index 70fdfe5..c74bae4 100644
--- a/plugins/meta/dnsname/files.go
+++ b/plugins/meta/dnsname/files.go
@@ -4,13 +4,14 @@ import (
 	"bufio"
 	"bytes"
 	"fmt"
-	"github.com/coreos/go-iptables/iptables"
 	"io/ioutil"
 	"net"
 	"os"
 	"strings"
 	"text/template"
 
+	"github.com/coreos/go-iptables/iptables"
+
 	"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk"
 	"github.com/sirupsen/logrus"
 )
@@ -20,7 +21,7 @@ type dnsNameLock struct {
 	lock *disk.FileLock
 }
 
-// release unlocks and closes the disk lock
+// release unlocks and closes the disk lock.
 func (m *dnsNameLock) release() error {
 	if err := m.lock.Unlock(); err != nil {
 		return err
@@ -28,16 +29,18 @@ func (m *dnsNameLock) release() error {
 	return m.lock.Close()
 }
 
-// getLock returns a dnsNameLock. the lock should be that of the configuration
-// directory for the domain.
+// acquire locks the disk lock.
+func (m *dnsNameLock) acquire() error {
+	return m.lock.Lock()
+}
+
+// getLock returns a dnsNameLock synchronizing the configuration directory for
+// the domain.
 func getLock(path string) (*dnsNameLock, error) {
 	l, err := disk.NewFileLock(path)
 	if err != nil {
 		return nil, err
 	}
-	if err := l.Lock(); err != nil {
-		return nil, err
-	}
 	return &dnsNameLock{l}, nil
 }
 
diff --git a/plugins/meta/dnsname/main.go b/plugins/meta/dnsname/main.go
index 9da7bb0..b84f2da 100644
--- a/plugins/meta/dnsname/main.go
+++ b/plugins/meta/dnsname/main.go
@@ -75,6 +75,9 @@ func cmdAdd(args *skel.CmdArgs) error {
 	if err != nil {
 		return err
 	}
+	if err := lock.acquire(); err != nil {
+		return err
+	}
 	defer func() {
 		if err := lock.release(); err != nil {
 			logrus.Errorf("unable to release lock for '%s': %q", dnsNameConf.AddOnHostsFile, err)
@@ -121,6 +124,9 @@ func cmdDel(args *skel.CmdArgs) error {
 	if err != nil {
 		return err
 	}
+	if err := lock.acquire(); err != nil {
+		return err
+	}
 	defer func() {
 		// if the lock isn't given up by another process
 		if err := lock.release(); err != nil {
@@ -171,6 +177,9 @@ func cmdCheck(args *skel.CmdArgs) error {
 	if err != nil {
 		return err
 	}
+	if err := lock.acquire(); err != nil {
+		return err
+	}
 	defer func() {
 		// if the lock isn't given up by another process
 		if err := lock.release(); err != nil {
-- 
GitLab