Skip to content

Commit

Permalink
implement pruning for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
nkubala committed Mar 6, 2019
1 parent 3e067ee commit 22d8784
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 66 deletions.
2 changes: 1 addition & 1 deletion pkg/skaffold/build/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (b *Builder) local(ctx context.Context, out io.Writer, tags tag.ImageTags,
}

func (b *Builder) Prune(ctx context.Context, out io.Writer) error {
// TODO(nkubala): implement
// bazel builds directly to the registry, so this is a noop
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/skaffold/build/plugin/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/plugin/builders/bazel"
Expand Down Expand Up @@ -67,6 +68,10 @@ func Execute() error {
err := <-errCh

if err == cancelError {
// there is a race between skaffold's cleanup process and us shutting down the plugin client.
// since we are actually using the plugin to perform the image pruning, we need to give skaffold
// a small window to call this before we can safely shutdown.
time.Sleep(30 * time.Millisecond)
hashiplugin.CleanupClients()
}
if err != nil {
Expand Down
93 changes: 93 additions & 0 deletions pkg/skaffold/plugin/shared/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package shared

import (
"context"
"io"
"net/rpc"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// BuilderRPC is an implementation of an rpc client
type BuilderRPC struct {
client *rpc.Client
}

func (b *BuilderRPC) Init(opts *config.SkaffoldOptions, env *latest.ExecutionEnvironment) {
// We don't expect a response, so we can just use interface{}
var resp interface{}
args := InitArgs{
Opts: opts,
Env: env,
}
b.client.Call("Plugin.Init", args, &resp)
}

func (b *BuilderRPC) DependenciesForArtifact(_ context.Context, artifact *latest.Artifact) ([]string, error) {
var resp []string
if err := convertPropertiesToBytes([]*latest.Artifact{artifact}); err != nil {
return nil, errors.Wrapf(err, "converting properties to bytes")
}
args := DependencyArgs{artifact}
err := b.client.Call("Plugin.DependenciesForArtifact", args, &resp)
if err != nil {
return nil, err
}
return resp, nil
}

func (b *BuilderRPC) Labels() map[string]string {
var resp map[string]string
err := b.client.Call("Plugin.Labels", new(interface{}), &resp)
if err != nil {
// Can't return error, so log it instead
logrus.Errorf("Unable to get labels from server: %v", err)
}
return resp
}

func (b *BuilderRPC) Build(_ context.Context, _ io.Writer, tags tag.ImageTags, artifacts []*latest.Artifact) ([]build.Artifact, error) {
var resp []build.Artifact
if err := convertPropertiesToBytes(artifacts); err != nil {
return nil, errors.Wrapf(err, "converting properties to bytes")
}
args := BuildArgs{
ImageTags: tags,
Artifacts: artifacts,
}
err := b.client.Call("Plugin.Build", args, &resp)
if err != nil {
return nil, err
}
return resp, nil
}

func (b *BuilderRPC) Prune(ctx context.Context, out io.Writer) error {
var resp interface{}
if err := b.client.Call("Plugin.Prune", new(interface{}), &resp); err != nil {
return err
}
return nil
}
70 changes: 5 additions & 65 deletions pkg/skaffold/plugin/shared/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package shared

import (
"context"
"io"
"net/rpc"
"os"

Expand All @@ -28,72 +27,9 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
plugin "github.com/hashicorp/go-plugin"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)

// BuilderRPC is an implementation of an rpc client
type BuilderRPC struct {
client *rpc.Client
}

func (b *BuilderRPC) Init(opts *config.SkaffoldOptions, env *latest.ExecutionEnvironment) {
// We don't expect a response, so we can just use interface{}
var resp interface{}
args := InitArgs{
Opts: opts,
Env: env,
}
b.client.Call("Plugin.Init", args, &resp)
}

func (b *BuilderRPC) DependenciesForArtifact(_ context.Context, artifact *latest.Artifact) ([]string, error) {
var resp []string
if err := convertPropertiesToBytes([]*latest.Artifact{artifact}); err != nil {
return nil, errors.Wrapf(err, "converting properties to bytes")
}
args := DependencyArgs{artifact}
err := b.client.Call("Plugin.DependenciesForArtifact", args, &resp)
if err != nil {
return nil, err
}
return resp, nil
}

func (b *BuilderRPC) Labels() map[string]string {
var resp map[string]string
err := b.client.Call("Plugin.Labels", new(interface{}), &resp)
if err != nil {
// Can't return error, so log it instead
logrus.Errorf("Unable to get labels from server: %v", err)
}
return resp
}

func (b *BuilderRPC) Build(_ context.Context, _ io.Writer, tags tag.ImageTags, artifacts []*latest.Artifact) ([]build.Artifact, error) {
var resp []build.Artifact
if err := convertPropertiesToBytes(artifacts); err != nil {
return nil, errors.Wrapf(err, "converting properties to bytes")
}
args := BuildArgs{
ImageTags: tags,
Artifacts: artifacts,
}
err := b.client.Call("Plugin.Build", args, &resp)
if err != nil {
return nil, err
}
return resp, nil
}

func (b *BuilderRPC) Prune(_ context.Context, _ io.Writer) error {
var resp error
if err := b.client.Call("Plugin.Prune", new(interface{}), &resp); err != nil {
return err
}
return resp
}

func convertPropertiesToBytes(artifacts []*latest.Artifact) error {
for _, a := range artifacts {
if a.BuilderPlugin.Properties == nil {
Expand All @@ -120,7 +56,7 @@ func (s *BuilderRPCServer) Init(args InitArgs, resp *interface{}) error {
return nil
}

func (s *BuilderRPCServer) Labels(args interface{}, resp *map[string]string) error {
func (s *BuilderRPCServer) Labels(_ interface{}, resp *map[string]string) error {
*resp = s.Impl.Labels()
return nil
}
Expand All @@ -134,6 +70,10 @@ func (s *BuilderRPCServer) Build(b BuildArgs, resp *[]build.Artifact) error {
return nil
}

func (s *BuilderRPCServer) Prune(args interface{}, resp *interface{}) error {
return s.Impl.Prune(context.Background(), os.Stdout)
}

func (s *BuilderRPCServer) DependenciesForArtifact(d DependencyArgs, resp *[]string) error {
dependencies, err := s.Impl.DependenciesForArtifact(context.Background(), d.Artifact)
if err != nil {
Expand Down

0 comments on commit 22d8784

Please sign in to comment.