Skip to content

Commit

Permalink
Merge pull request #2155 from nkubala/runner-refactor
Browse files Browse the repository at this point in the history
Refactor the runner a bit
  • Loading branch information
nkubala authored May 21, 2019
2 parents d048cdd + 1827195 commit 88b66f4
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 156 deletions.
57 changes: 57 additions & 0 deletions pkg/skaffold/runner/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
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 runner

import (
"context"
"io"

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

// BuildAndTest builds artifacts and runs tests on built artifacts
func (r *SkaffoldRunner) BuildAndTest(ctx context.Context, out io.Writer, artifacts []*latest.Artifact) ([]build.Artifact, error) {
tags, err := r.imageTags(ctx, out, artifacts)
if err != nil {
return nil, errors.Wrap(err, "generating tag")
}
r.hasBuilt = true

artifactsToBuild, res, err := r.cache.RetrieveCachedArtifacts(ctx, out, artifacts)
if err != nil {
return nil, errors.Wrap(err, "retrieving cached artifacts")
}

bRes, err := r.Build(ctx, out, tags, artifactsToBuild)
if err != nil {
return nil, errors.Wrap(err, "build failed")
}
r.cache.RetagLocalImages(ctx, out, artifactsToBuild, bRes)
bRes = append(bRes, res...)
if err := r.cache.CacheArtifacts(ctx, artifacts, bRes); err != nil {
logrus.Warnf("error caching artifacts: %v", err)
}
if !r.runCtx.Opts.SkipTests {
if err = r.Test(ctx, out, bRes); err != nil {
return nil, errors.Wrap(err, "test failed")
}
}
return bRes, err
}
47 changes: 47 additions & 0 deletions pkg/skaffold/runner/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 runner

import (
"context"
"io"

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

// Deploy deploys build artifacts.
func (r *SkaffoldRunner) Deploy(ctx context.Context, out io.Writer, artifacts []build.Artifact) error {
if err := r.deploy(ctx, out, artifacts); err != nil {
return err
}
if r.runCtx.Opts.Tail {
images := make([]string, len(artifacts))
for i, a := range artifacts {
images[i] = a.ImageName
}
logger := r.newLoggerForImages(out, images)
return r.TailLogs(ctx, out, logger)
}
return nil
}

// Deploy deploys the given artifacts and tail logs if tail present
func (r *SkaffoldRunner) deploy(ctx context.Context, out io.Writer, artifacts []build.Artifact) error {
err := r.Deployer.Deploy(ctx, out, artifacts, r.labellers)
r.hasDeployed = true
return err
}
46 changes: 46 additions & 0 deletions pkg/skaffold/runner/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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 runner

import (
"context"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/pkg/errors"
)

func (r *SkaffoldRunner) newLogger(out io.Writer, artifacts []*latest.Artifact) *kubernetes.LogAggregator {
var imageNames []string
for _, artifact := range artifacts {
imageNames = append(imageNames, artifact.ImageName)
}
return r.newLoggerForImages(out, imageNames)
}

func (r *SkaffoldRunner) newLoggerForImages(out io.Writer, images []string) *kubernetes.LogAggregator {
return kubernetes.NewLogAggregator(out, images, r.imageList, r.runCtx.Namespaces)
}

func (r *SkaffoldRunner) TailLogs(ctx context.Context, out io.Writer, logger *kubernetes.LogAggregator) error {
if err := logger.Start(ctx); err != nil {
return errors.Wrap(err, "starting logger")
}
<-ctx.Done()
return nil
}
36 changes: 36 additions & 0 deletions pkg/skaffold/runner/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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 runner

import (
"context"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

// Run builds artifacts, runs tests on built artifacts, and then deploys them.
func (r *SkaffoldRunner) Run(ctx context.Context, out io.Writer, artifacts []*latest.Artifact) error {
if err := r.buildTestDeploy(ctx, out, artifacts); err != nil {
return err
}
if r.runCtx.Opts.Tail {
logger := r.newLogger(out, artifacts)
return r.TailLogs(ctx, out, logger)
}
return nil
}
85 changes: 85 additions & 0 deletions pkg/skaffold/runner/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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 runner

import (
"context"
"io/ioutil"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd/api"
)

func TestRun(t *testing.T) {
restore := testutil.SetupFakeKubernetesContext(t, api.Config{CurrentContext: "cluster1"})
defer restore()

var tests = []struct {
description string
testBench *TestBench
shouldErr bool
expectedActions []Actions
}{
{
description: "run no error",
testBench: &TestBench{},
expectedActions: []Actions{{
Built: []string{"img:1"},
Tested: []string{"img:1"},
Deployed: []string{"img:1"},
}},
},
{
description: "run build error",
testBench: &TestBench{buildErrors: []error{errors.New("")}},
shouldErr: true,
expectedActions: []Actions{{}},
},
{
description: "run test error",
testBench: &TestBench{testErrors: []error{errors.New("")}},
shouldErr: true,
expectedActions: []Actions{{
Built: []string{"img:1"},
}},
},
{
description: "run deploy error",
testBench: &TestBench{deployErrors: []error{errors.New("")}},
shouldErr: true,
expectedActions: []Actions{{
Built: []string{"img:1"},
Tested: []string{"img:1"},
}},
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
runner := createRunner(t, test.testBench)

err := runner.Run(context.Background(), ioutil.Discard, []*latest.Artifact{{
ImageName: "img",
}})

testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expectedActions, test.testBench.Actions())
})
}
}
Loading

0 comments on commit 88b66f4

Please sign in to comment.