Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakaviani committed Aug 12, 2019
0 parents commit 6f939b7
Show file tree
Hide file tree
Showing 40 changed files with 2,351 additions and 0 deletions.
1 change: 1 addition & 0 deletions .envrc
@@ -0,0 +1 @@
export GO111MODULE=on
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
go.sum
kni*
3 changes: 3 additions & 0 deletions Makefile
@@ -0,0 +1,3 @@
test-integration:
./hack/build.sh
./hack/test.sh
67 changes: 67 additions & 0 deletions README.md
@@ -0,0 +1,67 @@
# Knative Inspect

A light-weight debugging tool for Knative's system components which is heavily
influenced by [github.com/k14s/kapp](https://github.com/k14s/kapp). It assumes
Kubernetes as the backend for Knative.

```
Usage:
kni [flags]
kni [command]
Available Commands:
config-changes Changes to Knative ConfigMaps
help Help about any command
inspect Inspect Knative services
kube-version Print Kubernetes version
logs Knative Component Logs
version Print version
Flags:
-h, --help help for kni
```

## Inspect Services

```
± |master ✓ | → ./kni inspect -s goapp
Resources in goapp
Namespace Name Kind Ready Reason
default goapp Service True
default goapp L Configuration True
default goapp-first | L Revision True
default goapp-first-deployment | | L Deployment -
default goapp-first-deployment-6466c9b6f6 | | | L ReplicaSet -
default goapp-first-cache | | L Image -
default goapp-first | | L PodAutoscaler False NoTraffic
default goapp-first | | L ServerlessService Unknown NoHealthyBackends
default goapp-first | | | L Endpoints -
default goapp-first | | | L Service -
default goapp-first-rctct | | | L Service -
default goapp-first-5d4rn | | L Service -
default goapp L Route True
```

## Inspect Changes in ConfigMaps
```diff
± |master ✓ | → ./kni config-changes
Changes in Knative ConfigMaps

* config-defaults
- *.MaxRevisionTimeoutSeconds(1) 600
+ *.MaxRevisionTimeoutSeconds(1) 660

* config-domain
- *.Domains(0){example.com}
+ *.Domains(0){nk-eirini-new5.us-south.containers.appdomain.cloud} &{map[]}
```

## Component Logs

```
± |master ✓ | → ./kni logs -p activator --lines 100 --filter error
2019/08/07 14:07:02 activator-64cb466d55-bmb57 > activator | {"level":"error","ts":"2019-08-07T06:26:03.757Z","logger":"activator","caller":"activator/throttler.go:253","msg":"updating capacity failed","knative.dev/controller":"activator","knative.dev/key":"kni-test/testapp-k5qjs","error":"revision.serving.knative.dev \"testapp-k5qjs\" not found","stacktrace":"github.com/knative/serving/pkg/activator.(*Throttler).endpointsUpdated\n\t/home/prow/go/src/github.com/knative/serving/pkg/activator/throttler.go:253\ngithub.com/knative/serving/vendor/github.com/knative/pkg/controller.PassNew.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/github.com/knative/pkg/controller/controller.go:69\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.ResourceEventHandlerFuncs.OnUpdate\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/controller.go:202\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.FilteringResourceEventHandler.OnUpdate\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/controller.go:236\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run.func1.1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:552\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.ExponentialBackoff\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:203\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:548\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:133\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.JitterUntil\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:134\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.Until\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:88\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:546\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.(*Group).Start.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:71"}
2019/08/07 14:07:02 activator-64cb466d55-bmb57 > activator | {"level":"error","ts":"2019-08-07T06:26:41.988Z","logger":"activator","caller":"activator/throttler.go:253","msg":"updating capacity failed","knative.dev/controller":"activator","knative.dev/key":"kni-test/testapp-p8mc5","error":"revision.serving.knative.dev \"testapp-p8mc5\" not found","stacktrace":"github.com/knative/serving/pkg/activator.(*Throttler).endpointsUpdated\n\t/home/prow/go/src/github.com/knative/serving/pkg/activator/throttler.go:253\ngithub.com/knative/serving/vendor/github.com/knative/pkg/controller.PassNew.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/github.com/knative/pkg/controller/controller.go:69\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.ResourceEventHandlerFuncs.OnUpdate\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/controller.go:202\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.FilteringResourceEventHandler.OnUpdate\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/controller.go:236\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run.func1.1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:552\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.ExponentialBackoff\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:203\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:548\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:133\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.JitterUntil\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:134\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.Until\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:88\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:546\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.(*Group).Start.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:71"}
2019/08/07 14:07:02 activator-64cb466d55-bmb57 > activator | {"level":"error","ts":"2019-08-07T06:26:42.018Z","logger":"activator","caller":"activator/throttler.go:253","msg":"updating capacity failed","knative.dev/controller":"activator","knative.dev/key":"kni-test/testapp-p8mc5","error":"revision.serving.knative.dev \"testapp-p8mc5\" not found","stacktrace":"github.com/knative/serving/pkg/activator.(*Throttler).endpointsUpdated\n\t/home/prow/go/src/github.com/knative/serving/pkg/activator/throttler.go:253\ngithub.com/knative/serving/vendor/github.com/knative/pkg/controller.PassNew.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/github.com/knative/pkg/controller/controller.go:69\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.ResourceEventHandlerFuncs.OnUpdate\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/controller.go:202\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.FilteringResourceEventHandler.OnUpdate\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/controller.go:236\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run.func1.1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:552\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.ExponentialBackoff\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:203\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:548\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:133\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.JitterUntil\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:134\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.Until\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:88\ngithub.com/knative/serving/vendor/k8s.io/client-go/tools/cache.(*processorListener).run\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/client-go/tools/cache/shared_informer.go:546\ngithub.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait.(*Group).Start.func1\n\t/home/prow/go/src/github.com/knative/serving/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:71"}
```
28 changes: 28 additions & 0 deletions cmd/main.go
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"math/rand"
"os"
"time"

"github.com/cppforlife/go-cli-ui/ui"
"github.com/nimakaviani/knative-inspect/pkg/cmd"

_ "k8s.io/client-go/plugin/pkg/client/auth"
)

