Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added completion for domain command #1562

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions pkg/kn/commands/completion_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ type completionConfig struct {

var (
resourceToFuncMap = map[string]func(config *completionConfig) []string{
"service": completeService,
"revision": completeRevision,
"broker": completeBroker,
"domain": completeDomain,
"revision": completeRevision,
"route": completeRoute,
"service": completeService,
}
)

Expand All @@ -43,7 +44,7 @@ func ResourceNameCompletionFunc(p *KnParams) func(cmd *cobra.Command, args []str

var use string
if cmd.Parent() != nil {
use = cmd.Parent().Use
use = cmd.Parent().Name()
}
config := completionConfig{
p,
Expand Down Expand Up @@ -204,3 +205,29 @@ func completeRoute(config *completionConfig) (suggestions []string) {
}
return
}

func completeDomain(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}
client, err := config.params.NewServingV1alpha1Client(namespace)
if err != nil {
return
}
domainMappingList, err := client.ListDomainMappings(config.command.Context())
if err != nil {
return
}
for _, sug := range domainMappingList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}
114 changes: 113 additions & 1 deletion pkg/kn/commands/completion_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ import (
"gotest.tools/v3/assert"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientv1alpha1 "knative.dev/client/pkg/serving/v1alpha1"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
clienttesting "k8s.io/client-go/testing"
clienteventingv1 "knative.dev/client/pkg/eventing/v1"
v1 "knative.dev/client/pkg/serving/v1"

eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
"knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1/fake"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
servingv1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1/fake"
servingv1alpha1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake"
)

type testType struct {
Expand Down Expand Up @@ -74,7 +79,8 @@ var (
}
testNsServices = []servingv1.Service{testSvc1, testSvc2, testSvc3}

fakeServing = &servingv1fake.FakeServingV1{Fake: &clienttesting.Fake{}}
fakeServing = &servingv1fake.FakeServingV1{Fake: &clienttesting.Fake{}}
fakeServingAlpha = &servingv1alpha1fake.FakeServingV1alpha1{Fake: &clienttesting.Fake{}}
)

var (
Expand Down Expand Up @@ -154,6 +160,31 @@ var (
testNsRoutes = []servingv1.Route{testRoute1, testRoute2, testRoute3}
)

var (
testDomain1 = v1alpha1.DomainMapping{
TypeMeta: metav1.TypeMeta{
Kind: "DomainMapping",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-domain-1", Namespace: testNs},
}
testDomain2 = v1alpha1.DomainMapping{
TypeMeta: metav1.TypeMeta{
Kind: "DomainMapping",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-domain-2", Namespace: testNs},
}
testDomain3 = v1alpha1.DomainMapping{
TypeMeta: metav1.TypeMeta{
Kind: "DomainMapping",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-domain-3", Namespace: testNs},
}
testNsDomains = []v1alpha1.DomainMapping{testDomain1, testDomain2, testDomain3}
)

var knParams = initialiseKnParams()

func initialiseKnParams() *KnParams {
Expand All @@ -167,6 +198,9 @@ func initialiseKnParams() *KnParams {
NewEventingClient: func(namespace string) (clienteventingv1.KnEventingClient, error) {
return clienteventingv1.NewKnEventingClient(fakeEventing, namespace), nil
},
NewServingV1alpha1Client: func(namespace string) (clientv1alpha1.KnServingClient, error) {
return clientv1alpha1.NewKnServingClient(fakeServingAlpha, namespace), nil
},
}
}

Expand Down Expand Up @@ -569,6 +603,84 @@ func TestResourceNameCompletionFuncRoute(t *testing.T) {
}
}

func TestResourceNameCompletionFuncDomain(t *testing.T) {
completionFunc := ResourceNameCompletionFunc(knParams)

fakeServingAlpha.AddReactor("list", "domainmappings",
func(a clienttesting.Action) (bool, runtime.Object, error) {
if a.GetNamespace() == errorNs {
return true, nil, errors.NewInternalError(fmt.Errorf("unable to list services"))
}
return true, &v1alpha1.DomainMappingList{Items: testNsDomains}, nil
})

tests := []testType{
{
"Empty suggestions when non-zero args",
testNs,
knParams,
[]string{"xyz"},
"",
"domain",
},
{
"Empty suggestions when no namespace flag",
"",
knParams,
nil,
"",
"domain",
},
{
"Suggestions when test-ns namespace set",
testNs,
knParams,
nil,
"",
"domain",
},
{
"Empty suggestions when toComplete is not a prefix",
testNs,
knParams,
nil,
"xyz",
"domain",
},
{
"Empty suggestions when error during list operation",
errorNs,
knParams,
nil,
"",
"domain",
},
}
for _, tt := range tests {
cmd := getResourceCommandWithTestSubcommand(tt.resource, tt.namespace != "", tt.resource != "no-parent")
t.Run(tt.name, func(t *testing.T) {
config := &completionConfig{
params: tt.p,
command: cmd,
args: tt.args,
toComplete: tt.toComplete,
}
expectedFunc := resourceToFuncMap[tt.resource]
if expectedFunc == nil {
expectedFunc = func(config *completionConfig) []string {
return []string{}
}
}
cmd.Flags().Set("namespace", tt.namespace)
actualSuggestions, actualDirective := completionFunc(cmd, tt.args, tt.toComplete)
expectedSuggestions := expectedFunc(config)
expectedDirective := cobra.ShellCompDirectiveNoFileComp
assert.DeepEqual(t, actualSuggestions, expectedSuggestions)
assert.Equal(t, actualDirective, expectedDirective)
})
}
}

func getResourceCommandWithTestSubcommand(resource string, addNamespace, addSubcommand bool) *cobra.Command {
testCommand := &cobra.Command{
Use: resource,
Expand Down
1 change: 1 addition & 0 deletions pkg/kn/commands/domain/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewDomainMappingDeleteCommand(p *commands.KnParams) *cobra.Command {
Example: `
# Delete domain mappings 'hello.example.com'
kn domain delete hello.example.com`,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("'kn domain delete' requires the domain name given as single argument")
Expand Down
1 change: 1 addition & 0 deletions pkg/kn/commands/domain/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewDomainMappingDescribeCommand(p *commands.KnParams) *cobra.Command {
Example: `
# Show details of for the domain 'hello.example.com'
kn domain describe hello.example.com`,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("'kn domain describe' requires name of the domain mapping as single argument")
Expand Down
1 change: 1 addition & 0 deletions pkg/kn/commands/domain/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewDomainMappingUpdateCommand(p *commands.KnParams) *cobra.Command {
Example: `
# Update a domain mappings 'hello.example.com' for Knative service 'hello'
kn domain update hello.example.com --refFlags hello`,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("'kn domain create' requires the domain name given as single argument")
Expand Down