Skip to content

Commit

Permalink
Compute with default mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Sep 18, 2024
1 parent d6c66ea commit d5d174c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 51 deletions.
23 changes: 3 additions & 20 deletions pkg/commands/flags/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
83 changes: 52 additions & 31 deletions pkg/flags/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -49,7 +50,6 @@ const (

// Reference represents either a URL or Kubernetes resource.
type Reference struct {
Type Type
*KubeReference
*apis.URL
}
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -139,18 +150,18 @@ 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)
}
return repr
}
return fmt.Errorf("%w: unexpected type %q",
ErrSinkIsInvalid, r.Type).Error()
ErrSinkIsInvalid, r.Type()).Error()
}

// GvrAsText returns the
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -220,25 +228,41 @@ 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
// the type by convention.
// 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
Expand All @@ -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(
Expand Down

0 comments on commit d5d174c

Please sign in to comment.