Add generated file
This PR adds generated files under pkg/client and vendor folder.
This commit is contained in:
41
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/BUILD
generated
vendored
Normal file
41
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/BUILD
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"doc.go",
|
||||
"filestore.go",
|
||||
"store.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubelet/util/store",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = ["//pkg/util/filesystem:go_default_library"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"filestore_test.go",
|
||||
"store_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/util/filesystem:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/require:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
18
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/doc.go
generated
vendored
Normal file
18
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/doc.go
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
Copyright 2015 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 store hosts a Store interface and its implementations.
|
||||
package store // import "k8s.io/kubernetes/pkg/kubelet/util/store"
|
167
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/filestore.go
generated
vendored
Normal file
167
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/filestore.go
generated
vendored
Normal file
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
Copyright 2017 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 store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
utilfs "k8s.io/kubernetes/pkg/util/filesystem"
|
||||
)
|
||||
|
||||
const (
|
||||
// Name prefix for the temporary files.
|
||||
tmpPrefix = "."
|
||||
)
|
||||
|
||||
// FileStore is an implementation of the Store interface which stores data in files.
|
||||
type FileStore struct {
|
||||
// Absolute path to the base directory for storing data files.
|
||||
directoryPath string
|
||||
|
||||
// filesystem to use.
|
||||
filesystem utilfs.Filesystem
|
||||
}
|
||||
|
||||
// NewFileStore returns an instance of FileStore.
|
||||
func NewFileStore(path string, fs utilfs.Filesystem) (Store, error) {
|
||||
if err := ensureDirectory(fs, path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &FileStore{directoryPath: path, filesystem: fs}, nil
|
||||
}
|
||||
|
||||
// Write writes the given data to a file named key.
|
||||
func (f *FileStore) Write(key string, data []byte) error {
|
||||
if err := ValidateKey(key); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ensureDirectory(f.filesystem, f.directoryPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return writeFile(f.filesystem, f.getPathByKey(key), data)
|
||||
}
|
||||
|
||||
// Read reads the data from the file named key.
|
||||
func (f *FileStore) Read(key string) ([]byte, error) {
|
||||
if err := ValidateKey(key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bytes, err := f.filesystem.ReadFile(f.getPathByKey(key))
|
||||
if os.IsNotExist(err) {
|
||||
return bytes, ErrKeyNotFound
|
||||
}
|
||||
return bytes, err
|
||||
}
|
||||
|
||||
// Delete deletes the key file.
|
||||
func (f *FileStore) Delete(key string) error {
|
||||
if err := ValidateKey(key); err != nil {
|
||||
return err
|
||||
}
|
||||
return removePath(f.filesystem, f.getPathByKey(key))
|
||||
}
|
||||
|
||||
// List returns all keys in the store.
|
||||
func (f *FileStore) List() ([]string, error) {
|
||||
keys := make([]string, 0)
|
||||
files, err := f.filesystem.ReadDir(f.directoryPath)
|
||||
if err != nil {
|
||||
return keys, err
|
||||
}
|
||||
for _, f := range files {
|
||||
if !strings.HasPrefix(f.Name(), tmpPrefix) {
|
||||
keys = append(keys, f.Name())
|
||||
}
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
// getPathByKey returns the full path of the file for the key.
|
||||
func (f *FileStore) getPathByKey(key string) string {
|
||||
return filepath.Join(f.directoryPath, key)
|
||||
}
|
||||
|
||||
// ensureDirectory creates the directory if it does not exist.
|
||||
func ensureDirectory(fs utilfs.Filesystem, path string) error {
|
||||
if _, err := fs.Stat(path); err != nil {
|
||||
// MkdirAll returns nil if directory already exists.
|
||||
return fs.MkdirAll(path, 0755)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeFile writes data to path in a single transaction.
|
||||
func writeFile(fs utilfs.Filesystem, path string, data []byte) (retErr error) {
|
||||
// Create a temporary file in the base directory of `path` with a prefix.
|
||||
tmpFile, err := fs.TempFile(filepath.Dir(path), tmpPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tmpPath := tmpFile.Name()
|
||||
shouldClose := true
|
||||
|
||||
defer func() {
|
||||
// Close the file.
|
||||
if shouldClose {
|
||||
if err := tmpFile.Close(); err != nil {
|
||||
if retErr == nil {
|
||||
retErr = fmt.Errorf("close error: %v", err)
|
||||
} else {
|
||||
retErr = fmt.Errorf("failed to close temp file after error %v; close error: %v", retErr, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up the temp file on error.
|
||||
if retErr != nil && tmpPath != "" {
|
||||
if err := removePath(fs, tmpPath); err != nil {
|
||||
retErr = fmt.Errorf("failed to remove the temporary file (%q) after error %v; remove error: %v", tmpPath, retErr, err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Write data.
|
||||
if _, err := tmpFile.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Sync file.
|
||||
if err := tmpFile.Sync(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Closing the file before renaming.
|
||||
err = tmpFile.Close()
|
||||
shouldClose = false
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fs.Rename(tmpPath, path)
|
||||
}
|
||||
|
||||
func removePath(fs utilfs.Filesystem, path string) error {
|
||||
if err := fs.Remove(path); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
126
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/filestore_test.go
generated
vendored
Normal file
126
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/filestore_test.go
generated
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
Copyright 2017 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 store
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/kubernetes/pkg/util/filesystem"
|
||||
)
|
||||
|
||||
func TestFileStore(t *testing.T) {
|
||||
path, err := ioutil.TempDir("", "FileStore")
|
||||
assert.NoError(t, err)
|
||||
store, err := NewFileStore(path, filesystem.DefaultFs{})
|
||||
assert.NoError(t, err)
|
||||
testStore(t, store)
|
||||
}
|
||||
|
||||
func TestFakeFileStore(t *testing.T) {
|
||||
store, err := NewFileStore("/tmp/test-fake-file-store", filesystem.NewFakeFs())
|
||||
assert.NoError(t, err)
|
||||
testStore(t, store)
|
||||
}
|
||||
|
||||
func testStore(t *testing.T, store Store) {
|
||||
testCases := []struct {
|
||||
key string
|
||||
data string
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
"id1",
|
||||
"data1",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"id2",
|
||||
"data2",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"/id1",
|
||||
"data1",
|
||||
true,
|
||||
},
|
||||
{
|
||||
".id1",
|
||||
"data1",
|
||||
true,
|
||||
},
|
||||
{
|
||||
" ",
|
||||
"data2",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"___",
|
||||
"data2",
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
// Test add data.
|
||||
for _, c := range testCases {
|
||||
t.Log("test case: ", c)
|
||||
_, err := store.Read(c.key)
|
||||
assert.Error(t, err)
|
||||
|
||||
err = store.Write(c.key, []byte(c.data))
|
||||
if c.expectErr {
|
||||
assert.Error(t, err)
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
// Test read data by key.
|
||||
data, err := store.Read(c.key)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, string(data), c.data)
|
||||
}
|
||||
|
||||
// Test list keys.
|
||||
keys, err := store.List()
|
||||
assert.NoError(t, err)
|
||||
sort.Strings(keys)
|
||||
assert.Equal(t, keys, []string{"id1", "id2"})
|
||||
|
||||
// Test Delete data
|
||||
for _, c := range testCases {
|
||||
if c.expectErr {
|
||||
continue
|
||||
}
|
||||
|
||||
err = store.Delete(c.key)
|
||||
require.NoError(t, err)
|
||||
_, err = store.Read(c.key)
|
||||
assert.EqualValues(t, ErrKeyNotFound, err)
|
||||
}
|
||||
|
||||
// Test delete non-existent key.
|
||||
err = store.Delete("id1")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Test list keys.
|
||||
keys, err = store.List()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(keys), 0)
|
||||
}
|
64
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/store.go
generated
vendored
Normal file
64
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/store.go
generated
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
Copyright 2017 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 store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const (
|
||||
keyMaxLength = 250
|
||||
|
||||
keyCharFmt string = "[A-Za-z0-9]"
|
||||
keyExtCharFmt string = "[-A-Za-z0-9_.]"
|
||||
qualifiedKeyFmt string = "(" + keyCharFmt + keyExtCharFmt + "*)?" + keyCharFmt
|
||||
)
|
||||
|
||||
var (
|
||||
// Key must consist of alphanumeric characters, '-', '_' or '.', and must start
|
||||
// and end with an alphanumeric character.
|
||||
keyRegex = regexp.MustCompile("^" + qualifiedKeyFmt + "$")
|
||||
|
||||
// ErrKeyNotFound is the error returned if key is not found in Store.
|
||||
ErrKeyNotFound = fmt.Errorf("key is not found")
|
||||
)
|
||||
|
||||
// Store provides the interface for storing keyed data.
|
||||
// Store must be thread-safe
|
||||
type Store interface {
|
||||
// key must contain one or more characters in [A-Za-z0-9]
|
||||
// Write writes data with key.
|
||||
Write(key string, data []byte) error
|
||||
// Read retrieves data with key
|
||||
// Read must return ErrKeyNotFound if key is not found.
|
||||
Read(key string) ([]byte, error)
|
||||
// Delete deletes data by key
|
||||
// Delete must not return error if key does not exist
|
||||
Delete(key string) error
|
||||
// List lists all existing keys.
|
||||
List() ([]string, error)
|
||||
}
|
||||
|
||||
// ValidateKey returns an error if the given key does not meet the requirement
|
||||
// of the key format and length.
|
||||
func ValidateKey(key string) error {
|
||||
if len(key) <= keyMaxLength && keyRegex.MatchString(key) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid key: %q", key)
|
||||
}
|
63
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/store_test.go
generated
vendored
Normal file
63
vendor/k8s.io/kubernetes/pkg/kubelet/util/store/store_test.go
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright 2017 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 store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsValidKey(t *testing.T) {
|
||||
testcases := []struct {
|
||||
key string
|
||||
valid bool
|
||||
}{
|
||||
{
|
||||
" ",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"/foo/bar",
|
||||
false,
|
||||
},
|
||||
{
|
||||
".foo",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a78768279290d33d0b82eaea43cb8346f500057cb5bd250e88c97a5585385d66",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a7.87-6_8",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a7.87-677-",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
if tc.valid {
|
||||
assert.NoError(t, ValidateKey(tc.key))
|
||||
} else {
|
||||
assert.Error(t, ValidateKey(tc.key))
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user