func main() {
rand.Seed(time.Now().UTC().UnixNano())

confUI := ui.NewConfUI(ui.NewNoopLogger())
defer confUI.Flush()

command := cmd.NewDefaultCmd(confUI)

err := command.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
57 changes: 57 additions & 0 deletions go.mod
@@ -0,0 +1,57 @@
module github.com/nimakaviani/knative-inspect

go 1.12

require (
bitbucket.org/ww/goautoneg v0.0.0-20120707110453-75cd24fc2f2c // indirect
contrib.go.opencensus.io/exporter/prometheus v0.1.0 // indirect
contrib.go.opencensus.io/exporter/stackdriver v0.12.4 // indirect
github.com/Azure/go-autorest v10.14.0+incompatible // indirect
github.com/cppforlife/cobrautil v0.0.0-20180924214100-a39a1714c920
github.com/cppforlife/go-cli-ui v0.0.0-20181113222104-5a69326440e8
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/emicklei/go-restful v2.9.6+incompatible // indirect
github.com/evanphx/json-patch v4.5.0+incompatible // indirect
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.0 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/google/go-containerregistry v0.0.0-20190729175742-ef12d49c8daf // indirect
github.com/google/gofuzz v1.0.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/googleapis/gnostic v0.3.0 // indirect
github.com/gophercloud/gophercloud v0.3.0 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/jetstack/cert-manager v0.6.1 // indirect
github.com/k14s/kapp v0.10.0 // indirect
github.com/kr/pty v1.1.8 // indirect
github.com/kubernetes-incubator/custom-metrics-apiserver v0.0.0-20190116221620-b7016fc85e1c // indirect
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/nimakaviani/kapp v0.0.0-20190807210239-9460cd5b6da8
github.com/nimakaviani/ytt v0.1.0
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/prometheus/common v0.6.0 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/vito/go-interact v1.0.0 // indirect
github.com/walmartlabs/object-diff v0.0.0-20190802100942-57273a9bdebb
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.10.0 // indirect
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/api v0.0.0-20190528110122-9ad12a4af326
k8s.io/apimachinery v0.0.0-20190221084156-01f179d85dbc
k8s.io/apiserver v0.0.0-20190528110248-2d60c3dee270 // indirect
k8s.io/client-go v0.0.0-20190528110200-4f3abb12cae2
k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30 // indirect
k8s.io/metrics v0.0.0-20190528110627-05eb8901940c // indirect
knative.dev/pkg v0.0.0-20190802214846-8a10634b4fa4
knative.dev/serving v0.7.1-0.20190804133254-e4f3cc382140
)
11 changes: 11 additions & 0 deletions hack/build-binaries.sh
@@ -0,0 +1,11 @@
#!/bin/bash

set -e -x -u

BUILD_VALUES= ./hack/build.sh

GOOS=darwin GOARCH=amd64 go build -o kni-darwin-amd64 ./cmd/...
GOOS=linux GOARCH=amd64 go build -o kni-linux-amd64 ./cmd/...
GOOS=windows GOARCH=amd64 go build -o kni-windows-amd64.exe ./cmd/...

shasum -a 256 ./kni-*-amd64*
9 changes: 9 additions & 0 deletions hack/build.sh
@@ -0,0 +1,9 @@
#!/bin/bash

set -e -x -u

go fmt ./cmd/... ./pkg/...

# build without website assets
go build -o kni ./cmd/...
./kni version
14 changes: 14 additions & 0 deletions hack/test.sh
@@ -0,0 +1,14 @@
#!/bin/bash

set -e -u

export PATH=$PATH:$PWD

set -x

export KNI_E2E_NAMESPACE=${1:-kni-test}

go fmt ./tests/...
go test ./tests/ -timeout 60m -test.v $@

echo E2E SUCCESS
114 changes: 114 additions & 0 deletions pkg/cmd/cmd.go
@@ -0,0 +1,114 @@
package cmd

import (
"fmt"
"io"
"strings"

"github.com/cppforlife/cobrautil"
"github.com/cppforlife/go-cli-ui/ui"
core "github.com/nimakaviani/knative-inspect/pkg/cmd/core"
"github.com/spf13/cobra"
)

func NewDefaultCmd(ui *ui.ConfUI) *cobra.Command {
config := core.NewConfig()
deps := core.NewDeps(config)

cmd := &cobra.Command{
Use: "kni",
Short: "Knative Inspect - little debugging tool for Knative services",

RunE: cobrautil.ShowHelp,

// Affects children as well
SilenceErrors: true,
SilenceUsage: true,

// Disable docs header
DisableAutoGenTag: true,

// TODO bash completion
}

cmd.SetOutput(uiBlockWriter{ui}) // setting output for cmd.Help()

// TODO bash completion
cmd.AddCommand(NewInspectCmd(NewInspectCmdOptions(ui, config, deps)))
cmd.AddCommand(NewConfigChangeCmd(NewConfigChangeOptions(ui, config, deps)))
cmd.AddCommand(NewKnativeLogsCmd(NewKnativeLogOptions(ui, config, deps)))
cmd.AddCommand(NewKubeVersionCmd(NewKubeVersionOptions(ui, config, deps)))
cmd.AddCommand(NewVersionCmd(NewVersionOptions(ui)))

// Last one runs first
cobrautil.VisitCommands(cmd, reconfigureCmdWithSubcmd)
cobrautil.VisitCommands(cmd, reconfigureLeafCmd)
cobrautil.VisitCommands(cmd, cobrautil.WrapRunEForCmd(cobrautil.ResolveFlagsForCmd))

return cmd
}

func reconfigureCmdWithSubcmd(cmd *cobra.Command) {
if len(cmd.Commands()) == 0 {
return
}

if cmd.Args == nil {
cmd.Args = cobra.ArbitraryArgs
}
if cmd.RunE == nil {
cmd.RunE = ShowSubcommands
}

var strs []string
for _, subcmd := range cmd.Commands() {
strs = append(strs, subcmd.Use)
}

cmd.Short += " (" + strings.Join(strs, ", ") + ")"
}

func reconfigureLeafCmd(cmd *cobra.Command) {
if len(cmd.Commands()) > 0 {
return
}

if cmd.RunE == nil {
panic(fmt.Sprintf("Internal: Command '%s' does not set RunE", cmd.CommandPath()))
}

if cmd.Args == nil {
origRunE := cmd.RunE
cmd.RunE = func(cmd2 *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("command '%s' does not accept extra arguments '%s'", args[0], cmd2.CommandPath())
}
return origRunE(cmd2, args)
}
cmd.Args = cobra.ArbitraryArgs
}
}

