Skip to content

Commit

Permalink
Merge pull request crossplane#5261 from jbw976/default-registry
Browse files Browse the repository at this point in the history
Update default registry to xpkg.upbound.io
  • Loading branch information
jbw976 authored Jan 23, 2024
2 parents efbec7a + 0bb6d0b commit 7130443
Show file tree
Hide file tree
Showing 27 changed files with 364 additions and 161 deletions.
2 changes: 1 addition & 1 deletion cmd/crank/xpkg/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Examples:
func (c *installCmd) Run(k *kong.Context, logger logging.Logger) error { //nolint:gocyclo // TODO(negz): Can anything be broken out here?
pkgName := c.Name
if pkgName == "" {
ref, err := name.ParseReference(c.Package, name.WithDefaultRegistry(DefaultRegistry))
ref, err := name.ParseReference(c.Package, name.WithDefaultRegistry(xpkg.DefaultRegistry))
if err != nil {
logger.Debug(errPkgIdentifier, "error", err)
return errors.Wrap(err, errPkgIdentifier)
Expand Down
7 changes: 2 additions & 5 deletions cmd/crank/xpkg/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ const (
errFmtWriteIndex = "failed to push an OCI image index of %d packages"
)

// DefaultRegistry for pushing Crossplane packages.
const DefaultRegistry = "xpkg.upbound.io"

// pushCmd pushes a package.
type pushCmd struct {
// Arguments.
Expand Down Expand Up @@ -94,7 +91,7 @@ func (c *pushCmd) AfterApply() error {

// Run runs the push cmd.
func (c *pushCmd) Run(logger logging.Logger) error { //nolint:gocyclo // This feels easier to read as-is.
tag, err := name.NewTag(c.Package, name.WithDefaultRegistry(DefaultRegistry))
tag, err := name.NewTag(c.Package, name.WithDefaultRegistry(xpkg.DefaultRegistry))
if err != nil {
return errors.Wrapf(err, errFmtNewTag, c.Package)
}
Expand Down Expand Up @@ -159,7 +156,7 @@ func (c *pushCmd) Run(logger logging.Logger) error { //nolint:gocyclo // This fe
return errors.Wrapf(err, errFmtGetDigest, file)
}
n := fmt.Sprintf("%s@%s", tag.Repository.Name(), d.String())
ref, err := name.NewDigest(n, name.WithDefaultRegistry(DefaultRegistry))
ref, err := name.NewDigest(n, name.WithDefaultRegistry(xpkg.DefaultRegistry))
if err != nil {
return errors.Wrapf(err, errFmtNewDigest, n, file)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crank/xpkg/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Examples:
func (c *updateCmd) Run(k *kong.Context, logger logging.Logger) error {
pkgName := c.Name
if pkgName == "" {
ref, err := name.ParseReference(c.Package, name.WithDefaultRegistry(DefaultRegistry))
ref, err := name.ParseReference(c.Package, name.WithDefaultRegistry(xpkg.DefaultRegistry))
if err != nil {
logger.Debug(errPkgIdentifier, "error", err)
return errors.Wrap(err, errPkgIdentifier)
Expand Down
4 changes: 1 addition & 3 deletions cmd/crossplane/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"time"

"github.com/alecthomas/kong"
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/afero"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -70,7 +69,7 @@ type Command struct {
// KongVars represent the kong variables associated with the CLI parser
// required for the Registry default variable interpolation.
var KongVars = kong.Vars{
"default_registry": name.DefaultRegistry,
"default_registry": xpkg.DefaultRegistry,
"default_user_agent": transport.DefaultUserAgent,
}

Expand Down Expand Up @@ -264,7 +263,6 @@ func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error { //noli
Options: o,
Namespace: c.Namespace,
ServiceAccount: c.ServiceAccount,
Registry: c.Registry,
FunctionRunner: functionRunner,
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/crossplane/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

"github.com/alecthomas/kong"
"github.com/google/go-containerregistry/pkg/name"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/leaderelection/resourcelock"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -35,6 +34,7 @@ import (

"github.com/crossplane/crossplane/internal/controller/rbac"
rbaccontroller "github.com/crossplane/crossplane/internal/controller/rbac/controller"
"github.com/crossplane/crossplane/internal/xpkg"
)

// Available RBAC management policies.
Expand All @@ -53,7 +53,7 @@ var KongVars = kong.Vars{
ManagementPolicyBasic,
},
", "),
"default_registry": name.DefaultRegistry,
"rbac_default_registry": xpkg.DefaultRegistry,
}

// Command runs the crossplane RBAC controllers
Expand All @@ -74,7 +74,7 @@ type startCommand struct {

ProviderClusterRole string `name:"provider-clusterrole" help:"A ClusterRole enumerating the permissions provider packages may request."`
LeaderElection bool `name:"leader-election" short:"l" help:"Use leader election for the controller manager." env:"LEADER_ELECTION"`
Registry string `short:"r" help:"Default registry used to fetch packages when not specified in tag." default:"${default_registry}" env:"REGISTRY"`
Registry string `short:"r" help:"Default registry used to fetch packages when not specified in tag." default:"${rbac_default_registry}" env:"REGISTRY"`

ManagementPolicy string `name:"manage" short:"m" hidden:""`
DeprecatedManagementPolicy string `name:"deprecated-manage" hidden:"" default:"${rbac_manage_default_var}" enum:"${rbac_manage_enum_var}"`
Expand Down
4 changes: 0 additions & 4 deletions internal/controller/apiextensions/controller/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ type Options struct {
// private registry authentication when pulling Composition Functions.
ServiceAccount string

// Registry is the default registry to use when pulling containers for
// Composition Functions
Registry string

// FunctionRunner used to run Composition Functions.
FunctionRunner *xfn.PackagedFunctionRunner
}
21 changes: 15 additions & 6 deletions internal/controller/pkg/resolver/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,21 @@ func WithFetcher(f xpkg.Fetcher) ReconcilerOption {
}
}

// WithDefaultRegistry sets the default registry to use.
func WithDefaultRegistry(registry string) ReconcilerOption {
return func(r *Reconciler) {
r.registry = registry
}
}

// Reconciler reconciles packages.
type Reconciler struct {
client client.Client
log logging.Logger
lock resource.Finalizer
newDag dag.NewDAGFn
fetcher xpkg.Fetcher
client client.Client
log logging.Logger
lock resource.Finalizer
newDag dag.NewDAGFn
fetcher xpkg.Fetcher
registry string
}

// Setup adds a controller that reconciles the Lock.
Expand All @@ -125,6 +133,7 @@ func Setup(mgr ctrl.Manager, o controller.Options) error {
r := NewReconciler(mgr,
WithLogger(o.Logger.WithValues("controller", name)),
WithFetcher(f),
WithDefaultRegistry(o.DefaultRegistry),
)

return ctrl.NewControllerManagedBy(mgr).
Expand Down Expand Up @@ -231,7 +240,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
log.Debug(errInvalidConstraint, "error", err)
return reconcile.Result{Requeue: false}, nil
}
ref, err := name.ParseReference(dep.Package)
ref, err := name.ParseReference(dep.Package, name.WithDefaultRegistry(r.registry))
if err != nil {
log.Debug(errInvalidDependency, "error", err)
return reconcile.Result{Requeue: false}, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/pkg/revision/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func SetupProviderRevision(mgr ctrl.Manager, o controller.Options) error {
}

if o.PackageRuntime == controller.PackageRuntimeDeployment {
ro = append(ro, WithRuntimeHooks(NewProviderHooks(mgr.GetClient())))
ro = append(ro, WithRuntimeHooks(NewProviderHooks(mgr.GetClient(), o.DefaultRegistry)))

if o.Features.Enabled(features.EnableBetaDeploymentRuntimeConfigs) {
cb = cb.Watches(&v1beta1.DeploymentRuntimeConfig{}, &EnqueueRequestForReferencingProviderRevisions{
Expand Down Expand Up @@ -429,7 +429,7 @@ func SetupFunctionRevision(mgr ctrl.Manager, o controller.Options) error {
}

if o.PackageRuntime == controller.PackageRuntimeDeployment {
ro = append(ro, WithRuntimeHooks(NewFunctionHooks(mgr.GetClient())))
ro = append(ro, WithRuntimeHooks(NewFunctionHooks(mgr.GetClient(), o.DefaultRegistry)))

if o.Features.Enabled(features.EnableBetaDeploymentRuntimeConfigs) {
cb = cb.Watches(&v1beta1.DeploymentRuntimeConfig{}, &EnqueueRequestForReferencingFunctionRevisions{
Expand Down
39 changes: 31 additions & 8 deletions internal/controller/pkg/revision/runtime_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

"github.com/google/go-containerregistry/pkg/name"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -47,20 +48,23 @@ const (
errApplyFunctionService = "cannot apply function package service"
errFmtUnavailableFunctionDeployment = "function package deployment is unavailable with message: %s"
errNoAvailableConditionFunctionDeployment = "function package deployment has no condition of type \"Available\" yet"
errParseFunctionImage = "cannot parse function package image"
)

// FunctionHooks performs runtime operations for function packages.
type FunctionHooks struct {
client resource.ClientApplicator
client resource.ClientApplicator
defaultRegistry string
}

// NewFunctionHooks returns a new FunctionHooks.
func NewFunctionHooks(client client.Client) *FunctionHooks {
func NewFunctionHooks(client client.Client, defaultRegistry string) *FunctionHooks {
return &FunctionHooks{
client: resource.ClientApplicator{
Client: client,
Applicator: resource.NewAPIPatchingApplicator(client),
},
defaultRegistry: defaultRegistry,
}
}

Expand Down Expand Up @@ -116,7 +120,14 @@ func (h *FunctionHooks) Post(ctx context.Context, pkg runtime.Object, pr v1.Pack
}

sa := build.ServiceAccount()
d := build.Deployment(sa.Name, functionDeploymentOverrides(functionMeta, pr)...)

// Determine the function's image, taking into account the default registry.
image, err := getFunctionImage(functionMeta, pr, h.defaultRegistry)
if err != nil {
return errors.Wrap(err, errParseFunctionImage)
}

d := build.Deployment(sa.Name, functionDeploymentOverrides(image)...)
// Create/Apply the SA only if the deployment references it.
// This is to avoid creating a SA that is NOT used by the deployment when
// the SA is managed externally by the user and configured by setting
Expand Down Expand Up @@ -165,7 +176,7 @@ func (h *FunctionHooks) Deactivate(ctx context.Context, _ v1.PackageRevisionWith
return nil
}

func functionDeploymentOverrides(functionMeta *pkgmetav1beta1.Function, pr v1.PackageRevisionWithRuntime) []DeploymentOverride {
func functionDeploymentOverrides(image string) []DeploymentOverride {
do := []DeploymentOverride{
DeploymentRuntimeWithAdditionalPorts([]corev1.ContainerPort{
{
Expand All @@ -175,10 +186,6 @@ func functionDeploymentOverrides(functionMeta *pkgmetav1beta1.Function, pr v1.Pa
}),
}

image := pr.GetSource()
if functionMeta.Spec.Image != nil {
image = *functionMeta.Spec.Image
}
do = append(do, DeploymentRuntimeWithOptionalImage(image))

return do
Expand All @@ -192,3 +199,19 @@ func functionServiceOverrides() []ServiceOverride {
ServiceWithClusterIP(corev1.ClusterIPNone),
}
}

// getFunctionImage determines a complete function image, taking into account a
// default registry. If the function meta specifies an image, we have a
// preference for that image over what is specified in the package revision.
func getFunctionImage(fm *pkgmetav1beta1.Function, pr v1.PackageRevisionWithRuntime, defaultRegistry string) (string, error) {
image := pr.GetSource()
if fm.Spec.Image != nil {
image = *fm.Spec.Image
}
ref, err := name.ParseReference(image, name.WithDefaultRegistry(defaultRegistry))
if err != nil {
return "", errors.Wrap(err, errParseFunctionImage)
}

return ref.Name(), nil
}
Loading

0 comments on commit 7130443

Please sign in to comment.