-
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
13 changed files
with
1,013 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,132 @@ | ||
/* | ||
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 | ||
keysOnly bool | ||
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 | ||
} | ||
} | ||
|
||
// WithKeysOnly sets the keys only for the target. | ||
func WithKeysOnly() OpOption { | ||
return func(o *Op) { | ||
o.keysOnly = true | ||
} | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.