-
Notifications
You must be signed in to change notification settings - Fork 5
/
cluster.go
200 lines (183 loc) · 6.16 KB
/
cluster.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package tektontaskrun
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/opendevstack/ods-pipeline/internal/command"
"github.com/opendevstack/ods-pipeline/internal/projectpath"
)
const (
DefaultServiceAccountName = "pipeline"
KinDMountHostPath = "/tmp/ods-pipeline/kind-mount"
KinDMountContainerPath = "/files"
KinDRegistry = "localhost:5000"
KinDName = "ods-pipeline"
)
var recreateClusterFlag = flag.Bool("ods-recreate-cluster", false, "Whether to remove and recreate the KinD cluster named 'ods-pipeline'")
var registryPortFlag = flag.String("ods-cluster-registry-port", "5000", "Port of cluster registry")
var outsideKindFlag = flag.Bool("ods-outside-kind", false, "Whether to continue if the Kube context is not set to the KinD cluster")
var reuseImagesFlag = flag.Bool("ods-reuse-images", false, "Whether to reuse existing images instead of building again")
var debugFlag = flag.Bool("ods-debug", false, "Turn on debug mode for scripts etc.")
// ClusterOpt allows to further configure the KinD cluster after its creation.
type ClusterOpt func(c *ClusterConfig) error
// ClusterConfig represents key configuration of the KinD cluster.
type ClusterConfig struct {
StorageSourceDir string
StorageCapacity string
StorageClassName string
Registry string
DefaultRepository string
}
// ImageBuildConfig represents the config used to build a container image.
type ImageBuildConfig struct {
Dockerfile string
Tag string
ContextDir string
}
// Process validates the configuration and defaults the image tag if unset
// using the defaultImageRepository and Dockerfile values.
func (ibc *ImageBuildConfig) Process(defaultImageRepository string) error {
if ibc.Dockerfile == "" || ibc.ContextDir == "" {
return errors.New("both Dockerfile and ContextDir must be set")
}
if ibc.Tag == "" {
imageName := strings.TrimPrefix(path.Base(ibc.Dockerfile), "Dockerfile.")
ibc.Tag = fmt.Sprintf("%s/%s:latest", defaultImageRepository, imageName)
}
return nil
}
// NewClusterConfig creates a new ClusterConfig instance.
func NewClusterConfig() *ClusterConfig {
return &ClusterConfig{
StorageClassName: "standard", // if using KinD, set it to "standard"
StorageCapacity: "1Gi",
StorageSourceDir: KinDMountContainerPath,
Registry: KinDRegistry,
DefaultRepository: "ods-pipeline",
}
}
// DefaultImageRepository returns the registry + default repository
// combination.
func (c *ClusterConfig) DefaultImageRepository() string {
return c.Registry + "/" + c.DefaultRepository
}
// DefaultTaskTemplateData returns a map with default values which can be used
// in task templates.
func (c *ClusterConfig) DefaultTaskTemplateData() map[string]string {
return map[string]string{
"ImageRepository": c.DefaultImageRepository(),
"Version": "latest",
}
}
// StartKinDCluster starts a KinD cluster with Tekton installed.
// Afterwards, any given ClusterOpt is applied.
func StartKinDCluster(opts ...ClusterOpt) (*ClusterConfig, error) {
flag.Parse()
if err := checkCluster(*outsideKindFlag); err != nil {
return nil, fmt.Errorf("check kubectl context: %s", err)
}
if err := createKinDCluster(*debugFlag); err != nil {
return nil, fmt.Errorf("create KinD cluster: %s", err)
}
if err := installTektonPipelines(*debugFlag); err != nil {
return nil, fmt.Errorf("install Tekton: %s", err)
}
c := NewClusterConfig()
for _, o := range opts {
err := o(c)
if err != nil {
return nil, err
}
}
return c, nil
}
// LoadImage builds a container image using the docker CLI based on the given
// ImageBuildConfig.
//
// The ImageBuildConfig must set at least Dockerfile and ContextDir option.
// If Tag is unset, it is inferred from the default registry and the Dockerfile
// name. For example, given a Dockerfile of "Dockerfile.foobar", the tag is
// defaulted to localhost:5000/ods-pipeline/foobar.
// Passing the flag -ods-reuse-images to the tests will skip image rebuilding.
func LoadImage(ibc ImageBuildConfig) ClusterOpt {
flag.Parse()
return func(c *ClusterConfig) error {
buildImage := true
err := ibc.Process(c.DefaultImageRepository())
if err != nil {
return fmt.Errorf("processing image build config: %s", err)
}
if *reuseImagesFlag {
cmd := exec.Command("docker", "images", "-q", ibc.Tag)
b, err := cmd.Output()
if err != nil {
return err
}
imageID := strings.TrimSpace(string(b))
if imageID != "" {
log.Printf("Reusing image ID %s for tag %s ...\n", imageID, ibc.Tag)
buildImage = false
}
}
if buildImage {
log.Printf("Building image %s from %s ...\n", ibc.Tag, ibc.Dockerfile)
if !path.IsAbs(ibc.Dockerfile) {
ibc.Dockerfile = filepath.Join(ibc.ContextDir, ibc.Dockerfile)
}
args := []string{
"build",
"-f", ibc.Dockerfile,
"-t", ibc.Tag,
ibc.ContextDir,
}
if err := command.Run("docker", args, []string{}, os.Stdout, os.Stderr); err != nil {
return err
}
}
return command.Run("docker", []string{"push", ibc.Tag}, []string{}, os.Stdout, os.Stderr)
}
}
func checkCluster(outsideKindAllowed bool) error {
if !outsideKindAllowed {
cmd := exec.Command("kubectl", "config", "current-context")
b, err := cmd.Output()
if err != nil || len(b) == 0 {
log.Println("did not detect existing kubectl context")
return nil
}
gotContext := strings.TrimSpace(string(b))
wantCluster := "ods-pipeline"
if gotContext != "kind-"+wantCluster {
return fmt.Errorf("not running tests outside KinD cluster ('%s') without -ods-outside-kind! Current context: %s", wantCluster, gotContext)
}
}
return nil
}
func createKinDCluster(debug bool) error {
args := []string{
projectpath.RootedPath("scripts/kind-with-registry.sh"),
"--registry-port=" + *registryPortFlag,
}
if *recreateClusterFlag {
args = append(args, "--recreate")
}
if debug {
args = append(args, "--verbose")
}
return command.Run("bash", args, []string{}, os.Stdout, os.Stderr)
}
func installTektonPipelines(debug bool) error {
args := []string{
projectpath.RootedPath("scripts/install-tekton-pipelines.sh"),
}
if debug {
args = append(args, "--verbose")
}
return command.Run("sh", args, []string{}, os.Stdout, os.Stderr)
}