From d5d174cb34b372da25a057ecfdf77e17c7fe3f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Wed, 18 Sep 2024 19:32:40 +0200 Subject: [PATCH] Compute with default mappings --- pkg/commands/flags/sink.go | 23 ++--------- pkg/flags/sink/sink.go | 83 ++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/pkg/commands/flags/sink.go b/pkg/commands/flags/sink.go index 10f2f9638..4e5dabf15 100644 --- a/pkg/commands/flags/sink.go +++ b/pkg/commands/flags/sink.go @@ -20,7 +20,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/runtime/schema" - "knative.dev/client/pkg/config" clientdynamic "knative.dev/client/pkg/dynamic" "knative.dev/client/pkg/flags/sink" "knative.dev/client/pkg/util/errors" @@ -66,26 +65,10 @@ func (i *SinkFlags) Add(cmd *cobra.Command) { // WithDefaultMappings will return a copy of SinkFlags with provided mappings // and the default ones. func (i *SinkFlags) WithDefaultMappings() *SinkFlags { - sf := &SinkFlags{ - Sink: i.Sink, - SinkMappings: make(map[string]schema.GroupVersionResource, - len(i.SinkMappings)+len(sink.DefaultMappings)), - } - for k, v := range sink.DefaultMappings { - sf.SinkMappings[k] = v - } - for k, v := range i.SinkMappings { - sf.SinkMappings[k] = v - } - for _, p := range config.GlobalConfig.SinkMappings() { - // user configuration might override the default configuration - sf.SinkMappings[p.Prefix] = schema.GroupVersionResource{ - Resource: p.Resource, - Group: p.Group, - Version: p.Version, - } + return &SinkFlags{ + Sink: i.Sink, + SinkMappings: sink.ComputeWithDefaultMappings(i.SinkMappings), } - return sf } // Parse returns the sink reference, which may refer to URL or to Kubernetes diff --git a/pkg/flags/sink/sink.go b/pkg/flags/sink/sink.go index 1c8a5591b..fc1c0c272 100644 --- a/pkg/flags/sink/sink.go +++ b/pkg/flags/sink/sink.go @@ -26,6 +26,7 @@ import ( "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" + "knative.dev/client/pkg/config" clientdynamic "knative.dev/client/pkg/dynamic" "knative.dev/pkg/apis" duckv1 "knative.dev/pkg/apis/duck/v1" @@ -49,7 +50,6 @@ const ( // Reference represents either a URL or Kubernetes resource. type Reference struct { - Type Type *KubeReference *apis.URL } @@ -93,15 +93,26 @@ var defaultMappingAliasses = map[string]string{ const knativeServiceShorthand = "ksvc" +// Type returns the type of the reference. +func (r *Reference) Type() Type { + if r.KubeReference != nil { + return TypeReference + } + if r.URL != nil { + return TypeURL + } + return Type(-1) // unknown type, unexpected +} + // Resolve returns the Destination referred to by the sink. It validates that // any object the user is referring to exists. func (r *Reference) Resolve(ctx context.Context, knclient clientdynamic.KnDynamicClient) (*duckv1.Destination, error) { - if r.Type == TypeURL { + if r.Type() == TypeURL { return &duckv1.Destination{URI: r.URL}, nil } - if r.Type != TypeReference { + if r.Type() != TypeReference { return nil, fmt.Errorf("%w: unexpected type %q", - ErrSinkIsInvalid, r.Type) + ErrSinkIsInvalid, r.Type()) } client := knclient.RawClient() obj, err := client.Resource(r.GVR). @@ -139,10 +150,10 @@ func (r *Reference) String() string { // AsText creates a text representation of the resource, and should // be used by giving a current namespace. func (r *Reference) AsText(currentNamespace string) string { - if r.Type == TypeURL { + if r.Type() == TypeURL { return r.URL.String() } - if r.Type == TypeReference { + if r.Type() == TypeReference { repr := r.GvrAsText() + ":" + r.Name if currentNamespace != r.Namespace { repr = fmt.Sprintf("%s:%s", repr, r.Namespace) @@ -150,7 +161,7 @@ func (r *Reference) AsText(currentNamespace string) string { return repr } return fmt.Errorf("%w: unexpected type %q", - ErrSinkIsInvalid, r.Type).Error() + ErrSinkIsInvalid, r.Type()).Error() } // GvrAsText returns the @@ -188,10 +199,7 @@ func Parse(sinkRepr, namespace string, mappings map[string]schema.GroupVersionRe if err != nil { return nil, fmt.Errorf("%w: %w", ErrSinkIsInvalid, err) } - return &Reference{ - Type: TypeURL, - URL: uri, - }, nil + return &Reference{URL: uri}, nil } gvr, ok := mappings[prefix] if !ok { @@ -220,14 +228,33 @@ func Parse(sinkRepr, namespace string, mappings map[string]schema.GroupVersionRe if ns != "" { namespace = ns } - return &Reference{ - Type: TypeReference, - KubeReference: &KubeReference{ - GVR: gvr, - Name: name, - Namespace: namespace, - }, - }, nil + return &Reference{KubeReference: &KubeReference{ + GVR: gvr, + Name: name, + Namespace: namespace, + }}, nil +} + +// ComputeWithDefaultMappings will compute mapping by including default mappings +// and mappings provided by the end-user. +func ComputeWithDefaultMappings(mappings map[string]schema.GroupVersionResource) map[string]schema.GroupVersionResource { + sm := make(map[string]schema.GroupVersionResource, + len(mappings)+len(DefaultMappings)) + for k, v := range DefaultMappings { + sm[k] = v + } + for k, v := range mappings { + sm[k] = v + } + for _, p := range config.GlobalConfig.SinkMappings() { + // user configuration might override the default configuration + sm[p.Prefix] = schema.GroupVersionResource{ + Resource: p.Resource, + Group: p.Group, + Version: p.Version, + } + } + return sm } // GuessFromDestination converts the duckv1.Destination to the Reference by guessing @@ -235,10 +262,7 @@ func Parse(sinkRepr, namespace string, mappings map[string]schema.GroupVersionRe // Will return nil, if given empty destination. func GuessFromDestination(dest duckv1.Destination) *Reference { if dest.URI != nil { - return &Reference{ - Type: TypeURL, - URL: dest.URI, - } + return &Reference{URL: dest.URI} } if dest.Ref == nil { return nil @@ -251,14 +275,11 @@ func GuessFromDestination(dest duckv1.Destination) *Reference { } gvk := ref.GroupVersionKind() gvr, _ := meta.UnsafeGuessKindToResource(gvk) - return &Reference{ - Type: TypeReference, - KubeReference: &KubeReference{ - GVR: gvr, - Name: ref.Name, - Namespace: ref.Namespace, - }, - } + return &Reference{KubeReference: &KubeReference{ + GVR: gvr, + Name: ref.Name, + Namespace: ref.Namespace, + }} } func withAliasses(