Avoid concurrent append to slices in DryRunTarget

Issue #600
This commit is contained in:
Justin Santa Barbara 2016-10-18 00:14:31 -04:00
parent 314cd341ea
commit 472c443d1b
1 changed files with 14 additions and 4 deletions

View File

@ -17,20 +17,23 @@ limitations under the License.
package fi package fi
import ( import (
"fmt"
"bytes" "bytes"
"github.com/golang/glog" "fmt"
"io" "io"
"k8s.io/kops/upup/pkg/fi/utils"
"reflect" "reflect"
"strings" "strings"
"sync"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi/utils"
) )
// DryRunTarget is a special Target that does not execute anything, but instead tracks all changes. // DryRunTarget is a special Target that does not execute anything, but instead tracks all changes.
// By running against a DryRunTarget, a list of changes that would be made can be easily collected, // By running against a DryRunTarget, a list of changes that would be made can be easily collected,
// without any special support from the Tasks. // without any special support from the Tasks.
type DryRunTarget struct { type DryRunTarget struct {
mutex sync.Mutex
changes []*render changes []*render
deletions []Deletion deletions []Deletion
@ -57,6 +60,9 @@ func (t *DryRunTarget) Render(a, e, changes Task) error {
valA := reflect.ValueOf(a) valA := reflect.ValueOf(a)
aIsNil := valA.IsNil() aIsNil := valA.IsNil()
t.mutex.Lock()
defer t.mutex.Unlock()
t.changes = append(t.changes, &render{ t.changes = append(t.changes, &render{
a: a, a: a,
aIsNil: aIsNil, aIsNil: aIsNil,
@ -67,7 +73,11 @@ func (t *DryRunTarget) Render(a, e, changes Task) error {
} }
func (t *DryRunTarget) Delete(deletion Deletion) error { func (t *DryRunTarget) Delete(deletion Deletion) error {
t.mutex.Lock()
defer t.mutex.Unlock()
t.deletions = append(t.deletions, deletion) t.deletions = append(t.deletions, deletion)
return nil return nil
} }