mirror of
https://github.com/hanzoai/volume.git
synced 2026-10-30 14:55:30 +00:00
A fork of Rancher's local-path-provisioner at v0.0.36. The behaviour is unchanged and deliberately so — a directory on the node a pod lands on is the right thing for a disk. What changes is identity. The provisioner answers to hanzo.ai/volume rather than rancher.io/local-path. That is not branding: two provisioners sharing one name race for the same claims, and a stock k3s carries the upstream one, so a cluster running both would settle our storage by chance. Data lives at /var/lib/hanzo/volume, so reading a Hanzo cluster does not mean learning another vendor's directory. Apache-2.0 as published, unaltered, with Rancher's attribution in NOTICE. The README says what this is not: durability. A volume here dies with its node. That is hanzoai/replicate's question, one layer up, and keeping the two apart is what lets the disk stay this simple.
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
/*
|
|
Copyright 2019 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 resourcelock
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
)
|
|
|
|
const (
|
|
UnknownLeader = "leaderelection.k8s.io/unknown"
|
|
)
|
|
|
|
// MultiLock is used for lock's migration
|
|
type MultiLock struct {
|
|
Primary Interface
|
|
Secondary Interface
|
|
}
|
|
|
|
// Get returns the older election record of the lock
|
|
func (ml *MultiLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
|
|
primary, primaryRaw, err := ml.Primary.Get(ctx)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
secondary, secondaryRaw, err := ml.Secondary.Get(ctx)
|
|
if err != nil {
|
|
// Lock is held by old client
|
|
if apierrors.IsNotFound(err) && primary.HolderIdentity != ml.Identity() {
|
|
return primary, primaryRaw, nil
|
|
}
|
|
return nil, nil, err
|
|
}
|
|
|
|
if primary.HolderIdentity != secondary.HolderIdentity {
|
|
primary.HolderIdentity = UnknownLeader
|
|
primaryRaw, err = json.Marshal(primary)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
return primary, ConcatRawRecord(primaryRaw, secondaryRaw), nil
|
|
}
|
|
|
|
// Create attempts to create both primary lock and secondary lock
|
|
func (ml *MultiLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
|
|
err := ml.Primary.Create(ctx, ler)
|
|
if err != nil && !apierrors.IsAlreadyExists(err) {
|
|
return err
|
|
}
|
|
return ml.Secondary.Create(ctx, ler)
|
|
}
|
|
|
|
// Update will update and existing annotation on both two resources.
|
|
func (ml *MultiLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
|
|
err := ml.Primary.Update(ctx, ler)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, _, err = ml.Secondary.Get(ctx)
|
|
if err != nil && apierrors.IsNotFound(err) {
|
|
return ml.Secondary.Create(ctx, ler)
|
|
}
|
|
return ml.Secondary.Update(ctx, ler)
|
|
}
|
|
|
|
// RecordEvent in leader election while adding meta-data
|
|
func (ml *MultiLock) RecordEvent(s string) {
|
|
ml.Primary.RecordEvent(s)
|
|
ml.Secondary.RecordEvent(s)
|
|
}
|
|
|
|
// Describe is used to convert details on current resource lock
|
|
// into a string
|
|
func (ml *MultiLock) Describe() string {
|
|
return ml.Primary.Describe()
|
|
}
|
|
|
|
// Identity returns the Identity of the lock
|
|
func (ml *MultiLock) Identity() string {
|
|
return ml.Primary.Identity()
|
|
}
|
|
|
|
func ConcatRawRecord(primaryRaw, secondaryRaw []byte) []byte {
|
|
return bytes.Join([][]byte{primaryRaw, secondaryRaw}, []byte(","))
|
|
}
|