generated from knative-extensions/sample-controller
-
Notifications
You must be signed in to change notification settings - Fork 27
/
build.go
113 lines (102 loc) · 3.36 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright 2020 The Knative 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 ko
import (
"context"
"fmt"
"strings"
"github.com/google/go-containerregistry/pkg/name"
"github.com/mattmoor/mink/pkg/constants"
tknv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/ptr"
)
// Options holds configuration options specific to Dockerfile builds
type Options struct {
// ImportPath is the path to ko publish
ImportPath string
}
var (
// KoImageString holds a reference to a built image of github.com/google/ko
// See ./hack/build-flags.sh for how this is replaced at link-time.
KoImageString = "ghcr.io/mattmoor/github.com/google/ko/cmd/ko:latest"
)
// Build returns a TaskRun suitable for performing a "ko publish" build over the
// provided source and publishing to the target tag.
func Build(ctx context.Context, source name.Reference, target name.Tag, opt Options) *tknv1beta1.TaskRun {
return &tknv1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "ko-publish-",
},
Spec: tknv1beta1.TaskRunSpec{
PodTemplate: &tknv1beta1.PodTemplate{
EnableServiceLinks: ptr.Bool(false),
},
TaskSpec: &tknv1beta1.TaskSpec{
Results: []tknv1beta1.TaskResult{{
Name: constants.ImageDigestResult,
}},
Steps: []tknv1beta1.Step{{
Container: corev1.Container{
Name: "extract-bundle",
Image: source.String(),
WorkingDir: "/workspace",
},
}, {
Container: corev1.Container{
Name: "ko-publish",
Image: KoImageString,
Env: []corev1.EnvVar{{
Name: "DOCKER_CONFIG",
Value: "/tekton/home/.docker",
}, {
Name: "KO_DOCKER_REPO",
Value: target.Repository.String(),
}},
Command: []string{
"/bin/bash", "-c",
},
Args: []string{
strings.Join([]string{
// Good for debugging.
"go env",
// Not set for some reason :rolls_eyes:
"export GOARCH=$(go env GOARCH)",
"export GOOS=$(go env GOOS)",
"export GOARM=$(go env GOARM)",
"export GOROOT=$(go env GOROOT)",
// Enable estargz support
"export GGCR_EXPERIMENT_ESTARGZ=1",
// Where the magic happens.
fmt.Sprintf("ko publish --bare %s | cut -d'@' -f 2 > /tekton/results/%s", opt.ImportPath, constants.ImageDigestResult),
}, " && "),
},
Resources: corev1.ResourceRequirements{
// Set requests based on a typical ko task,
// but do not set limits because it could
// go well beyond this (in theory).
Requests: corev1.ResourceList{
// 1 is typical, but I've seen up to 2.
"cpu": resource.MustParse("1"),
// 500-700Mi was typical.
"memory": resource.MustParse("1Gi"),
},
},
},
}},
},
},
}
}