func ShowSubcommands(cmd *cobra.Command, args []string) error {
var strs []string
for _, subcmd := range cmd.Commands() {
strs = append(strs, subcmd.Use)
}
return fmt.Errorf("Use one of available subcommands: %s", strings.Join(strs, ", "))
}

func ShowHelp(cmd *cobra.Command, args []string) error {
cmd.Help()
return fmt.Errorf("Invalid command - see available commands/subcommands above")
}

type uiBlockWriter struct {
ui ui.UI
}

var _ io.Writer = uiBlockWriter{}

func (w uiBlockWriter) Write(p []byte) (n int, err error) {
w.ui.PrintBlock(p)
return len(p), nil
}
80 changes: 80 additions & 0 deletions pkg/cmd/config_changes.go
@@ -0,0 +1,80 @@
package cmd

import (
"time"

"github.com/cppforlife/go-cli-ui/ui"
"github.com/nimakaviani/knative-inspect/pkg/cmd/core"
"github.com/nimakaviani/knative-inspect/pkg/cmd/flags"
kiui "github.com/nimakaviani/knative-inspect/pkg/cmd/ui"
cfg "github.com/nimakaviani/knative-inspect/pkg/inspect/config"
"github.com/spf13/cobra"
)

const (
servingNamespace = "knative-serving"
)

