Skip to content

Commit

Permalink
Introduce a --registry-insecure flag (#2234)
Browse files Browse the repository at this point in the history
* fix env var casing otherwise flags with hyphens will continue to have hyphens

* Introduce an --registry-insecure flag

* fix build tests

* fix TestList

* fix insecure (http) push
  • Loading branch information
dprotaso authored Mar 16, 2024
1 parent 8f3f718 commit 1d0d761
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
16 changes: 9 additions & 7 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ NAME
SYNOPSIS
{{rootCmdUse}} build [-r|--registry] [--builder] [--builder-image] [--push]
[--platform] [-p|--path] [-c|--confirm] [-v|--verbose]
[--build-timestamp]
[--build-timestamp] [--registry-insecure]
DESCRIPTION
Expand Down Expand Up @@ -66,7 +66,7 @@ EXAMPLES
`,
SuggestFor: []string{"biuld", "buidl", "built"},
PreRunE: bindEnv("image", "path", "builder", "registry", "confirm", "push", "builder-image", "platform", "verbose", "build-timestamp"),
PreRunE: bindEnv("image", "path", "builder", "registry", "confirm", "push", "builder-image", "platform", "verbose", "build-timestamp", "registry-insecure"),
RunE: func(cmd *cobra.Command, args []string) error {
return runBuild(cmd, args, newClient)
},
Expand Down Expand Up @@ -98,6 +98,7 @@ EXAMPLES
fmt.Sprintf("Builder to use when creating the function's container. Currently supported builders are %s. ($FUNC_BUILDER)", KnownBuilders()))
cmd.Flags().StringP("registry", "r", cfg.Registry,
"Container registry + registry namespace. (ex 'ghcr.io/myuser'). The full image name is automatically determined using this along with function name. ($FUNC_REGISTRY)")
cmd.Flags().Bool("registry-insecure", cfg.RegistryInsecure, "Disable HTTPS when communicating to the registry ($FUNC_REGISTRY_INSECURE)")

// Function-Context Flags:
// Options whose value is available on the function with context only
Expand Down Expand Up @@ -215,10 +216,11 @@ type buildConfig struct {
func newBuildConfig() buildConfig {
return buildConfig{
Global: config.Global{
Builder: viper.GetString("builder"),
Confirm: viper.GetBool("confirm"),
Registry: registry(), // deferred defaulting
Verbose: viper.GetBool("verbose"),
Builder: viper.GetString("builder"),
Confirm: viper.GetBool("confirm"),
Registry: registry(), // deferred defaulting
Verbose: viper.GetBool("verbose"),
RegistryInsecure: viper.GetBool("registry-insecure"),
},
BuilderImage: viper.GetString("builder-image"),
Image: viper.GetString("image"),
Expand Down Expand Up @@ -341,7 +343,7 @@ func (c buildConfig) clientOptions() ([]fn.Option, error) {
if c.Builder == builders.Host {
o = append(o,
fn.WithBuilder(oci.NewBuilder(builders.Host, c.Verbose)),
fn.WithPusher(oci.NewPusher(false, c.Verbose)))
fn.WithPusher(oci.NewPusher(c.RegistryInsecure, false, c.Verbose)))
} else if c.Builder == builders.Pack {
o = append(o,
fn.WithBuilder(pack.NewBuilder(
Expand Down
4 changes: 3 additions & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SYNOPSIS
[-b|--build] [--builder] [--builder-image] [-p|--push]
[--domain] [--platform] [--build-timestamp] [--pvc-size]
[--service-account] [-c|--confirm] [-v|--verbose]
[--registry-insecure]
DESCRIPTION
Expand Down Expand Up @@ -124,7 +125,7 @@ EXAMPLES
`,
SuggestFor: []string{"delpoy", "deplyo"},
PreRunE: bindEnv("build", "build-timestamp", "builder", "builder-image", "confirm", "domain", "env", "git-branch", "git-dir", "git-url", "image", "namespace", "path", "platform", "push", "pvc-size", "service-account", "registry", "remote", "verbose"),
PreRunE: bindEnv("build", "build-timestamp", "builder", "builder-image", "confirm", "domain", "env", "git-branch", "git-dir", "git-url", "image", "namespace", "path", "platform", "push", "pvc-size", "service-account", "registry", "registry-insecure", "remote", "verbose"),
RunE: func(cmd *cobra.Command, args []string) error {
return runDeploy(cmd, newClient)
},
Expand All @@ -151,6 +152,7 @@ EXAMPLES
fmt.Sprintf("Builder to use when creating the function's container. Currently supported builders are %s.", KnownBuilders()))
cmd.Flags().StringP("registry", "r", cfg.Registry,
"Container registry + registry namespace. (ex 'ghcr.io/myuser'). The full image name is automatically determined using this along with function name. ($FUNC_REGISTRY)")
cmd.Flags().Bool("registry-insecure", cfg.RegistryInsecure, "Disable HTTPS when communicating to the registry ($FUNC_REGISTRY_INSECURE)")
cmd.Flags().StringP("namespace", "n", cfg.Namespace,
"Deploy into a specific namespace. Will use function's current namespace by default if already deployed, and the currently active namespace if it can be determined. ($FUNC_NAMESPACE)")

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Learn more about Knative at: https://knative.dev`, cfg.Name),
// a version prefixed by "FUNC_"
viper.AutomaticEnv() // read in environment variables for FUNC_<flag>
viper.SetEnvPrefix("func") // ensure that all have the prefix
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// Client
// Use the provided ClientFactory or default to NewClient
Expand Down Expand Up @@ -170,6 +171,7 @@ func bindEnv(flags ...string) bindFunc {
}
viper.AutomaticEnv() // read in environment variables for FUNC_<flag>
viper.SetEnvPrefix("func") // ensure that all have the prefix
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
return
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type Global struct {
// NOTE: all members must include their yaml serialized names, even when
// this is the default, because these tag values are used for the static
// getter/setter accessors to match requests.

RegistryInsecure bool `yaml:"registryInsecure,omitempty"`
}

