-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add build context support #456
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
base: prod-staging
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // Copyright (c) 2025, Unikraft GmbH and The Unikraft CLI Authors. | ||
| // Licensed under the BSD-3-Clause License (the "License"). | ||
| // You may not use this file except in compliance with the License. | ||
|
|
||
| package builder | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/distribution/reference" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| // ParseContextNames parses --build-context values in the form name=value. | ||
| func ParseContextNames(values []string) (map[string]string, error) { | ||
| if len(values) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| result := make(map[string]string, len(values)) | ||
| for _, value := range values { | ||
| if value == "" { | ||
| continue | ||
| } | ||
|
|
||
| kv := strings.SplitN(value, "=", 2) | ||
| if len(kv) != 2 { | ||
| return nil, errors.Errorf( | ||
| "invalid context value: %s, expected key=value", | ||
| value, | ||
| ) | ||
| } | ||
|
|
||
| named, err := reference.ParseNormalizedNamed(kv[0]) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "invalid context name %s", kv[0]) | ||
| } | ||
|
|
||
| name := strings.TrimSuffix( | ||
| reference.FamiliarString(named), | ||
| ":latest", | ||
| ) | ||
|
|
||
| result[name] = kv[1] | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // Copyright (c) 2025, Unikraft GmbH and The Unikraft CLI Authors. | ||
| // Licensed under the BSD-3-Clause License (the "License"). | ||
| // You may not use this file except in compliance with the License. | ||
|
|
||
| package builder | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseContextNames(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| values []string | ||
| want map[string]string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "single context", | ||
| values: []string{"foo=./foo"}, | ||
| want: map[string]string{ | ||
| "foo": "./foo", | ||
| }, | ||
| }, | ||
| { | ||
| name: "multiple contexts", | ||
| values: []string{ | ||
| "foo=./foo", | ||
| "bar=../bar", | ||
| }, | ||
| want: map[string]string{ | ||
| "foo": "./foo", | ||
| "bar": "../bar", | ||
| }, | ||
| }, | ||
| { | ||
| name: "missing value", | ||
| values: []string{"foo"}, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty", | ||
| values: []string{""}, | ||
| want: map[string]string{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := ParseContextNames(tt.values) | ||
|
|
||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("ParseContextNames() error = %v, wantErr %v", err, tt.wantErr) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why no use of |
||
| } | ||
|
|
||
| if !tt.wantErr { | ||
| if len(got) != len(tt.want) { | ||
| t.Fatalf("got %v, want %v", got, tt.want) | ||
| } | ||
|
|
||
| for name, wantPath := range tt.want { | ||
| if got[name] != wantPath { | ||
| t.Errorf("context %q = %q, want %q", name, got[name], wantPath) | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buildkit also supports build-contexts that are local directories? Or git repositories? We shouldn't just support images.