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

Add augerctl get #82

Merged
merged 1 commit into from
Aug 14, 2024
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
136 changes: 136 additions & 0 deletions augerctl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# augerctl

`augerctl` is a command line client for [Kubernetes][kubernetes] specific [etcd][etcd],
and as close as possible to [kubectl][kubectl].
It can be used in scripts or for administrators to explore an etcd cluster.

## Getting augerctl

The latest release is not yet available as a binary on [Github][github-release],
the next release will be available.

so that it can be built from source.

``` bash
git clone https://github.com/etcd-io/auger
cd auger
go install ./augerctl
```

or

``` bash
go install github.com/etcd-io/auger/augerctl@main
```

and the binary will be available in the path `$GOBIN` or `$GOPATH/bin`

## Configuration

### --endpoints
+ gRPC endpoints of etcd cluster
+ default: `"http://127.0.0.1:2379"`

### --cert
+ path to the etcd client TLS cert file
+ default: none

### --key
+ path to the etcd client TLS key file
+ default: none

### --cacert
+ path to the etcd client TLS CA cert file
+ default: none

### --user
+ username for authentication, provide username[:password]
+ default: none

### --password
+ password for authentication, only available if --user has no password
+ default: none

## Usage

### Setting a resource

TODO

### Retrieving a resource

List a single service with namespace `default` and name `kubernetes`

``` bash
augerctl get services -n default kubernetes

# Nearly equivalent
kubectl get services -n default kubernetes -o yaml
```

List a single resource of type `priorityclasses` and name `system-node-critical` without namespaced

``` bash
augerctl get priorityclasses system-node-critical

# Nearly equivalent
kubectl get priorityclasses system-node-critical -o yaml
```

List all leases with namespace `kube-system`

``` bash
augerctl get leases -n kube-system

# Nearly equivalent
kubectl get leases -n kube-system -o yaml
```

List a single resource of type `apiservices.apiregistration.k8s.io` and name `v1.apps`

``` bash
augerctl get apiservices.apiregistration.k8s.io v1.apps

# Nearly equivalent
kubectl get apiservices.apiregistration.k8s.io v1.apps -o yaml
```

List all resources

``` bash
augerctl get

# Nearly equivalent
kubectl get $(kubectl api-resources --verbs=list --output=name | paste -s -d, - ) -A -o yaml
```

### Deleting a resource

TODO

### Watching for changes

TODO

## Endpoint

If the etcd cluster isn't available on `http://127.0.0.1:2379`, specify a `--endpoints` flag.

## Project Details

### Versioning

augerctl uses [semantic versioning][semver].
Releases will follow with the [Kubernetes][kubernetes] release cycle as possible (need API updates),
but the version numbers will be not.

### License

augerctl is under the Apache 2.0 license. See the [LICENSE][license] file for details.

[kubernetes]: https://kubernetes.io/
[kubectl]: https://kubectl.sigs.k8s.io/
[etcd]: https://github.com/etcd-io/etcd
[github-release]: https://github.com/etcd-io/auger/releases/
[license]: ../LICENSE
[semver]: http://semver.org/
59 changes: 59 additions & 0 deletions augerctl/command/ctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package command is a simple command line client for directly accessing data objects stored in etcd by Kubernetes.
package command

import (
"github.com/spf13/cobra"

"go.etcd.io/etcd/client/pkg/v3/transport"
)

type flagpole struct {
Endpoints []string

InsecureSkipVerify bool
InsecureDiscovery bool
TLS transport.TLSInfo

User string
Password string
}

