Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break Pipelines' dependency on Build #648

Merged
merged 4 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ required = [
name = "go.uber.org/zap"
revision = "67bc79d13d155c02fd008f721863ff8cc5f30659"

[[constraint]]
name = "github.com/knative/build"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 🎉

revision = "dd3ceb3323922b899a0a913f885fcf59943e7b59"

[prune]
go-tests = true
unused-packages = true
Expand Down
3 changes: 0 additions & 3 deletions config/200-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ rules:
- apiGroups: ["tekton.dev"]
resources: ["tasks/status", "clustertasks/status", "taskruns/status", "pipelines/status", "pipelineruns/status", "pipelineresources/status"]
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
- apiGroups: ["build.knative.dev"]
resources: ["builds", "buildtemplates", "clusterbuildtemplates"]
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1alpha1/task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (ts *TaskSpec) Validate(ctx context.Context) *apis.FieldError {
if len(ts.Steps) == 0 {
return apis.ErrMissingField("steps")
}
if err := validateVolumes(ts.Volumes).ViaField("volumes"); err != nil {
if err := ValidateVolumes(ts.Volumes).ViaField("volumes"); err != nil {
return err
}
if err := validateSteps(ts.Steps).ViaField("steps"); err != nil {
Expand Down Expand Up @@ -94,7 +94,7 @@ func (ts *TaskSpec) Validate(ctx context.Context) *apis.FieldError {
return nil
}

func validateVolumes(volumes []corev1.Volume) *apis.FieldError {
func ValidateVolumes(volumes []corev1.Volume) *apis.FieldError {
// Task must not have duplicate volume names.
vols := map[string]struct{}{}
for _, v := range volumes {
Expand Down
25 changes: 11 additions & 14 deletions pkg/reconciler/v1alpha1/taskrun/entrypoint/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
lru "github.com/hashicorp/golang-lru"
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"

"github.com/knative/build/pkg/apis/build/v1alpha1"
)

const (
Expand Down Expand Up @@ -87,12 +85,11 @@ func AddToEntrypointCache(c *Cache, sha string, ep []string) {
c.set(sha, ep)
}

// AddCopyStep will prepend a BuildStep (Container) that will
// AddCopyStep will prepend a Step (Container) that will
// copy the entrypoint binary from the entrypoint image into the
// volume mounted at MountPoint, so that it can be mounted by
// subsequent steps and used to capture logs.
func AddCopyStep(b *v1alpha1.BuildSpec) {

func AddCopyStep(spec *v1alpha1.TaskSpec) {
cp := corev1.Container{
Name: InitContainerName,
Image: *entrypointImage,
Expand All @@ -101,21 +98,21 @@ func AddCopyStep(b *v1alpha1.BuildSpec) {
Args: []string{"-c", fmt.Sprintf("if [[ -d /ko-app ]]; then cp /ko-app/entrypoint %s; else cp /ko-app %s; fi;", BinaryLocation, BinaryLocation)},
VolumeMounts: []corev1.VolumeMount{toolsMount},
}
b.Steps = append([]corev1.Container{cp}, b.Steps...)
spec.Steps = append([]corev1.Container{cp}, spec.Steps...)

}

// RedirectSteps will modify each of the steps/containers such that
// the binary being run is no longer the one specified by the Command
// and the Args, but is instead the entrypoint binary, which will
// itself invoke the Command and Args, but also capture logs.
func RedirectSteps(cache *Cache, steps []corev1.Container, kubeclient kubernetes.Interface, build *buildv1alpha1.Build, logger *zap.SugaredLogger) error {
func RedirectSteps(cache *Cache, steps []corev1.Container, kubeclient kubernetes.Interface, taskRun *v1alpha1.TaskRun, logger *zap.SugaredLogger) error {
for i := range steps {
step := &steps[i]
if len(step.Command) == 0 {
logger.Infof("Getting Cmd from remote entrypoint for step: %s", step.Name)
var err error
step.Command, err = GetRemoteEntrypoint(cache, step.Image, kubeclient, build)
step.Command, err = GetRemoteEntrypoint(cache, step.Image, kubeclient, taskRun)
if err != nil {
logger.Errorf("Error getting entry point image", err.Error())
return err
Expand Down Expand Up @@ -158,11 +155,11 @@ func GetArgs(stepNum int, commands, args []string) []string {
// GetRemoteEntrypoint accepts a cache of digest lookups, as well as the digest
// to look for. If the cache does not contain the digest, it will lookup the
// metadata from the images registry, and then commit that to the cache
func GetRemoteEntrypoint(cache *Cache, digest string, kubeclient kubernetes.Interface, build *buildv1alpha1.Build) ([]string, error) {
func GetRemoteEntrypoint(cache *Cache, digest string, kubeclient kubernetes.Interface, taskRun *v1alpha1.TaskRun) ([]string, error) {
if ep, ok := cache.get(digest); ok {
return ep, nil
}
img, err := getRemoteImage(digest, kubeclient, build)
img, err := getRemoteImage(digest, kubeclient, taskRun)
if err != nil {
return nil, fmt.Errorf("Failed to fetch remote image %s: %v", digest, err)
}
Expand All @@ -179,16 +176,16 @@ func GetRemoteEntrypoint(cache *Cache, digest string, kubeclient kubernetes.Inte
return command, nil
}

func getRemoteImage(image string, kubeclient kubernetes.Interface, build *buildv1alpha1.Build) (v1.Image, error) {
func getRemoteImage(image string, kubeclient kubernetes.Interface, taskRun *v1alpha1.TaskRun) (v1.Image, error) {
// verify the image name, then download the remote config file
ref, err := name.ParseReference(image, name.WeakValidation)
if err != nil {
return nil, fmt.Errorf("Failed to parse image %s: %v", image, err)
}

kc, err := k8schain.New(kubeclient, k8schain.Options{
Namespace: build.Namespace,
ServiceAccountName: build.Spec.ServiceAccountName,
Namespace: taskRun.Namespace,
ServiceAccountName: taskRun.Spec.ServiceAccount,
})
if err != nil {
return nil, fmt.Errorf("Failed to create k8schain: %v", err)
Expand Down
94 changes: 48 additions & 46 deletions pkg/reconciler/v1alpha1/taskrun/entrypoint/entrypoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/partial"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/knative/build/pkg/apis/build/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
corev1 "k8s.io/api/core/v1"
Expand All @@ -37,23 +37,25 @@ func TestRewriteSteps(t *testing.T) {
Args: []string{"efgh"},
},
}
build := &v1alpha1.Build{
taskRun := &v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "build",
Name: "taskRun",
},
Spec: v1alpha1.BuildSpec{
Steps: []corev1.Container{{
Image: "ubuntu",
Command: []string{"echo"},
Args: []string{"hello"},
}},
Spec: v1alpha1.TaskRunSpec{
TaskSpec: &v1alpha1.TaskSpec{
Steps: []corev1.Container{{
Image: "ubuntu",
Command: []string{"echo"},
Args: []string{"hello"},
}},
},
},
}
observer, _ := observer.New(zap.InfoLevel)
entrypointCache, _ := NewCache()
c := fakekubeclientset.NewSimpleClientset()
err := RedirectSteps(entrypointCache, inputs, c, build, zap.New(observer).Sugar())
err := RedirectSteps(entrypointCache, inputs, c, taskRun, zap.New(observer).Sugar())
if err != nil {
t.Errorf("failed to get resources: %v", err)
}
Expand Down Expand Up @@ -231,18 +233,20 @@ func TestGetRemoteEntrypoint(t *testing.T) {
if err != nil {
t.Fatalf("couldn't create new entrypoint cache: %v", err)
}
build := &v1alpha1.Build{
taskRun := &v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "build",
Name: "taskRun",
},
Spec: v1alpha1.BuildSpec{
ServiceAccountName: "default",
Steps: []corev1.Container{{
Image: "ubuntu",
Command: []string{"echo"},
Args: []string{"hello"},
}},
Spec: v1alpha1.TaskRunSpec{
ServiceAccount: "default",
TaskSpec: &v1alpha1.TaskSpec{
Steps: []corev1.Container{{
Image: "ubuntu",
Command: []string{"echo"},
Args: []string{"hello"},
}},
},
},
}
c := fakekubeclientset.NewSimpleClientset(&corev1.ServiceAccount{
Expand All @@ -251,7 +255,7 @@ func TestGetRemoteEntrypoint(t *testing.T) {
Namespace: "foo",
},
})
ep, err := GetRemoteEntrypoint(entrypointCache, finalDigest, c, build)
ep, err := GetRemoteEntrypoint(entrypointCache, finalDigest, c, taskRun)
if err != nil {
t.Errorf("couldn't get entrypoint remote: %v", err)
}
Expand Down Expand Up @@ -298,18 +302,20 @@ func TestGetRemoteEntrypointWithNonDefaultSA(t *testing.T) {
if err != nil {
t.Fatalf("couldn't create new entrypoint cache: %v", err)
}
build := &v1alpha1.Build{
taskRun := &v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "build",
Name: "taskRun",
},
Spec: v1alpha1.BuildSpec{
ServiceAccountName: "some-other-sa",
Steps: []corev1.Container{{
Image: "ubuntu",
Command: []string{"echo"},
Args: []string{"hello"},
}},
Spec: v1alpha1.TaskRunSpec{
ServiceAccount: "some-other-sa",
TaskSpec: &v1alpha1.TaskSpec{
Steps: []corev1.Container{{
Image: "ubuntu",
Command: []string{"echo"},
Args: []string{"hello"},
}},
},
},
}
c := fakekubeclientset.NewSimpleClientset(&corev1.ServiceAccount{
Expand All @@ -318,7 +324,7 @@ func TestGetRemoteEntrypointWithNonDefaultSA(t *testing.T) {
Namespace: "foo",
},
})
ep, err := GetRemoteEntrypoint(entrypointCache, finalDigest, c, build)
ep, err := GetRemoteEntrypoint(entrypointCache, finalDigest, c, taskRun)
if err != nil {
t.Errorf("couldn't get entrypoint remote: %v", err)
}
Expand Down Expand Up @@ -353,24 +359,20 @@ func TestEntrypointCacheLRU(t *testing.T) {
}

func TestAddCopyStep(t *testing.T) {

bs := &v1alpha1.BuildSpec{
Steps: []corev1.Container{
{
Name: "test",
},
{
Name: "test",
},
},
ts := &v1alpha1.TaskSpec{
Steps: []corev1.Container{{
Name: "test",
}, {
Name: "test",
}},
}

expectedSteps := len(bs.Steps) + 1
AddCopyStep(bs)
if len(bs.Steps) != 3 {
t.Errorf("BuildSpec has the wrong step count: %d should be %d", len(bs.Steps), expectedSteps)
expectedSteps := len(ts.Steps) + 1
AddCopyStep(ts)
if len(ts.Steps) != 3 {
t.Errorf("BuildSpec has the wrong step count: %d should be %d", len(ts.Steps), expectedSteps)
}
if bs.Steps[0].Name != InitContainerName {
t.Errorf("entrypoint is incorrect: %s should be %s", bs.Steps[0].Name, InitContainerName)
if ts.Steps[0].Name != InitContainerName {
t.Errorf("entrypoint is incorrect: %s should be %s", ts.Steps[0].Name, InitContainerName)
}
}
31 changes: 15 additions & 16 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ package resources
import (
"fmt"

buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/templating"
)

// ApplyParameters applies the params from a TaskRun.Input.Parameters to a BuildSpec.
func ApplyParameters(b *buildv1alpha1.Build, tr *v1alpha1.TaskRun, defaults ...v1alpha1.TaskParam) *buildv1alpha1.Build {
// ApplyParameters applies the params from a TaskRun.Input.Parameters to a TaskSpec
func ApplyParameters(spec *v1alpha1.TaskSpec, tr *v1alpha1.TaskRun, defaults ...v1alpha1.TaskParam) *v1alpha1.TaskSpec {
// This assumes that the TaskRun inputs have been validated against what the Task requests.
replacements := map[string]string{}
// Set all the default replacements
Expand All @@ -39,12 +38,12 @@ func ApplyParameters(b *buildv1alpha1.Build, tr *v1alpha1.TaskRun, defaults ...v
replacements[fmt.Sprintf("inputs.params.%s", p.Name)] = p.Value
}

return ApplyReplacements(b, replacements)
return ApplyReplacements(spec, replacements)
}

// ApplyResources applies the templating from values in resources which are referenced in b as subitems
// ApplyResources applies the templating from values in resources which are referenced in spec as subitems
// of the replacementStr. It retrieves the referenced resources via the getter.
func ApplyResources(b *buildv1alpha1.Build, resources []v1alpha1.TaskResourceBinding, getter GetResource, replacementStr string) (*buildv1alpha1.Build, error) {
func ApplyResources(spec *v1alpha1.TaskSpec, resources []v1alpha1.TaskResourceBinding, getter GetResource, replacementStr string) (*v1alpha1.TaskSpec, error) {
replacements := map[string]string{}

for _, r := range resources {
Expand All @@ -61,15 +60,15 @@ func ApplyResources(b *buildv1alpha1.Build, resources []v1alpha1.TaskResourceBin
replacements[fmt.Sprintf("%s.resources.%s.%s", replacementStr, r.Name, k)] = v
}
}
return ApplyReplacements(b, replacements), nil
return ApplyReplacements(spec, replacements), nil
}

// ApplyReplacements replaces placeholders for declared parameters with the specified replacements.
func ApplyReplacements(build *buildv1alpha1.Build, replacements map[string]string) *buildv1alpha1.Build {
build = build.DeepCopy()
func ApplyReplacements(spec *v1alpha1.TaskSpec, replacements map[string]string) *v1alpha1.TaskSpec {
spec = spec.DeepCopy()

// Apply variable expansion to steps fields.
steps := build.Spec.Steps
steps := spec.Steps
for i := range steps {
steps[i].Name = templating.ApplyReplacements(steps[i].Name, replacements)
steps[i].Image = templating.ApplyReplacements(steps[i].Image, replacements)
Expand All @@ -91,18 +90,18 @@ func ApplyReplacements(build *buildv1alpha1.Build, replacements map[string]strin
}

// Apply variable expansion to the build's volumes
for i, v := range build.Spec.Volumes {
build.Spec.Volumes[i].Name = templating.ApplyReplacements(v.Name, replacements)
for i, v := range spec.Volumes {
spec.Volumes[i].Name = templating.ApplyReplacements(v.Name, replacements)
if v.VolumeSource.ConfigMap != nil {
build.Spec.Volumes[i].ConfigMap.Name = templating.ApplyReplacements(v.ConfigMap.Name, replacements)
spec.Volumes[i].ConfigMap.Name = templating.ApplyReplacements(v.ConfigMap.Name, replacements)
}
if v.VolumeSource.Secret != nil {
build.Spec.Volumes[i].Secret.SecretName = templating.ApplyReplacements(v.Secret.SecretName, replacements)
spec.Volumes[i].Secret.SecretName = templating.ApplyReplacements(v.Secret.SecretName, replacements)
}
if v.PersistentVolumeClaim != nil {
build.Spec.Volumes[i].PersistentVolumeClaim.ClaimName = templating.ApplyReplacements(v.PersistentVolumeClaim.ClaimName, replacements)
spec.Volumes[i].PersistentVolumeClaim.ClaimName = templating.ApplyReplacements(v.PersistentVolumeClaim.ClaimName, replacements)
}
}

return build
return spec
}
Loading