Skip to content

Commit

Permalink
Remove duplication in eventing code
Browse files Browse the repository at this point in the history
And increase test coverage

Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Mar 19, 2019
1 parent f6c7f56 commit ea1b9bf
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 214 deletions.
41 changes: 6 additions & 35 deletions pkg/skaffold/build/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -67,49 +66,21 @@ func InParallel(ctx context.Context, out io.Writer, tags tag.ImageTags, artifact
}

color.Default.Fprintf(cw, "Building [%s]...\n", artifacts[i].ImageName)
event.Handle(&proto.Event{
EventType: &proto.Event_BuildEvent{
BuildEvent: &proto.BuildEvent{
Artifact: artifacts[i].ImageName,
Status: event.InProgress,
},
},
})

event.BuildInProgress(artifacts[i].ImageName)

tag, present := tags[artifacts[i].ImageName]
if !present {
errs[i] = fmt.Errorf("unable to find tag for image %s", artifacts[i].ImageName)
event.Handle(&proto.Event{
EventType: &proto.Event_BuildEvent{
BuildEvent: &proto.BuildEvent{
Artifact: artifacts[i].ImageName,
Status: event.Failed,
Err: errs[i].Error(),
},
},
})
event.BuildFailed(artifacts[i].ImageName, errs[i])
} else {
finalTags[i], errs[i] = buildArtifact(ctx, cw, artifacts[i], tag)
if errs[i] != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_BuildEvent{
BuildEvent: &proto.BuildEvent{
Artifact: artifacts[i].ImageName,
Status: event.Failed,
Err: errs[i].Error(),
},
},
})
event.BuildFailed(artifacts[i].ImageName, errs[i])
}
}
event.Handle(&proto.Event{
EventType: &proto.Event_BuildEvent{
BuildEvent: &proto.BuildEvent{
Artifact: artifacts[i].ImageName,
Status: event.Complete,
},
},
})

event.BuildComplete(artifacts[i].ImageName)
cw.Close()
}()

Expand Down
31 changes: 5 additions & 26 deletions pkg/skaffold/build/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/pkg/errors"
)
Expand All @@ -35,14 +34,8 @@ func InSequence(ctx context.Context, out io.Writer, tags tag.ImageTags, artifact

for _, artifact := range artifacts {
color.Default.Fprintf(out, "Building [%s]...\n", artifact.ImageName)
event.Handle(&proto.Event{
EventType: &proto.Event_BuildEvent{
BuildEvent: &proto.BuildEvent{
Artifact: artifact.ImageName,
Status: event.InProgress,
},
},
})

event.BuildInProgress(artifact.ImageName)

tag, present := tags[artifact.ImageName]
if !present {
Expand All @@ -51,25 +44,11 @@ func InSequence(ctx context.Context, out io.Writer, tags tag.ImageTags, artifact

finalTag, err := buildArtifact(ctx, out, artifact, tag)
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_BuildEvent{
BuildEvent: &proto.BuildEvent{
Artifact: artifact.ImageName,
Status: event.Failed,
Err: err.Error(),
},
},
})
event.BuildFailed(artifact.ImageName, err)
return nil, errors.Wrapf(err, "building [%s]", artifact.ImageName)
}
event.Handle(&proto.Event{
EventType: &proto.Event_BuildEvent{
BuildEvent: &proto.BuildEvent{
Artifact: artifact.ImageName,
Status: event.Complete,
},
},
})

event.BuildComplete(artifact.ImageName)

builds = append(builds, Artifact{
ImageName: artifact.ImageName,
Expand Down
32 changes: 8 additions & 24 deletions pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
Expand Down Expand Up @@ -70,40 +69,25 @@ func (h *HelmDeployer) Labels() map[string]string {
func (h *HelmDeployer) Deploy(ctx context.Context, out io.Writer, builds []build.Artifact, labellers []Labeller) error {
var dRes []Artifact

labels := merge(labellers...)
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.InProgress,
},
},
})
event.DeployInProgress()

for _, r := range h.Releases {
results, err := h.deployRelease(ctx, out, r, builds)
if err != nil {
releaseName, _ := evaluateReleaseName(r.Name)
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})

event.DeployFailed(err)
return errors.Wrapf(err, "deploying %s", releaseName)
}

dRes = append(dRes, results...)
}
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Complete,
},
},
})

event.DeployComplete()

labels := merge(labellers...)
labelDeployResults(labels, dRes)

return nil
}

Expand Down
54 changes: 7 additions & 47 deletions pkg/skaffold/deploy/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
Expand Down Expand Up @@ -69,24 +68,12 @@ func (k *KubectlDeployer) Deploy(ctx context.Context, out io.Writer, builds []bu
if err := k.kubectl.CheckVersion(ctx); err != nil {
color.Default.Fprintln(out, err)
}
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.InProgress,
},
},
})

event.DeployInProgress()

manifests, err := k.readManifests(ctx)
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})
event.DeployFailed(err)
return errors.Wrap(err, "reading manifests")
}

Expand All @@ -96,49 +83,22 @@ func (k *KubectlDeployer) Deploy(ctx context.Context, out io.Writer, builds []bu

manifests, err = manifests.ReplaceImages(builds, k.defaultRepo)
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})
event.DeployFailed(err)
return errors.Wrap(err, "replacing images in manifests")
}

manifests, err = manifests.SetLabels(merge(labellers...))
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})
event.DeployFailed(err)
return errors.Wrap(err, "setting labels in manifests")
}

err = k.kubectl.Apply(ctx, out, manifests)
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})
event.DeployFailed(err)
}

event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Complete,
},
},
})
event.DeployComplete()
return err
}

Expand Down
58 changes: 9 additions & 49 deletions pkg/skaffold/deploy/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
Expand Down Expand Up @@ -95,73 +94,34 @@ func (k *KustomizeDeployer) Deploy(ctx context.Context, out io.Writer, builds []

manifests, err := k.readManifests(ctx)
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})
event.DeployFailed(err)
return errors.Wrap(err, "reading manifests")
}

if len(manifests) == 0 {
return nil
}

event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.InProgress,
},
},
})
event.DeployInProgress()

manifests, err = manifests.ReplaceImages(builds, k.defaultRepo)
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})
event.DeployFailed(err)
return errors.Wrap(err, "replacing images in manifests")
}

manifests, err = manifests.SetLabels(merge(labellers...))
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})
event.DeployFailed(err)
return errors.Wrap(err, "setting labels in manifests")
}

err = k.kubectl.Apply(ctx, out, manifests)
if err != nil {
event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Failed,
Err: err.Error(),
},
},
})
}

event.Handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: &proto.DeployEvent{
Status: event.Complete,
},
},
})
event.DeployFailed(err)
}

event.DeployComplete()
return nil
}

Expand Down
Loading

0 comments on commit ea1b9bf

Please sign in to comment.