@@ -63,6 +63,36 @@ type Client interface {
|
||||
SupportsMultipleExports() bool
|
||||
}
|
||||
|
||||
// registryGetter is something that can return a list of [Registry].
|
||||
type registryGetter interface {
|
||||
GetRegistries() []Registry
|
||||
}
|
||||
|
||||
// clientF builds a Docker client. The order of registryGetters is significant.
|
||||
// We typically prefer credentials from args, then provider config, then the
|
||||
// host. Provide them to this function in order of increasing priority: host,
|
||||
// config, args.
|
||||
//
|
||||
// We ignore state because if its creds differ from those in args then they are
|
||||
// likely volatile and also likely expired.
|
||||
type clientF func(context.Context, *host, ...registryGetter) (Client, error)
|
||||
|
||||
// RealClientF builds a real Docker client with auth layered on top of the
|
||||
// host's latent credentials.
|
||||
func RealClientF(_ context.Context, host *host, getters ...registryGetter) (Client, error) {
|
||||
auths := []Registry{}
|
||||
for _, rg := range getters {
|
||||
auths = append(auths, rg.GetRegistries()...)
|
||||
}
|
||||
return wrap(host, auths...)
|
||||
}
|
||||
|
||||
func mockClientF(c Client) clientF {
|
||||
return func(context.Context, *host, ...registryGetter) (Client, error) {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Build encapsulates all of the user-provider build parameters and options.
|
||||
type Build interface {
|
||||
BuildOptions() controllerapi.BuildOptions
|
||||
|
||||
@@ -38,7 +38,7 @@ import (
|
||||
|
||||
provider "github.com/pulumi/pulumi-go-provider"
|
||||
"github.com/pulumi/pulumi-go-provider/infer"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/property"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
|
||||
)
|
||||
|
||||
@@ -61,7 +61,10 @@ var _imageExamples string
|
||||
var _migration string
|
||||
|
||||
// Image is a Docker image build using buildkit.
|
||||
type Image struct{}
|
||||
type Image struct {
|
||||
clientF clientF
|
||||
config *Config
|
||||
}
|
||||
|
||||
// Annotate provides a description of the Image resource.
|
||||
func (i *Image) Annotate(a infer.Annotator) {
|
||||
@@ -281,6 +284,11 @@ func (ia *ImageArgs) Annotate(a infer.Annotator) {
|
||||
a.SetDefault(&ia.Network, Default)
|
||||
}
|
||||
|
||||
// GetRegistries returns the image's registries, if any.
|
||||
func (ia ImageArgs) GetRegistries() []Registry {
|
||||
return ia.Registries
|
||||
}
|
||||
|
||||
// ImageState is serialized to the program's state file.
|
||||
type ImageState struct {
|
||||
ImageArgs
|
||||
@@ -329,38 +337,25 @@ func (is *ImageState) Annotate(a infer.Annotator) {
|
||||
|
||||
// client produces a CLI client scoped to this resource and layered on top of
|
||||
// any host-level credentials.
|
||||
func (i *Image) client(ctx context.Context, state ImageState, args ImageArgs) (Client, error) {
|
||||
cfg := infer.GetConfig[Config](ctx)
|
||||
|
||||
if cli, ok := ctx.Value(_mockClientKey).(Client); ok {
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
// We prefer auth from args, the provider, and state in that order. We
|
||||
// build a slice in reverse order because wrap() will overwrite earlier
|
||||
// entries with later ones.
|
||||
auths := []Registry{}
|
||||
auths = append(auths, cfg.Registries...)
|
||||
auths = append(auths, args.Registries...)
|
||||
|
||||
return wrap(cfg.host, auths...)
|
||||
func (i *Image) client(ctx context.Context, args ImageArgs) (Client, error) {
|
||||
return i.clientF(ctx, i.config.getHost(), i.config, args)
|
||||
}
|
||||
|
||||
// Check validates ImageArgs, sets defaults, and ensures our client is
|
||||
// authenticated.
|
||||
func (i *Image) Check(
|
||||
ctx context.Context,
|
||||
_ string,
|
||||
_ resource.PropertyMap,
|
||||
news resource.PropertyMap,
|
||||
) (ImageArgs, []provider.CheckFailure, error) {
|
||||
args, failures, err := infer.DefaultCheck[ImageArgs](ctx, news)
|
||||
req infer.CheckRequest,
|
||||
) (infer.CheckResponse[ImageArgs], error) {
|
||||
args, failures, err := infer.DefaultCheck[ImageArgs](ctx, req.NewInputs)
|
||||
if err != nil || len(failures) != 0 {
|
||||
return args, failures, err
|
||||
return infer.CheckResponse[ImageArgs]{Failures: failures, Inputs: args}, err
|
||||
}
|
||||
|
||||
// :(
|
||||
preview := news.ContainsUnknowns()
|
||||
// If the inputs aren't fully resolved we perform a weaker validation, for
|
||||
// example we might not be able to check the Dockerfile for syntactic
|
||||
// correctness.
|
||||
preview := property.New(req.NewInputs).HasComputed()
|
||||
|
||||
cfg := infer.GetConfig[Config](ctx)
|
||||
supportsMultipleExports := true
|
||||
@@ -376,7 +371,7 @@ func (i *Image) Check(
|
||||
}
|
||||
}
|
||||
|
||||
return args, failures, err
|
||||
return infer.CheckResponse[ImageArgs]{Failures: failures, Inputs: args}, err
|
||||
}
|
||||
|
||||
type checkFailure struct {
|
||||
@@ -683,12 +678,11 @@ func (ia *ImageArgs) validate(supportsMultipleExports, preview bool) (controller
|
||||
// Create builds an image using buildkit.
|
||||
func (i *Image) Create(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
input ImageArgs,
|
||||
preview bool,
|
||||
) (string, ImageState, error) {
|
||||
req infer.CreateRequest[ImageArgs],
|
||||
) (infer.CreateResponse[ImageState], error) {
|
||||
input := req.Inputs
|
||||
state := ImageState{ImageArgs: input}
|
||||
id := name
|
||||
id := req.Name
|
||||
|
||||
// Default our ref to one of our tags.
|
||||
for _, tag := range state.Tags {
|
||||
@@ -699,22 +693,31 @@ func (i *Image) Create(
|
||||
break
|
||||
}
|
||||
|
||||
cli, err := i.client(ctx, state, input)
|
||||
cli, err := i.client(ctx, input)
|
||||
if err != nil {
|
||||
return id, state, err
|
||||
return infer.CreateResponse[ImageState]{ID: id, Output: state}, err
|
||||
}
|
||||
|
||||
ok, err := cli.BuildKitEnabled()
|
||||
if err != nil {
|
||||
return id, state, fmt.Errorf("checking buildkit compatibility: %w", err)
|
||||
return infer.CreateResponse[ImageState]{
|
||||
ID: id,
|
||||
Output: state,
|
||||
}, fmt.Errorf("checking buildkit compatibility: %w", err)
|
||||
}
|
||||
if !ok {
|
||||
return id, state, errors.New("buildkit is not supported on this host")
|
||||
return infer.CreateResponse[ImageState]{
|
||||
ID: id,
|
||||
Output: state,
|
||||
}, errors.New("buildkit is not supported on this host")
|
||||
}
|
||||
|
||||
build, err := input.toBuild(ctx, cli.SupportsMultipleExports(), preview)
|
||||
build, err := input.toBuild(ctx, cli.SupportsMultipleExports(), req.DryRun)
|
||||
if err != nil {
|
||||
return id, state, fmt.Errorf("preparing: %w", err)
|
||||
return infer.CreateResponse[ImageState]{
|
||||
ID: id,
|
||||
Output: state,
|
||||
}, fmt.Errorf("preparing: %w", err)
|
||||
}
|
||||
|
||||
hash, err := hashBuildContext(
|
||||
@@ -723,21 +726,24 @@ func (i *Image) Create(
|
||||
input.Context.Named.Map(),
|
||||
)
|
||||
if err != nil {
|
||||
return id, state, fmt.Errorf("hashing build context: %w", err)
|
||||
return infer.CreateResponse[ImageState]{
|
||||
ID: id,
|
||||
Output: state,
|
||||
}, fmt.Errorf("hashing build context: %w", err)
|
||||
}
|
||||
state.ContextHash = hash
|
||||
|
||||
if preview && !input.shouldBuildOnPreview() {
|
||||
return id, state, nil
|
||||
if req.DryRun && !input.shouldBuildOnPreview() {
|
||||
return infer.CreateResponse[ImageState]{ID: id, Output: state}, nil
|
||||
}
|
||||
if preview && !input.buildable() {
|
||||
if req.DryRun && !input.buildable() {
|
||||
provider.GetLogger(ctx).Warning("Skipping preview build because some inputs are unknown.")
|
||||
return id, state, nil
|
||||
return infer.CreateResponse[ImageState]{ID: id, Output: state}, nil
|
||||
}
|
||||
|
||||
result, err := cli.Build(ctx, build)
|
||||
if err != nil {
|
||||
return id, state, err
|
||||
return infer.CreateResponse[ImageState]{ID: id, Output: state}, err
|
||||
}
|
||||
|
||||
if d, ok := result.ExporterResponse[exptypes.ExporterImageDigestKey]; ok {
|
||||
@@ -747,7 +753,7 @@ func (i *Image) Create(
|
||||
|
||||
if state.Digest == "" {
|
||||
// Can't construct a ref, nothing else to do.
|
||||
return id, state, nil
|
||||
return infer.CreateResponse[ImageState]{ID: id, Output: state}, nil
|
||||
}
|
||||
|
||||
// Take the first registry tag we find and add a digest to it. That becomes
|
||||
@@ -762,7 +768,7 @@ func (i *Image) Create(
|
||||
break
|
||||
}
|
||||
|
||||
return id, state, nil
|
||||
return infer.CreateResponse[ImageState]{ID: id, Output: state}, nil
|
||||
}
|
||||
|
||||
// Update builds a new image. Normally we create-replace resources, but for
|
||||
@@ -770,36 +776,41 @@ func (i *Image) Create(
|
||||
// updates and simply re-build the image without deleting anything.
|
||||
func (i *Image) Update(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
_ ImageState,
|
||||
input ImageArgs,
|
||||
preview bool,
|
||||
) (ImageState, error) {
|
||||
_, state, err := i.Create(ctx, name, input, preview)
|
||||
return state, err
|
||||
req infer.UpdateRequest[ImageArgs, ImageState],
|
||||
) (infer.UpdateResponse[ImageState], error) {
|
||||
resp, err := i.Create(ctx,
|
||||
infer.CreateRequest[ImageArgs]{Name: req.ID, Inputs: req.Inputs, DryRun: req.DryRun},
|
||||
)
|
||||
return infer.UpdateResponse[ImageState]{Output: resp.Output}, err
|
||||
}
|
||||
|
||||
// Read attempts to read manifests from an image's exports. An image without
|
||||
// exports will have no manifests.
|
||||
func (i *Image) Read(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
input ImageArgs,
|
||||
state ImageState,
|
||||
req infer.ReadRequest[ImageArgs, ImageState],
|
||||
) (
|
||||
string, // id
|
||||
ImageArgs, // normalized inputs
|
||||
ImageState, // normalized state
|
||||
infer.ReadResponse[ImageArgs, ImageState],
|
||||
error,
|
||||
) {
|
||||
cli, err := i.client(ctx, state, input)
|
||||
state, input := req.State, req.Inputs
|
||||
|
||||
cli, err := i.client(ctx, input)
|
||||
if err != nil {
|
||||
return name, input, state, err
|
||||
return infer.ReadResponse[ImageArgs, ImageState]{
|
||||
ID: req.ID,
|
||||
Inputs: input,
|
||||
State: state,
|
||||
}, err
|
||||
}
|
||||
|
||||
if !state.isExported() {
|
||||
// Nothing was pushed -- all done.
|
||||
return name, input, state, nil
|
||||
return infer.ReadResponse[ImageArgs, ImageState]{
|
||||
ID: req.ID,
|
||||
Inputs: input,
|
||||
State: state,
|
||||
}, nil
|
||||
}
|
||||
|
||||
tagsToKeep := []string{}
|
||||
@@ -835,29 +846,29 @@ func (i *Image) Read(
|
||||
// If we couldn't find the tags we expected then return an empty ID to
|
||||
// delete the resource.
|
||||
if len(input.Tags) > 0 && len(tagsToKeep) == 0 {
|
||||
return "", input, state, nil
|
||||
return infer.ReadResponse[ImageArgs, ImageState]{ID: "", Inputs: input, State: state}, nil
|
||||
}
|
||||
|
||||
state.Tags = tagsToKeep
|
||||
|
||||
return name, input, state, nil
|
||||
return infer.ReadResponse[ImageArgs, ImageState]{ID: req.ID, Inputs: input, State: state}, nil
|
||||
}
|
||||
|
||||
// Delete deletes an Image. If the Image was already deleted out-of-band it is
|
||||
// treated as a success.
|
||||
func (i *Image) Delete(
|
||||
ctx context.Context,
|
||||
_ string,
|
||||
state ImageState,
|
||||
) error {
|
||||
cli, err := i.client(ctx, state, state.ImageArgs)
|
||||
req infer.DeleteRequest[ImageState],
|
||||
) (infer.DeleteResponse, error) {
|
||||
state := req.State
|
||||
cli, err := i.client(ctx, state.ImageArgs)
|
||||
if err != nil {
|
||||
return err
|
||||
return infer.DeleteResponse{}, err
|
||||
}
|
||||
|
||||
if state.Digest == "" {
|
||||
// Nothing was exported. Just try to delete the local image.
|
||||
return cli.Delete(ctx, state.Ref)
|
||||
return infer.DeleteResponse{}, cli.Delete(ctx, state.Ref)
|
||||
}
|
||||
|
||||
digests := []string{}
|
||||
@@ -885,17 +896,17 @@ func (i *Image) Delete(
|
||||
multierr = errors.Join(multierr, err)
|
||||
}
|
||||
|
||||
return multierr
|
||||
return infer.DeleteResponse{}, multierr
|
||||
}
|
||||
|
||||
// Diff re-implements most of the default diff behavior, with the exception of
|
||||
// ignoring "password" changes on registry inputs.
|
||||
func (*Image) Diff(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
olds ImageState,
|
||||
news ImageArgs,
|
||||
req infer.DiffRequest[ImageArgs, ImageState],
|
||||
) (provider.DiffResponse, error) {
|
||||
olds, news := req.State, req.Inputs
|
||||
|
||||
diff := map[string]provider.PropertyDiff{}
|
||||
update := provider.PropertyDiff{Kind: provider.Update}
|
||||
|
||||
|
||||
@@ -35,9 +35,11 @@ import (
|
||||
"go.uber.org/mock/gomock"
|
||||
|
||||
provider "github.com/pulumi/pulumi-go-provider"
|
||||
"github.com/pulumi/pulumi-go-provider/infer"
|
||||
"github.com/pulumi/pulumi-go-provider/integration"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/mapper"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/property"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
|
||||
)
|
||||
|
||||
@@ -84,46 +86,44 @@ func TestImageLifecycle(t *testing.T) {
|
||||
},
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"push": resource.NewBoolProperty(false),
|
||||
"tags": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{
|
||||
resource.NewStringProperty("docker.io/pulumibot/buildkit-e2e"),
|
||||
resource.NewStringProperty("docker.io/pulumibot/buildkit-e2e:main"),
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"push": property.New(false),
|
||||
"tags": property.New(
|
||||
[]property.Value{
|
||||
property.New("docker.io/pulumibot/buildkit-e2e"),
|
||||
property.New("docker.io/pulumibot/buildkit-e2e:main"),
|
||||
},
|
||||
),
|
||||
"platforms": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{
|
||||
resource.NewStringProperty("linux/arm64"),
|
||||
resource.NewStringProperty("linux/amd64"),
|
||||
"platforms": property.New(
|
||||
[]property.Value{
|
||||
property.New("linux/arm64"),
|
||||
property.New("linux/amd64"),
|
||||
},
|
||||
),
|
||||
"context": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"location": resource.NewStringProperty("testdata/noop"),
|
||||
"context": property.New(map[string]property.Value{
|
||||
"location": property.New("testdata/noop"),
|
||||
}),
|
||||
"dockerfile": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"location": resource.NewStringProperty("testdata/noop/Dockerfile"),
|
||||
"dockerfile": property.New(map[string]property.Value{
|
||||
"location": property.New("testdata/noop/Dockerfile"),
|
||||
}),
|
||||
"exports": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{
|
||||
resource.NewObjectProperty(resource.PropertyMap{
|
||||
"raw": resource.NewStringProperty("type=registry"),
|
||||
"exports": property.New(
|
||||
[]property.Value{
|
||||
property.New(map[string]property.Value{
|
||||
"raw": property.New("type=registry"),
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
"registries": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{
|
||||
resource.NewObjectProperty(resource.PropertyMap{
|
||||
"address": resource.NewStringProperty("fakeaddress"),
|
||||
"username": resource.NewStringProperty("fakeuser"),
|
||||
"password": resource.MakeSecret(
|
||||
resource.NewStringProperty("password"),
|
||||
),
|
||||
"registries": property.New(
|
||||
[]property.Value{
|
||||
property.New(map[string]property.Value{
|
||||
"address": property.New("fakeaddress"),
|
||||
"username": property.New("fakeuser"),
|
||||
"password": property.New("password").WithSecret(true),
|
||||
}),
|
||||
},
|
||||
),
|
||||
},
|
||||
}),
|
||||
}
|
||||
},
|
||||
},
|
||||
@@ -132,20 +132,20 @@ func TestImageLifecycle(t *testing.T) {
|
||||
client: noClient,
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"push": resource.NewBoolProperty(false),
|
||||
"tags": resource.NewArrayProperty([]resource.PropertyValue{}),
|
||||
"context": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"location": resource.NewStringProperty("testdata/noop"),
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"push": property.New(false),
|
||||
"tags": property.New([]property.Value{}),
|
||||
"context": property.New(map[string]property.Value{
|
||||
"location": property.New("testdata/noop"),
|
||||
}),
|
||||
"exports": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{
|
||||
resource.NewObjectProperty(resource.PropertyMap{
|
||||
"raw": resource.NewStringProperty("type=registry"),
|
||||
"exports": property.New(
|
||||
[]property.Value{
|
||||
property.New(map[string]property.Value{
|
||||
"raw": property.New("type=registry"),
|
||||
}),
|
||||
},
|
||||
),
|
||||
},
|
||||
}),
|
||||
ExpectFailure: true,
|
||||
CheckFailures: []provider.CheckFailure{
|
||||
{
|
||||
@@ -161,19 +161,19 @@ func TestImageLifecycle(t *testing.T) {
|
||||
client: noClient,
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"push": resource.NewBoolProperty(false),
|
||||
"tags": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{resource.NewStringProperty("invalid-exports")},
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"push": property.New(false),
|
||||
"tags": property.New(
|
||||
[]property.Value{property.New("invalid-exports")},
|
||||
),
|
||||
"exports": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{
|
||||
resource.NewObjectProperty(resource.PropertyMap{
|
||||
"raw": resource.NewStringProperty("type="),
|
||||
"exports": property.New(
|
||||
[]property.Value{
|
||||
property.New(map[string]property.Value{
|
||||
"raw": property.New("type="),
|
||||
}),
|
||||
},
|
||||
),
|
||||
},
|
||||
}),
|
||||
ExpectFailure: true,
|
||||
CheckFailures: []provider.CheckFailure{{
|
||||
Property: "exports[0]",
|
||||
@@ -194,15 +194,15 @@ func TestImageLifecycle(t *testing.T) {
|
||||
},
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"push": resource.NewBoolProperty(false),
|
||||
"tags": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{resource.NewStringProperty("foo")},
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"push": property.New(false),
|
||||
"tags": property.New(
|
||||
[]property.Value{property.New("foo")},
|
||||
),
|
||||
"context": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"location": resource.NewStringProperty("testdata/noop"),
|
||||
"context": property.New(map[string]property.Value{
|
||||
"location": property.New("testdata/noop"),
|
||||
}),
|
||||
},
|
||||
}),
|
||||
ExpectFailure: true,
|
||||
}
|
||||
},
|
||||
@@ -221,15 +221,15 @@ func TestImageLifecycle(t *testing.T) {
|
||||
},
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"push": resource.NewBoolProperty(false),
|
||||
"tags": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{resource.NewStringProperty("foo")},
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"push": property.New(false),
|
||||
"tags": property.New(
|
||||
[]property.Value{property.New("foo")},
|
||||
),
|
||||
"context": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"location": resource.NewStringProperty("testdata/noop"),
|
||||
"context": property.New(map[string]property.Value{
|
||||
"location": property.New("testdata/noop"),
|
||||
}),
|
||||
},
|
||||
}),
|
||||
ExpectFailure: true,
|
||||
}
|
||||
},
|
||||
@@ -254,24 +254,24 @@ func TestImageLifecycle(t *testing.T) {
|
||||
},
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"push": resource.NewBoolProperty(false),
|
||||
"tags": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{
|
||||
resource.NewStringProperty("default-dockerfile"),
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"push": property.New(false),
|
||||
"tags": property.New(
|
||||
[]property.Value{
|
||||
property.New("default-dockerfile"),
|
||||
},
|
||||
),
|
||||
"context": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"location": resource.NewStringProperty("testdata/noop"),
|
||||
"context": property.New(map[string]property.Value{
|
||||
"location": property.New("testdata/noop"),
|
||||
}),
|
||||
},
|
||||
Hook: func(_, output resource.PropertyMap) {
|
||||
dockerfile := output["dockerfile"]
|
||||
}),
|
||||
Hook: func(_, output property.Map) {
|
||||
dockerfile := output.Get("dockerfile")
|
||||
require.NotNil(t, dockerfile)
|
||||
require.True(t, dockerfile.IsObject())
|
||||
location := dockerfile.ObjectValue()["location"]
|
||||
require.True(t, dockerfile.IsMap())
|
||||
location := dockerfile.AsMap().Get("location")
|
||||
require.True(t, location.IsString())
|
||||
assert.Equal(t, "testdata/noop/Dockerfile", location.StringValue())
|
||||
assert.Equal(t, "testdata/noop/Dockerfile", location.AsString())
|
||||
},
|
||||
}
|
||||
},
|
||||
@@ -296,25 +296,25 @@ func TestImageLifecycle(t *testing.T) {
|
||||
},
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"push": resource.NewBoolProperty(false),
|
||||
"tags": resource.NewArrayProperty(
|
||||
[]resource.PropertyValue{
|
||||
resource.NewStringProperty("inline-dockerfile"),
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"push": property.New(false),
|
||||
"tags": property.New(
|
||||
[]property.Value{
|
||||
property.New("inline-dockerfile"),
|
||||
},
|
||||
),
|
||||
"buildOnPreview": resource.NewBoolProperty(true),
|
||||
"dockerfile": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"inline": resource.NewStringProperty("FROM alpine:latest"),
|
||||
"buildOnPreview": property.New(true),
|
||||
"dockerfile": property.New(map[string]property.Value{
|
||||
"inline": property.New("FROM alpine:latest"),
|
||||
}),
|
||||
},
|
||||
Hook: func(_, output resource.PropertyMap) {
|
||||
context := output["context"]
|
||||
}),
|
||||
Hook: func(_, output property.Map) {
|
||||
context := output.Get("context")
|
||||
require.NotNil(t, context)
|
||||
require.True(t, context.IsObject())
|
||||
location := context.ObjectValue()["location"]
|
||||
require.True(t, context.IsMap())
|
||||
location := context.AsMap().Get("location")
|
||||
require.True(t, location.IsString())
|
||||
assert.Equal(t, ".", location.StringValue())
|
||||
assert.Equal(t, ".", location.AsString())
|
||||
},
|
||||
}
|
||||
},
|
||||
@@ -328,7 +328,7 @@ func TestImageLifecycle(t *testing.T) {
|
||||
Resource: "docker-build:index:Image",
|
||||
Create: tt.op(t),
|
||||
}
|
||||
s := newServer(tt.client(t))
|
||||
s := newServer(t.Context(), t, mockClientF(tt.client(t)))
|
||||
|
||||
err := s.Configure(provider.ConfigureRequest{})
|
||||
require.NoError(t, err)
|
||||
@@ -353,21 +353,16 @@ func TestDelete(t *testing.T) {
|
||||
Delete(gomock.Any(), "docker.io/pulumi/test@sha256:foo").
|
||||
Return(errNotFound{})
|
||||
|
||||
s := newServer(client)
|
||||
err := s.Configure(provider.ConfigureRequest{})
|
||||
require.NoError(t, err)
|
||||
i := &Image{clientF: mockClientF(client)}
|
||||
|
||||
err = s.Delete(provider.DeleteRequest{
|
||||
ID: "foo,bar",
|
||||
Urn: _fakeURN,
|
||||
Properties: resource.PropertyMap{
|
||||
"tags": resource.NewArrayProperty([]resource.PropertyValue{
|
||||
resource.NewStringProperty("docker.io/pulumi/test:foo"),
|
||||
}),
|
||||
"push": resource.NewBoolProperty(true),
|
||||
"digest": resource.NewStringProperty("sha256:foo"),
|
||||
"contextHash": resource.NewStringProperty(""),
|
||||
"ref": resource.NewStringProperty(""),
|
||||
_, err := i.Delete(t.Context(), infer.DeleteRequest[ImageState]{
|
||||
ID: "foo,bar",
|
||||
State: ImageState{
|
||||
ImageArgs: ImageArgs{
|
||||
Tags: []string{"docker.io/pulumi/test:foo"},
|
||||
Push: true,
|
||||
},
|
||||
Digest: "sha256:foo",
|
||||
},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
@@ -391,27 +386,21 @@ func TestRead(t *testing.T) {
|
||||
},
|
||||
}, nil)
|
||||
|
||||
s := newServer(client)
|
||||
err := s.Configure(provider.ConfigureRequest{})
|
||||
require.NoError(t, err)
|
||||
i := &Image{clientF: mockClientF(client)}
|
||||
|
||||
resp, err := s.Read(provider.ReadRequest{
|
||||
ID: "my-image",
|
||||
Urn: _fakeURN,
|
||||
Properties: resource.PropertyMap{
|
||||
"exports": resource.NewArrayProperty([]resource.PropertyValue{
|
||||
resource.NewObjectProperty(resource.PropertyMap{
|
||||
"raw": resource.NewStringProperty("type=registry"),
|
||||
}),
|
||||
}),
|
||||
"tags": resource.NewArrayProperty([]resource.PropertyValue{
|
||||
resource.NewStringProperty(tag),
|
||||
}),
|
||||
"digest": resource.NewStringProperty(digest),
|
||||
resp, err := i.Read(t.Context(), infer.ReadRequest[ImageArgs, ImageState]{
|
||||
ID: "my-image",
|
||||
State: ImageState{
|
||||
ImageArgs: ImageArgs{
|
||||
Exports: []Export{{Raw: "type=registry"}},
|
||||
Tags: []string{tag},
|
||||
},
|
||||
Digest: digest,
|
||||
},
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, resp.Properties["exports"].ArrayValue()[0].ObjectValue()["manifest"])
|
||||
assert.Equal(t, []string{tag}, resp.State.Tags)
|
||||
}
|
||||
|
||||
func TestImageDiff(t *testing.T) {
|
||||
@@ -432,21 +421,21 @@ func TestImageDiff(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
olds func(*testing.T, ImageState) ImageState
|
||||
news func(*testing.T, ImageArgs) ImageArgs
|
||||
name string
|
||||
state func(*testing.T, ImageState) ImageState
|
||||
inputs func(*testing.T, ImageArgs) ImageArgs
|
||||
|
||||
wantChanges bool
|
||||
}{
|
||||
{
|
||||
name: "no diff if build context is unchanged",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(*testing.T, ImageArgs) ImageArgs { return baseArgs },
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(*testing.T, ImageArgs) ImageArgs { return baseArgs },
|
||||
wantChanges: false,
|
||||
},
|
||||
{
|
||||
name: "no diff if registry password changes",
|
||||
olds: func(_ *testing.T, s ImageState) ImageState {
|
||||
state: func(_ *testing.T, s ImageState) ImageState {
|
||||
s.Registries = []Registry{{
|
||||
Address: "foo",
|
||||
Username: "foo",
|
||||
@@ -454,7 +443,7 @@ func TestImageDiff(t *testing.T) {
|
||||
}}
|
||||
return s
|
||||
},
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Registries = []Registry{{
|
||||
Address: "foo",
|
||||
Username: "foo",
|
||||
@@ -466,11 +455,11 @@ func TestImageDiff(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "no diff if pull=true but no exports",
|
||||
olds: func(_ *testing.T, is ImageState) ImageState {
|
||||
state: func(_ *testing.T, is ImageState) ImageState {
|
||||
is.Pull = true
|
||||
return is
|
||||
},
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
ia.Pull = true
|
||||
return ia
|
||||
},
|
||||
@@ -478,12 +467,12 @@ func TestImageDiff(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "diff if pull=true with exports",
|
||||
olds: func(_ *testing.T, is ImageState) ImageState {
|
||||
state: func(_ *testing.T, is ImageState) ImageState {
|
||||
is.Pull = true
|
||||
is.Load = true
|
||||
return is
|
||||
},
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
ia.Pull = true
|
||||
ia.Load = true
|
||||
return ia
|
||||
@@ -491,9 +480,9 @@ func TestImageDiff(t *testing.T) {
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if build context changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(t *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if build context changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(t *testing.T, a ImageArgs) ImageArgs {
|
||||
tmp := filepath.Join(a.Context.Location, "tmp")
|
||||
err := os.WriteFile(tmp, []byte{}, 0o600)
|
||||
require.NoError(t, err)
|
||||
@@ -503,9 +492,9 @@ func TestImageDiff(t *testing.T) {
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if registry added",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if registry added",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Registries = []Registry{{}}
|
||||
return a
|
||||
},
|
||||
@@ -513,7 +502,7 @@ func TestImageDiff(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "diff if registry user changes",
|
||||
olds: func(_ *testing.T, s ImageState) ImageState {
|
||||
state: func(_ *testing.T, s ImageState) ImageState {
|
||||
s.Registries = []Registry{{
|
||||
Address: "foo",
|
||||
Username: "foo",
|
||||
@@ -521,7 +510,7 @@ func TestImageDiff(t *testing.T) {
|
||||
}}
|
||||
return s
|
||||
},
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Registries = []Registry{{
|
||||
Address: "DIFFERENT USER",
|
||||
Username: "foo",
|
||||
@@ -532,9 +521,9 @@ func TestImageDiff(t *testing.T) {
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if buildArgs changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if buildArgs changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.BuildArgs = map[string]string{
|
||||
"foo": "bar",
|
||||
}
|
||||
@@ -543,36 +532,36 @@ func TestImageDiff(t *testing.T) {
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if pull changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
name: "diff if pull changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
ia.Pull = true
|
||||
return ia
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if load changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
name: "diff if load changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
ia.Load = true
|
||||
return ia
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if push changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
name: "diff if push changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
ia.Push = true
|
||||
return ia
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if buildOnPreview doesn't change",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
name: "diff if buildOnPreview doesn't change",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
val := true
|
||||
ia.BuildOnPreview = &val
|
||||
return ia
|
||||
@@ -580,9 +569,9 @@ func TestImageDiff(t *testing.T) {
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if buildOnPreview changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
name: "diff if buildOnPreview changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
val := false
|
||||
ia.BuildOnPreview = &val
|
||||
return ia
|
||||
@@ -590,171 +579,171 @@ func TestImageDiff(t *testing.T) {
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if ssh changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
name: "diff if ssh changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
ia.SSH = []SSH{{ID: "default"}}
|
||||
return ia
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if hosts change",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
name: "diff if hosts change",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(t *testing.T, ia ImageArgs) ImageArgs {
|
||||
ia.AddHosts = []string{"localhost"}
|
||||
return ia
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if cacheFrom changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if cacheFrom changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.CacheFrom = []CacheFrom{{Raw: "a"}}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if cacheTo changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if cacheTo changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.CacheTo = []CacheTo{{Raw: "a"}}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if context changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if context changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Context = &BuildContext{Context: Context{Location: "testdata/ignores"}}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if named context changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if named context changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Context = &BuildContext{Named: NamedContexts{"foo": Context{Location: "bar"}}}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if network changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if network changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Network = &host
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if dockerfile location changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if dockerfile location changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Dockerfile = &Dockerfile{Location: "testdata/ignores/basedir/Dockerfile"}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if dockerfile inline changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if dockerfile inline changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Dockerfile = &Dockerfile{Inline: "FROM scratch"}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if platforms change",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if platforms change",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Platforms = []Platform{"linux/amd64"}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if pull changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if pull changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Pull = true
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if builder changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if builder changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Builder = &BuilderConfig{Name: "foo"}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if tags change",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if tags change",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Tags = []string{"foo"}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if exports change",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if exports change",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Exports = []Export{{Raw: "foo"}}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if target changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if target changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Target = "foo"
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if pulling",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if pulling",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Pull = true
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if noCache changes",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if noCache changes",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.NoCache = true
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if labels change",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if labels change",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Labels = map[string]string{"foo": "bar"}
|
||||
return a
|
||||
},
|
||||
wantChanges: true,
|
||||
},
|
||||
{
|
||||
name: "diff if secrets change",
|
||||
olds: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
news: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
name: "diff if secrets change",
|
||||
state: func(*testing.T, ImageState) ImageState { return baseState },
|
||||
inputs: func(_ *testing.T, a ImageArgs) ImageArgs {
|
||||
a.Secrets = map[string]string{"foo": "bar"}
|
||||
return a
|
||||
},
|
||||
@@ -762,13 +751,13 @@ func TestImageDiff(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "diff if local export doesn't exist",
|
||||
olds: func(t *testing.T, state ImageState) ImageState {
|
||||
state: func(t *testing.T, state ImageState) ImageState {
|
||||
state.Exports = []Export{
|
||||
{Local: &ExportLocal{Dest: "not-real"}},
|
||||
}
|
||||
return state
|
||||
},
|
||||
news: func(_ *testing.T, args ImageArgs) ImageArgs {
|
||||
inputs: func(_ *testing.T, args ImageArgs) ImageArgs {
|
||||
args.Exports = []Export{
|
||||
{Local: &ExportLocal{Dest: "not-real"}},
|
||||
}
|
||||
@@ -778,13 +767,13 @@ func TestImageDiff(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "diff if tar export doesn't exist",
|
||||
olds: func(t *testing.T, state ImageState) ImageState {
|
||||
state: func(t *testing.T, state ImageState) ImageState {
|
||||
state.Exports = []Export{
|
||||
{Tar: &ExportTar{ExportLocal: ExportLocal{Dest: "not-real"}}},
|
||||
}
|
||||
return state
|
||||
},
|
||||
news: func(_ *testing.T, args ImageArgs) ImageArgs {
|
||||
inputs: func(_ *testing.T, args ImageArgs) ImageArgs {
|
||||
args.Exports = []Export{
|
||||
{Tar: &ExportTar{ExportLocal: ExportLocal{Dest: "not-real"}}},
|
||||
}
|
||||
@@ -794,12 +783,12 @@ func TestImageDiff(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
s := newServer(nil)
|
||||
s := newServer(t.Context(), t, nil)
|
||||
|
||||
encode := func(t *testing.T, x any) resource.PropertyMap {
|
||||
encode := func(t *testing.T, x any) property.Map {
|
||||
raw, err := mapper.New(&mapper.Opts{IgnoreMissing: true}).Encode(x)
|
||||
require.NoError(t, err)
|
||||
return resource.NewPropertyMapFromMap(raw)
|
||||
return resource.FromResourcePropertyMap(resource.NewPropertyMapFromMap(raw))
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -808,9 +797,9 @@ func TestImageDiff(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
resp, err := s.Diff(provider.DiffRequest{
|
||||
Urn: _fakeURN,
|
||||
Olds: encode(t, tt.olds(t, baseState)),
|
||||
News: encode(t, tt.news(t, baseArgs)),
|
||||
Urn: _fakeURN,
|
||||
State: encode(t, tt.state(t, baseState)),
|
||||
Inputs: encode(t, tt.inputs(t, baseArgs)),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.wantChanges, resp.HasChanges, resp.DetailedDiff)
|
||||
|
||||
@@ -28,7 +28,6 @@ import (
|
||||
|
||||
provider "github.com/pulumi/pulumi-go-provider"
|
||||
"github.com/pulumi/pulumi-go-provider/infer"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -47,7 +46,10 @@ var (
|
||||
var _indexExamples string
|
||||
|
||||
// Index is an OCI index or manifest list on a remote registry.
|
||||
type Index struct{}
|
||||
type Index struct {
|
||||
clientF clientF
|
||||
config *Config
|
||||
}
|
||||
|
||||
// IndexArgs instantiate an Index.
|
||||
type IndexArgs struct {
|
||||
@@ -64,6 +66,14 @@ func (i IndexArgs) isPushed() bool {
|
||||
return *i.Push
|
||||
}
|
||||
|
||||
// GetRegistries returns the index's registry.
|
||||
func (i IndexArgs) GetRegistries() []Registry {
|
||||
if i.Registry == nil {
|
||||
return nil
|
||||
}
|
||||
return []Registry{*i.Registry}
|
||||
}
|
||||
|
||||
// IndexState captures the state of an Index.
|
||||
type IndexState struct {
|
||||
IndexArgs
|
||||
@@ -132,66 +142,82 @@ func (i *IndexState) Annotate(a infer.Annotator) {
|
||||
// Create is a passthrough to Update.
|
||||
func (i *Index) Create(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
input IndexArgs,
|
||||
preview bool,
|
||||
) (string, IndexState, error) {
|
||||
state, err := i.Update(ctx, name, IndexState{}, input, preview)
|
||||
return name, state, err
|
||||
req infer.CreateRequest[IndexArgs],
|
||||
) (infer.CreateResponse[IndexState], error) {
|
||||
resp, err := i.Update(ctx,
|
||||
infer.UpdateRequest[IndexArgs, IndexState]{
|
||||
ID: req.Name,
|
||||
State: IndexState{},
|
||||
Inputs: req.Inputs,
|
||||
DryRun: req.DryRun,
|
||||
},
|
||||
)
|
||||
return infer.CreateResponse[IndexState]{ID: req.Name, Output: resp.Output}, err
|
||||
}
|
||||
|
||||
// Update performs `buildx imagetools create` to create a new OCI index /
|
||||
// manifest list.
|
||||
func (i *Index) Update(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
state IndexState,
|
||||
input IndexArgs,
|
||||
preview bool,
|
||||
) (IndexState, error) {
|
||||
req infer.UpdateRequest[IndexArgs, IndexState],
|
||||
) (infer.UpdateResponse[IndexState], error) {
|
||||
state, input := req.State, req.Inputs
|
||||
|
||||
state.IndexArgs = input
|
||||
state.Ref = input.Tag
|
||||
|
||||
cli, err := i.client(ctx, state, input)
|
||||
cli, err := i.client(ctx, input)
|
||||
if err != nil {
|
||||
return state, err
|
||||
return infer.UpdateResponse[IndexState]{Output: state}, err
|
||||
}
|
||||
|
||||
if preview {
|
||||
return state, nil
|
||||
if req.DryRun {
|
||||
return infer.UpdateResponse[IndexState]{Output: state}, nil
|
||||
}
|
||||
|
||||
provider.GetLogger(ctx).Debugf("creating index with tag %s and sources %s", input.Tag, input.Sources)
|
||||
provider.GetLogger(ctx).
|
||||
Debugf("creating index with tag %s and sources %s", input.Tag, input.Sources)
|
||||
|
||||
err = cli.ManifestCreate(ctx, input.isPushed(), input.Tag, input.Sources...)
|
||||
if err != nil {
|
||||
return state, fmt.Errorf("creating: %w", err)
|
||||
return infer.UpdateResponse[IndexState]{Output: state}, fmt.Errorf("creating: %w", err)
|
||||
}
|
||||
|
||||
_, _, state, err = i.Read(ctx, name, input, state)
|
||||
// Read remote manifest information, if it exists.
|
||||
live, err := i.Read(ctx,
|
||||
infer.ReadRequest[IndexArgs, IndexState]{ID: req.ID, Inputs: input, State: state},
|
||||
)
|
||||
if err != nil {
|
||||
return state, fmt.Errorf("reading: %w", err)
|
||||
return infer.UpdateResponse[IndexState]{Output: state}, fmt.Errorf("reading: %w", err)
|
||||
}
|
||||
return state, nil
|
||||
return infer.UpdateResponse[IndexState]{Output: live.State}, nil
|
||||
}
|
||||
|
||||
func (i *Index) Read(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
input IndexArgs,
|
||||
state IndexState,
|
||||
) (string, IndexArgs, IndexState, error) {
|
||||
req infer.ReadRequest[IndexArgs, IndexState],
|
||||
) (infer.ReadResponse[IndexArgs, IndexState], error) {
|
||||
state, input := req.State, req.Inputs
|
||||
|
||||
state.IndexArgs = input
|
||||
state.Ref = input.Tag
|
||||
|
||||
if !input.isPushed() {
|
||||
provider.GetLogger(ctx).Debug("skipping read because index was not pushed")
|
||||
return name, input, state, nil // Nothing to read.
|
||||
return infer.ReadResponse[IndexArgs, IndexState]{
|
||||
ID: req.ID,
|
||||
Inputs: input,
|
||||
State: state,
|
||||
}, nil // Nothing to read.
|
||||
}
|
||||
|
||||
cli, err := i.client(ctx, state, input)
|
||||
cli, err := i.client(ctx, input)
|
||||
if err != nil {
|
||||
return name, input, state, err
|
||||
return infer.ReadResponse[IndexArgs, IndexState]{
|
||||
ID: req.ID,
|
||||
Inputs: input,
|
||||
State: state,
|
||||
}, err
|
||||
}
|
||||
|
||||
provider.GetLogger(ctx).Debug("reading index with tag " + input.Tag)
|
||||
@@ -199,21 +225,29 @@ func (i *Index) Read(
|
||||
digest, err := cli.ManifestInspect(ctx, input.Tag)
|
||||
if errors.Is(err, errs.ErrNotFound) {
|
||||
// A remote tag was expected but isn't there -- delete the resource.
|
||||
return "", input, state, nil
|
||||
return infer.ReadResponse[IndexArgs, IndexState]{ID: "", Inputs: input, State: state}, nil
|
||||
}
|
||||
if errors.Is(err, errs.ErrHTTPUnauthorized) {
|
||||
provider.GetLogger(ctx).Warning("invalid credentials, skipping")
|
||||
return name, input, state, nil
|
||||
return infer.ReadResponse[IndexArgs, IndexState]{
|
||||
ID: req.ID,
|
||||
Inputs: input,
|
||||
State: state,
|
||||
}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return name, input, state, err
|
||||
return infer.ReadResponse[IndexArgs, IndexState]{
|
||||
ID: req.ID,
|
||||
Inputs: input,
|
||||
State: state,
|
||||
}, err
|
||||
}
|
||||
|
||||
if ref, ok := addDigest(input.Tag, digest); ok {
|
||||
state.Ref = ref
|
||||
}
|
||||
|
||||
return name, input, state, nil
|
||||
return infer.ReadResponse[IndexArgs, IndexState]{ID: req.ID, Inputs: input, State: state}, nil
|
||||
}
|
||||
|
||||
// Check confirms the Index's tag and source refs are all valid. This doesn't
|
||||
@@ -222,13 +256,11 @@ func (i *Index) Read(
|
||||
// cases for now.
|
||||
func (i *Index) Check(
|
||||
ctx context.Context,
|
||||
_ string,
|
||||
_ resource.PropertyMap,
|
||||
news resource.PropertyMap,
|
||||
) (IndexArgs, []provider.CheckFailure, error) {
|
||||
args, failures, err := infer.DefaultCheck[IndexArgs](ctx, news)
|
||||
req infer.CheckRequest,
|
||||
) (infer.CheckResponse[IndexArgs], error) {
|
||||
args, failures, err := infer.DefaultCheck[IndexArgs](ctx, req.NewInputs)
|
||||
if err != nil {
|
||||
return args, failures, err
|
||||
return infer.CheckResponse[IndexArgs]{Failures: failures, Inputs: args}, err
|
||||
}
|
||||
|
||||
if _, err := normalizeReference(args.Tag); args.Tag != "" && err != nil {
|
||||
@@ -253,27 +285,31 @@ func (i *Index) Check(
|
||||
}
|
||||
}
|
||||
|
||||
return args, failures, nil
|
||||
return infer.CheckResponse[IndexArgs]{Failures: failures, Inputs: args}, nil
|
||||
}
|
||||
|
||||
// Delete attempts to delete the remote manifest.
|
||||
func (i *Index) Delete(ctx context.Context, _ string, state IndexState) error {
|
||||
func (i *Index) Delete(
|
||||
ctx context.Context,
|
||||
req infer.DeleteRequest[IndexState],
|
||||
) (infer.DeleteResponse, error) {
|
||||
state := req.State
|
||||
if !state.isPushed() {
|
||||
return nil // Nothing to delete.
|
||||
return infer.DeleteResponse{}, nil // Nothing to delete.
|
||||
}
|
||||
|
||||
cli, err := i.client(ctx, state, state.IndexArgs)
|
||||
cli, err := i.client(ctx, state.IndexArgs)
|
||||
if err != nil {
|
||||
return err
|
||||
return infer.DeleteResponse{}, err
|
||||
}
|
||||
|
||||
err = cli.ManifestDelete(ctx, state.Ref)
|
||||
// TODO: Upstream buildx swallows the error types we'd like to test for
|
||||
// here.
|
||||
if err != nil && strings.Contains(err.Error(), "No such manifest:") {
|
||||
return nil
|
||||
return infer.DeleteResponse{}, nil
|
||||
}
|
||||
return err
|
||||
return infer.DeleteResponse{}, err
|
||||
}
|
||||
|
||||
// Diff returns a diff of proposed changes against current state. Ideally we
|
||||
@@ -282,10 +318,10 @@ func (i *Index) Delete(ctx context.Context, _ string, state IndexState) error {
|
||||
// change all the time due to short-lived AWS credentials).
|
||||
func (i *Index) Diff(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
olds IndexState,
|
||||
news IndexArgs,
|
||||
req infer.DiffRequest[IndexArgs, IndexState],
|
||||
) (provider.DiffResponse, error) {
|
||||
olds, news := req.State, req.Inputs
|
||||
|
||||
diff := map[string]provider.PropertyDiff{}
|
||||
update := provider.PropertyDiff{Kind: provider.Update}
|
||||
replace := provider.PropertyDiff{Kind: provider.UpdateReplace}
|
||||
@@ -323,23 +359,7 @@ func (i *Index) Diff(
|
||||
// any host-level credentials.
|
||||
func (i *Index) client(
|
||||
ctx context.Context,
|
||||
_ IndexState,
|
||||
args IndexArgs,
|
||||
) (Client, error) {
|
||||
cfg := infer.GetConfig[Config](ctx)
|
||||
|
||||
if cli, ok := ctx.Value(_mockClientKey).(Client); ok {
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
// We prefer auth from args, the provider, and state in that order. We
|
||||
// build a slice in reverse order because wrap() will overwrite earlier
|
||||
// entries with later ones.
|
||||
auths := []Registry{}
|
||||
auths = append(auths, cfg.Registries...)
|
||||
if args.Registry != nil {
|
||||
auths = append(auths, *args.Registry)
|
||||
}
|
||||
|
||||
return wrap(cfg.host, auths...)
|
||||
return i.clientF(ctx, i.config.getHost(), i.config, args)
|
||||
}
|
||||
|
||||
@@ -27,34 +27,35 @@ import (
|
||||
"github.com/pulumi/pulumi-go-provider/integration"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/mapper"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/property"
|
||||
)
|
||||
|
||||
func TestIndexLifecycle(t *testing.T) {
|
||||
t.Parallel()
|
||||
realClient := func(t *testing.T) Client { return nil }
|
||||
realClient := func(t *testing.T) clientF { return RealClientF }
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
skip bool
|
||||
|
||||
op func(t *testing.T) integration.Operation
|
||||
client func(t *testing.T) Client
|
||||
client func(t *testing.T) clientF
|
||||
}{
|
||||
{
|
||||
name: "not pushed",
|
||||
client: realClient,
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"tag": resource.NewStringProperty(
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"tag": property.New(
|
||||
"docker.io/pulumibot/buildkit-e2e:manifest-unit",
|
||||
),
|
||||
"sources": resource.NewArrayProperty([]resource.PropertyValue{
|
||||
resource.NewStringProperty("docker.io/pulumibot/buildkit-e2e:arm64"),
|
||||
resource.NewStringProperty("docker.io/pulumibot/buildkit-e2e:amd64"),
|
||||
"sources": property.New([]property.Value{
|
||||
property.New("docker.io/pulumibot/buildkit-e2e:arm64"),
|
||||
property.New("docker.io/pulumibot/buildkit-e2e:amd64"),
|
||||
}),
|
||||
"push": resource.NewBoolProperty(false),
|
||||
},
|
||||
"push": property.New(false),
|
||||
}),
|
||||
}
|
||||
},
|
||||
},
|
||||
@@ -64,59 +65,51 @@ func TestIndexLifecycle(t *testing.T) {
|
||||
client: realClient,
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"tag": resource.NewStringProperty(
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"tag": property.New(
|
||||
"docker.io/pulumibot/buildkit-e2e:manifest",
|
||||
),
|
||||
"sources": resource.NewArrayProperty([]resource.PropertyValue{
|
||||
resource.NewStringProperty("docker.io/pulumibot/buildkit-e2e:arm64"),
|
||||
resource.NewStringProperty("docker.io/pulumibot/buildkit-e2e:amd64"),
|
||||
"sources": property.New([]property.Value{
|
||||
property.New("docker.io/pulumibot/buildkit-e2e:arm64"),
|
||||
property.New("docker.io/pulumibot/buildkit-e2e:amd64"),
|
||||
}),
|
||||
"push": resource.NewBoolProperty(true),
|
||||
"registry": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"address": resource.NewStringProperty("docker.io"),
|
||||
"username": resource.NewStringProperty("pulumibot"),
|
||||
"password": resource.NewSecretProperty(&resource.Secret{
|
||||
Element: resource.NewStringProperty(
|
||||
os.Getenv("DOCKER_HUB_PASSWORD"),
|
||||
),
|
||||
}),
|
||||
"push": property.New(true),
|
||||
"registry": property.New(map[string]property.Value{
|
||||
"address": property.New("docker.io"),
|
||||
"username": property.New("pulumibot"),
|
||||
"password": property.New(os.Getenv("DOCKER_HUB_PASSWORD")).WithSecret(true),
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "expired credentials",
|
||||
client: func(t *testing.T) Client {
|
||||
client: func(t *testing.T) clientF {
|
||||
ctrl := gomock.NewController(t)
|
||||
c := NewMockClient(ctrl)
|
||||
c.EXPECT().ManifestCreate(gomock.Any(), true, gomock.Any(), gomock.Any())
|
||||
c.EXPECT().ManifestInspect(gomock.Any(), gomock.Any()).Return("", errs.ErrHTTPUnauthorized)
|
||||
c.EXPECT().ManifestDelete(gomock.Any(), gomock.Any()).Return(nil)
|
||||
return c
|
||||
return mockClientF(c)
|
||||
},
|
||||
op: func(t *testing.T) integration.Operation {
|
||||
return integration.Operation{
|
||||
Inputs: resource.PropertyMap{
|
||||
"tag": resource.NewStringProperty(
|
||||
Inputs: property.NewMap(map[string]property.Value{
|
||||
"tag": property.New(
|
||||
"docker.io/pulumibot/buildkit-e2e:manifest",
|
||||
),
|
||||
"sources": resource.NewArrayProperty([]resource.PropertyValue{
|
||||
resource.NewStringProperty("docker.io/pulumibot/buildkit-e2e:arm64"),
|
||||
resource.NewStringProperty("docker.io/pulumibot/buildkit-e2e:amd64"),
|
||||
"sources": property.New([]property.Value{
|
||||
property.New("docker.io/pulumibot/buildkit-e2e:arm64"),
|
||||
property.New("docker.io/pulumibot/buildkit-e2e:amd64"),
|
||||
}),
|
||||
"push": resource.NewBoolProperty(true),
|
||||
"registry": resource.NewObjectProperty(resource.PropertyMap{
|
||||
"address": resource.NewStringProperty("docker.io"),
|
||||
"username": resource.NewStringProperty("pulumibot"),
|
||||
"password": resource.NewSecretProperty(&resource.Secret{
|
||||
Element: resource.NewStringProperty(
|
||||
os.Getenv("DOCKER_HUB_PASSWORD"),
|
||||
),
|
||||
}),
|
||||
"push": property.New(true),
|
||||
"registry": property.New(map[string]property.Value{
|
||||
"address": property.New("docker.io"),
|
||||
"username": property.New("pulumibot"),
|
||||
"password": property.New(os.Getenv("DOCKER_HUB_PASSWORD")).WithSecret(true),
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}
|
||||
},
|
||||
},
|
||||
@@ -132,7 +125,7 @@ func TestIndexLifecycle(t *testing.T) {
|
||||
Resource: "docker-build:index:Index",
|
||||
Create: tt.op(t),
|
||||
}
|
||||
s := newServer(tt.client(t))
|
||||
s := newServer(t.Context(), t, tt.client(t))
|
||||
|
||||
err := s.Configure(provider.ConfigureRequest{})
|
||||
require.NoError(t, err)
|
||||
@@ -149,22 +142,22 @@ func TestIndexDiff(t *testing.T) {
|
||||
baseState := IndexState{IndexArgs: baseArgs}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
olds func(*testing.T, IndexState) IndexState
|
||||
news func(*testing.T, IndexArgs) IndexArgs
|
||||
name string
|
||||
state func(*testing.T, IndexState) IndexState
|
||||
inputs func(*testing.T, IndexArgs) IndexArgs
|
||||
|
||||
wantChanges bool
|
||||
}{
|
||||
{
|
||||
name: "no diff if no changes",
|
||||
olds: func(*testing.T, IndexState) IndexState { return baseState },
|
||||
news: func(*testing.T, IndexArgs) IndexArgs { return baseArgs },
|
||||
state: func(*testing.T, IndexState) IndexState { return baseState },
|
||||
inputs: func(*testing.T, IndexArgs) IndexArgs { return baseArgs },
|
||||
wantChanges: false,
|
||||
},
|
||||
{
|
||||
name: "diff if tag changes",
|
||||
olds: func(*testing.T, IndexState) IndexState { return baseState },
|
||||
news: func(t *testing.T, a IndexArgs) IndexArgs {
|
||||
name: "diff if tag changes",
|
||||
state: func(*testing.T, IndexState) IndexState { return baseState },
|
||||
inputs: func(t *testing.T, a IndexArgs) IndexArgs {
|
||||
a.Tag = "new-tag"
|
||||
return a
|
||||
},
|
||||
@@ -172,7 +165,7 @@ func TestIndexDiff(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "no diff if registry password changes",
|
||||
olds: func(_ *testing.T, s IndexState) IndexState {
|
||||
state: func(_ *testing.T, s IndexState) IndexState {
|
||||
s.Registry = &Registry{
|
||||
Address: "foo",
|
||||
Username: "foo",
|
||||
@@ -180,7 +173,7 @@ func TestIndexDiff(t *testing.T) {
|
||||
}
|
||||
return s
|
||||
},
|
||||
news: func(_ *testing.T, a IndexArgs) IndexArgs {
|
||||
inputs: func(_ *testing.T, a IndexArgs) IndexArgs {
|
||||
a.Registry = &Registry{
|
||||
Address: "foo",
|
||||
Username: "foo",
|
||||
@@ -191,9 +184,9 @@ func TestIndexDiff(t *testing.T) {
|
||||
wantChanges: false,
|
||||
},
|
||||
{
|
||||
name: "diff if registry added",
|
||||
olds: func(*testing.T, IndexState) IndexState { return baseState },
|
||||
news: func(_ *testing.T, a IndexArgs) IndexArgs {
|
||||
name: "diff if registry added",
|
||||
state: func(*testing.T, IndexState) IndexState { return baseState },
|
||||
inputs: func(_ *testing.T, a IndexArgs) IndexArgs {
|
||||
a.Registry = &Registry{Address: "foo.com", Username: "foo", Password: "foo"}
|
||||
return a
|
||||
},
|
||||
@@ -201,7 +194,7 @@ func TestIndexDiff(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "diff if registry user changes",
|
||||
olds: func(_ *testing.T, s IndexState) IndexState {
|
||||
state: func(_ *testing.T, s IndexState) IndexState {
|
||||
s.Registry = &Registry{
|
||||
Address: "foo",
|
||||
Username: "foo",
|
||||
@@ -209,7 +202,7 @@ func TestIndexDiff(t *testing.T) {
|
||||
}
|
||||
return s
|
||||
},
|
||||
news: func(_ *testing.T, a IndexArgs) IndexArgs {
|
||||
inputs: func(_ *testing.T, a IndexArgs) IndexArgs {
|
||||
a.Registry = &Registry{
|
||||
Address: "DIFFERENT USER",
|
||||
Username: "foo",
|
||||
@@ -221,21 +214,21 @@ func TestIndexDiff(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
s := newServer(nil)
|
||||
s := newServer(t.Context(), t, nil)
|
||||
|
||||
encode := func(t *testing.T, x any) resource.PropertyMap {
|
||||
encode := func(t *testing.T, x any) property.Map {
|
||||
raw, err := mapper.New(&mapper.Opts{IgnoreMissing: true}).Encode(x)
|
||||
require.NoError(t, err)
|
||||
return resource.NewPropertyMapFromMap(raw)
|
||||
return resource.FromResourcePropertyMap(resource.NewPropertyMapFromMap(raw))
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
resp, err := s.Diff(provider.DiffRequest{
|
||||
Urn: urn,
|
||||
Olds: encode(t, tt.olds(t, baseState)),
|
||||
News: encode(t, tt.news(t, baseArgs)),
|
||||
Urn: urn,
|
||||
State: encode(t, tt.state(t, baseState)),
|
||||
Inputs: encode(t, tt.inputs(t, baseArgs)),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.wantChanges, resp.HasChanges, resp.DetailedDiff)
|
||||
|
||||
@@ -45,9 +45,6 @@ type Config struct {
|
||||
host *host
|
||||
}
|
||||
|
||||
// _mockClientKey is used by tests to inject a mock Docker client.
|
||||
var _mockClientKey any = "mock-client"
|
||||
|
||||
// Annotate provides user-facing descriptions and defaults for Config's fields.
|
||||
func (c *Config) Annotate(a infer.Annotator) {
|
||||
a.Describe(&c.Host, "The build daemon's address.")
|
||||
@@ -64,8 +61,23 @@ func (c *Config) Configure(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRegistries returns the config's registries, if any.
|
||||
func (c Config) GetRegistries() []Registry {
|
||||
return c.Registries
|
||||
}
|
||||
|
||||
// getHost returns the config's host, or nil if the config is also nil.
|
||||
func (c *Config) getHost() *host {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return c.host
|
||||
}
|
||||
|
||||
// NewBuildxProvider returns a new buildx provider.
|
||||
func NewBuildxProvider() provider.Provider {
|
||||
func NewBuildxProvider(clientF clientF) provider.Provider {
|
||||
config := &Config{}
|
||||
|
||||
prov := infer.Provider(
|
||||
infer.Options{
|
||||
Metadata: pschema.Metadata{
|
||||
@@ -113,41 +125,22 @@ func NewBuildxProvider() provider.Provider {
|
||||
},
|
||||
},
|
||||
Resources: []infer.InferredResource{
|
||||
infer.Resource[*Image](),
|
||||
infer.Resource[*Index](),
|
||||
infer.Resource(&Image{clientF: clientF, config: config}),
|
||||
infer.Resource(&Index{clientF: clientF, config: config}),
|
||||
},
|
||||
ModuleMap: map[tokens.ModuleName]tokens.ModuleName{
|
||||
"internal": "index",
|
||||
},
|
||||
Config: infer.Config[*Config](),
|
||||
Config: infer.Config(config),
|
||||
},
|
||||
)
|
||||
|
||||
prov.DiffConfig = diffConfigIgnoreInternal(prov.DiffConfig)
|
||||
|
||||
return prov
|
||||
}
|
||||
|
||||
// TODO(pulumi/pulumi-docker-build#404): Remove this function once the bug is fixed in either
|
||||
// upstream pu/pu or pulumi-go-provider.
|
||||
|
||||
// diffConfigInternalIgnore is a custom DiffConfig implementation for the buildx provider. This is required to
|
||||
// circumvent the bug identified in https://github.com/pulumi/pulumi-docker-build/issues/404.
|
||||
// Since `__internal` is currently populated in new inputs, but stripped in old state, we need to
|
||||
// ignore this field in the diff. There is no easy way to override DiffConfig to compare inputs only.
|
||||
func diffConfigIgnoreInternal(
|
||||
diffConfig func(ctx context.Context, req provider.DiffRequest) (provider.DiffResponse, error),
|
||||
) func(ctx context.Context, req provider.DiffRequest) (provider.DiffResponse, error) {
|
||||
return func(ctx context.Context, req provider.DiffRequest) (provider.DiffResponse, error) {
|
||||
delete(req.News, "__internal")
|
||||
|
||||
return diffConfig(ctx, req)
|
||||
}
|
||||
}
|
||||
|
||||
// Schema returns our package specification.
|
||||
func Schema(ctx context.Context, version string) schema.PackageSpec {
|
||||
p := NewBuildxProvider()
|
||||
p := NewBuildxProvider(nil)
|
||||
spec, err := provider.GetSchema(ctx, "docker-build", version, p)
|
||||
contract.AssertNoErrorf(err, "missing schema")
|
||||
return spec
|
||||
|
||||
@@ -20,18 +20,18 @@ import (
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
provider "github.com/pulumi/pulumi-go-provider"
|
||||
"github.com/pulumi/pulumi-go-provider/infer"
|
||||
"github.com/pulumi/pulumi-go-provider/integration"
|
||||
mwcontext "github.com/pulumi/pulumi-go-provider/middleware/context"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
|
||||
)
|
||||
|
||||
func TestConfigure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := newServer(nil)
|
||||
s := newServer(t.Context(), t, nil)
|
||||
|
||||
err := s.Configure(
|
||||
provider.ConfigureRequest{},
|
||||
@@ -60,7 +60,7 @@ func TestAnnotate(t *testing.T) {
|
||||
func TestSchema(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := newServer(nil)
|
||||
s := newServer(t.Context(), t, nil)
|
||||
|
||||
_, err := s.GetSchema(provider.GetSchemaRequest{Version: 0})
|
||||
assert.NoError(t, err)
|
||||
@@ -68,21 +68,27 @@ func TestSchema(t *testing.T) {
|
||||
|
||||
type annotator struct{}
|
||||
|
||||
func (annotator) Deprecate(_ any, _ string) {}
|
||||
func (annotator) Describe(_ any, _ string) {}
|
||||
func (annotator) SetDefault(_, _ any, _ ...string) {}
|
||||
func (annotator) SetToken(tokens.ModuleName, tokens.TypeName) {}
|
||||
func (annotator) AddAlias(tokens.ModuleName, tokens.TypeName) {}
|
||||
func (annotator) SetResourceDeprecationMessage(_ string) {}
|
||||
|
||||
func newServer(client Client) integration.Server {
|
||||
p := NewBuildxProvider()
|
||||
func newServer(ctx context.Context, t *testing.T, clientF clientF) integration.Server {
|
||||
t.Helper()
|
||||
|
||||
// Inject a mock client if provided.
|
||||
if client != nil {
|
||||
p = mwcontext.Wrap(p, func(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, _mockClientKey, client)
|
||||
})
|
||||
if clientF == nil {
|
||||
clientF = RealClientF
|
||||
}
|
||||
|
||||
return integration.NewServer("docker-build", semver.Version{Major: 0}, p)
|
||||
p := NewBuildxProvider(clientF)
|
||||
|
||||
s, err := integration.NewServer(
|
||||
ctx,
|
||||
"docker-build", semver.Version{Major: 0},
|
||||
integration.WithProvider(p),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -15,13 +15,8 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pulumi/pulumi-docker-build/provider/internal"
|
||||
"github.com/pulumi/pulumi-docker-build/provider/internal/deprecated"
|
||||
gp "github.com/pulumi/pulumi-go-provider"
|
||||
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
||||
"github.com/pulumi/pulumi/pkg/v3/resource/provider"
|
||||
rpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
|
||||
)
|
||||
@@ -39,37 +34,5 @@ func Serve() error {
|
||||
|
||||
// New creates a new provider.
|
||||
func New(host *provider.HostClient) (rpc.ResourceProviderServer, error) {
|
||||
return gp.RawServer(Name, Version, configurableProvider(internal.NewBuildxProvider()))(host)
|
||||
}
|
||||
|
||||
// configurableProvider is a workaround for
|
||||
// https://github.com/pulumi/pulumi-go-provider/issues/171 and
|
||||
// In short, our SDKs send provider Configure requests as simple strings
|
||||
// instead of rich objects. We don't want to preserve this behavior in
|
||||
// pulumi-go-provider, but we also haven't updated SDKs yet to send rich types.
|
||||
//
|
||||
// If you find yourself in a position where you need to copy this -- STOP!
|
||||
// https://github.com/pulumi/pulumi/pull/15032 should be merged with this fix.
|
||||
func configurableProvider(p gp.Provider) gp.Provider {
|
||||
configure := p.Configure
|
||||
|
||||
p.Configure = func(ctx context.Context, req gp.ConfigureRequest) error {
|
||||
r, err := p.GetSchema(ctx, gp.GetSchemaRequest{Version: 0})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
spec := schema.PackageSpec{}
|
||||
err = json.Unmarshal([]byte(r.Schema), &spec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ce := deprecated.New(spec.Config)
|
||||
if props, err := ce.UnmarshalProperties(req.Args); err == nil {
|
||||
req.Args = props
|
||||
}
|
||||
return configure(ctx, req)
|
||||
}
|
||||
|
||||
return p
|
||||
return gp.RawServer(Name, Version, internal.NewBuildxProvider(internal.RealClientF))(host)
|
||||
}
|
||||
|
||||
@@ -22,37 +22,8 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
emptypb "google.golang.org/protobuf/types/known/emptypb"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
|
||||
"github.com/pulumi/pulumi-docker-build/provider/internal"
|
||||
provider "github.com/pulumi/pulumi-go-provider"
|
||||
"github.com/pulumi/pulumi-go-provider/integration"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
|
||||
)
|
||||
|
||||
// TestConfigure checks backwards-compatibility with SDKs that still send
|
||||
// provider config as JSON-encoded strings. This test can be removed once we
|
||||
// upgrade to a version of pulumi that no longer generates SDKs with that
|
||||
// behavior.
|
||||
func TestConfigure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
p := configurableProvider(internal.NewBuildxProvider())
|
||||
|
||||
args, err := structpb.NewStruct(map[string]any{
|
||||
"registries": `[{"address": "docker.io"}]`,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
argsMap, err := plugin.UnmarshalProperties(args, plugin.MarshalOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
s := integration.NewServer("docker-build", semver.Version{Major: 0}, p)
|
||||
err = s.Configure(provider.ConfigureRequest{
|
||||
Args: argsMap,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user