-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to override the test image resolver, to use env variables. (#168)
Also, fixing test of test/pkg/k8s.TestResolveAddress which behaved flaky on downstream projects.
- Loading branch information
Showing
13 changed files
with
423 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package e2e | ||
|
||
import ( | ||
"knative.dev/kn-plugin-event/test/images" | ||
"knative.dev/reconciler-test/pkg/environment" | ||
) | ||
|
||
// ConfigureImages will register packages to be built into test images. | ||
func ConfigureImages(t images.TestingT) { | ||
environment.RegisterPackage(watholaForwarderPackage) | ||
images.ResolveImages(t, []string{ | ||
"knative.dev/reconciler-test/cmd/eventshub", | ||
watholaForwarderPackage, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package images | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
) | ||
|
||
var ( | ||
// ErrNotFound is returned when there is no environment variable set for | ||
// given KO path. | ||
ErrNotFound = errors.New("expected environment variable not found") | ||
nonAlphaNumeric = regexp.MustCompile("[^A-Z0-9]+") | ||
) | ||
|
||
// EnvironmentalBasedResolver will try to resolve the images from prefixed | ||
// environment variables. | ||
type EnvironmentalBasedResolver struct { | ||
Prefix string | ||
} | ||
|
||
func (c *EnvironmentalBasedResolver) Applicable() bool { | ||
prefix := c.normalizedPrefix() | ||
for _, environment := range os.Environ() { | ||
const equalitySignParts = 2 | ||
parts := strings.SplitN(environment, "=", equalitySignParts) | ||
key := parts[0] | ||
if strings.HasPrefix(key, prefix) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (c *EnvironmentalBasedResolver) Resolve(kopath string) (name.Reference, error) { | ||
prefix := c.normalizedPrefix() | ||
shortName := normalize(path.Base(kopath)) | ||
key := fmt.Sprintf("%s_%s", prefix, shortName) | ||
val, ok := os.LookupEnv(key) | ||
if !ok { | ||
return nil, fmt.Errorf("%w: '%s' - kopath: %s", ErrNotFound, key, kopath) | ||
} | ||
ref, err := name.ParseReference(val) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: %v", ErrNotFound, err) | ||
} | ||
return ref, nil | ||
} | ||
|
||
func (c *EnvironmentalBasedResolver) normalizedPrefix() string { | ||
return normalize(c.Prefix) | ||
} | ||
|
||
func normalize(in string) string { | ||
return strings.Trim( | ||
nonAlphaNumeric.ReplaceAllString(strings.ToUpper(in), "_"), | ||
"_", | ||
) | ||
} | ||
|
||
var _ Resolver = &EnvironmentalBasedResolver{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package images | ||
|
||
// Resolvers an array of resolvers to which resolvers could be added for | ||
// downstream projects. | ||
var Resolvers = make([]Resolver, 0, 1) //nolint:gochecknoglobals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package images | ||
|
||
import ( | ||
"github.com/google/go-containerregistry/pkg/name" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
"knative.dev/reconciler-test/pkg/environment" | ||
) | ||
|
||
// Resolver will resolve given KO package paths into real OCI images references. | ||
// This interface probably should be moved into reconciler-test framework. See: | ||
// https://github.com/knative-sandbox/reconciler-test/issues/303 | ||
type Resolver interface { | ||
// Resolve will resolve given KO package path into real OCI image reference. | ||
Resolve(kopath string) (name.Reference, error) | ||
// Applicable will tell that given resolver is applicable to current runtime | ||
// environment, or not. | ||
Applicable() bool | ||
} | ||
|
||
// TestingT a subset of testing.T. | ||
type TestingT interface { | ||
Logf(fmt string, args ...interface{}) | ||
Fatal(args ...interface{}) | ||
Fatalf(fmt string, args ...interface{}) | ||
} | ||
|
||
// ResolveImages will try to resolve the images, using given resolver(s). | ||
func ResolveImages(t TestingT, packages []string) { | ||
for _, resolver := range Resolvers { | ||
if resolver.Applicable() { | ||
resolveImagesWithResolver(t, packages, resolver) | ||
return | ||
} | ||
} | ||
if len(Resolvers) > 0 { | ||
t.Fatalf("Couldn't resolve images with registered resolvers: %+q", Resolvers) | ||
} | ||
} | ||
|
||
func resolveImagesWithResolver(t TestingT, packages []string, resolver Resolver) { | ||
resolved := make(map[string]string) | ||
for _, pack := range packages { | ||
kopath := "ko://" + pack | ||
image, err := resolver.Resolve(kopath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
resolved[kopath] = image.String() | ||
} | ||
repr, err := json.Marshal(resolved) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Logf("Images resolved to: %s", string(repr)) | ||
environment.WithImages(resolved) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.