-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add augerctl get
Signed-off-by: Shiming Zhang <[email protected]>
Showing
12 changed files
with
918 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
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 main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/etcd-io/auger/pkg/cmd/ctl" | ||
) | ||
|
||
func main() { | ||
if err := ctl.NewCtlCommand().Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 client | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
// Client is an interface that defines the operations that can be performed on an etcd client. | ||
type Client interface { | ||
// Get is a method that retrieves a key-value pair from the etcd server. | ||
// It returns the revision of the key-value pair | ||
Get(ctx context.Context, prefix string, opOpts ...OpOption) (rev int64, err error) | ||
} | ||
|
||
// client is the etcd client. | ||
type client struct { | ||
client *clientv3.Client | ||
} | ||
|
||
// NewClient creates a new etcd client. | ||
func NewClient(cli *clientv3.Client) Client { | ||
return &client{ | ||
client: cli, | ||
} | ||
} | ||
|
||
// op is the option for the operation. | ||
type op struct { | ||
gr schema.GroupResource | ||
name string | ||
namespace string | ||
response func(kv *KeyValue) error | ||
pageLimit int64 | ||
revision int64 | ||
} | ||
|
||
func (o op) getPrefix(prefix string) (string, bool, error) { | ||
var single bool | ||
var arr [4]string | ||
s := arr[:0] | ||
s = append(s, prefix) | ||
|
||
if !o.gr.Empty() { | ||
p, err := PrefixFromGR(o.gr) | ||
if err != nil { | ||
return "", false, err | ||
} | ||
s = append(s, p) | ||
if o.namespace != "" { | ||
s = append(s, o.namespace) | ||
} | ||
if o.name != "" { | ||
s = append(s, o.name) | ||
single = true | ||
} | ||
} | ||
return strings.Join(s, "/"), single, nil | ||
} | ||
|
||
// OpOption is the option for the operation. | ||
type OpOption func(*op) | ||
|
||
// WithGR sets the gr for the target. | ||
func WithGR(gr schema.GroupResource) OpOption { | ||
return func(o *op) { | ||
o.gr = gr | ||
} | ||
} | ||
|
||
// WithName sets the name and namespace for the target. | ||
func WithName(name, namespace string) OpOption { | ||
return func(o *op) { | ||
o.name = name | ||
o.namespace = namespace | ||
} | ||
} | ||
|
||
// WithResponse sets the response callback for the target. | ||
func WithResponse(response func(kv *KeyValue) error) OpOption { | ||
return func(o *op) { | ||
o.response = response | ||
} | ||
} | ||
|
||
// WithPageLimit sets the page limit for the target. | ||
func WithPageLimit(pageLimit int64) OpOption { | ||
return func(o *op) { | ||
o.pageLimit = pageLimit | ||
} | ||
} | ||
|
||
func opOption(opts []OpOption) op { | ||
var opt op | ||
for _, o := range opts { | ||
o(&opt) | ||
} | ||
return opt | ||
} | ||
|
||
// KeyValue is the key-value pair. | ||
type KeyValue struct { | ||
Key []byte | ||
Value []byte | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
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 client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
func (c *client) Get(ctx context.Context, prefix string, opOpts ...OpOption) (rev int64, err error) { | ||
opt := opOption(opOpts) | ||
if opt.response == nil { | ||
return 0, fmt.Errorf("response is required") | ||
} | ||
|
||
prefix, single, err := opt.getPrefix(prefix) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
opts := []clientv3.OpOption{} | ||
|
||
if single || opt.pageLimit == 0 { | ||
if !single { | ||
opts = append(opts, clientv3.WithPrefix()) | ||
} | ||
resp, err := c.client.Get(ctx, prefix, opts...) | ||
if err != nil { | ||
return 0, err | ||
} | ||
for _, kv := range resp.Kvs { | ||
r := &KeyValue{ | ||
Key: kv.Key, | ||
Value: kv.Value, | ||
} | ||
err = opt.response(r) | ||
if err != nil { | ||
return 0, err | ||
} | ||
} | ||
return resp.Header.Revision, nil | ||
} | ||
|
||
opts = append(opts, clientv3.WithLimit(opt.pageLimit)) | ||
if opt.revision != 0 { | ||
rev = opt.revision | ||
opts = append(opts, clientv3.WithRev(rev)) | ||
} | ||
|
||
var key string | ||
if len(prefix) == 0 { | ||
// If len(s.prefix) == 0, we will sync the entire key-value space. | ||
// We then range from the smallest key (0x00) to the end. | ||
opts = append(opts, clientv3.WithFromKey()) | ||
key = "\x00" | ||
} else { | ||
// If len(s.prefix) != 0, we will sync key-value space with given prefix. | ||
// We then range from the prefix to the next prefix if exists. Or we will | ||
// range from the prefix to the end if the next prefix does not exists. | ||
opts = append(opts, clientv3.WithRange(clientv3.GetPrefixRangeEnd(prefix))) | ||
key = prefix | ||
} | ||
|
||
for { | ||
resp, err := c.client.Get(ctx, key, opts...) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
for _, kv := range resp.Kvs { | ||
r := &KeyValue{ | ||
Key: kv.Key, | ||
Value: kv.Value, | ||
} | ||
err = opt.response(r) | ||
if err != nil { | ||
return 0, err | ||
} | ||
} | ||
|
||
if rev == 0 { | ||
rev = resp.Header.Revision | ||
opts = append(opts, clientv3.WithRev(resp.Header.Revision)) | ||
} | ||
|
||
if !resp.More { | ||
break | ||
} | ||
|
||
// move to next key | ||
key = string(append(resp.Kvs[len(resp.Kvs)-1].Key, 0)) | ||
} | ||
|
||
return rev, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
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 client | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/etcd-io/auger/pkg/encoding" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
// specialDefaultResourcePrefixes are prefixes compiled into Kubernetes. | ||
// see k8s.io/kubernetes/pkg/kubeapiserver/default_storage_factory_builder.go | ||
var specialDefaultResourcePrefixes = map[schema.GroupResource]string{ | ||
{Group: "", Resource: "replicationcontrollers"}: "controllers", | ||
{Group: "", Resource: "endpoints"}: "services/endpoints", | ||
{Group: "", Resource: "services"}: "services/specs", | ||
{Group: "", Resource: "nodes"}: "minions", | ||
{Group: "extensions", Resource: "ingresses"}: "ingress", | ||
{Group: "networking.k8s.io", Resource: "ingresses"}: "ingress", | ||
} | ||
|
||
var specialDefaultMediaTypes = map[string]struct{}{ | ||
"apiextensions.k8s.io": {}, | ||
"apiregistration.k8s.io": {}, | ||
} | ||
|
||
// PrefixFromGR returns the prefix of the given GroupResource. | ||
func PrefixFromGR(gr schema.GroupResource) (prefix string, err error) { | ||
groupPrefix := false | ||
|
||
if _, ok := specialDefaultMediaTypes[gr.Group]; ok { | ||
groupPrefix = true | ||
} else if !strings.Contains(gr.Group, ".") || strings.HasSuffix(gr.Group, ".k8s.io") { | ||
// custom resources | ||
groupPrefix = false | ||
} else { | ||
// builtin resource | ||
groupPrefix = true | ||
} | ||
|
||
if prefix, ok := specialDefaultResourcePrefixes[gr]; ok { | ||
return prefix, nil | ||
} | ||
|
||
if groupPrefix { | ||
return gr.Group + "/" + gr.Resource, nil | ||
} | ||
|
||
return gr.Resource, nil | ||
} | ||
|
||
// MediaTypeFromGR returns the media type of the given GroupResource. | ||
func MediaTypeFromGR(gr schema.GroupResource) (mediaType string, err error) { | ||
if _, ok := specialDefaultMediaTypes[gr.Group]; ok { | ||
return encoding.JsonMediaType, nil | ||
} | ||
|
||
if !strings.Contains(gr.Group, ".") { | ||
return encoding.StorageBinaryMediaType, nil | ||
} | ||
|
||
if strings.HasSuffix(gr.Group, ".k8s.io") { | ||
return encoding.StorageBinaryMediaType, nil | ||
} | ||
|
||
return encoding.JsonMediaType, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/* | ||
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 client | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/etcd-io/auger/pkg/encoding" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
func TestPrefixFromGR(t *testing.T) { | ||
type args struct { | ||
gr schema.GroupResource | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantPrefix string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "pod", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "", | ||
Resource: "pods", | ||
}, | ||
}, | ||
wantPrefix: "pods", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "deployment", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "apps", | ||
Resource: "deployments", | ||
}, | ||
}, | ||
wantPrefix: "deployments", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "service", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "", | ||
Resource: "services", | ||
}, | ||
}, | ||
wantPrefix: "services/specs", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "ingress", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "networking.k8s.io", | ||
Resource: "ingresses", | ||
}, | ||
}, | ||
wantPrefix: "ingress", | ||
}, | ||
{ | ||
name: "apiextensions.k8s.io", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "apiextensions.k8s.io", | ||
Resource: "customresourcedefinitions", | ||
}, | ||
}, | ||
wantPrefix: "apiextensions.k8s.io/customresourcedefinitions", | ||
}, | ||
{ | ||
name: "x-k8s.io", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "auger.x-k8s.io", | ||
Resource: "foo", | ||
}, | ||
}, | ||
wantPrefix: "auger.x-k8s.io/foo", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotPrefix, err := PrefixFromGR(tt.args.gr) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("PrefixFromGR() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if gotPrefix != tt.wantPrefix { | ||
t.Errorf("PrefixFromGR() gotPrefix = %v, want %v", gotPrefix, tt.wantPrefix) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMediaTypeFromGR(t *testing.T) { | ||
type args struct { | ||
gr schema.GroupResource | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantMediaType string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "pod", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "", | ||
Resource: "pods", | ||
}, | ||
}, | ||
wantMediaType: encoding.StorageBinaryMediaType, | ||
}, | ||
{ | ||
name: "deployment", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "apps", | ||
Resource: "deployments", | ||
}, | ||
}, | ||
wantMediaType: encoding.StorageBinaryMediaType, | ||
}, | ||
{ | ||
name: "service", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "", | ||
Resource: "services", | ||
}, | ||
}, | ||
wantMediaType: encoding.StorageBinaryMediaType, | ||
}, | ||
{ | ||
name: "ingress", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "networking.k8s.io", | ||
Resource: "ingresses", | ||
}, | ||
}, | ||
wantMediaType: encoding.StorageBinaryMediaType, | ||
}, | ||
{ | ||
name: "apiextensions.k8s.io", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "apiextensions.k8s.io", | ||
Resource: "customresourcedefinitions", | ||
}, | ||
}, | ||
wantMediaType: encoding.JsonMediaType, | ||
}, | ||
{ | ||
name: "x-k8s.io", | ||
args: args{ | ||
gr: schema.GroupResource{ | ||
Group: "auger.x-k8s.io", | ||
Resource: "foo", | ||
}, | ||
}, | ||
wantMediaType: encoding.JsonMediaType, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotMediaType, err := MediaTypeFromGR(tt.args.gr) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("MediaTypeFromGR() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if gotMediaType != tt.wantMediaType { | ||
t.Errorf("MediaTypeFromGR() gotMediaType = %v, want %v", gotMediaType, tt.wantMediaType) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ctl is A simple command line client for directly access data objects stored in etcd by Kubernetes. | ||
package ctl | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
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 ctl | ||
|
||
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 | ||
} | ||
|
||
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", | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
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 ctl | ||
|
||
import ( | ||
"crypto/tls" | ||
|
||
"github.com/etcd-io/auger/pkg/client" | ||
|
||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
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 | ||
} | ||
|
||
cfg.Username = f.User | ||
cfg.Password = f.Password | ||
|
||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
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 ctl | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/etcd-io/auger/pkg/client" | ||
"github.com/etcd-io/auger/pkg/encoding" | ||
"github.com/etcd-io/auger/pkg/scheme" | ||
) | ||
|
||
type Printer interface { | ||
Print(kv *client.KeyValue) error | ||
} | ||
|
||
func NewPrinter(w io.Writer, printerType string) Printer { | ||
switch printerType { | ||
case "yaml": | ||
return &yamlPrinter{w: w} | ||
} | ||
return nil | ||
} | ||
|
||
func formatResponse(w io.Writer, outMediaType string, kv *client.KeyValue) error { | ||
value := kv.Value | ||
inMediaType, _, err := encoding.DetectAndExtract(value) | ||
if err != nil { | ||
_, err0 := fmt.Fprintf(w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value) | ||
if err0 != nil { | ||
return errors.Join(err, err0) | ||
} | ||
return nil | ||
} | ||
data, _, err := encoding.Convert(scheme.Codecs, inMediaType, outMediaType, value) | ||
if err != nil { | ||
_, err0 := fmt.Fprintf(w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value) | ||
if err0 != nil { | ||
return errors.Join(err, err0) | ||
} | ||
return nil | ||
} | ||
_, err = fmt.Fprintf(w, "---\n# %s | %s\n%s\n", kv.Key, inMediaType, data) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
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 ctl | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/etcd-io/auger/pkg/client" | ||
"github.com/etcd-io/auger/pkg/encoding" | ||
) | ||
|
||
type yamlPrinter struct { | ||
w io.Writer | ||
} | ||
|
||
func (p *yamlPrinter) Print(kv *client.KeyValue) error { | ||
return formatResponse(p.w, encoding.YamlMediaType, kv) | ||
} |