Skip to content

Commit

Permalink
wip: Only get PartialObjectMetadata in mappings GC
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kosiewski committed Oct 18, 2024
1 parent ce8c161 commit c95931f
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 135 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
- id: set-paths-matrix
run: |
set -x
sudo apt-get install -y jq yq
sudo apt-get install -y jq
paths=$(ls -d ./test/e2e*)
echo "matrix=$(printf '%s\n' "${paths}" | jq -R . | jq -cs .)" >> "$GITHUB_OUTPUT"
outputs:
Expand Down Expand Up @@ -268,6 +268,9 @@ jobs:
run: |
sudo apt-get install -y sed
- name: Install yq@v4
run: go install github.com/mikefarah/yq/v4@latest

- name: create vcluster with current cli
run: |
chmod +x ./vcluster-current
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ linters:
- asasalint
- asciicheck
- bidichk
- copyloopvar
- decorder
- dupl
- durationcheck
- errcheck
- errname
- errorlint
- exhaustive
- exportloopref
- ginkgolinter
- gocheckcompilerdirectives
- gofmt
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ signs:
artifacts: checksum

snapshot:
name_template: "{{ incpatch .Version }}-next"
version_template: "{{ incpatch .Version }}-next"

changelog:
use: github
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.release
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ARG KINE_VERSION="v0.13.1"
FROM rancher/kine:${KINE_VERSION} as kine
FROM rancher/kine:${KINE_VERSION} AS kine

# Build the manager binary
FROM alpine:3.20 as builder
FROM alpine:3.20 AS builder

WORKDIR /vcluster-dev

Expand Down
22 changes: 22 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,25 @@ gen-license-report:
go-licenses save --save_path=./licenses --ignore github.com/loft-sh ./...

cp -r ./licenses ./cmd/vclusterctl/cmd/credits

build-dev-image tag="":
TELEMETRY_PRIVATE_KEY="" goreleaser build --snapshot --clean

cp dist/vcluster_linux_$(go env GOARCH | sed s/amd64/amd64_v1/g)/vcluster ./vcluster
docker build -t vcluster:dev-{{tag}} -f Dockerfile.release --build-arg TARGETARCH=$(uname -m) --build-arg TARGETOS=linux .
rm ./vcluster

run-conformance k8s_version="1.31.1" mode="conformance-lite" tag="conf": (build-dev-image tag)
minikube start --kubernetes-version {{k8s_version}} --nodes=2
minikube addons enable metrics-server
minikube image load vcluster:dev-{{tag}}

vcluster create vcluster -n vcluster -f vcluster.yaml

sonobuoy run --mode={{mode}} --level=debug

conformance-status:
sonobuoy status

conformance-logs:
sonobuoy logs
2 changes: 1 addition & 1 deletion cmd/vcluster/cmd/debug/etcd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ExecuteKeys(ctx context.Context, options *KeysOptions) error {
}

// create new etcd backend & list mappings
keyValues, err := etcdClient.List(ctx, options.Prefix, 0)
keyValues, err := etcdClient.List(ctx, options.Prefix)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions hack/schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
"gopkg.in/yaml.v3"
)