// New Config struct with all members set to static defaults. See NewDefaults
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func TestList(t *testing.T) {
"language",
"namespace",
"registry",
"registryInsecure",
"verbose",
}

Expand Down
32 changes: 20 additions & 12 deletions pkg/oci/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ import (

// Pusher of OCI multi-arch layout directories.
type Pusher struct {
Insecure bool
Verbose bool
Username string
Token string
Insecure bool
Anonymous bool
Verbose bool
Username string
Token string

updates chan v1.Update
done chan bool
}

func NewPusher(insecure, verbose bool) *Pusher {
func NewPusher(insecure, anon, verbose bool) *Pusher {
fmt.Println(insecure, anon, verbose)
return &Pusher{
Insecure: insecure,
Verbose: verbose,
updates: make(chan v1.Update, 10),
done: make(chan bool, 1),
Insecure: insecure,
Anonymous: anon,
Verbose: verbose,
updates: make(chan v1.Update, 10),
done: make(chan bool, 1),
}
}

Expand All @@ -44,10 +47,15 @@ func (p *Pusher) Push(ctx context.Context, f fn.Function) (digest string, err er
if err != nil {
return
}

var opts []name.Option
if p.Insecure {
opts = append(opts, name.Insecure)
}
// TODO: GitOps Tagging: tag :latest by default, :[branch] for pinned
// environments and :[user]-[branch] for development/testing feature branches.
// has been enabled, where branch is tag-encoded.
ref, err := name.ParseReference(f.Build.Image)
ref, err := name.ParseReference(f.Build.Image, opts...)
if err != nil {
return
}
Expand Down Expand Up @@ -79,8 +87,8 @@ func getLastBuildDir(f fn.Function) (string, error) {
}

func (p *Pusher) writeIndex(ctx context.Context, ref name.Reference, ii v1.ImageIndex) error {
// If we're set to insecure, just try as-is and return on failure
if p.Insecure {
// If we're set to anonymous, just try as-is and return on failure
if p.Anonymous {
return remote.WriteIndex(ref, ii,
remote.WithContext(ctx),
remote.WithProgress(p.updates))
Expand Down
3 changes: 2 additions & 1 deletion pkg/oci/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestPusher(t *testing.T) {
root, done = Mktemp(t)
verbose = false
insecure = true
anon = true
success = false
err error
)
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestPusher(t *testing.T) {
// Create and push a function
client := fn.New(
fn.WithBuilder(NewBuilder("", verbose)),
fn.WithPusher(NewPusher(insecure, verbose)))
fn.WithPusher(NewPusher(insecure, anon, verbose)))

f := fn.Function{Root: root, Runtime: "go", Name: "f", Registry: l.Addr().String() + "/funcs"}

Expand Down

0 comments on commit 1d0d761

Please sign in to comment.