Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move sync code to pkg/skaffold/sync/kubectl #1138

Merged
merged 2 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still leave this as unexported, I dont think we want it used outside the package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the Sync function in the kubectl package calls it, so I think it has to be exported?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doh you are correct, LGTM then

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