airshipctl/pkg/k8s/client/client.go
Kostiantyn Kalynovskyi 9afeccb785 [#20] Add abstraction to kubernetes interactions
Object Cluster will be a entry point to intractions with kubernetes
It will provide:
  - kubectl abstraction which will emulate kubectl apply
and other commands (in the future)
  - kubernets client-go kubernetes interface which has all operations,
including CRUD to kubernetes apps, core, extentions, etc... objects

Change-Id: Ie2961f68a160e720c264c622c1124f283bded161
2020-02-25 12:21:37 -06:00

69 lines
1.8 KiB
Go

package client
import (
"path/filepath"
"k8s.io/client-go/kubernetes"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/k8s/kubectl"
k8sutils "opendev.org/airship/airshipctl/pkg/k8s/utils"
)
const (
// buffDir is a directory that is used as a tmp storage for kubectl
buffDir = ".airship"
)
// Interface provides an abstraction layer to interactions
// with kubernetes clusters by getting Clientset which includes
// all kubernetes core objects with standard operations and kubectl
// interface that is built on top of kubectl libraries and implements
// such kubectl subcommands as kubectl apply (more will be added)
type Interface interface {
ClientSet() kubernetes.Interface
Kubectl() kubectl.Interface
}
// Client is implementation of Cluster interface
type Client struct {
kubectl kubectl.Interface
clientset kubernetes.Interface
}
// ClientSet getter for Clientset interface
func (c *Client) ClientSet() kubernetes.Interface {
return c.clientset
}
// Kubectl getter for Kubectl interface
func (c *Client) Kubectl() kubectl.Interface {
return c.kubectl
}
// NewClient returns Cluster interface with Kubectl
// and Clientset interfaces initialized
func NewClient(as *environment.AirshipCTLSettings) (Interface, error) {
f := k8sutils.FactoryFromKubeconfigPath(as.KubeConfigPath())
kctl := kubectl.NewKubectl(f).
WithBufferDir(filepath.Dir(as.AirshipConfigPath()) + buffDir)
clientSet, err := f.KubernetesClientSet()
if err != nil {
return nil, err
}
client := &Client{}
client.SetClientset(clientSet)
client.SetKubectl(kctl)
return client, nil
}
// SetClientset setter for Clientset interface
func (c *Client) SetClientset(cs kubernetes.Interface) {
c.clientset = cs
}
// SetKubectl setter for Kubectl interface
func (c *Client) SetKubectl(kctl kubectl.Interface) {
c.kubectl = kctl
}