-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shiming Zhang <[email protected]>
- Loading branch information
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/augerctl" | ||
) | ||
|
||
func main() { | ||
if err := augerctl.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,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 augerctl | ||
|
||
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 augerctl | ||
|
||
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 augerctl | ||
|
||
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 | ||
} |
Oops, something went wrong.