Skip to content

Commit

Permalink
Import gwctl into gateway-api repo
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravkghildiyal committed Sep 25, 2023
1 parent 4e097a8 commit b46cb7a
Show file tree
Hide file tree
Showing 24 changed files with 3,418 additions and 0 deletions.
137 changes: 137 additions & 0 deletions gwctl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# gwctl

gwctl is a tool that improves the usability of the Gateway API by providing a better way to view and manage policies ([GEP-713](https://gateway-api.sigs.k8s.io/geps/gep-713)). The aim is to make it available as a standalone binary, a kubectl plugin, and a library.

gwctl allows you to view all Gateway API policy types that are present in a cluster, as well as all "policy bindings" in a namespace (or across all namespaces). It also shows you the attached policies when you view any Gateway resource (like HTTPRoute, Gateway, GatewayClass, etc.)

gwctl uses the `gateway.networking.k8s.io/policy=true` label to identify Policy CRDs (https://gateway-api.sigs.k8s.io/geps/gep-713/#kubectl-plugin)

Please note that gwctl is <b>still under development and may have bugs</b>. There may be changes at various places, including the command-line interface, the output format, and the supported features.

In the future, gwctl may be able to read status from the policy resource to determine if it has been applied correctly.

## Try it out!

```bash
# Clone the gwctl repository
git clone https://github.com/kubernetes-sigs/gateway-api.git

# Go to the gwctl directory
cd gateway-api

# Ensure vendor depedencies
go mod tidy
go mod vendor

# Build the gwctl binary
go build -o bin/gwctl cmd/gwctl/main.go

# Add binary to PATH
export PATH=./bin:${PATH}

# Start using!
gwctl help
```

## Examples
Here are some examples of how gwctl can be used:

```bash
# List all policies in the cluster. This will also give the resource they bind to.
gwctl get policies -A

# List all available policy types
gwctl get policycrds

# Describe all HTTPRoutes in namespace ns2
gwctl describe httproutes -n ns2

# Describe a single HTTPRoute in default namespace
gwctl describe httproutes demo-httproute-1

# Describe all Gateways across all namespaces.
gwctl describe gateways -A

# Describe a single GatewayClass
gwctl describe gatewayclasses foo-com-external-gateway-class
```

Here are some commands with their sample output:
```bash
❯ gwctl get policies -A
POLICYNAME POLICYKIND TARGETNAME TARGETKIND
demo-timeout-policy-on-gatewayclass TimeoutPolicy foo-com-external-gateway-class GatewayClass
demo-timeout-policy-on-namespace TimeoutPolicy default Namespace
demo-health-check-1 HealthCheckPolicy demo-gateway-1 Gateway
demo-retry-policy-1 RetryOnPolicy demo-gateway-1 Gateway
demo-retry-policy-2 RetryOnPolicy demo-httproute-2 HTTPRoute
demo-tls-min-version-policy-1 TLSMinimumVersionPolicy demo-httproute-1 HTTPRoute
demo-tls-min-version-policy-2 TLSMinimumVersionPolicy demo-gateway-2 Gateway

❯ gwctl describe httproutes -n ns2
Name: demo-httproute-3
Namespace: ns2
Hostnames:
- example.com
ParentRefs:
- group: gateway.networking.k8s.io
kind: Gateway
name: demo-gateway-2
EffectivePolicies:
ns2/demo-gateway-2:
TLSMinimumVersionPolicy.baz.com:
default:
sampleField: hello


Name: demo-httproute-4
Namespace: ns2
Hostnames:
- demo.com
ParentRefs:
- group: gateway.networking.k8s.io
kind: Gateway
name: demo-gateway-1
namespace: default
EffectivePolicies:
default/demo-gateway-1:
HealthCheckPolicy.foo.com:
default:
sampleField: hello
RetryOnPolicy.foo.com:
default:
sampleField: hello
TimeoutPolicy.bar.com:
timeout1: parent
timeout2: child
timeout3: parent
timeout4: child

❯ gwctl describe backends service/demo-svc
Kind: Service
Name: demo-svc
Namespace: default
EffectivePolicies:
default/demo-gateway-1:
HealthCheckPolicy.foo.com:
default:
sampleField: hello
RetryOnPolicy.foo.com:
default:
sampleField: hello
TLSMinimumVersionPolicy.baz.com: {}
TimeoutPolicy.bar.com:
timeout1: parent
timeout2: child
timeout3: parent
timeout4: child
ns2/demo-gateway-2:
TLSMinimumVersionPolicy.baz.com:
default:
sampleField: hello
TimeoutPolicy.bar.com:
timeout1: child
timeout2: child
timeout3: child
timeout4: child
```
72 changes: 72 additions & 0 deletions gwctl/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"context"
_ "embed"
"flag"
"fmt"
"os"
"path"

"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/spf13/cobra"
cobraflag "github.com/spf13/pflag"
"sigs.k8s.io/gateway-api/gwctl/pkg/cmd"
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"sigs.k8s.io/gateway-api/gwctl/pkg/types"
)

func main() {
klog.InitFlags(nil)
flag.Parse()
cobraflag.CommandLine.AddGoFlagSet(flag.CommandLine)

kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig == "" {
kubeconfig = path.Join(os.Getenv("HOME"), ".kube/config")
}

restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(fmt.Sprintf("Failed to get restConfig from BuildConfigFromFlags: %v", err))
}

client, err := client.New(restConfig, client.Options{})
if err != nil {
panic(fmt.Sprintf("Error initializing Kubernetes client: %v", err))
}
gatewayv1alpha2.AddToScheme(client.Scheme())
gatewayv1beta1.AddToScheme(client.Scheme())

dc := dynamic.NewForConfigOrDie(restConfig)

policyManager := policymanager.New(dc)
if err := policyManager.Init(context.Background()); err != nil {
panic(err)
}

params := &types.Params{
Client: client,
DC: dc,
DiscoveryClient: discovery.NewDiscoveryClientForConfigOrDie(restConfig),
PolicyManager: policyManager,
Out: os.Stdout,
}

rootCmd := &cobra.Command{
Use: "gwctl",
}
rootCmd.AddCommand(cmd.NewGetCommand(params))
rootCmd.AddCommand(cmd.NewDescribeCommand(params))

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
58 changes: 58 additions & 0 deletions gwctl/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module sigs.k8s.io/gateway-api/gwctl

go 1.21

require (
github.com/evanphx/json-patch v4.12.0+incompatible
github.com/google/go-cmp v0.5.9
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
k8s.io/api v0.27.3
k8s.io/apiextensions-apiserver v0.27.3
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.3
k8s.io/klog/v2 v2.100.1
k8s.io/utils v0.0.0-20230209194617-a36077c30491
sigs.k8s.io/controller-runtime v0.14.6
sigs.k8s.io/gateway-api v0.7.1
sigs.k8s.io/yaml v1.3.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading

0 comments on commit b46cb7a

Please sign in to comment.