2021-06-11 01:29:30 +10:00
|
|
|
/*
|
|
|
|
|
Copyright 2018 Google LLC All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
2019-11-05 15:24:08 -05:00
|
|
|
|
|
|
|
|
package testing
|
|
|
|
|
|
|
|
|
|
import (
|
2019-11-10 01:23:09 +08:00
|
|
|
"context"
|
2020-10-31 12:55:28 -04:00
|
|
|
"errors"
|
2019-11-05 15:24:08 -05:00
|
|
|
"fmt"
|
2020-04-29 19:32:30 -07:00
|
|
|
"strings"
|
2019-11-05 15:24:08 -05:00
|
|
|
|
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
|
"github.com/google/ko/pkg/build"
|
|
|
|
|
"github.com/google/ko/pkg/publish"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type fixedBuild struct {
|
2020-09-24 15:58:08 -07:00
|
|
|
entries map[string]build.Result
|
2019-11-05 15:24:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewFixedBuild returns a build.Interface implementation that simply resolves
|
|
|
|
|
// particular references to fixed v1.Image objects
|
2020-09-24 15:58:08 -07:00
|
|
|
func NewFixedBuild(entries map[string]build.Result) build.Interface {
|
2019-11-05 15:24:08 -05:00
|
|
|
return &fixedBuild{entries}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 01:29:30 +10:00
|
|
|
// QualifyImport implements build.Interface
|
|
|
|
|
func (f *fixedBuild) QualifyImport(ip string) (string, error) {
|
|
|
|
|
return ip, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 15:24:08 -05:00
|
|
|
// IsSupportedReference implements build.Interface
|
2020-10-31 12:55:28 -04:00
|
|
|
func (f *fixedBuild) IsSupportedReference(s string) error {
|
2020-04-29 19:32:30 -07:00
|
|
|
s = strings.TrimPrefix(s, build.StrictScheme)
|
2020-10-31 12:55:28 -04:00
|
|
|
if _, ok := f.entries[s]; !ok {
|
|
|
|
|
return errors.New("importpath is not supported")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2019-11-05 15:24:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build implements build.Interface
|
2020-09-24 15:58:08 -07:00
|
|
|
func (f *fixedBuild) Build(_ context.Context, s string) (build.Result, error) {
|
2020-04-29 19:32:30 -07:00
|
|
|
s = strings.TrimPrefix(s, build.StrictScheme)
|
2019-11-05 15:24:08 -05:00
|
|
|
if img, ok := f.entries[s]; ok {
|
|
|
|
|
return img, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("unsupported reference: %q", s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type fixedPublish struct {
|
|
|
|
|
base name.Repository
|
|
|
|
|
entries map[string]v1.Hash
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewFixedPublish returns a publish.Interface implementation that simply
|
|
|
|
|
// resolves particular references to fixed name.Digest references.
|
|
|
|
|
func NewFixedPublish(base name.Repository, entries map[string]v1.Hash) publish.Interface {
|
|
|
|
|
return &fixedPublish{base, entries}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Publish implements publish.Interface
|
2020-12-21 11:47:05 -08:00
|
|
|
func (f *fixedPublish) Publish(_ context.Context, _ build.Result, s string) (name.Reference, error) {
|
2020-04-29 19:32:30 -07:00
|
|
|
s = strings.TrimPrefix(s, build.StrictScheme)
|
2019-11-05 15:24:08 -05:00
|
|
|
h, ok := f.entries[s]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("unsupported importpath: %q", s)
|
|
|
|
|
}
|
|
|
|
|
d, err := name.NewDigest(fmt.Sprintf("%s/%s@%s", f.base, s, h))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &d, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 09:30:01 -08:00
|
|
|
func (f *fixedPublish) Close() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 15:24:08 -05:00
|
|
|
func ComputeDigest(base name.Repository, ref string, h v1.Hash) string {
|
|
|
|
|
d, err := name.NewDigest(fmt.Sprintf("%s/%s@%s", base, ref, h))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return d.String()
|
|
|
|
|
}
|