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: add job build image capability #38

Merged
merged 3 commits into from
Nov 8, 2023
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
8 changes: 8 additions & 0 deletions pkg/build/buildkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func (b *BuildKit) Build(ctx context.Context, r *pb.BuildRequest, w io.Writer) (
case "BUILD_KIND_APP_BUILD_WITH_CONTAINER_IMAGE":
return b.buildFromContainerImage(ctx, c, r, ow)

case "BUILD_KIND_JOB_CREATE_WITH_CONTAINER_IMAGE":
return b.buildFromContainerImage(ctx, c, r, ow)

case "BUILD_KIND_APP_BUILD_WITH_CONTAINER_FILE":
return b.buildFromContainerFile(ctx, c, r, ow)

Expand Down Expand Up @@ -418,6 +421,11 @@ func callBuildKitBuild(ctx context.Context, c *client.Client, buildContextDir st
ID: "tsuru-app-envvars",
FilePath: filepath.Join(buildContextDir, "secrets", "envs.sh"),
})
} else if r.Job != nil {
secretSources = append(secretSources, secretsprovider.Source{
ID: "tsuru-job-envvars",
FilePath: filepath.Join(buildContextDir, "secrets", "envs.sh"),
})
}

secrets, err := secretsprovider.NewStore(secretSources)
Expand Down
24 changes: 24 additions & 0 deletions pkg/build/buildkit/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,30 @@ func TestBuildKit_Build_FromContainerImages(t *testing.T) {
},
}, appFiles)
})

t.Run("container image without Tsuru app files (tsuru.yaml, Procfile) + job image push", func(t *testing.T) {
req := &pb.BuildRequest{
Kind: pb.BuildKind_BUILD_KIND_JOB_CREATE_WITH_CONTAINER_IMAGE,
Job: &pb.TsuruJob{
Name: "my-job",
},
SourceImage: "nginx:1.22-alpine",
DestinationImages: []string{baseRegistry(t, "job-my-job", "")},
PushOptions: &pb.PushOptions{InsecureRegistry: registryHTTP},
}

appFiles, err := NewBuildKit(bc, BuildKitOptions{TempDir: t.TempDir()}).
Build(context.TODO(), req, os.Stdout)

require.NoError(t, err)
assert.Equal(t, &pb.TsuruConfig{
ImageConfig: &pb.ContainerImageConfig{
Entrypoint: []string{"/docker-entrypoint.sh"},
Cmd: []string{"nginx", "-g", "daemon off;"},
ExposedPorts: []string{"80/tcp"},
},
}, appFiles)
})
}

func TestBuildKit_Build_FromContainerFile(t *testing.T) {
Expand Down
337 changes: 220 additions & 117 deletions pkg/build/grpc_build_v1/build_service.pb.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions pkg/build/grpc_build_v1/build_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ message BuildRequest {

// PushOptions contains the options push the generated images.
PushOptions push_options = 10;

// Job is the Tsuru job which is being deployed, if any.
//
// NOTE: mandatory field when build kind starts with BUILD_KIND_JOB_.
TsuruJob job = 11;
}

enum BuildKind {
Expand All @@ -65,6 +70,8 @@ enum BuildKind {

BUILD_KIND_PLATFORM_WITH_CONTAINER_IMAGE = 5; // tsuru platform add/update ... -i registry.example.com/tsuru/python:latest
BUILD_KIND_PLATFORM_WITH_CONTAINER_FILE = 6; // tsuru platform add/update ... --dockerfile Dockerfile

BUILD_KIND_JOB_CREATE_WITH_CONTAINER_IMAGE = 7; // tsuru job create ... -i registry.example.com/tsuru/my-job:latest
}

message BuildResponse {
Expand All @@ -83,6 +90,13 @@ message TsuruApp {
map<string, string> env_vars = 3;
}

message TsuruJob {
// Name is the Tsuru job name.
string name = 1;
// EnvVars are the enviroment variables set on the job.
map<string, string> env_vars = 3;
}

message TsuruPlatform {
// Name is the Tsuru platform name.
string name = 1;
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/grpc_build_v1/build_service_grpc.pb.go

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

4 changes: 4 additions & 0 deletions pkg/build/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func validateBuildRequest(r *pb.BuildRequest) error {
return status.Error(codes.InvalidArgument, "app cannot be nil")
}

if strings.HasPrefix(kind, "BUILD_KIND_JOB_") && r.Job == nil {
return status.Error(codes.InvalidArgument, "job cannot be nil")
}

if strings.HasPrefix(kind, "BUILD_KIND_PLATFORM_") && r.Platform == nil {
return status.Error(codes.InvalidArgument, "platform cannot be nil")
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/build/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ func TestBuild(t *testing.T) {
},
},

"missing job, when kind is from job": {
req: &pb.BuildRequest{
SourceImage: "tsuru/scratch:latest",
DestinationImages: []string{"registry.example.com/tsuru/app-my-app:v1"},
Kind: pb.BuildKind_BUILD_KIND_JOB_CREATE_WITH_CONTAINER_IMAGE,
},
assert: func(t *testing.T, stream pb.Build_BuildClient, err error) {
require.NoError(t, err)
require.NotNil(t, stream)
_, _, err = readResponse(t, stream)
assert.EqualError(t, err, status.Error(codes.InvalidArgument, "job cannot be nil").Error())
},
},

"deploy from source code, empty source image": {
req: &pb.BuildRequest{
DestinationImages: []string{"registry.example.com/tsuru/app-my-app:v1"},
Expand Down
Loading