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

feat: create init container for job #979

Merged
merged 78 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
c71a185
ref: rename test - test-suite in opanapi spec nad model part1
exu Feb 10, 2022
3b4c31c
rename _test to _test_suite
exu Feb 10, 2022
9902bda
fix: generated testsuite from openapispec
exu Feb 10, 2022
e9c7d76
Merge remote-tracking branch 'origin/main' into jacek/fix/nameing-ref…
exu Feb 10, 2022
3449e28
chore: rename TestKube to Testkube
exu Feb 10, 2022
7a6f0f6
rename _script to _test
exu Feb 10, 2022
c7575f1
rename _script to _test part2
exu Feb 10, 2022
0b9513d
testName to testSuiteName and scriptName to testName
exu Feb 10, 2022
2a7f814
api client refactor
exu Feb 10, 2022
b7c80e4
chore: rename testsuite to test and scripts to tests - commands
exu Feb 10, 2022
8878bc4
script to test refactor
exu Feb 10, 2022
a0bce83
mappers renaming
exu Feb 10, 2022
a1cf781
scripts
exu Feb 10, 2022
7b65be8
more rename
exu Feb 10, 2022
8e3fb14
more scripts renaming
exu Feb 10, 2022
0476f89
feat: bump operator version
exu Feb 10, 2022
4e69e5b
fix: using new operator clients and apis
exu Feb 10, 2022
88658e8
docs update
exu Feb 10, 2022
122abfb
fixed tests
exu Feb 10, 2022
d657676
renamed proxy client in tests
exu Feb 10, 2022
fca69c7
docs - generated from cobra commandfs
exu Feb 10, 2022
04624f4
fix: integration tests
exu Feb 10, 2022
a309433
fix: more scripts to tests renaming
exu Feb 11, 2022
63fe640
fix: more scripts to tests renaming
exu Feb 11, 2022
806532a
fix: docs with testsuites
exu Feb 11, 2022
8ce9a77
fix: renaming
exu Feb 11, 2022
6d63b0c
fix: renaming
exu Feb 11, 2022
63841cc
fix: renaming in postman tests
exu Feb 12, 2022
0490e3d
feat: new testkube logos
exu Feb 14, 2022
0eef0e7
feat: new squishies image
exu Feb 14, 2022
0d609dc
fix: nil pointer panic
exu Feb 16, 2022
139a223
fix: kubectl plugin test suite fix
exu Feb 16, 2022
924ff97
feat: pass directory parameter for temp dir ror file
vsukhin Feb 16, 2022
2607af3
fix: update unit test
vsukhin Feb 16, 2022
5a8ec80
feat: create data volume for job
vsukhin Feb 16, 2022
0e40b56
feat: pass init image to job
vsukhin Feb 16, 2022
2926f95
fix: fix unit test
vsukhin Feb 16, 2022
fdeef15
fix: fixed sanity suite
exu Feb 17, 2022
74dee04
Update cmd/kubectl-testkube/commands/tests/update.go
exu Feb 17, 2022
7f0e078
Update cmd/kubectl-testkube/commands/testsuites/common.go
exu Feb 17, 2022
6168649
Update cmd/kubectl-testkube/commands/testsuites/create.go
exu Feb 17, 2022
00b8673
Update cmd/kubectl-testkube/commands/testsuites/delete.go
exu Feb 17, 2022
ef52ae4
Update cmd/kubectl-testkube/commands/testsuites/deleteall.go
exu Feb 17, 2022
5bece8a
Update cmd/kubectl-testkube/commands/testsuites/common.go
exu Feb 17, 2022
9c4027b
Update cmd/kubectl-testkube/commands/testsuites/common.go
exu Feb 17, 2022
ae290f2
Update cmd/kubectl-testkube/commands/testsuites/common.go
exu Feb 17, 2022
92aea9d
Update cmd/kubectl-testkube/commands/testsuites/delete.go
exu Feb 17, 2022
fa6f0b1
Update cmd/kubectl-testkube/commands/testsuites/deleteall.go
exu Feb 17, 2022
1daeb8a
Update cmd/kubectl-testkube/commands/testsuites/delete.go
exu Feb 17, 2022
ac7db09
fix: renamed test suites commands
exu Feb 17, 2022
197b8cc
Update cmd/kubectl-testkube/commands/testsuites/executions.go
exu Feb 17, 2022
48a5ebb
Update cmd/kubectl-testkube/commands/testsuites/executions.go
exu Feb 17, 2022
ec67ac2
Update cmd/kubectl-testkube/commands/testsuites/get.go
exu Feb 17, 2022
b65dbf1
Update cmd/kubectl-testkube/commands/testsuites/get.go
exu Feb 17, 2022
7ac08eb
Update cmd/kubectl-testkube/commands/testsuites/executions.go
exu Feb 17, 2022
2794837
Update docs/tests-creating.md
exu Feb 17, 2022
e93dfe9
Update docs/tests-creating.md
exu Feb 17, 2022
6a6e792
Update docs/cli/testkube_testsuites_create.md
exu Feb 17, 2022
f96ae25
Update cmd/kubectl-testkube/commands/testsuites/list.go
exu Feb 17, 2022
0d718c4
Update cmd/kubectl-testkube/commands/testsuites/list.go
exu Feb 17, 2022
0d6ace1
Update cmd/kubectl-testkube/commands/testsuites/update.go
exu Feb 17, 2022
0210096
Update docs/cli/testkube_testsuites_watch.md
exu Feb 17, 2022
33740ce
Update cmd/kubectl-testkube/commands/testsuites/list.go
exu Feb 17, 2022
faedb51
Update cmd/kubectl-testkube/commands/testsuites/run.go
exu Feb 17, 2022
37c0b1d
Update docs/cli/testkube_testsuites_watch.md
exu Feb 17, 2022
397919e
fix: more rename leftovers
exu Feb 17, 2022
2bc6438
fix: more rename leftovers
exu Feb 17, 2022
7dbc727
fix: more rename leftovers
exu Feb 17, 2022
1e4d26b
fix: more rename leftovers
exu Feb 17, 2022
4957f5b
fix: more rename leftovers
exu Feb 17, 2022
b033dce
fix: more rename leftovers + get back to namespace
exu Feb 17, 2022
e284be7
Merge remote-tracking branch 'origin/jacek/fix/nameing-refactor' into…
vsukhin Feb 17, 2022
d47d09c
Merge remote-tracking branch 'origin/main' into vsukhin/feature/suppo…
vsukhin Feb 18, 2022
3535779
fix: return init image uri
vsukhin Feb 18, 2022
6e74a9f
Merge remote-tracking branch 'origin/main' into vsukhin/feature/suppo…
vsukhin Feb 18, 2022
b2d04ab
fix: locate init image by name
vsukhin Feb 21, 2022
a0ed32d
Merge remote-tracking branch 'origin/main' into vsukhin/feature/suppo…
vsukhin Feb 21, 2022
7dd5d48
Merge remote-tracking branch 'origin/main' into vsukhin/feature/suppo…
vsukhin Feb 22, 2022
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
4 changes: 2 additions & 2 deletions cmd/tools/commands/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewReleaseCmd() *cobra.Command {
pushVersionTag(nextAppVersion)

// Let's checkout helm chart repo and put changes to particular app
dir, err := git.PartialCheckout("https://github.com/kubeshop/helm-charts.git", appName, "main")
dir, err := git.PartialCheckout("https://github.com/kubeshop/helm-charts.git", appName, "main", "")
ui.ExitOnError("checking out "+appName+" chart to "+dir, err)

chart, path, err := helm.GetChart(dir)
Expand All @@ -52,7 +52,7 @@ func NewReleaseCmd() *cobra.Command {
gitAddCommitAndPush(dir, "updating "+appName+" chart version to "+nextAppVersion)

// Checkout main testkube chart and bump main chart with next version
dir, err = git.PartialCheckout("https://github.com/kubeshop/helm-charts.git", "testkube", "main")
dir, err = git.PartialCheckout("https://github.com/kubeshop/helm-charts.git", "testkube", "main", "")
ui.ExitOnError("checking out testkube chart to "+dir, err)

chart, path, err = helm.GetChart(dir)
Expand Down
35 changes: 20 additions & 15 deletions internal/app/api/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ func NewServer(
var httpConfig server.Config
envconfig.Process("APISERVER", &httpConfig)

executor, err := client.NewJobExecutor(executionsResults)
if err != nil {
panic(err)
}

s := TestkubeAPI{
HTTPServer: server.NewServer(httpConfig),
TestExecutionResults: testExecutionsResults,
ExecutionResults: executionsResults,
Executor: executor,
TestsClient: testsClient,
ExecutorsClient: executorsClient,
SecretClient: secretClient,
TestsSuitesClient: testsuitesClient,
Metrics: NewMetrics(),
}

if err = s.loadDefaultExecutors(os.Getenv("TESTKUBE_NAMESPACE"), os.Getenv("TESTKUBE_DEFAULT_EXECUTORS")); err != nil {
initImage, err := s.loadDefaultExecutors(os.Getenv("TESTKUBE_NAMESPACE"), os.Getenv("TESTKUBE_DEFAULT_EXECUTORS"))
if err != nil {
s.Log.Warnf("load default executors %w", err)
}

s.Executor, err = client.NewJobExecutor(executionsResults, initImage)
if err != nil {
panic(err)
}

s.Init()
return s
}
Expand Down Expand Up @@ -239,27 +239,32 @@ func getFilterFromRequest(c *fiber.Ctx) result.Filter {
}

// loadDefaultExecutors loads default executors
func (s TestkubeAPI) loadDefaultExecutors(namespace, data string) error {
func (s TestkubeAPI) loadDefaultExecutors(namespace, data string) (initImage string, err error) {
var executors []testkube.ExecutorDetails

if data == "" {
return nil
return "", nil
}

dataDecoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return err
return "", err
}

if err := json.Unmarshal([]byte(dataDecoded), &executors); err != nil {
return err
return "", err
}

for _, executor := range executors {
if executor.Executor == nil {
continue
}

if executor.Name == "executor-init" {
initImage = executor.Executor.Image
continue
}

obj := &executorv1.Executor{
ObjectMeta: metav1.ObjectMeta{
Name: executor.Name,
Expand All @@ -274,20 +279,20 @@ func (s TestkubeAPI) loadDefaultExecutors(namespace, data string) error {

result, err := s.ExecutorsClient.Get(namespace, executor.Name)
if err != nil && !errors.IsNotFound(err) {
return err
return "", err
}

if err != nil {
if _, err = s.ExecutorsClient.Create(obj); err != nil {
return err
return "", err
}
} else {
result.Spec = obj.Spec
if _, err = s.ExecutorsClient.Update(result); err != nil {
return err
return "", err
}
}
}

return nil
return initImage, nil
}
4 changes: 2 additions & 2 deletions pkg/executor/client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"go.uber.org/zap"
)

func NewJobExecutor(repo result.Repository) (client JobExecutor, err error) {
jobClient, err := jobs.NewJobClient()
func NewJobExecutor(repo result.Repository, initImage string) (client JobExecutor, err error) {
jobClient, err := jobs.NewJobClient(initImage)
if err != nil {
return client, fmt.Errorf("can't get k8s jobs client: %w", err)
}
Expand Down
22 changes: 16 additions & 6 deletions pkg/executor/content/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/git"
)

func NewFetcher() Fetcher {
return Fetcher{}
func NewFetcher(dir string) Fetcher {
return Fetcher{
dir: dir,
}
}

type Fetcher struct {
dir string
}

func (f Fetcher) Fetch(content *testkube.TestContent) (path string, err error) {
Expand Down Expand Up @@ -63,10 +67,10 @@ func (f Fetcher) FetchGitDir(repo *testkube.Repository) (path string, err error)

// if path not set make full repo checkout
if repo.Path == "" {
return git.Checkout(uri, repo.Branch)
return git.Checkout(uri, repo.Branch, f.dir)
}

return git.PartialCheckout(uri, repo.Path, repo.Branch)
return git.PartialCheckout(uri, repo.Path, repo.Branch, f.dir)
}

// FetchGitFile returns path to git based file saved in local temp directory
Expand All @@ -76,7 +80,7 @@ func (f Fetcher) FetchGitFile(repo *testkube.Repository) (path string, err error
return path, err
}

repoPath, err := git.Checkout(uri, repo.Branch)
repoPath, err := git.Checkout(uri, repo.Branch, f.dir)
if err != nil {
return path, err
}
Expand All @@ -101,7 +105,13 @@ func (f Fetcher) gitURI(repo *testkube.Repository) (uri string, err error) {
}

func (f Fetcher) saveTempFile(reader io.Reader) (path string, err error) {
tmpFile, err := ioutil.TempFile("", "fetcher-save-temp-file")
var tmpFile *os.File
filename := "fetcher-save-temp-file"
if f.dir == "" {
tmpFile, err = ioutil.TempFile("", filename)
} else {
tmpFile, err = os.Create(filepath.Join(f.dir, filename))
}
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/content/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const fileContent = `{"some":"json","file":"with content"}
`

func TestFetcher(t *testing.T) {
f := NewFetcher()
f := NewFetcher("")

t.Run("test fetch uri", func(t *testing.T) {

Expand Down
26 changes: 15 additions & 11 deletions pkg/git/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
)

// Checkout will checkout directory from Git repository
func Checkout(uri, branch string) (outputDir string, err error) {

tmpDir, err := ioutil.TempDir("", "git-checkout")
if err != nil {
return tmpDir, err
func Checkout(uri, branch, dir string) (outputDir string, err error) {
tmpDir := dir
if tmpDir == "" {
tmpDir, err = ioutil.TempDir("", "git-checkout")
if err != nil {
return "", err
}
}

_, err = process.ExecuteInDir(
Expand All @@ -32,12 +34,14 @@ func Checkout(uri, branch string) (outputDir string, err error) {
return tmpDir + "/repo/", nil
}

// Partial checkout will checkout only given directory from Git repository
func PartialCheckout(uri, path, branch string) (outputDir string, err error) {

tmpDir, err := ioutil.TempDir("", "git-sparse-checkout")
if err != nil {
return tmpDir, err
// PartialCheckout will checkout only given directory from Git repository
func PartialCheckout(uri, path, branch, dir string) (outputDir string, err error) {
tmpDir := dir
if tmpDir == "" {
tmpDir, err = ioutil.TempDir("", "git-sparse-checkout")
if err != nil {
return "", err
}
}

_, err = process.ExecuteInDir(
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/checkout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestCheckOut(t *testing.T) {

repo := "https://github.com/cirosantilli/test-git-partial-clone-big-small"

dir, err := PartialCheckout(repo, "small", "main")
dir, err := PartialCheckout(repo, "small", "main", "")
t.Logf("partial repo checkedout to dir: %s", dir)
assert.NoError(t, err)
t.Fail()
Expand Down
48 changes: 44 additions & 4 deletions pkg/jobs/jobclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (

pollTimeout = 24 * time.Hour
pollInterval = 200 * time.Millisecond

volumeName = "data-volume"
volumeDir = "/data"
)

type JobClient struct {
Expand All @@ -46,9 +49,10 @@ type JobClient struct {
Namespace string
Cmd string
Log *zap.SugaredLogger
initImage string
}

func NewJobClient() (*JobClient, error) {
func NewJobClient(initImage string) (*JobClient, error) {
clientSet, err := k8sclient.ConnectToK8s()
if err != nil {
return nil, err
Expand All @@ -58,6 +62,7 @@ func NewJobClient() (*JobClient, error) {
ClientSet: clientSet,
Namespace: "testkube",
Log: log.DefaultLogger,
initImage: initImage,
}, nil
}

Expand All @@ -77,7 +82,7 @@ func (c *JobClient) LaunchK8sJobSync(image string, repo result.Repository, execu
return result.Err(err), err
}

jobSpec := NewJobSpec(execution.Id, c.Namespace, image, string(jsn), execution.TestName, hasSecrets)
jobSpec := NewJobSpec(execution.Id, c.Namespace, image, string(jsn), execution.TestName, c.initImage, hasSecrets)

_, err = jobs.Create(ctx, jobSpec, metav1.CreateOptions{})
if err != nil {
Expand Down Expand Up @@ -151,7 +156,7 @@ func (c *JobClient) LaunchK8sJob(image string, repo result.Repository, execution
return result.Err(err), err
}

jobSpec := NewJobSpec(execution.Id, c.Namespace, image, string(jsn), execution.TestName, hasSecrets)
jobSpec := NewJobSpec(execution.Id, c.Namespace, image, string(jsn), execution.TestName, c.initImage, hasSecrets)

_, err = jobs.Create(ctx, jobSpec, metav1.CreateOptions{})
if err != nil {
Expand Down Expand Up @@ -434,7 +439,7 @@ func (c *JobClient) CreatePersistentVolumeClaim(name string) error {
}

// NewJobSpec is a method to create new job spec
func NewJobSpec(id, namespace, image, jsn, testName string, hasSecrets bool) *batchv1.Job {
func NewJobSpec(id, namespace, image, jsn, testName, executorInitImage string, hasSecrets bool) *batchv1.Job {
var TTLSecondsAfterFinished int32 = 180
// TODO backOff need to be handled correctly by Logs and by Running job spec - currently we can get unexpected results
var backOffLimit int32 = 0
Expand Down Expand Up @@ -467,6 +472,25 @@ func NewJobSpec(id, namespace, image, jsn, testName string, hasSecrets bool) *ba
}
}

var initContainers []corev1.Container
if executorInitImage != "" {
initContainers = []corev1.Container{
{
Name: id + "-init",
Image: executorInitImage,
Command: []string{"/bin/runner", jsn},
ImagePullPolicy: corev1.PullAlways,
Env: append(envVars, secretEnvVars...),
VolumeMounts: []corev1.VolumeMount{
{
Name: volumeName,
MountPath: volumeDir,
},
},
},
}
}

return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: id,
Expand All @@ -476,13 +500,25 @@ func NewJobSpec(id, namespace, image, jsn, testName string, hasSecrets bool) *ba
TTLSecondsAfterFinished: &TTLSecondsAfterFinished,
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
InitContainers: initContainers,
Containers: []corev1.Container{
{
Name: id,
Image: image,
Command: []string{"/bin/runner", jsn},
ImagePullPolicy: corev1.PullAlways,
Env: append(envVars, secretEnvVars...),
VolumeMounts: []corev1.VolumeMount{
{
Name: volumeName,
MountPath: volumeDir,
},
},
},
},
Volumes: []corev1.Volume{
{
Name: volumeName,
},
},
RestartPolicy: corev1.RestartPolicyNever,
Expand Down Expand Up @@ -526,6 +562,10 @@ var envVars = []corev1.EnvVar{
Name: "RUNNER_SCRAPPERENABLED",
Value: os.Getenv("SCRAPPERENABLED"),
},
{
Name: "RUNNER_DATADIR",
Value: volumeDir,
},
}

// IsPodReady defines if pod is ready or failed for logs scrapping
Expand Down