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-12-22 14:43:44 +00:00
"fmt"
"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"
2017-11-29 00:51:08 +00:00
"k8s.io/client-go/kubernetes"
2017-12-22 14:43:44 +00:00
"github.com/heptio/ark/pkg/apis/ark/v1"
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 {
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 )
// Client returns an ArkClient. 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 )
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
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
}
2017-12-22 14:43:44 +00:00
if config , err := LoadConfig ( ) ; err == nil {
f . namespace = config [ ConfigKeyNamespace ]
} else {
fmt . Fprintf ( os . Stderr , "WARNING: error retrieving namespace from config file: %v\n" , err )
}
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" )
2017-12-22 14:43:44 +00:00
f . flags . StringVarP ( & f . namespace , "namespace" , "n" , f . namespace , "The namespace in which Ark 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 )
}
func ( f * factory ) Client ( ) ( clientset . Interface , error ) {
2018-02-26 18:42:58 +00:00
clientConfig , err := Config ( f . kubeconfig , f . kubecontext , 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 ) {
2018-02-26 18:42:58 +00:00
clientConfig , err := Config ( f . kubeconfig , f . kubecontext , f . baseName )
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
func ( f * factory ) Namespace ( ) string {
return f . namespace
}