-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2155 from nkubala/runner-refactor
Refactor the runner a bit
- Loading branch information
Showing
7 changed files
with
283 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.