type ConfigChangeOptions struct {
ui ui.UI

Debug bool
opts cfg.ConfigMapChangeOpts

kubeconfigFlags flags.KubeconfigFlags
namespaceFlags flags.NamespaceFlags
}

func NewConfigChangeOptions(ui *ui.ConfUI, config core.Config, deps core.Deps) *ConfigChangeOptions {
return &ConfigChangeOptions{
ui: ui,
opts: cfg.ConfigMapChangeOpts{Config: config, Deps: deps},
}
}

func NewConfigChangeCmd(o *ConfigChangeOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "config-changes",
Aliases: []string{"cc"},
Short: "Changes to Knative ConfigMaps",
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
}

cmd.Flags().StringSliceVarP(
&o.opts.ConfigMapNames,
"config", "c",
[]string{},
"Knative config maps to check (can be specified multiple times)",
)

o.kubeconfigFlags.Set(cmd)
o.namespaceFlags.Set(cmd, servingNamespace)

o.opts.Config.ConfigurePathResolver(o.kubeconfigFlags.Path.Value)
o.opts.Config.ConfigureContextResolver(o.kubeconfigFlags.Context.Value)
return cmd
}

func (o *ConfigChangeOptions) Run() error {
ui := core.NewPlainUI(o.Debug)
t1 := time.Now()

defer func() {
ui.Debugf("total: %s\n", time.Since(t1))
}()

return o.changes()
}

func (o *ConfigChangeOptions) changes() error {
// set the name based on whatever comes through from the flag
o.opts.Namespace = o.namespaceFlags.Name

diffs, err := cfg.NewConfigInspector(o.opts).Run()
if err != nil {
return err
}

kiui.DiffView{Source: "Knative ConfigMaps", ChangeSet: diffs}.Print(o.ui)
return nil
}

0 comments on commit 6f939b7

Please sign in to comment.