Skip to content

Commit

Permalink
simplificate sidecar initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 6f9b980 commit c3f4c88
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
40 changes: 17 additions & 23 deletions internal/docker/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ type Sidecar struct {
mainContainer Container
}

func (s *Sidecar) CommitMainContainer(ctx context.Context, image string) (Image, error) {
if s.mainContainer.ID == "" {
if err := s.setup(); err != nil {
return Image{}, err
// NewSidecar initializes a Sidecar
func NewSidecar(client *Client) (*Sidecar, error) {
if client == nil {
var err error
client, err = NewClient("")
if err != nil {
return nil, fmt.Errorf("failed to create docker client: %v", err)
}
}
sidecar := Sidecar{Client: client}
if err := sidecar.setup(); err != nil {
return nil, err
}
return &sidecar, nil
}

func (s *Sidecar) CommitMainContainer(ctx context.Context, image string) (Image, error) {
img, err := s.Client.Commit(ctx, s.mainContainer.ID, image)
if err != nil {
return Image{}, fmt.Errorf("error commiting image %v: %v", image, err)
Expand All @@ -35,11 +46,6 @@ func (s *Sidecar) CommitMainContainer(ctx context.Context, image string) (Image,

// UploadToMainContainer uploads a file to the main container
func (s *Sidecar) UploadToMainContainer(ctx context.Context, fileName string) error {
if s.mainContainer.ID == "" {
if err := s.setup(); err != nil {
return err
}
}
file, err := os.Open(fileName)
if err != nil {
return fmt.Errorf("failed to open input file %q: %v", fileName, err)
Expand Down Expand Up @@ -77,27 +83,15 @@ func (s *Sidecar) UploadToMainContainer(ctx context.Context, fileName string) er
return s.Client.Upload(ctx, s.mainContainer.ID, "/", buf)
}

func (s *Sidecar) ExecutorForUser(user string) (exec.Executor, error) {
if s.mainContainer.ID == "" {
if err := s.setup(); err != nil {
return nil, err
}
}
func (s *Sidecar) ExecutorForUser(user string) exec.Executor {
return &Executor{
Client: s.Client,
ContainerID: s.mainContainer.ID,
DefaultUser: user,
}, nil
}
}

func (s *Sidecar) setup() error {
if s.Client == nil {
dockerClient, err := NewClient("")
if err != nil {
return fmt.Errorf("failed to create docker client: %v", err)
}
s.Client = dockerClient
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
mainContainer, err := getMainContainer(ctx, s.Client)
cancel()
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func main() {
if err != nil {
fatal("failed to create docker client: %v", err)
}
sideCar := docker.Sidecar{Client: dockerClient}
executor, err = sideCar.ExecutorForUser(config.RunAsUser)
sideCar, err := docker.NewSidecar(dockerClient)
if err != nil {
fatal("failed to obtain executor for sidecar: %v", err)
fatal("failed to create sidecar: %v", err)
}
executor = sideCar.ExecutorForUser(config.RunAsUser)
filesystem = &executorFS{executor: executor}
err = sideCar.UploadToMainContainer(context.Background(), config.InputFile)
if err != nil {
Expand Down

0 comments on commit c3f4c88

Please sign in to comment.