2019-05-15 07:30:22 -07:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-31 17:59:36 -07:00
|
|
|
"strings"
|
2019-05-15 07:30:22 -07:00
|
|
|
"time"
|
|
|
|
|
2019-05-31 17:59:36 -07:00
|
|
|
"github.com/golang/glog"
|
2019-05-21 11:00:09 -07:00
|
|
|
types "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
|
|
|
|
"github.com/nirmata/kyverno/pkg/config"
|
2019-05-15 07:30:22 -07:00
|
|
|
apps "k8s.io/api/apps/v1"
|
|
|
|
certificates "k8s.io/api/certificates/v1beta1"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
2019-05-22 00:16:22 -07:00
|
|
|
"k8s.io/client-go/discovery"
|
|
|
|
"k8s.io/client-go/discovery/cached/memory"
|
2019-05-15 07:30:22 -07:00
|
|
|
"k8s.io/client-go/dynamic"
|
2019-05-22 00:16:22 -07:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2019-05-15 07:30:22 -07:00
|
|
|
csrtype "k8s.io/client-go/kubernetes/typed/certificates/v1beta1"
|
|
|
|
event "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//Client enables interaction with k8 resource
|
2019-05-15 07:30:22 -07:00
|
|
|
type Client struct {
|
2019-06-11 14:35:26 -07:00
|
|
|
client dynamic.Interface
|
|
|
|
cachedClient discovery.CachedDiscoveryInterface
|
|
|
|
clientConfig *rest.Config
|
|
|
|
kclient kubernetes.Interface
|
|
|
|
discoveryClient IDiscovery
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//NewClient creates new instance of client
|
2019-05-31 17:59:36 -07:00
|
|
|
func NewClient(config *rest.Config) (*Client, error) {
|
2019-06-11 14:35:26 -07:00
|
|
|
dclient, err := dynamic.NewForConfig(config)
|
2019-05-15 07:30:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-20 10:56:12 -07:00
|
|
|
|
2019-05-22 00:16:22 -07:00
|
|
|
kclient, err := kubernetes.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-11 14:35:26 -07:00
|
|
|
client := Client{
|
|
|
|
client: dclient,
|
2019-05-15 07:30:22 -07:00
|
|
|
clientConfig: config,
|
2019-05-22 00:16:22 -07:00
|
|
|
kclient: kclient,
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
// Set discovery client
|
|
|
|
discoveryClient := ServerPreferredResources{memory.NewMemCacheClient(kclient.Discovery())}
|
|
|
|
client.SetDiscovery(discoveryClient)
|
|
|
|
return &client, nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//GetKubePolicyDeployment returns kube policy depoyment value
|
2019-05-15 07:30:22 -07:00
|
|
|
func (c *Client) GetKubePolicyDeployment() (*apps.Deployment, error) {
|
|
|
|
kubePolicyDeployment, err := c.GetResource("deployments", config.KubePolicyNamespace, config.KubePolicyDeploymentName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
deploy := apps.Deployment{}
|
|
|
|
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(kubePolicyDeployment.UnstructuredContent(), &deploy); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &deploy, nil
|
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//GetEventsInterface provides typed interface for events
|
2019-05-15 07:30:22 -07:00
|
|
|
//TODO: can we use dynamic client to fetch the typed interface
|
|
|
|
// or generate a kube client value to access the interface
|
|
|
|
func (c *Client) GetEventsInterface() (event.EventInterface, error) {
|
2019-05-22 00:16:22 -07:00
|
|
|
return c.kclient.CoreV1().Events(""), nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//GetCSRInterface provides type interface for CSR
|
2019-05-15 07:30:22 -07:00
|
|
|
func (c *Client) GetCSRInterface() (csrtype.CertificateSigningRequestInterface, error) {
|
2019-05-22 00:16:22 -07:00
|
|
|
return c.kclient.CertificatesV1beta1().CertificateSigningRequests(), nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-22 00:16:22 -07:00
|
|
|
func (c *Client) getInterface(resource string) dynamic.NamespaceableResourceInterface {
|
|
|
|
return c.client.Resource(c.getGroupVersionMapper(resource))
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-22 00:16:22 -07:00
|
|
|
func (c *Client) getResourceInterface(resource string, namespace string) dynamic.ResourceInterface {
|
2019-05-15 07:30:22 -07:00
|
|
|
// Get the resource interface
|
2019-05-22 00:16:22 -07:00
|
|
|
namespaceableInterface := c.getInterface(resource)
|
2019-05-15 07:30:22 -07:00
|
|
|
// Get the namespacable interface
|
|
|
|
var resourceInteface dynamic.ResourceInterface
|
|
|
|
if namespace != "" {
|
|
|
|
resourceInteface = namespaceableInterface.Namespace(namespace)
|
|
|
|
} else {
|
|
|
|
resourceInteface = namespaceableInterface
|
|
|
|
}
|
|
|
|
return resourceInteface
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep this a stateful as the resource list will be based on the kubernetes version we connect to
|
2019-05-22 00:16:22 -07:00
|
|
|
func (c *Client) getGroupVersionMapper(resource string) schema.GroupVersionResource {
|
|
|
|
//TODO: add checks to see if the resource is supported
|
|
|
|
//TODO: build the resource list dynamically( by querying the registered resources)
|
2019-05-15 07:30:22 -07:00
|
|
|
//TODO: the error scenarios
|
2019-06-11 14:35:26 -07:00
|
|
|
return c.discoveryClient.getGVR(resource)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetResource returns the resource in unstructured/json format
|
2019-05-22 00:16:22 -07:00
|
|
|
func (c *Client) GetResource(resource string, namespace string, name string) (*unstructured.Unstructured, error) {
|
|
|
|
return c.getResourceInterface(resource, namespace).Get(name, meta.GetOptions{})
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListResource returns the list of resources in unstructured/json format
|
|
|
|
// Access items using []Items
|
2019-05-22 00:16:22 -07:00
|
|
|
func (c *Client) ListResource(resource string, namespace string) (*unstructured.UnstructuredList, error) {
|
|
|
|
return c.getResourceInterface(resource, namespace).List(meta.ListOptions{})
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
// DeleteResouce deletes the specified resource
|
2019-06-18 13:52:12 -07:00
|
|
|
func (c *Client) DeleteResouce(resource string, namespace string, name string, dryRun bool) error {
|
|
|
|
options := meta.DeleteOptions{}
|
|
|
|
if dryRun {
|
|
|
|
options = meta.DeleteOptions{DryRun: []string{meta.DryRunAll}}
|
|
|
|
}
|
|
|
|
return c.getResourceInterface(resource, namespace).Delete(name, &options)
|
2019-05-15 07:30:22 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-22 00:16:22 -07:00
|
|
|
// CreateResource creates object for the specified resource/namespace
|
2019-06-18 13:52:12 -07:00
|
|
|
func (c *Client) CreateResource(resource string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) {
|
|
|
|
options := meta.CreateOptions{}
|
|
|
|
if dryRun {
|
|
|
|
options = meta.CreateOptions{DryRun: []string{meta.DryRunAll}}
|
|
|
|
}
|
2019-05-15 07:30:22 -07:00
|
|
|
// convert typed to unstructured obj
|
|
|
|
if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil {
|
2019-06-18 13:52:12 -07:00
|
|
|
return c.getResourceInterface(resource, namespace).Create(unstructuredObj, options)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unable to create resource ")
|
|
|
|
}
|
|
|
|
|
2019-05-22 00:16:22 -07:00
|
|
|
// UpdateResource updates object for the specified resource/namespace
|
2019-06-18 13:52:12 -07:00
|
|
|
func (c *Client) UpdateResource(resource string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) {
|
|
|
|
options := meta.UpdateOptions{}
|
|
|
|
if dryRun {
|
|
|
|
options = meta.UpdateOptions{DryRun: []string{meta.DryRunAll}}
|
|
|
|
}
|
2019-05-15 07:30:22 -07:00
|
|
|
// convert typed to unstructured obj
|
|
|
|
if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil {
|
2019-06-18 13:52:12 -07:00
|
|
|
return c.getResourceInterface(resource, namespace).Update(unstructuredObj, options)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unable to update resource ")
|
|
|
|
}
|
|
|
|
|
2019-05-15 12:29:09 -07:00
|
|
|
// UpdateStatusResource updates the resource "status" subresource
|
2019-06-18 13:52:12 -07:00
|
|
|
func (c *Client) UpdateStatusResource(resource string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) {
|
|
|
|
options := meta.UpdateOptions{}
|
|
|
|
if dryRun {
|
|
|
|
options = meta.UpdateOptions{DryRun: []string{meta.DryRunAll}}
|
|
|
|
}
|
2019-05-15 12:29:09 -07:00
|
|
|
// convert typed to unstructured obj
|
|
|
|
if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil {
|
2019-06-18 13:52:12 -07:00
|
|
|
return c.getResourceInterface(resource, namespace).UpdateStatus(unstructuredObj, options)
|
2019-05-15 12:29:09 -07:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unable to update resource ")
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:30:22 -07:00
|
|
|
func convertToUnstructured(obj interface{}) *unstructured.Unstructured {
|
2019-05-21 18:50:51 -07:00
|
|
|
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&obj)
|
2019-05-15 07:30:22 -07:00
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("Unable to convert : %v", err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &unstructured.Unstructured{Object: unstructuredObj}
|
|
|
|
}
|
|
|
|
|
2019-06-03 16:02:34 -07:00
|
|
|
// GenerateResource creates resource of the specified kind(supports 'clone' & 'data')
|
2019-05-31 17:59:36 -07:00
|
|
|
func (c *Client) GenerateResource(generator types.Generation, namespace string) error {
|
|
|
|
var err error
|
2019-06-11 14:35:26 -07:00
|
|
|
rGVR := c.discoveryClient.getGVRFromKind(generator.Kind)
|
2019-05-31 17:59:36 -07:00
|
|
|
resource := &unstructured.Unstructured{}
|
2019-05-15 07:30:22 -07:00
|
|
|
|
2019-05-31 17:59:36 -07:00
|
|
|
var rdata map[string]interface{}
|
|
|
|
// data -> create new resource
|
|
|
|
if generator.Data != nil {
|
|
|
|
rdata, err = runtime.DefaultUnstructuredConverter.ToUnstructured(&generator.Data)
|
2019-05-22 20:22:26 -07:00
|
|
|
if err != nil {
|
2019-05-31 17:59:36 -07:00
|
|
|
utilruntime.HandleError(err)
|
2019-05-22 20:22:26 -07:00
|
|
|
return err
|
|
|
|
}
|
2019-05-31 17:59:36 -07:00
|
|
|
}
|
2019-05-31 18:45:23 -07:00
|
|
|
// clone -> copy from existing resource
|
|
|
|
if generator.Clone != nil {
|
|
|
|
resource, err = c.GetResource(rGVR.Resource, generator.Clone.Namespace, generator.Clone.Name)
|
2019-05-22 20:22:26 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-31 17:59:36 -07:00
|
|
|
rdata = resource.UnstructuredContent()
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
2019-05-15 11:24:27 -07:00
|
|
|
|
2019-05-31 17:59:36 -07:00
|
|
|
resource.SetUnstructuredContent(rdata)
|
|
|
|
resource.SetName(generator.Name)
|
2019-06-03 16:02:34 -07:00
|
|
|
resource.SetNamespace(namespace)
|
2019-05-31 17:59:36 -07:00
|
|
|
resource.SetResourceVersion("")
|
2019-05-15 07:30:22 -07:00
|
|
|
|
2019-05-31 17:59:36 -07:00
|
|
|
err = c.waitUntilNamespaceIsCreated(namespace)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Can't create a resource %s: %v", generator.Name, err)
|
|
|
|
return nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
2019-06-18 13:52:12 -07:00
|
|
|
_, err = c.CreateResource(rGVR.Resource, namespace, resource, false)
|
2019-05-31 17:59:36 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
2019-05-31 17:59:36 -07:00
|
|
|
return nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-31 17:59:36 -07:00
|
|
|
//To-Do remove this to use unstructured type
|
2019-05-21 18:50:51 -07:00
|
|
|
func convertToSecret(obj *unstructured.Unstructured) (v1.Secret, error) {
|
2019-05-15 07:30:22 -07:00
|
|
|
secret := v1.Secret{}
|
|
|
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &secret); err != nil {
|
2019-05-21 18:50:51 -07:00
|
|
|
return secret, err
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
2019-05-21 18:50:51 -07:00
|
|
|
return secret, nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-31 17:59:36 -07:00
|
|
|
//To-Do remove this to use unstructured type
|
2019-05-15 07:30:22 -07:00
|
|
|
func convertToCSR(obj *unstructured.Unstructured) (*certificates.CertificateSigningRequest, error) {
|
|
|
|
csr := certificates.CertificateSigningRequest{}
|
|
|
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &csr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &csr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Waits until namespace is created with maximum duration maxWaitTimeForNamespaceCreation
|
|
|
|
func (c *Client) waitUntilNamespaceIsCreated(name string) error {
|
|
|
|
timeStart := time.Now()
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
var lastError error
|
2019-05-15 07:30:22 -07:00
|
|
|
for time.Now().Sub(timeStart) < namespaceCreationMaxWaitTime {
|
2019-05-22 00:16:22 -07:00
|
|
|
_, lastError = c.GetResource(Namespaces, "", name)
|
2019-05-15 07:30:22 -07:00
|
|
|
if lastError == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(namespaceCreationWaitInterval)
|
|
|
|
}
|
|
|
|
return lastError
|
|
|
|
}
|
2019-05-15 11:24:27 -07:00
|
|
|
|
2019-06-11 14:35:26 -07:00
|
|
|
type IDiscovery interface {
|
|
|
|
getGVR(resource string) schema.GroupVersionResource
|
|
|
|
getGVRFromKind(kind string) schema.GroupVersionResource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetDiscovery(discoveryClient IDiscovery) {
|
|
|
|
c.discoveryClient = discoveryClient
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerPreferredResources struct {
|
|
|
|
cachedClient discovery.CachedDiscoveryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ServerPreferredResources) getGVR(resource string) schema.GroupVersionResource {
|
2019-05-22 00:16:22 -07:00
|
|
|
emptyGVR := schema.GroupVersionResource{}
|
|
|
|
serverresources, err := c.cachedClient.ServerPreferredResources()
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return emptyGVR
|
|
|
|
}
|
|
|
|
resources, err := discovery.GroupVersionResources(serverresources)
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return emptyGVR
|
|
|
|
}
|
|
|
|
//TODO using cached client to support cache validation and invalidation
|
|
|
|
// iterate over the key to compare the resource
|
2019-06-05 17:43:59 -07:00
|
|
|
for gvr := range resources {
|
2019-05-22 00:16:22 -07:00
|
|
|
if gvr.Resource == resource {
|
|
|
|
return gvr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return emptyGVR
|
2019-05-15 11:24:27 -07:00
|
|
|
}
|
2019-05-31 17:59:36 -07:00
|
|
|
|
2019-06-03 10:36:40 -07:00
|
|
|
//To-do: measure performance
|
|
|
|
//To-do: evaluate DefaultRESTMapper to fetch kind->resource mapping
|
2019-06-11 14:35:26 -07:00
|
|
|
func (c ServerPreferredResources) getGVRFromKind(kind string) schema.GroupVersionResource {
|
2019-05-31 17:59:36 -07:00
|
|
|
emptyGVR := schema.GroupVersionResource{}
|
|
|
|
serverresources, err := c.cachedClient.ServerPreferredResources()
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return emptyGVR
|
|
|
|
}
|
|
|
|
for _, serverresource := range serverresources {
|
|
|
|
for _, resource := range serverresource.APIResources {
|
|
|
|
if resource.Kind == kind && !strings.Contains(resource.Name, "/") {
|
|
|
|
gv, err := schema.ParseGroupVersion(serverresource.GroupVersion)
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return emptyGVR
|
|
|
|
}
|
|
|
|
return gv.WithResource(resource.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return emptyGVR
|
|
|
|
}
|