Skip to content

Commit

Permalink
Merge branch 'main' into pr/completion-revision
Browse files Browse the repository at this point in the history
  • Loading branch information
vyasgun authored Jan 10, 2022
2 parents be4e23c + c6997da commit 1993a94
Show file tree
Hide file tree
Showing 460 changed files with 31,251 additions and 3,540 deletions.
1 change: 1 addition & 0 deletions .github/workflows/knative-style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jobs:
version: v1.43

- name: Install Tools
if: ${{ always() }}
env:
WOKE_VERSION: v0.13.0
run: |
Expand Down
20 changes: 9 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/emicklei/go-restful v2.15.0+incompatible // indirect
github.com/go-openapi/spec v0.20.2 // indirect
github.com/google/go-cmp v0.5.6
github.com/googleapis/gnostic v0.5.3 // indirect
github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/smartystreets/assertions v1.0.0 // indirect
Expand All @@ -15,17 +14,16 @@ require (
github.com/spf13/viper v1.8.1
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
gotest.tools/v3 v3.0.3
k8s.io/api v0.21.4
k8s.io/apiextensions-apiserver v0.21.4
k8s.io/apimachinery v0.21.4
k8s.io/api v0.22.5
k8s.io/apiextensions-apiserver v0.22.5
k8s.io/apimachinery v0.22.5
k8s.io/cli-runtime v0.21.4
k8s.io/client-go v0.21.4
k8s.io/code-generator v0.21.4
k8s.io/utils v0.0.0-20210111153108-fddb29f9d009 // indirect
knative.dev/eventing v0.28.1-0.20211222204918-d8297456d455
k8s.io/client-go v0.22.5
k8s.io/code-generator v0.22.5
knative.dev/eventing v0.28.1-0.20220107145225-eb4c06c8009d
knative.dev/hack v0.0.0-20211222071919-abd085fc43de
knative.dev/networking v0.0.0-20211223013028-62388a5f2853
knative.dev/pkg v0.0.0-20211216142117-79271798f696
knative.dev/serving v0.28.1-0.20211221064617-c69f92cdfce7
knative.dev/networking v0.0.0-20220107020122-0dbedcd88acf
knative.dev/pkg v0.0.0-20220105211333-96f18522d78d
knative.dev/serving v0.28.1-0.20220107170125-03091748d279
sigs.k8s.io/yaml v1.3.0
)
112 changes: 85 additions & 27 deletions go.sum

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions pkg/kn/commands/broker/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func NewBrokerDeleteCommand(p *commands.KnParams) *cobra.Command {
var waitFlags commands.WaitFlags

cmd := &cobra.Command{
Use: "delete NAME",
Short: "Delete a broker",
Example: deleteExample,
Use: "delete NAME",
Short: "Delete a broker",
Example: deleteExample,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("'broker delete' requires the broker name given as single argument")
Expand Down
7 changes: 4 additions & 3 deletions pkg/kn/commands/broker/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func NewBrokerDescribeCommand(p *commands.KnParams) *cobra.Command {
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")

cmd := &cobra.Command{
Use: "describe NAME",
Short: "Describe broker",
Example: describeExample,
Use: "describe NAME",
Short: "Describe broker",
Example: describeExample,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("'broker describe' requires the broker name given as single argument")
Expand Down
28 changes: 28 additions & 0 deletions pkg/kn/commands/completion_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
resourceToFuncMap = map[string]func(config *completionConfig) []string{
"service": completeService,
"revision": completeRevision,
"broker": completeBroker,
}
)

Expand Down Expand Up @@ -125,6 +126,33 @@ func completeService(config *completionConfig) (suggestions []string) {
return
}


func completeBroker(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.NewEventingClient(namespace)
if err != nil {
return
}
brokerList, err := client.ListBrokers(config.command.Context())
if err != nil {
return
}
for _, sug := range brokerList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}

func completeRevision(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
Expand Down
135 changes: 125 additions & 10 deletions pkg/kn/commands/completion_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ import (
"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"
v12 "knative.dev/serving/pkg/apis/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"
servingv1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1/fake"
)

Expand All @@ -48,39 +51,74 @@ const (
)

var (
testSvc1 = v12.Service{
testSvc1 = servingv1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "serving.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-1", Namespace: testNs},
}
testSvc2 = v12.Service{
testSvc2 = servingv1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "serving.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-2", Namespace: testNs},
}
testSvc3 = v12.Service{
testSvc3 = servingv1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "serving.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-3", Namespace: testNs},
}
testNsServices = []v12.Service{testSvc1, testSvc2, testSvc3}
testNsServices = []servingv1.Service{testSvc1, testSvc2, testSvc3}

fakeServing = &servingv1fake.FakeServingV1{Fake: &clienttesting.Fake{}}
knParams = &KnParams{
)

var (
testBroker1 = eventingv1.Broker{
TypeMeta: metav1.TypeMeta{
Kind: "Broker",
APIVersion: "eventing.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-broker-1", Namespace: testNs},
}
testBroker2 = eventingv1.Broker{
TypeMeta: metav1.TypeMeta{
Kind: "Broker",
APIVersion: "eventing.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-broker-2", Namespace: testNs},
}
testBroker3 = eventingv1.Broker{
TypeMeta: metav1.TypeMeta{
Kind: "Broker",
APIVersion: "eventing.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-broker-3", Namespace: testNs},
}
testNsBrokers = []eventingv1.Broker{testBroker1, testBroker2, testBroker3}

fakeEventing = &fake.FakeEventingV1{Fake: &clienttesting.Fake{}}
)

var knParams = initialiseKnParams()

func initialiseKnParams() *KnParams {
return &KnParams{
NewServingClient: func(namespace string) (v1.KnServingClient, error) {
return v1.NewKnServingClient(fakeServing, namespace), nil
},
NewGitopsServingClient: func(namespace string, dir string) (v1.KnServingClient, error) {
return v1.NewKnServingGitOpsClient(namespace, dir), nil
},
NewEventingClient: func(namespace string) (clienteventingv1.KnEventingClient, error) {
return clienteventingv1.NewKnEventingClient(fakeEventing, namespace), nil
},
}
)
}

var (
testRev1 = v12.Revision{
Expand Down Expand Up @@ -115,7 +153,7 @@ func TestResourceNameCompletionFuncService(t *testing.T) {
if a.GetNamespace() == errorNs {
return true, nil, errors.NewInternalError(fmt.Errorf("unable to list services"))
}
return true, &v12.ServiceList{Items: testNsServices}, nil
return true, &servingv1.ServiceList{Items: testNsServices}, nil
})

tests := []testType{
Expand Down Expand Up @@ -168,6 +206,83 @@ func TestResourceNameCompletionFuncService(t *testing.T) {
"service",
},
}

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 TestResourceNameCompletionFuncBroker(t *testing.T) {
completionFunc := ResourceNameCompletionFunc(knParams)

fakeEventing.AddReactor("list", "brokers", func(action clienttesting.Action) (bool, runtime.Object, error) {
if action.GetNamespace() == errorNs {
return true, nil, errors.NewInternalError(fmt.Errorf("unable to list services"))
}
return true, &eventingv1.BrokerList{Items: testNsBrokers}, nil
})
tests := []testType{
{
"Empty suggestions when non-zero args",
testNs,
knParams,
[]string{"xyz"},
"",
"broker",
},
{
"Empty suggestions when no namespace flag",
"",
knParams,
nil,
"",
"broker",
},
{
"Suggestions when test-ns namespace set",
testNs,
knParams,
nil,
"",
"broker",
},
{
"Empty suggestions when toComplete is not a prefix",
testNs,
knParams,
nil,
"xyz",
"broker",
},
{
"Empty suggestions when error during list operation",
errorNs,
knParams,
nil,
"",
"broker",
},
}
for _, tt := range tests {
cmd := getResourceCommandWithTestSubcommand(tt.resource, tt.namespace != "", tt.resource != "no-parent")
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -376,7 +491,7 @@ func setupTempDir(t *testing.T) string {
err = os.MkdirAll(svcPath, 0700)
assert.NilError(t, err)

for i, testSvc := range []v12.Service{testSvc1, testSvc2, testSvc3} {
for i, testSvc := range []servingv1.Service{testSvc1, testSvc2, testSvc3} {
tempFile, err := os.Create(path.Join(svcPath, fmt.Sprintf("test-svc-%d.yaml", i+1)))
assert.NilError(t, err)
writeToFile(t, testSvc, tempFile)
Expand All @@ -385,7 +500,7 @@ func setupTempDir(t *testing.T) string {
return tempDir
}

func writeToFile(t *testing.T, testSvc v12.Service, tempFile *os.File) {
func writeToFile(t *testing.T, testSvc servingv1.Service, tempFile *os.File) {
yamlPrinter, err := genericclioptions.NewJSONYamlPrintFlags().ToPrinter("yaml")
assert.NilError(t, err)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright 2021 The ANTLR Project

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit 1993a94

Please sign in to comment.