Skip to content

Commit

Permalink
Merge pull request #1138 from priyawadhwa/sync
Browse files Browse the repository at this point in the history
Move sync code to pkg/skaffold/sync/kubectl
  • Loading branch information
r2d4 authored Oct 10, 2018
2 parents 017cf58 + 971c262 commit 060791e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 185 deletions.
136 changes: 0 additions & 136 deletions pkg/skaffold/kubernetes/sync_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/sync"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/sync/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/test"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/watch"

Expand Down Expand Up @@ -108,7 +109,7 @@ func NewForConfig(opts *config.SkaffoldOptions, cfg *latest.SkaffoldPipeline) (*
Deployer: deployer,
Tagger: tagger,
Trigger: trigger,
Syncer: &kubernetes.KubectlSyncer{},
Syncer: &kubectl.Syncer{},
opts: opts,
watchFactory: watch.NewWatcher,
}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,32 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package kubernetes
package kubectl

import (
"context"
"fmt"
"os/exec"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/sync"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type KubectlSyncer struct{}
type Syncer struct{}

func (k *KubectlSyncer) Sync(ctx context.Context, s *sync.Item) error {
func (k *Syncer) Sync(ctx context.Context, s *sync.Item) error {
logrus.Infoln("Copying files:", s.Copy, "to", s.Image)

if err := perform(ctx, s.Image, s.Copy, copyFileFn); err != nil {
if err := sync.Perform(ctx, s.Image, s.Copy, copyFileFn); err != nil {
return errors.Wrap(err, "copying files")
}

logrus.Infoln("Deleting files:", s.Delete, "from", s.Image)

if err := perform(ctx, s.Image, s.Delete, deleteFileFn); err != nil {
if err := sync.Perform(ctx, s.Image, s.Delete, deleteFileFn); err != nil {
return errors.Wrap(err, "deleting files")
}

Expand All @@ -55,44 +53,3 @@ func deleteFileFn(ctx context.Context, pod v1.Pod, container v1.Container, src,
func copyFileFn(ctx context.Context, pod v1.Pod, container v1.Container, src, dst string) *exec.Cmd {
return exec.CommandContext(ctx, "kubectl", "cp", src, fmt.Sprintf("%s/%s:%s", pod.Namespace, pod.Name, dst), "-c", container.Name)
}

func perform(ctx context.Context, image string, files map[string]string, cmdFn func(context.Context, v1.Pod, v1.Container, string, string) *exec.Cmd) error {
if len(files) == 0 {
return nil
}

client, err := Client()
if err != nil {
return errors.Wrap(err, "getting k8s client")
}

pods, err := client.CoreV1().Pods("").List(meta_v1.ListOptions{})
if err != nil {
return errors.Wrap(err, "getting pods")
}

synced := map[string]bool{}

for _, p := range pods.Items {
for _, c := range p.Spec.Containers {
if c.Image != image {
continue
}

for src, dst := range files {
cmd := cmdFn(ctx, p, c, src, dst)
if err := util.RunCmd(cmd); err != nil {
return err
}

synced[src] = true
}
}
}

if len(synced) != len(files) {
return errors.New("couldn't sync all the files")
}

return nil
}
45 changes: 45 additions & 0 deletions pkg/skaffold/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ package sync
import (
"context"
"fmt"
"os/exec"
"path"
"path/filepath"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/watch"
"github.com/pkg/errors"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type Syncer interface {
Expand Down Expand Up @@ -108,3 +112,44 @@ func intersect(context string, syncMap map[string]string, files []string) (map[s
}
return ret, nil
}

func Perform(ctx context.Context, image string, files map[string]string, cmdFn func(context.Context, v1.Pod, v1.Container, string, string) *exec.Cmd) error {
if len(files) == 0 {
return nil
}

client, err := kubernetes.Client()
if err != nil {
return errors.Wrap(err, "getting k8s client")
}

pods, err := client.CoreV1().Pods("").List(meta_v1.ListOptions{})
if err != nil {
return errors.Wrap(err, "getting pods")
}

synced := map[string]bool{}

for _, p := range pods.Items {
for _, c := range p.Spec.Containers {
if c.Image != image {
continue
}

for src, dst := range files {
cmd := cmdFn(ctx, p, c, src, dst)
if err := util.RunCmd(cmd); err != nil {
return err
}

synced[src] = true
}
}
}

if len(synced) != len(files) {
return errors.New("couldn't sync all the files")
}

return nil
}
Loading

0 comments on commit 060791e

Please sign in to comment.