Skip to content

Commit

Permalink
Revert "Cross-platform multi process support (eksctl-io#2230)"
Browse files Browse the repository at this point in the history
This reverts commit 7af3408.

This broke eksctl on windows again, unfortunately.
  • Loading branch information
michaelbeaumont committed Jun 26, 2020
1 parent 60256ca commit 788eb45
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 121 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/fluxcd/helm-operator/pkg/install v0.0.0-20200407140510-8d71b0072a3e // helm-operator 1.0.0
github.com/github-release/github-release v0.8.1
github.com/gobwas/glob v0.2.3
github.com/gofrs/flock v0.7.1
github.com/golangci/golangci-lint v1.27.0
github.com/google/go-github/v31 v31.0.0
github.com/goreleaser/goreleaser v0.136.0
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,8 @@ github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/flock v0.7.0 h1:pGFUjl501gafK9HBt1VGL1KCOd/YhIooID+xgyJCf3g=
github.com/gofrs/flock v0.7.0/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/flock v0.7.1 h1:DP+LD/t0njgoPBvT5MJLeliUIVQR03hiKR6vezdwHlc=
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gogo/protobuf v0.0.0-20171007142547-342cbe0a0415/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
80 changes: 3 additions & 77 deletions pkg/utils/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/weaveworks/eksctl/pkg/utils/file"

"os/exec"

"github.com/gofrs/flock"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/utils/file"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
Expand Down Expand Up @@ -180,62 +179,12 @@ func AppendAuthenticator(config *clientcmdapi.Config, clusterMeta *api.ClusterMe
}
}

// ensureDirectory should probably be handled in flock
func ensureDirectory(filePath string) error {
dir := filepath.Dir(filePath)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0755); err != nil {
return err
}
}
return nil
}

func lockConfigFile(filePath string) error {
// Make sure the directory exists, otherwise flock fails
if err := ensureDirectory(filePath); err != nil {
return err
}
flock := flock.New(filePath)
err := flock.Lock()
if err != nil {
return errors.Wrap(err, "flock: failed to obtain exclusive lock on kubeconfig file")
}

return nil
}

func unlockConfigFile(filePath string) error {
// Make sure the directory exists, otherwise flock fails
if err := ensureDirectory(filePath); err != nil {
return err
}
flock := flock.New(filePath)
err := flock.Unlock()
if err != nil {
return errors.Wrap(err, "flock: failed to release exclusive lock on kubeconfig file")
}

return nil
}

// Write will write Kubernetes client configuration to a file.
// If path isn't specified then the path will be determined by client-go.
// If file pointed to by path doesn't exist it will be created.
// If the file already exists then the configuration will be merged with the existing file.
func Write(path string, newConfig clientcmdapi.Config, setContext bool) (string, error) {
configAccess := getConfigAccess(path)
configFileName := configAccess.GetDefaultFilename()
err := lockConfigFile(configFileName)
if err != nil {
return "", err
}

defer func() {
if err := unlockConfigFile(configFileName); err != nil {
logger.Critical(err.Error())
}
}()

config, err := configAccess.GetStartingConfig()
if err != nil {
Expand All @@ -254,7 +203,7 @@ func Write(path string, newConfig clientcmdapi.Config, setContext bool) (string,
return "", errors.Wrapf(err, "unable to modify kubeconfig %s", path)
}

return configFileName, nil
return configAccess.GetDefaultFilename(), nil
}

func getConfigAccess(explicitPath string) clientcmd.ConfigAccess {
Expand Down Expand Up @@ -314,17 +263,6 @@ func MaybeDeleteConfig(meta *api.ClusterMeta) {
p := AutoPath(meta.Name)

if file.Exists(p) {
err := lockConfigFile(p)
if err != nil {
logger.Critical(err.Error())
}

defer func() {
if err := unlockConfigFile(p); err != nil {
logger.Critical(err.Error())
}
}()

if err := isValidConfig(p, meta.Name); err != nil {
logger.Debug(err.Error())
return
Expand All @@ -336,18 +274,6 @@ func MaybeDeleteConfig(meta *api.ClusterMeta) {
}

configAccess := getConfigAccess(DefaultPath())
defaultFilename := configAccess.GetDefaultFilename()
err := lockConfigFile(defaultFilename)
if err != nil {
logger.Critical(err.Error())
}

defer func() {
if err := unlockConfigFile(defaultFilename); err != nil {
logger.Critical(err.Error())
}
}()

config, err := configAccess.GetStartingConfig()
if err != nil {
logger.Debug("error reading kubeconfig file %q: %s", DefaultPath(), err.Error())
Expand Down
41 changes: 0 additions & 41 deletions pkg/utils/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io/ioutil"
"os"
"path"
"sync"

eksctlapi "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/utils/kubeconfig"
Expand Down Expand Up @@ -309,44 +308,4 @@ var _ = Describe("Kubeconfig", func() {
Expect(configFileAsBytes).To(MatchYAML(twoClustersAsBytes), "Should not change")
})
})

It("safely handles concurrent read-modify-write operations", func() {
var (
oneClusterAsBytes []byte
twoClustersAsBytes []byte
)
ChangeKubeconfig()

var err error

if oneClusterAsBytes, err = ioutil.ReadFile("testdata/one_cluster.golden"); err != nil {
GinkgoT().Fatalf("failed reading .golden: %v", err)
}

if twoClustersAsBytes, err = ioutil.ReadFile("testdata/two_clusters.golden"); err != nil {
GinkgoT().Fatalf("failed reading .golden: %v", err)
}

var wg sync.WaitGroup
multiplier := 3
iters := 100
for i := 0; i < multiplier; i++ {
for k := 0; k < iters; k++ {
wg.Add(2)
go func() {
defer wg.Done()
_, err := configFile.Write(oneClusterAsBytes)
Expect(err).To(BeNil())
}()
go func() {
defer wg.Done()
_, err := configFile.Write(twoClustersAsBytes)
Expect(err).To(BeNil())
}()
}
}

wg.Wait()

})
})

0 comments on commit 788eb45

Please sign in to comment.