const OutFile = "chart/values.schema.json"
const ValuesOutFile = "chart/values.yaml"
const (
OutFile = "chart/values.schema.json"
ValuesOutFile = "chart/values.yaml"
)
const (
defsPrefix = "#/$defs/"
externalConfigName = "ExternalConfig"
Expand Down Expand Up @@ -100,7 +102,6 @@ func addPlatformSchema(toSchema *jsonschema.Schema) error {
}

for pair := platformConfigSchema.Properties.Oldest(); pair != nil; pair = pair.Next() {
pair := pair
platformNode.Properties.AddPairs(*pair)
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/cli/add_vcluster_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func AddVClusterHelm(
var addErrors []error
log.Debugf("trying to add %d vCluster instances to platform", len(vClusters))
for _, vCluster := range vClusters {
vCluster := vCluster
log.Infof("adding %s vCluster to platform", vCluster.Name)
err := addVClusterHelm(ctx, options, globalFlags, vCluster.Name, &vCluster, kubeClient, log)
if err != nil {
Expand Down Expand Up @@ -131,7 +130,6 @@ func addVClusterHelm(

return !lifecycle.IsPaused(vCluster), nil
})

if err != nil {
return fmt.Errorf("error waiting for vCluster to wake up %w", err)
}
Expand Down
119 changes: 41 additions & 78 deletions pkg/etcd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,23 @@ import (
vconfig "github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/constants"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
)

type Value struct {
Key []byte
Data []byte
Revision int64
Key []byte
Data []byte
}

var (
ErrNotFound = errors.New("etcdwrapper: key not found")
)
var ErrNotFound = errors.New("etcdwrapper: key not found")

type Client interface {
List(ctx context.Context, key string, rev int) ([]Value, error)
Watch(ctx context.Context, key string, rev int) clientv3.WatchChan
List(ctx context.Context, key string) ([]Value, error)
Watch(ctx context.Context, key string) clientv3.WatchChan
Get(ctx context.Context, key string) (Value, error)
Put(ctx context.Context, key string, value []byte) error
Create(ctx context.Context, key string, value []byte) error
Update(ctx context.Context, key string, revision int64, value []byte) error
Delete(ctx context.Context, key string, revision int64) error
Compact(ctx context.Context, revision int64) (int64, error)
Delete(ctx context.Context, key string) error
Close() error
}

Expand Down Expand Up @@ -102,106 +97,74 @@ func New(ctx context.Context, certificates *Certificates, endpoints ...string) (
}, nil
}

func (c *client) Watch(ctx context.Context, key string, rev int) clientv3.WatchChan {
return c.c.Watch(ctx, key, clientv3.WithPrefix(), clientv3.WithRev(int64(rev)))
func (c *client) Watch(ctx context.Context, key string) clientv3.WatchChan {
return c.c.Watch(ctx, key, clientv3.WithPrefix(), clientv3.WithPrevKV(), clientv3.WithProgressNotify())
}

func (c *client) List(ctx context.Context, key string, rev int) ([]Value, error) {
resp, err := c.c.Get(ctx, key, clientv3.WithPrefix(), clientv3.WithRev(int64(rev)))
func (c *client) List(ctx context.Context, key string) ([]Value, error) {
resp, err := c.c.Get(ctx, key, clientv3.WithPrefix(), clientv3.WithRev(0))
if err != nil {
return nil, err
}

var vals []Value
for _, kv := range resp.Kvs {
vals = append(vals, Value{
Key: kv.Key,
Data: kv.Value,
Revision: kv.ModRevision,
Key: kv.Key,
Data: kv.Value,
})
}

return vals, nil
}

func (c *client) Get(ctx context.Context, key string) (Value, error) {
resp, err := c.c.Get(ctx, key)
resp, err := c.c.Get(ctx, key, clientv3.WithRev(0))
if err != nil {
return Value{}, err
return Value{}, fmt.Errorf("etcd get: %w", err)
}

if len(resp.Kvs) == 0 {
return Value{}, ErrNotFound
}

if len(resp.Kvs) == 1 {
return Value{
Key: resp.Kvs[0].Key,
Data: resp.Kvs[0].Value,
Revision: resp.Kvs[0].ModRevision,
Key: resp.Kvs[0].Key,
Data: resp.Kvs[0].Value,
}, nil
}

return Value{}, ErrNotFound
}

func (c *client) Put(ctx context.Context, key string, value []byte) error {
val, err := c.Get(ctx, key)
if err != nil && !errors.Is(err, ErrNotFound) {
return err
}
if val.Revision == 0 {
return c.Create(ctx, key, value)
highestRevision := &mvccpb.KeyValue{ModRevision: -1}
for _, kv := range resp.Kvs {
if kv.ModRevision > highestRevision.ModRevision {
highestRevision = kv
}
}
return c.Update(ctx, key, val.Revision, value)
}

func (c *client) Create(ctx context.Context, key string, value []byte) error {
resp, err := c.c.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision(key), "=", 0)).
Then(clientv3.OpPut(key, string(value))).
Commit()
if err != nil {
return err
}
if !resp.Succeeded {
return fmt.Errorf("key exists")
}
return nil
return Value{
Key: highestRevision.Key,
Data: highestRevision.Value,
}, nil
}

func (c *client) Update(ctx context.Context, key string, revision int64, value []byte) error {
resp, err := c.c.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision(key), "=", revision)).
Then(clientv3.OpPut(key, string(value))).
Else(clientv3.OpGet(key)).
func (c *client) Put(ctx context.Context, key string, value []byte) error {
_, err := c.c.Txn(ctx).
If(clientv3.Compare(clientv3.Version(key), ">", 0)).
Then(clientv3.OpPut(key, string(value), clientv3.WithIgnoreLease())).
Else(clientv3.OpPut(key, string(value))).
Commit()
if err != nil {
return err
}
if !resp.Succeeded {
return fmt.Errorf("revision %d doesnt match", revision)
}
return nil

return err
}

func (c *client) Delete(ctx context.Context, key string, revision int64) error {
resp, err := c.c.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision(key), "=", revision)).
func (c *client) Delete(ctx context.Context, key string) error {
_, err := c.c.Txn(ctx).
If(clientv3.Compare(clientv3.Version(key), ">", 0)).
Then(clientv3.OpDelete(key)).
Else(clientv3.OpGet(key)).
Commit()
if err != nil {
return err
}
if !resp.Succeeded {
return fmt.Errorf("revision %d doesnt match", revision)
}
return nil
}

func (c *client) Compact(ctx context.Context, revision int64) (int64, error) {
resp, err := c.c.Compact(ctx, revision)
if resp != nil {
return resp.Header.GetRevision(), err
}
return 0, err
return err
}

func (c *client) Close() error {
Expand Down
5 changes: 5 additions & 0 deletions pkg/etcd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func getClientConfig(ctx context.Context, certificates *Certificates, endpoints
DialTimeout: 5 * time.Second,

Logger: zap.L().Named("etcd-client"),

// DialOptions: []grpc.DialOption{
// grpc.WithDisableRetry(),
// },
// MaxUnaryRetries: 1,
}

if len(endpoints) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/mappings/generic/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func RecordMapping(ctx *synccontext.SyncContext, pName, vName types.NamespacedNa
}

// record the reference
err := ctx.Mappings.Store().AddReference(ctx, synccontext.NameMapping{
err := ctx.Mappings.Store().AddReferenceAndSave(ctx, synccontext.NameMapping{
GroupVersionKind: gvk,

HostName: pName,
Expand Down
5 changes: 2 additions & 3 deletions pkg/mappings/store/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ type Backend interface {
}

type BackendWatchResponse struct {
Err error
Events []*BackendWatchEvent

Err error
}

type BackendWatchEvent struct {
Type BackendWatchEventType
Mapping *Mapping
Type BackendWatchEventType
}

type BackendWatchEventType string
Expand Down
Loading

0 comments on commit c95931f

Please sign in to comment.