Skip to content

Commit

Permalink
fix: Remove Kubernetes support in ref
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimansk committed Mar 26, 2021
1 parent 29fb2ec commit c7fbd6d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
2 changes: 1 addition & 1 deletion docs/cmd/kn_domain_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kn domain create NAME
```
-h, --help help for create
-n, --namespace string Specify the namespace to operate in.
--ref string Addressable target reference for Domain Mapping. You can specify a Knative service, a Knative soute or a Kubernetes service.Examples: '--ref ksvc:hello' or simply '--ref hello' for a Knative service 'hello', '--ref kroute:hello' for a Knative route 'hello', '--ref svc:hello' for a Kubernetes service 'hello'. If a prefix is not provided, it is considered as a Knative service.
--ref string Addressable target reference for Domain Mapping. You can specify a Knative service, a Knative soute or a Kubernetes service.Examples: '--ref ksvc:hello' or simply '--ref hello' for a Knative service 'hello', '--ref kroute:hello' for a Knative route 'hello'. If a prefix is not provided, it is considered as a Knative service.
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_domain_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kn domain update NAME
```
-h, --help help for update
-n, --namespace string Specify the namespace to operate in.
--ref string Addressable target reference for Domain Mapping. You can specify a Knative service, a Knative soute or a Kubernetes service.Examples: '--ref ksvc:hello' or simply '--ref hello' for a Knative service 'hello', '--ref kroute:hello' for a Knative route 'hello', '--ref svc:hello' for a Kubernetes service 'hello'. If a prefix is not provided, it is considered as a Knative service.
--ref string Addressable target reference for Domain Mapping. You can specify a Knative service, a Knative soute or a Kubernetes service.Examples: '--ref ksvc:hello' or simply '--ref hello' for a Knative service 'hello', '--ref kroute:hello' for a Knative route 'hello'. If a prefix is not provided, it is considered as a Knative service.
```

### Options inherited from parent commands
Expand Down
34 changes: 18 additions & 16 deletions pkg/kn/commands/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,17 @@ var refMappings = map[string]schema.GroupVersionResource{
Group: "serving.knative.dev",
Version: "v1",
},
"svc": {
Resource: "services",
Group: "",
Version: "v1",
},
}

func (f *RefFlags) Add(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.reference, "ref", "", "")
cmd.Flag("ref").Usage = "Addressable target reference for Domain Mapping. " +
"You can specify a Knative service, a Knative soute or a Kubernetes service." +
"Examples: '--ref ksvc:hello' or simply '--ref hello' for a Knative service 'hello', " +
"'--ref kroute:hello' for a Knative route 'hello', " +
"'--ref svc:hello' for a Kubernetes service 'hello'. " +
"If a prefix is not provided, it is considered as a Knative service."
"Examples: '--ref' ksvc:hello' or simply '--ref hello' for a Knative service 'hello', " +
"'--ref' kroute:hello' for a Knative route 'hello'. " +
"'--ref ksvc:mysvc:mynamespace' for a Knative service 'mysvc' in another namespace 'mynamespace', " +
"If a prefix is not provided, it is considered as a Knative service in the current namespace. " +
"If referring to a Knative service in another namespace, 'ksvc:name:namespace' combination must be provided explicitly."
}

func (f RefFlags) Resolve(knclient clientdynamic.KnDynamicClient, namespace string) (*duckv1.KReference, error) {
Expand All @@ -80,11 +76,14 @@ func (f RefFlags) Resolve(knclient clientdynamic.KnDynamicClient, namespace stri
return nil, nil
}

prefix, name := parseType(f.reference)
prefix, name, refNamespace := parseType(f.reference)
gvr, ok := refMappings[prefix]
if !ok {
return nil, fmt.Errorf("unsupported sink prefix: '%s'", prefix)
}
if refNamespace != "" {
namespace = refNamespace
}
obj, err := client.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return nil, err
Expand All @@ -99,11 +98,14 @@ func (f RefFlags) Resolve(knclient clientdynamic.KnDynamicClient, namespace stri
return result, nil
}

func parseType(ref string) (string, string) {
parts := strings.SplitN(ref, ":", 2)
if len(parts) == 1 {
return "ksvc", parts[0]
} else {
return parts[0], parts[1]
func parseType(ref string) (string, string, string) {
parts := strings.SplitN(ref, ":", 3)
switch {
case len(parts) == 1:
return "ksvc", parts[0], ""
case len(parts) == 3:
return parts[0], parts[1], parts[2]
default:
return parts[0], parts[1], ""
}
}
50 changes: 30 additions & 20 deletions pkg/kn/commands/domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"bytes"
"testing"

v1 "k8s.io/api/core/v1"

"gotest.tools/v3/assert"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -83,41 +81,53 @@ type resolveCase struct {
}

func TestResolve(t *testing.T) {
mysvc := &servingv1.Service{
myksvc := &servingv1.Service{
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "serving.knative.dev/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "mysvc", Namespace: "default"},
ObjectMeta: metav1.ObjectMeta{Name: "myksvc", Namespace: "default"},
}
myroute := &servingv1.Route{
mykroute := &servingv1.Route{
TypeMeta: metav1.TypeMeta{Kind: "Route", APIVersion: "serving.knative.dev/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "myroute", Namespace: "default"},
ObjectMeta: metav1.ObjectMeta{Name: "mykroute", Namespace: "default"},
}
myksvcInOther := &servingv1.Service{
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "serving.knative.dev/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "myksvc", Namespace: "other"},
}
mykubesvc := &v1.Service{
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "mykubesvc", Namespace: "default"},
mykrouteInOther := &servingv1.Route{
TypeMeta: metav1.TypeMeta{Kind: "Route", APIVersion: "serving.knative.dev/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "mykroute", Namespace: "other"},
}

cases := []resolveCase{
{"mysvc", &duckv1.KReference{Kind: "Service",
// Test 'name' is considered as Knative service
{"myksvc", &duckv1.KReference{Kind: "Service",
APIVersion: "serving.knative.dev/v1",
Namespace: "default",
Name: "mysvc"}, ""},
{"ksvc:mysvc", &duckv1.KReference{Kind: "Service",
Name: "myksvc"}, ""},
// Test 'type:name' format
{"ksvc:myksvc", &duckv1.KReference{Kind: "Service",
APIVersion: "serving.knative.dev/v1",
Namespace: "default",
Name: "mysvc"}, ""},
{"kroute:myroute", &duckv1.KReference{Kind: "Route",
Name: "myksvc"}, ""},
{"kroute:mykroute", &duckv1.KReference{Kind: "Route",
APIVersion: "serving.knative.dev/v1",
Namespace: "default",
Name: "myroute"}, ""},
{"svc:mykubesvc", &duckv1.KReference{Kind: "Service",
APIVersion: "v1",
Namespace: "default",
Name: "mykubesvc"}, ""},
Name: "mykroute"}, ""},
// Test 'type:name:namespace' format
{"ksvc:myksvc:other", &duckv1.KReference{Kind: "Service",
APIVersion: "serving.knative.dev/v1",
Namespace: "other",
Name: "myksvc"}, ""},
{"kroute:mykroute:other", &duckv1.KReference{Kind: "Route",
APIVersion: "serving.knative.dev/v1",
Namespace: "other",
Name: "mykroute"}, ""},

{"k8ssvc:foo", nil, "unsupported sink prefix: 'k8ssvc'"},
{"svc:foo", nil, "unsupported sink prefix: 'svc'"},
{"service:foo", nil, "unsupported sink prefix: 'service'"},
}
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", mysvc, myroute, mykubesvc)
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", myksvc, mykroute, myksvcInOther, mykrouteInOther)
for _, c := range cases {
i := &RefFlags{c.ref}
result, err := i.Resolve(dynamicClient, "default")
Expand Down

0 comments on commit c7fbd6d

Please sign in to comment.