2017-08-02 17:27:17 +00:00
/ *
2019-08-09 17:23:16 +00:00
Copyright 2017 , 2019 the Velero contributors .
2017-08-02 17:27:17 +00:00
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 client
import (
2017-12-22 14:43:44 +00:00
"os"
2017-09-14 21:27:31 +00:00
"github.com/pkg/errors"
2017-08-02 17:27:17 +00:00
"github.com/spf13/pflag"
2019-07-17 21:19:14 +00:00
"k8s.io/client-go/dynamic"
2017-11-29 00:51:08 +00:00
"k8s.io/client-go/kubernetes"
2019-08-09 17:23:16 +00:00
"k8s.io/client-go/rest"
2017-11-29 00:51:08 +00:00
2019-09-30 21:26:56 +00:00
v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
clientset "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned"
2017-08-02 17:27:17 +00:00
)
2019-01-25 03:33:07 +00:00
// Factory knows how to create a VeleroClient and Kubernetes client.
2017-08-02 17:27:17 +00:00
type Factory interface {
2017-12-22 14:43:44 +00:00
// BindFlags binds common flags (--kubeconfig, --namespace) to the passed-in FlagSet.
2017-08-02 17:27:17 +00:00
BindFlags ( flags * pflag . FlagSet )
2019-01-25 03:33:07 +00:00
// Client returns a VeleroClient. It uses the following priority to specify the cluster
2017-12-22 14:43:44 +00:00
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
2017-08-02 17:27:17 +00:00
Client ( ) ( clientset . Interface , error )
2017-11-29 00:51:08 +00:00
// KubeClient returns a Kubernetes client. It uses the following priority to specify the cluster
2017-12-22 14:43:44 +00:00
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
2017-11-29 00:51:08 +00:00
KubeClient ( ) ( kubernetes . Interface , error )
2019-07-17 21:19:14 +00:00
// DynamicClient returns a Kubernetes dynamic client. It uses the following priority to specify the cluster
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
DynamicClient ( ) ( dynamic . Interface , error )
2019-08-09 17:23:16 +00:00
// SetBasename changes the basename for an already-constructed client.
// This is useful for generating clients that require a different user-agent string below the root `velero`
// command, such as the server subcommand.
SetBasename ( string )
// SetClientQPS sets the Queries Per Second for a client.
SetClientQPS ( float32 )
// SetClientBurst sets the Burst for a client.
SetClientBurst ( int )
// ClientConfig returns a rest.Config struct used for client-go clients.
ClientConfig ( ) ( * rest . Config , error )
// Namespace returns the namespace which the Factory will create clients for.
2017-12-22 14:43:44 +00:00
Namespace ( ) string
2017-08-02 17:27:17 +00:00
}
type factory struct {
2018-02-26 18:42:58 +00:00
flags * pflag . FlagSet
kubeconfig string
kubecontext string
baseName string
namespace string
2019-08-09 17:23:16 +00:00
clientQPS float32
clientBurst int
2017-08-02 17:27:17 +00:00
}
// NewFactory returns a Factory.
2019-09-18 16:57:04 +00:00
func NewFactory ( baseName string , config VeleroConfig ) Factory {
2017-08-02 17:27:17 +00:00
f := & factory {
2017-09-07 04:10:31 +00:00
flags : pflag . NewFlagSet ( "" , pflag . ContinueOnError ) ,
baseName : baseName ,
2017-08-02 17:27:17 +00:00
}
2017-12-22 14:43:44 +00:00
2019-08-09 17:23:16 +00:00
f . namespace = os . Getenv ( "VELERO_NAMESPACE" )
2019-09-18 16:57:04 +00:00
if config . Namespace ( ) != "" {
f . namespace = config . Namespace ( )
2017-12-22 14:43:44 +00:00
}
2019-08-09 17:23:16 +00:00
// We didn't get the namespace via env var or config file, so use the default.
// Command line flags will override when BindFlags is called.
2017-12-22 14:43:44 +00:00
if f . namespace == "" {
f . namespace = v1 . DefaultNamespace
}
2017-08-02 17:27:17 +00:00
f . flags . StringVar ( & f . kubeconfig , "kubeconfig" , "" , "Path to the kubeconfig file to use to talk to the Kubernetes apiserver. If unset, try the environment variable KUBECONFIG, as well as in-cluster configuration" )
2019-01-25 03:33:07 +00:00
f . flags . StringVarP ( & f . namespace , "namespace" , "n" , f . namespace , "The namespace in which Velero should operate" )
2018-02-26 18:42:58 +00:00
f . flags . StringVar ( & f . kubecontext , "kubecontext" , "" , "The context to use to talk to the Kubernetes apiserver. If unset defaults to whatever your current-context is (kubectl config current-context)" )
2017-08-02 17:27:17 +00:00
return f
}
func ( f * factory ) BindFlags ( flags * pflag . FlagSet ) {
flags . AddFlagSet ( f . flags )
}
2019-08-09 17:23:16 +00:00
func ( f * factory ) ClientConfig ( ) ( * rest . Config , error ) {
return Config ( f . kubeconfig , f . kubecontext , f . baseName , f . clientQPS , f . clientBurst )
}
2017-08-02 17:27:17 +00:00
func ( f * factory ) Client ( ) ( clientset . Interface , error ) {
2019-08-09 17:23:16 +00:00
clientConfig , err := f . ClientConfig ( )
2017-08-02 17:27:17 +00:00
if err != nil {
return nil , err
}
2019-01-25 03:33:07 +00:00
veleroClient , err := clientset . NewForConfig ( clientConfig )
2017-08-02 17:27:17 +00:00
if err != nil {
2017-09-14 21:27:31 +00:00
return nil , errors . WithStack ( err )
2017-08-02 17:27:17 +00:00
}
2019-01-25 03:33:07 +00:00
return veleroClient , nil
2017-08-02 17:27:17 +00:00
}
2017-11-29 00:51:08 +00:00
func ( f * factory ) KubeClient ( ) ( kubernetes . Interface , error ) {
2019-08-09 17:23:16 +00:00
clientConfig , err := f . ClientConfig ( )
2017-11-29 00:51:08 +00:00
if err != nil {
return nil , err
}
kubeClient , err := kubernetes . NewForConfig ( clientConfig )
if err != nil {
return nil , errors . WithStack ( err )
}
return kubeClient , nil
}
2017-12-22 14:43:44 +00:00
2019-07-17 21:19:14 +00:00
func ( f * factory ) DynamicClient ( ) ( dynamic . Interface , error ) {
2019-08-09 17:23:16 +00:00
clientConfig , err := f . ClientConfig ( )
2019-07-17 21:19:14 +00:00
if err != nil {
return nil , err
}
dynamicClient , err := dynamic . NewForConfig ( clientConfig )
if err != nil {
return nil , errors . WithStack ( err )
}
return dynamicClient , nil
}
2019-08-09 17:23:16 +00:00
func ( f * factory ) SetBasename ( name string ) {
f . baseName = name
}
func ( f * factory ) SetClientQPS ( qps float32 ) {
f . clientQPS = qps
}
func ( f * factory ) SetClientBurst ( burst int ) {
f . clientBurst = burst
}
2017-12-22 14:43:44 +00:00
func ( f * factory ) Namespace ( ) string {
return f . namespace
}