2017-08-02 17:27:17 +00:00
/ *
2018-01-02 18:51:49 +00:00
Copyright 2017 the Heptio Ark 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-09-14 21:27:31 +00:00
"github.com/pkg/errors"
2017-08-02 17:27:17 +00:00
"github.com/spf13/pflag"
2017-11-29 00:51:08 +00:00
"k8s.io/client-go/kubernetes"
2017-10-25 16:42:03 +00:00
clientset "github.com/heptio/ark/pkg/generated/clientset/versioned"
2017-08-02 17:27:17 +00:00
)
2017-11-29 00:51:08 +00:00
// Factory knows how to create an ArkClient and Kubernetes client.
2017-08-02 17:27:17 +00:00
type Factory interface {
// BindFlags binds common flags such as --kubeconfig to the passed-in FlagSet.
BindFlags ( flags * pflag . FlagSet )
// Client returns an ArkClient. It uses the following priority to specify the cluster
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
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
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
KubeClient ( ) ( kubernetes . Interface , error )
2017-08-02 17:27:17 +00:00
}
type factory struct {
flags * pflag . FlagSet
kubeconfig string
2017-09-07 04:10:31 +00:00
baseName string
2017-08-02 17:27:17 +00:00
}
// NewFactory returns a Factory.
2017-09-07 04:10:31 +00:00
func NewFactory ( baseName string ) 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
}
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" )
return f
}
func ( f * factory ) BindFlags ( flags * pflag . FlagSet ) {
flags . AddFlagSet ( f . flags )
}
func ( f * factory ) Client ( ) ( clientset . Interface , error ) {
2017-09-07 04:10:31 +00:00
clientConfig , err := Config ( f . kubeconfig , f . baseName )
2017-08-02 17:27:17 +00:00
if err != nil {
return nil , err
}
arkClient , err := clientset . NewForConfig ( clientConfig )
if err != nil {
2017-09-14 21:27:31 +00:00
return nil , errors . WithStack ( err )
2017-08-02 17:27:17 +00:00
}
return arkClient , nil
}
2017-11-29 00:51:08 +00:00
func ( f * factory ) KubeClient ( ) ( kubernetes . Interface , error ) {
clientConfig , err := Config ( f . kubeconfig , f . baseName )
if err != nil {
return nil , err
}
kubeClient , err := kubernetes . NewForConfig ( clientConfig )
if err != nil {
return nil , errors . WithStack ( err )
}
return kubeClient , nil
}