update based on resource type

feature/stateful_set
Karolis Rusenas 2018-04-17 08:53:13 +01:00
parent c85a2c6f35
commit f2937eff8a
1 changed files with 46 additions and 5 deletions

View File

@ -3,9 +3,13 @@ package kubernetes
import (
"fmt"
"github.com/keel-hq/keel/internal/k8s"
apps_v1 "k8s.io/api/apps/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
core_v1 "k8s.io/client-go/kubernetes/typed/core/v1"
// core_v1 "k8s.io/api/core/v1"
// "k8s.io/api/core/v1"
"k8s.io/api/core/v1"
@ -21,9 +25,10 @@ import (
type Implementer interface {
Namespaces() (*v1.NamespaceList, error)
Deployment(namespace, name string) (*v1beta1.Deployment, error)
// Deployment(namespace, name string) (*v1beta1.Deployment, error)
Deployments(namespace string) (*v1beta1.DeploymentList, error)
Update(deployment *v1beta1.Deployment) error
// Update(deployment *v1beta1.Deployment) error
Update(obj *k8s.GenericResource) error
Secret(namespace, name string) (*v1.Secret, error)
@ -87,6 +92,10 @@ func NewKubernetesImplementer(opts *Opts) (*KubernetesImplementer, error) {
return &KubernetesImplementer{client: client, cfg: cfg}, nil
}
func (i *KubernetesImplementer) Client() *kubernetes.Clientset {
return i.client
}
// Namespaces - get all namespaces
func (i *KubernetesImplementer) Namespaces() (*v1.NamespaceList, error) {
namespaces := i.client.Core().Namespaces()
@ -107,7 +116,21 @@ func (i *KubernetesImplementer) Deployments(namespace string) (*v1beta1.Deployme
}
// Update - update deployment
func (i *KubernetesImplementer) Update(deployment *v1beta1.Deployment) error {
// func (i *KubernetesImplementer) Update(deployment *v1beta1.Deployment) error {
// // retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
// // // Retrieve the latest version of Deployment before attempting update
// // // RetryOnConflict uses exponential backoff to avoid exhausting the apiserver
// // _, updateErr := i.client.Extensions().Deployments(deployment.Namespace).Update(deployment)
// // return updateErr
// // })
// // return retryErr
// _, err := i.client.Extensions().Deployments(deployment.Namespace).Update(deployment)
// return err
// }
// Update converts generic resource into specific kubernetes type and updates it
func (i *KubernetesImplementer) Update(obj *k8s.GenericResource) error {
// retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
// // Retrieve the latest version of Deployment before attempting update
// // RetryOnConflict uses exponential backoff to avoid exhausting the apiserver
@ -116,8 +139,26 @@ func (i *KubernetesImplementer) Update(deployment *v1beta1.Deployment) error {
// })
// return retryErr
_, err := i.client.Extensions().Deployments(deployment.Namespace).Update(deployment)
return err
switch resource := obj.GetResource().(type) {
case *apps_v1.Deployment:
_, err := i.client.Apps().Deployments(resource.Namespace).Update(resource)
if err != nil {
return err
}
case *apps_v1.StatefulSet:
_, err := i.client.Apps().StatefulSets(resource.Namespace).Update(resource)
if err != nil {
return err
}
case *apps_v1.DaemonSet:
_, err := i.client.Apps().DaemonSets(resource.Namespace).Update(resource)
if err != nil {
return err
}
}
// _, err := i.client.Extensions().Deployments(deployment.Namespace).Update(deployment)
return fmt.Errorf("unsupported object type")
}
// Secret - get secret