-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for TTLs in etcd migration. #31242
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,70 @@ | ||
/* | ||
Copyright 2016 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 ( | ||
"flag" | ||
"strings" | ||
"time" | ||
|
||
"github.com/coreos/etcd/clientv3" | ||
"github.com/golang/glog" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
var ( | ||
etcdAddress = flag.String("etcd-address", "", "Etcd address") | ||
ttlKeysPrefix = flag.String("ttl-keys-prefix", "", "Prefix for TTL keys") | ||
leaseDuration = flag.Duration("lease-duration", time.Hour, "Lease duration (seconds granularity)") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *etcdAddress == "" { | ||
glog.Fatalf("--etcd-address flag is required") | ||
} | ||
client, err := clientv3.New(clientv3.Config{Endpoints: []string{*etcdAddress}}) | ||
if err != nil { | ||
glog.Fatalf("Error while creating etcd client: %v", err) | ||
} | ||
|
||
// Make sure that ttlKeysPrefix is ended with "/" so that we only get children "directories". | ||
if !strings.HasSuffix(*ttlKeysPrefix, "/") { | ||
*ttlKeysPrefix += "/" | ||
} | ||
ctx := context.Background() | ||
|
||
objectsResp, err := client.KV.Get(ctx, *ttlKeysPrefix, clientv3.WithPrefix()) | ||
if err != nil { | ||
glog.Fatalf("Error while getting objects to attach to the lease") | ||
} | ||
|
||
lease, err := client.Lease.Grant(ctx, int64(*leaseDuration/time.Second)) | ||
if err != nil { | ||
glog.Fatalf("Error while creating lease: %v", err) | ||
} | ||
glog.Infof("Lease with TTL: %v created", lease.TTL) | ||
|
||
glog.Infof("Attaching lease to %d entries", len(objectsResp.Kvs)) | ||
for _, kv := range objectsResp.Kvs { | ||
_, err := client.KV.Put(ctx, string(kv.Key), string(kv.Value), clientv3.WithLease(lease.ID)) | ||
if err != nil { | ||
glog.Errorf("Error while attaching lease to: %s", string(kv.Key)) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -34,22 +34,62 @@ if [ -z "${DATA_DIRECTORY:-}" ]; then | |
exit 0 | ||
fi | ||
|
||
ETCD="${ETCD:-/usr/local/bin/etcd}" | ||
ETCDCTL="${ETCDCTL:-/usr/local/bin/etcdctl}" | ||
ATTACHLEASE="${ATTACHLEASE:-/usr/local/bin/attachlease}" | ||
VERSION_FILE="version.txt" | ||
CURRENT_STORAGE='etcd2' | ||
if [ -e "${DATA_DIRECTORY}/${VERSION_FILE}" ]; then | ||
CURRENT_STORAGE="$(cat ${DATA_DIRECTORY}/${VERSION_FILE})" | ||
fi | ||
|
||
start_etcd() { | ||
ETCD_PORT=18629 | ||
ETCD_PEER_PORT=18630 | ||
${ETCD} --data-dir=${DATA_DIRECTORY} \ | ||
--listen-client-urls http://127.0.0.1:${ETCD_PORT} \ | ||
--advertise-client-urls http://127.0.0.1:${ETCD_PORT} \ | ||
--listen-peer-urls http://127.0.0.1:${ETCD_PEER_PORT} \ | ||
--initial-advertise-peer-urls http://127.0.01:${ETCD_PEER_PORT} \ | ||
1>>/dev/null 2>&1 & | ||
ETCD_PID=$! | ||
# Wait until etcd is up. | ||
for i in $(seq 30); do | ||
local out | ||
if out=$(wget -q --timeout=1 http://127.0.0.1:${ETCD_PORT}/v2/machines 2> /dev/null); then | ||
echo "Etcd on port ${ETCD_PORT} is up." | ||
return 0 | ||
fi | ||
sleep 0.5 | ||
done | ||
echo "Timeout while waiting for etcd on port ${ETCD_PORT}" | ||
return 1 | ||
} | ||
|
||
stop_etcd() { | ||
kill "${ETCD_PID-}" >/dev/null 2>&1 || : | ||
wait "${ETCD_PID-}" >/dev/null 2>&1 || : | ||
} | ||
|
||
if [ "${CURRENT_STORAGE}" = "etcd2" -a "${TARGET_STORAGE}" = "etcd3" ]; then | ||
# If directory doesn't exist or is empty, this means that there aren't any | ||
# data for migration, which means we can skip this step. | ||
if [ -d "${DATA_DIRECTORY}" ]; then | ||
if [ "$(ls -A ${DATA_DIRECTORY})" ]; then | ||
echo "Performing etcd2 -> etcd3 migration" | ||
# TODO: Pass a correct transformer to handle TTLs. | ||
echo "ETCDCTL_API=3 ${ETCDCTL} migrate --data-dir=${DATA_DIRECTORY}" | ||
ETCDCTL_API=3 ${ETCDCTL} migrate --data-dir=${DATA_DIRECTORY} | ||
echo "Attaching leases to TTL entries" | ||
# Now attach lease to all keys. | ||
# To do it, we temporarily start etcd on a random port (so that | ||
# apiserver actually cannot access it). | ||
start_etcd | ||
# Create a lease and attach all keys to it. | ||
${ATTACHLEASE} \ | ||
--etcd-address http://127.0.0.1:${ETCD_PORT} \ | ||
--ttl-keys-prefix "${TTL_KEYS_DIRECTORY:-/registry/events}" \ | ||
--lease-duration 1h | ||
# Kill etcd and wait until this is down. | ||
stop_etcd | ||
fi | ||
fi | ||
fi | ||
|
@@ -58,6 +98,8 @@ if [ "${CURRENT_STORAGE}" = "etcd3" -a "${TARGET_STORAGE}" = "etcd2" ]; then | |
echo "Performing etcd3 -> etcd2 migration" | ||
# TODO: Implement rollback once this will be supported. | ||
echo "etcd3 -> etcd2 downgrade is NOT supported." | ||
# FIXME: On rollback, we will not support TTLs - we will simply clear | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it's bad? Hopefully we will never use this procedure... |
||
# all events. | ||
fi | ||
|
||
# Write current storage version to avoid future migrations. | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
127.0.01 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right. can you send a pr to fix this? Thank you!