2017-11-20 09:30:52 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2019-01-16 06:56:37 -05:00
|
|
|
"github.com/docker/cli/kubernetes"
|
2018-04-09 09:07:11 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-04-09 09:13:16 -04:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2018-04-09 09:07:11 -04:00
|
|
|
kubeclient "k8s.io/client-go/kubernetes"
|
2017-11-20 09:30:52 -05:00
|
|
|
appsv1beta2 "k8s.io/client-go/kubernetes/typed/apps/v1beta2"
|
|
|
|
typesappsv1beta2 "k8s.io/client-go/kubernetes/typed/apps/v1beta2"
|
|
|
|
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
|
restclient "k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Factory is the kubernetes client factory
|
|
|
|
type Factory struct {
|
|
|
|
namespace string
|
|
|
|
config *restclient.Config
|
2019-01-11 11:23:44 -05:00
|
|
|
coreClientSet corev1.CoreV1Interface
|
|
|
|
appsClientSet appsv1beta2.AppsV1beta2Interface
|
2018-04-09 09:07:11 -04:00
|
|
|
clientSet *kubeclient.Clientset
|
2019-01-16 06:56:37 -05:00
|
|
|
experimental bool
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFactory creates a kubernetes client factory
|
2019-01-16 06:56:37 -05:00
|
|
|
func NewFactory(namespace string, config *restclient.Config, clientSet *kubeclient.Clientset, experimental bool) (*Factory, error) {
|
2017-11-20 09:30:52 -05:00
|
|
|
coreClientSet, err := corev1.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
appsClientSet, err := appsv1beta2.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Factory{
|
|
|
|
namespace: namespace,
|
|
|
|
config: config,
|
|
|
|
coreClientSet: coreClientSet,
|
|
|
|
appsClientSet: appsClientSet,
|
2018-04-09 09:07:11 -04:00
|
|
|
clientSet: clientSet,
|
2019-01-16 06:56:37 -05:00
|
|
|
experimental: experimental,
|
2017-11-20 09:30:52 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigMaps returns a client for kubernetes's config maps
|
|
|
|
func (s *Factory) ConfigMaps() corev1.ConfigMapInterface {
|
|
|
|
return s.coreClientSet.ConfigMaps(s.namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Secrets returns a client for kubernetes's secrets
|
|
|
|
func (s *Factory) Secrets() corev1.SecretInterface {
|
|
|
|
return s.coreClientSet.Secrets(s.namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Services returns a client for kubernetes's secrets
|
|
|
|
func (s *Factory) Services() corev1.ServiceInterface {
|
|
|
|
return s.coreClientSet.Services(s.namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pods returns a client for kubernetes's pods
|
|
|
|
func (s *Factory) Pods() corev1.PodInterface {
|
|
|
|
return s.coreClientSet.Pods(s.namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes returns a client for kubernetes's nodes
|
|
|
|
func (s *Factory) Nodes() corev1.NodeInterface {
|
|
|
|
return s.coreClientSet.Nodes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplicationControllers returns a client for kubernetes replication controllers
|
|
|
|
func (s *Factory) ReplicationControllers() corev1.ReplicationControllerInterface {
|
|
|
|
return s.coreClientSet.ReplicationControllers(s.namespace)
|
|
|
|
}
|
|
|
|
|
2018-04-09 09:13:16 -04:00
|
|
|
// ReplicaSets returns a client for kubernetes replace sets
|
2017-11-20 09:30:52 -05:00
|
|
|
func (s *Factory) ReplicaSets() typesappsv1beta2.ReplicaSetInterface {
|
|
|
|
return s.appsClientSet.ReplicaSets(s.namespace)
|
|
|
|
}
|
2018-04-09 09:07:11 -04:00
|
|
|
|
2018-04-24 06:28:08 -04:00
|
|
|
// DaemonSets returns a client for kubernetes daemon sets
|
|
|
|
func (s *Factory) DaemonSets() typesappsv1beta2.DaemonSetInterface {
|
|
|
|
return s.appsClientSet.DaemonSets(s.namespace)
|
|
|
|
}
|
|
|
|
|
2018-04-09 09:13:16 -04:00
|
|
|
// Stacks returns a client for Docker's Stack on Kubernetes
|
|
|
|
func (s *Factory) Stacks(allNamespaces bool) (StackClient, error) {
|
2019-01-16 06:56:37 -05:00
|
|
|
version, err := kubernetes.GetStackAPIVersion(s.clientSet.Discovery(), s.experimental)
|
2018-04-09 09:07:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-09 09:13:16 -04:00
|
|
|
namespace := s.namespace
|
|
|
|
if allNamespaces {
|
|
|
|
namespace = metav1.NamespaceAll
|
|
|
|
}
|
2018-04-09 09:07:11 -04:00
|
|
|
|
|
|
|
switch version {
|
|
|
|
case kubernetes.StackAPIV1Beta1:
|
2018-04-09 09:13:16 -04:00
|
|
|
return newStackV1Beta1(s.config, namespace)
|
2018-04-09 09:07:11 -04:00
|
|
|
case kubernetes.StackAPIV1Beta2:
|
2018-04-09 09:13:16 -04:00
|
|
|
return newStackV1Beta2(s.config, namespace)
|
2019-01-16 06:56:37 -05:00
|
|
|
case kubernetes.StackAPIV1Alpha3:
|
|
|
|
return newStackV1Alpha3(s.config, namespace)
|
2018-04-09 09:07:11 -04:00
|
|
|
default:
|
2019-01-16 06:56:37 -05:00
|
|
|
return nil, errors.Errorf("unsupported stack API version: %q", version)
|
2018-04-09 09:07:11 -04:00
|
|
|
}
|
|
|
|
}
|