// NewCtlCommand returns a new cobra.Command for use ctl
func NewCtlCommand() *cobra.Command {
flags := &flagpole{}

cmd := &cobra.Command{
Use: "augerctl",
Short: "A simple command line client for directly accessing data objects stored in etcd by Kubernetes.",
}
cmd.PersistentFlags().StringSliceVar(&flags.Endpoints, "endpoints", []string{"127.0.0.1:2379"}, "gRPC endpoints of etcd cluster")

cmd.PersistentFlags().BoolVar(&flags.InsecureDiscovery, "insecure-discovery", true, "accept insecure SRV records describing cluster endpoints")
cmd.PersistentFlags().BoolVar(&flags.InsecureSkipVerify, "insecure-skip-tls-verify", false, "skip server certificate verification")
cmd.PersistentFlags().StringVar(&flags.TLS.CertFile, "cert", "", "path to the etcd client TLS cert file")
cmd.PersistentFlags().StringVar(&flags.TLS.KeyFile, "key", "", "path to the etcd client TLS key file")
cmd.PersistentFlags().StringVar(&flags.TLS.TrustedCAFile, "cacert", "", "path to the etcd client TLS CA cert file")
cmd.PersistentFlags().StringVar(&flags.User, "user", "", "username for authentication, provide username[:password]")
cmd.PersistentFlags().StringVar(&flags.Password, "password", "", "password for authentication, only available if --user has no password")

cmd.AddCommand(
newCtlGetCommand(flags),
)
return cmd
}
137 changes: 137 additions & 0 deletions augerctl/command/get_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import (
"context"
"fmt"
"os"

"github.com/etcd-io/auger/pkg/client"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type getFlagpole struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be an option to limit the number of results?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, but kubectl doesn't have this yet, maybe we should discuss this after the PR merge.

Namespace string
Output string
ChunkSize int64
Prefix string
}

var (
getExample = `
wzshiming marked this conversation as resolved.
Show resolved Hide resolved
# List a single service with namespace "default" and name "kubernetes"
augerctl get services -n default kubernetes
# Nearly equivalent
kubectl get services -n default kubernetes -o yaml

# List a single resource of type "priorityclasses" and name "system-node-critical" without namespaced
augerctl get priorityclasses system-node-critical
# Nearly equivalent
kubectl get priorityclasses system-node-critical -A -o yaml

# List all leases with namespace "kube-system"
augerctl get leases -n kube-system
# Nearly equivalent
kubectl get leases -n kube-system -o yaml

# List a single resource of type "apiservices.apiregistration.k8s.io" and name "v1.apps"
augerctl get apiservices.apiregistration.k8s.io v1.apps
# Nearly equivalent
kubectl get apiservices.apiregistration.k8s.io v1.apps -o yaml

# List all resources
augerctl get
# Nearly equivalent
kubectl get $(kubectl api-resources --verbs=list --output=name | paste -s -d, - ) -A -o yaml
`
)

func newCtlGetCommand(f *flagpole) *cobra.Command {
flags := &getFlagpole{}

cmd := &cobra.Command{
Args: cobra.RangeArgs(0, 2),
Use: "get [resource] [name]",
Short: "Gets the resource of Kubernetes in etcd",
Example: getExample,
RunE: func(cmd *cobra.Command, args []string) error {
etcdclient, err := clientFromCmd(f)
if err != nil {
return err
}
err = getCommand(cmd.Context(), etcdclient, flags, args)

if err != nil {
return fmt.Errorf("%v: %w", args, err)
}
return nil
},
}

cmd.Flags().StringVarP(&flags.Output, "output", "o", "yaml", "output format. One of: (yaml).")
cmd.Flags().StringVarP(&flags.Namespace, "namespace", "n", "", "namespace of resource")
cmd.Flags().Int64Var(&flags.ChunkSize, "chunk-size", 500, "chunk size of the list pager")
cmd.Flags().StringVar(&flags.Prefix, "prefix", "/registry", "prefix to prepend to the resource")

return cmd
}

func getCommand(ctx context.Context, etcdclient client.Client, flags *getFlagpole, args []string) error {
var targetGr schema.GroupResource
var targetName string
var targetNamespace string
if len(args) != 0 {
// TODO: Support get information from CRD and scheme.Codecs
// Support short name
// Check for namespaced

gr := schema.ParseGroupResource(args[0])
if gr.Empty() {
return fmt.Errorf("invalid resource %q", args[0])
}
targetGr = gr
targetNamespace = flags.Namespace
if len(args) >= 2 {
targetName = args[1]
}
}

printer := NewPrinter(os.Stdout, flags.Output)
if printer == nil {
return fmt.Errorf("invalid output format: %q", flags.Output)
}

opOpts := []client.OpOption{
client.WithName(targetName, targetNamespace),
client.WithGroupResource(targetGr),
client.WithChunkSize(flags.ChunkSize),
client.WithResponse(printer.Print),
}

// TODO: Support watch

_, err := etcdclient.Get(ctx, flags.Prefix,
opOpts...,
)
if err != nil {
return err
}

return nil
}
Loading