Skip to content

Commit

Permalink
Add augerctl get
Browse files Browse the repository at this point in the history
Signed-off-by: Shiming Zhang <[email protected]>
  • Loading branch information
wzshiming committed Jul 29, 2024
1 parent 52beb8e commit cf2ce74
Show file tree
Hide file tree
Showing 13 changed files with 1,077 additions and 0 deletions.
105 changes: 105 additions & 0 deletions cmd/augerctl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
augerctl
========

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

## Getting augerctl

TODO: The latest release is available as a binary at [Github][github-release] along with etcd.

augerctl can also be built from source using the build script found in the parent directory.

## Configuration

### --endpoint
+ a comma-delimited list of machine addresses in the cluster
+ default: `"http://127.0.0.1:2379"`

### --cert-file
+ identify HTTPS client using this SSL certificate file
+ default: none

### --key-file
+ identify HTTPS client using this SSL key file
+ default: none

### --ca-file
+ verify certificates of HTTPS-enabled servers using this CA bundle
+ default: none

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

### --password
+ provide password
+ default: none

### --output, -o
+ output response in the given format (`yaml`)
+ default: `"yaml"`

## Usage

### Setting Key Values

TODO

### Retrieving a key value

List a single services with namespace "default"

``` bash
augerctl get services -n default kubernetes
```

List a single resource without namespaced

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

List all leases with namespace "kube-system"

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

List all resources

``` bash
augerctl get
```

### Deleting a key

TODO

### Watching for changes

TODO

## Endpoint

If the etcd cluster isn't available on `http://127.0.0.1:2379`, specify a `--endpoint` 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 cmd/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 augerctl is A simple command line client for directly access 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 access data objects stored in etcd by Kubernetes.",
}
cmd.PersistentFlags().StringSliceVar(&flags.Endpoints, "endpoints", []string{"127.0.0.1:2379"}, "gRPC endpoints")

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", "", "identify secure client using this TLS certificate file")
cmd.PersistentFlags().StringVar(&flags.TLS.KeyFile, "key", "", "identify secure client using this TLS key file")
cmd.PersistentFlags().StringVar(&flags.TLS.TrustedCAFile, "cacert", "", "verify certificates of TLS-enabled secure servers using this CA bundle")
cmd.PersistentFlags().StringVar(&flags.User, "user", "", "username for authentication")
cmd.PersistentFlags().StringVar(&flags.Password, "password", "", "password for authentication")

cmd.AddCommand(
newCtlGetCommand(flags),
)
return cmd
}
124 changes: 124 additions & 0 deletions cmd/augerctl/command/get_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
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 {
Namespace string
Output string
ChunkSize int64
Prefix string
}

var (
getExample = `
# List a single services with namespace "default"
augerctl get services -n default kubernetes
# List a single resource without namespaced
augerctl get priorityclasses system-node-critical
# List all leases with namespace "kube-system"
augerctl get leases -n kube-system
# List all resources
augerctl get
`
)

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.WithGR(targetGr),
client.WithPageLimit(flags.ChunkSize),
client.WithResponse(printer.Print),
}

// TODO: Support watch

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

return nil
}
85 changes: 85 additions & 0 deletions cmd/augerctl/command/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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 (
"crypto/tls"

"github.com/etcd-io/auger/pkg/client"

clientv3 "go.etcd.io/etcd/client/v3"
"strings"
)

func clientFromCmd(f *flagpole) (client.Client, error) {
cfg, err := clientConfigFromCmd(f)
if err != nil {
return nil, err
}

cli, err := clientv3.New(cfg)
if err != nil {
return nil, err
}

return client.NewClient(cli), nil
}

func clientConfigFromCmd(f *flagpole) (clientv3.Config, error) {
cfg := clientv3.Config{
Endpoints: f.Endpoints,
}

if !f.TLS.Empty() {
clientTLS, err := f.TLS.ClientConfig()
if err != nil {
return clientv3.Config{}, err
}
cfg.TLS = clientTLS
}

// if key/cert is not given but user wants secure connection, we
// should still setup an empty tls configuration for gRPC to setup
// secure connection.
if cfg.TLS == nil && !f.InsecureDiscovery {
cfg.TLS = &tls.Config{}
}

// If the user wants to skip TLS verification then we should set
// the InsecureSkipVerify flag in tls configuration.
if cfg.TLS != nil && f.InsecureSkipVerify {
cfg.TLS.InsecureSkipVerify = true
}

if f.User != "" {
if f.Password == "" {
splitted := strings.SplitN(f.User, ":", 2)
if len(splitted) < 2 {
cfg.Username = f.User
// TODO:
} else {
cfg.Username = splitted[0]
cfg.Password = splitted[1]
}
} else {
cfg.Username = f.User
cfg.Password = f.Password
}
}

return cfg, nil
}
Loading

0 comments on commit cf2ce74

Please sign in to comment.