mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-04 23:26:59 +00:00
Merge pull request #634 from marpaia/k8s-1.11
Update Kubernetes and Client-Go for 1.11.0 / 8.0.0
This commit is contained in:
+4
-13
@@ -17,8 +17,6 @@ limitations under the License.
|
||||
package client
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -38,24 +36,17 @@ type DynamicFactory interface {
|
||||
|
||||
// dynamicFactory implements DynamicFactory.
|
||||
type dynamicFactory struct {
|
||||
clientPool dynamic.ClientPool
|
||||
dynamicClient dynamic.Interface
|
||||
}
|
||||
|
||||
// NewDynamicFactory returns a new ClientPool-based dynamic factory.
|
||||
func NewDynamicFactory(clientPool dynamic.ClientPool) DynamicFactory {
|
||||
return &dynamicFactory{clientPool: clientPool}
|
||||
func NewDynamicFactory(dynamicClient dynamic.Interface) DynamicFactory {
|
||||
return &dynamicFactory{dynamicClient: dynamicClient}
|
||||
}
|
||||
|
||||
func (f *dynamicFactory) ClientForGroupVersionResource(gv schema.GroupVersion, resource metav1.APIResource, namespace string) (Dynamic, error) {
|
||||
// client-go doesn't actually use the kind when getting the dynamic client from the client pool;
|
||||
// it only needs the group and version.
|
||||
dynamicClient, err := f.clientPool.ClientForGroupVersionKind(gv.WithKind(""))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error getting client for GroupVersion %s, Resource %s", gv.String, resource.String())
|
||||
}
|
||||
|
||||
return &dynamicResourceClient{
|
||||
resourceClient: dynamicClient.Resource(&resource, namespace),
|
||||
resourceClient: f.dynamicClient.Resource(gv.WithResource(resource.Name)).Namespace(namespace),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,9 @@ import (
|
||||
func TestGetVolumeID(t *testing.T) {
|
||||
b := &blockStore{}
|
||||
|
||||
pv := &unstructured.Unstructured{}
|
||||
pv := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{},
|
||||
}
|
||||
|
||||
// missing spec.awsElasticBlockStore -> no error
|
||||
volumeID, err := b.GetVolumeID(pv)
|
||||
@@ -69,7 +71,9 @@ func TestGetVolumeID(t *testing.T) {
|
||||
func TestSetVolumeID(t *testing.T) {
|
||||
b := &blockStore{}
|
||||
|
||||
pv := &unstructured.Unstructured{}
|
||||
pv := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{},
|
||||
}
|
||||
|
||||
// missing spec.awsElasticBlockStore -> error
|
||||
updatedPV, err := b.SetVolumeID(pv, "vol-updated")
|
||||
|
||||
@@ -29,7 +29,9 @@ import (
|
||||
func TestGetVolumeID(t *testing.T) {
|
||||
b := &blockStore{}
|
||||
|
||||
pv := &unstructured.Unstructured{}
|
||||
pv := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{},
|
||||
}
|
||||
|
||||
// missing spec.azureDisk -> no error
|
||||
volumeID, err := b.GetVolumeID(pv)
|
||||
@@ -58,7 +60,9 @@ func TestSetVolumeID(t *testing.T) {
|
||||
subscription: "sub",
|
||||
}
|
||||
|
||||
pv := &unstructured.Unstructured{}
|
||||
pv := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{},
|
||||
}
|
||||
|
||||
// missing spec.azureDisk -> error
|
||||
updatedPV, err := b.SetVolumeID(pv, "updated")
|
||||
|
||||
@@ -32,7 +32,9 @@ import (
|
||||
func TestGetVolumeID(t *testing.T) {
|
||||
b := &blockStore{}
|
||||
|
||||
pv := &unstructured.Unstructured{}
|
||||
pv := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{},
|
||||
}
|
||||
|
||||
// missing spec.gcePersistentDisk -> no error
|
||||
volumeID, err := b.GetVolumeID(pv)
|
||||
@@ -58,7 +60,9 @@ func TestGetVolumeID(t *testing.T) {
|
||||
func TestSetVolumeID(t *testing.T) {
|
||||
b := &blockStore{}
|
||||
|
||||
pv := &unstructured.Unstructured{}
|
||||
pv := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{},
|
||||
}
|
||||
|
||||
// missing spec.gcePersistentDisk -> error
|
||||
updatedPV, err := b.SetVolumeID(pv, "abc123")
|
||||
|
||||
@@ -151,7 +151,7 @@ type server struct {
|
||||
backupService cloudprovider.BackupService
|
||||
snapshotService cloudprovider.SnapshotService
|
||||
discoveryClient discovery.DiscoveryInterface
|
||||
clientPool dynamic.ClientPool
|
||||
dynamicClient dynamic.Interface
|
||||
sharedInformerFactory informers.SharedInformerFactory
|
||||
ctx context.Context
|
||||
cancelFunc context.CancelFunc
|
||||
@@ -182,6 +182,11 @@ func newServer(namespace, baseName, pluginDir, metricsAddr string, logger *logru
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dynamicClient, err := dynamic.NewForConfig(clientConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx, cancelFunc := context.WithCancel(context.Background())
|
||||
|
||||
s := &server{
|
||||
@@ -191,7 +196,7 @@ func newServer(namespace, baseName, pluginDir, metricsAddr string, logger *logru
|
||||
kubeClient: kubeClient,
|
||||
arkClient: arkClient,
|
||||
discoveryClient: arkClient.Discovery(),
|
||||
clientPool: dynamic.NewDynamicClientPool(clientConfig),
|
||||
dynamicClient: dynamicClient,
|
||||
sharedInformerFactory: informers.NewFilteredSharedInformerFactory(arkClient, 0, namespace, nil),
|
||||
ctx: ctx,
|
||||
cancelFunc: cancelFunc,
|
||||
@@ -562,7 +567,7 @@ func (s *server) runControllers(config *api.Config) error {
|
||||
|
||||
backupper, err := backup.NewKubernetesBackupper(
|
||||
discoveryHelper,
|
||||
client.NewDynamicFactory(s.clientPool),
|
||||
client.NewDynamicFactory(s.dynamicClient),
|
||||
podexec.NewPodCommandExecutor(s.kubeClientConfig, s.kubeClient.CoreV1().RESTClient()),
|
||||
s.snapshotService,
|
||||
s.resticManager,
|
||||
@@ -638,7 +643,7 @@ func (s *server) runControllers(config *api.Config) error {
|
||||
|
||||
restorer, err := restore.NewKubernetesRestorer(
|
||||
discoveryHelper,
|
||||
client.NewDynamicFactory(s.clientPool),
|
||||
client.NewDynamicFactory(s.dynamicClient),
|
||||
s.backupService,
|
||||
s.snapshotService,
|
||||
config.ResourcePriorities,
|
||||
|
||||
@@ -162,7 +162,6 @@ func NewPrinter(cmd *cobra.Command) (*printers.HumanReadablePrinter, error) {
|
||||
}
|
||||
|
||||
printer := printers.NewHumanReadablePrinter(
|
||||
nil, // encoder, only needed if we want/need to convert unstructured/unknown to typed objects
|
||||
nil, // decoder, only needed if we want/need to convert unstructured/unknown to typed objects
|
||||
options,
|
||||
)
|
||||
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/discovery"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/restmapper"
|
||||
)
|
||||
|
||||
// Helper exposes functions for interacting with the Kubernetes discovery
|
||||
@@ -91,11 +91,11 @@ func (h *helper) Refresh() error {
|
||||
h.lock.Lock()
|
||||
defer h.lock.Unlock()
|
||||
|
||||
groupResources, err := discovery.GetAPIGroupResources(h.discoveryClient)
|
||||
groupResources, err := restmapper.GetAPIGroupResources(h.discoveryClient)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
mapper := discovery.NewRESTMapper(groupResources, dynamic.VersionInterfaces)
|
||||
mapper := restmapper.NewDiscoveryRESTMapper(groupResources)
|
||||
shortcutExpander, err := kcmdutil.NewShortcutExpander(mapper, h.discoveryClient, h.logger)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
|
||||
@@ -19,7 +19,6 @@ limitations under the License.
|
||||
package versioned
|
||||
|
||||
import (
|
||||
glog "github.com/golang/glog"
|
||||
arkv1 "github.com/heptio/ark/pkg/generated/clientset/versioned/typed/ark/v1"
|
||||
discovery "k8s.io/client-go/discovery"
|
||||
rest "k8s.io/client-go/rest"
|
||||
@@ -74,7 +73,6 @@ func NewForConfig(c *rest.Config) (*Clientset, error) {
|
||||
|
||||
cs.DiscoveryClient, err = discovery.NewDiscoveryClientForConfig(&configShallowCopy)
|
||||
if err != nil {
|
||||
glog.Errorf("failed to create the DiscoveryClient: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return &cs, nil
|
||||
|
||||
@@ -41,9 +41,10 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {
|
||||
}
|
||||
}
|
||||
|
||||
fakePtr := testing.Fake{}
|
||||
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o))
|
||||
fakePtr.AddWatchReactor("*", func(action testing.Action) (handled bool, ret watch.Interface, err error) {
|
||||
cs := &Clientset{}
|
||||
cs.discovery = &fakediscovery.FakeDiscovery{Fake: &cs.Fake}
|
||||
cs.AddReactor("*", "*", testing.ObjectReaction(o))
|
||||
cs.AddWatchReactor("*", func(action testing.Action) (handled bool, ret watch.Interface, err error) {
|
||||
gvr := action.GetResource()
|
||||
ns := action.GetNamespace()
|
||||
watch, err := o.Watch(gvr, ns)
|
||||
@@ -53,7 +54,7 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {
|
||||
return true, watch, nil
|
||||
})
|
||||
|
||||
return &Clientset{fakePtr, &fakediscovery.FakeDiscovery{Fake: &fakePtr}}
|
||||
return cs
|
||||
}
|
||||
|
||||
// Clientset implements clientset.Interface. Meant to be embedded into a
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakeBackups) List(opts v1.ListOptions) (result *ark_v1.BackupList, err
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.BackupList{}
|
||||
list := &ark_v1.BackupList{ListMeta: obj.(*ark_v1.BackupList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.BackupList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakeConfigs) List(opts v1.ListOptions) (result *ark_v1.ConfigList, err
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.ConfigList{}
|
||||
list := &ark_v1.ConfigList{ListMeta: obj.(*ark_v1.ConfigList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.ConfigList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakeDeleteBackupRequests) List(opts v1.ListOptions) (result *ark_v1.Del
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.DeleteBackupRequestList{}
|
||||
list := &ark_v1.DeleteBackupRequestList{ListMeta: obj.(*ark_v1.DeleteBackupRequestList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.DeleteBackupRequestList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakeDownloadRequests) List(opts v1.ListOptions) (result *ark_v1.Downloa
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.DownloadRequestList{}
|
||||
list := &ark_v1.DownloadRequestList{ListMeta: obj.(*ark_v1.DownloadRequestList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.DownloadRequestList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakePodVolumeBackups) List(opts v1.ListOptions) (result *ark_v1.PodVolu
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.PodVolumeBackupList{}
|
||||
list := &ark_v1.PodVolumeBackupList{ListMeta: obj.(*ark_v1.PodVolumeBackupList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.PodVolumeBackupList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakePodVolumeRestores) List(opts v1.ListOptions) (result *ark_v1.PodVol
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.PodVolumeRestoreList{}
|
||||
list := &ark_v1.PodVolumeRestoreList{ListMeta: obj.(*ark_v1.PodVolumeRestoreList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.PodVolumeRestoreList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakeResticRepositories) List(opts v1.ListOptions) (result *ark_v1.Resti
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.ResticRepositoryList{}
|
||||
list := &ark_v1.ResticRepositoryList{ListMeta: obj.(*ark_v1.ResticRepositoryList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.ResticRepositoryList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakeRestores) List(opts v1.ListOptions) (result *ark_v1.RestoreList, er
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.RestoreList{}
|
||||
list := &ark_v1.RestoreList{ListMeta: obj.(*ark_v1.RestoreList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.RestoreList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *FakeSchedules) List(opts v1.ListOptions) (result *ark_v1.ScheduleList,
|
||||
if label == nil {
|
||||
label = labels.Everything()
|
||||
}
|
||||
list := &ark_v1.ScheduleList{}
|
||||
list := &ark_v1.ScheduleList{ListMeta: obj.(*ark_v1.ScheduleList).ListMeta}
|
||||
for _, item := range obj.(*ark_v1.ScheduleList).Items {
|
||||
if label.Matches(labels.Set(item.Labels)) {
|
||||
list.Items = append(list.Items, item)
|
||||
|
||||
@@ -32,12 +32,16 @@ import (
|
||||
cache "k8s.io/client-go/tools/cache"
|
||||
)
|
||||
|
||||
// SharedInformerOption defines the functional option type for SharedInformerFactory.
|
||||
type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
|
||||
|
||||
type sharedInformerFactory struct {
|
||||
client versioned.Interface
|
||||
namespace string
|
||||
tweakListOptions internalinterfaces.TweakListOptionsFunc
|
||||
lock sync.Mutex
|
||||
defaultResync time.Duration
|
||||
customResync map[reflect.Type]time.Duration
|
||||
|
||||
informers map[reflect.Type]cache.SharedIndexInformer
|
||||
// startedInformers is used for tracking which informers have been started.
|
||||
@@ -45,23 +49,62 @@ type sharedInformerFactory struct {
|
||||
startedInformers map[reflect.Type]bool
|
||||
}
|
||||
|
||||
// NewSharedInformerFactory constructs a new instance of sharedInformerFactory
|
||||
// WithCustomResyncConfig sets a custom resync period for the specified informer types.
|
||||
func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
|
||||
return func(factory *sharedInformerFactory) *sharedInformerFactory {
|
||||
for k, v := range resyncConfig {
|
||||
factory.customResync[reflect.TypeOf(k)] = v
|
||||
}
|
||||
return factory
|
||||
}
|
||||
}
|
||||
|
||||
// WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
|
||||
func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
|
||||
return func(factory *sharedInformerFactory) *sharedInformerFactory {
|
||||
factory.tweakListOptions = tweakListOptions
|
||||
return factory
|
||||
}
|
||||
}
|
||||
|
||||
// WithNamespace limits the SharedInformerFactory to the specified namespace.
|
||||
func WithNamespace(namespace string) SharedInformerOption {
|
||||
return func(factory *sharedInformerFactory) *sharedInformerFactory {
|
||||
factory.namespace = namespace
|
||||
return factory
|
||||
}
|
||||
}
|
||||
|
||||
// NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
|
||||
func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
|
||||
return NewFilteredSharedInformerFactory(client, defaultResync, v1.NamespaceAll, nil)
|
||||
return NewSharedInformerFactoryWithOptions(client, defaultResync)
|
||||
}
|
||||
|
||||
// NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
|
||||
// Listers obtained via this SharedInformerFactory will be subject to the same filters
|
||||
// as specified here.
|
||||
// Deprecated: Please use NewSharedInformerFactoryWithOptions instead
|
||||
func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
|
||||
return &sharedInformerFactory{
|
||||
return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
|
||||
}
|
||||
|
||||
// NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
|
||||
func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
|
||||
factory := &sharedInformerFactory{
|
||||
client: client,
|
||||
namespace: namespace,
|
||||
tweakListOptions: tweakListOptions,
|
||||
namespace: v1.NamespaceAll,
|
||||
defaultResync: defaultResync,
|
||||
informers: make(map[reflect.Type]cache.SharedIndexInformer),
|
||||
startedInformers: make(map[reflect.Type]bool),
|
||||
customResync: make(map[reflect.Type]time.Duration),
|
||||
}
|
||||
|
||||
// Apply all options
|
||||
for _, opt := range options {
|
||||
factory = opt(factory)
|
||||
}
|
||||
|
||||
return factory
|
||||
}
|
||||
|
||||
// Start initializes all requested informers.
|
||||
@@ -110,7 +153,13 @@ func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internal
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = newFunc(f.client, f.defaultResync)
|
||||
|
||||
resyncPeriod, exists := f.customResync[informerType]
|
||||
if !exists {
|
||||
resyncPeriod = f.defaultResync
|
||||
}
|
||||
|
||||
informer = newFunc(f.client, resyncPeriod)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
|
||||
Reference in New Issue
Block a user