From 033e760c8bb15ba25bbbe414434f5b89176b46a4 Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Fri, 17 Jul 2020 18:35:20 +0530 Subject: [PATCH] Incode CRD validation --- .../v1/workerpodautoscaler.go | 31 +++++++++++++++++-- .../v1alpha1/workerpodautoscaler.go | 31 +++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/pkg/generated/clientset/versioned/typed/workerpodautoscaler/v1/workerpodautoscaler.go b/pkg/generated/clientset/versioned/typed/workerpodautoscaler/v1/workerpodautoscaler.go index db560520..b7148aca 100644 --- a/pkg/generated/clientset/versioned/typed/workerpodautoscaler/v1/workerpodautoscaler.go +++ b/pkg/generated/clientset/versioned/typed/workerpodautoscaler/v1/workerpodautoscaler.go @@ -19,6 +19,7 @@ limitations under the License. package v1 import ( + "fmt" "time" wpav1 "github.com/practo/k8s-worker-pod-autoscaler/pkg/apis/workerpodautoscaler/v1" @@ -63,6 +64,18 @@ func newWorkerPodAutoScalers(c *K8sV1Client, namespace string) *workerPodAutoSca } } +// validate check if the caller has set the right fields. +func validate(s *wpav1.WorkerPodAutoScaler) error { + if *s.Spec.MaxReplicas < 1 { + return fmt.Errorf("'max' is a required parameter and must be at least 1") + } + if *s.Spec.MinReplicas > *s.Spec.MaxReplicas { + return fmt.Errorf("'max' must be greater than or equal to 'min'") + } + + return nil +} + // Get takes name of the workerPodAutoScaler, and returns the corresponding workerPodAutoScaler object, and an error if there is any. func (c *workerPodAutoScalers) Get(name string, options v1.GetOptions) (result *wpav1.WorkerPodAutoScaler, err error) { result = &wpav1.WorkerPodAutoScaler{} @@ -111,18 +124,31 @@ func (c *workerPodAutoScalers) Watch(opts v1.ListOptions) (watch.Interface, erro // Create takes the representation of a workerPodAutoScaler and creates it. Returns the server's representation of the workerPodAutoScaler, and an error, if there is any. func (c *workerPodAutoScalers) Create(workerPodAutoScaler *wpav1.WorkerPodAutoScaler) (result *wpav1.WorkerPodAutoScaler, err error) { result = &wpav1.WorkerPodAutoScaler{} + + err = validate(workerPodAutoScaler) + if err != nil { + return result, err + } + err = c.client.Post(). Namespace(c.ns). Resource("workerpodautoscalers"). Body(workerPodAutoScaler). Do(). Into(result) - return + + return result, err } // Update takes the representation of a workerPodAutoScaler and updates it. Returns the server's representation of the workerPodAutoScaler, and an error, if there is any. func (c *workerPodAutoScalers) Update(workerPodAutoScaler *wpav1.WorkerPodAutoScaler) (result *wpav1.WorkerPodAutoScaler, err error) { result = &wpav1.WorkerPodAutoScaler{} + + err = validate(workerPodAutoScaler) + if err != nil { + return result, err + } + err = c.client.Put(). Namespace(c.ns). Resource("workerpodautoscalers"). @@ -130,7 +156,8 @@ func (c *workerPodAutoScalers) Update(workerPodAutoScaler *wpav1.WorkerPodAutoSc Body(workerPodAutoScaler). Do(). Into(result) - return + + return result, err } // UpdateStatus was generated because the type contains a Status member. diff --git a/pkg/generated/clientset/versioned/typed/workerpodautoscaler/v1alpha1/workerpodautoscaler.go b/pkg/generated/clientset/versioned/typed/workerpodautoscaler/v1alpha1/workerpodautoscaler.go index 088087f5..4923272b 100644 --- a/pkg/generated/clientset/versioned/typed/workerpodautoscaler/v1alpha1/workerpodautoscaler.go +++ b/pkg/generated/clientset/versioned/typed/workerpodautoscaler/v1alpha1/workerpodautoscaler.go @@ -19,6 +19,7 @@ limitations under the License. package v1alpha1 import ( + "fmt" "time" v1alpha1 "github.com/practo/k8s-worker-pod-autoscaler/pkg/apis/workerpodautoscaler/v1alpha1" @@ -63,6 +64,18 @@ func newWorkerPodAutoScalers(c *K8sV1alpha1Client, namespace string) *workerPodA } } +// validate check if the caller has set the right fields. +func validate(s *v1alpha1.WorkerPodAutoScaler) error { + if *s.Spec.MaxReplicas < 1 { + return fmt.Errorf("'max' is a required parameter and must be at least 1") + } + if *s.Spec.MinReplicas > *s.Spec.MaxReplicas { + return fmt.Errorf("'max' must be greater than or equal to 'min'") + } + + return nil +} + // Get takes name of the workerPodAutoScaler, and returns the corresponding workerPodAutoScaler object, and an error if there is any. func (c *workerPodAutoScalers) Get(name string, options v1.GetOptions) (result *v1alpha1.WorkerPodAutoScaler, err error) { result = &v1alpha1.WorkerPodAutoScaler{} @@ -111,18 +124,31 @@ func (c *workerPodAutoScalers) Watch(opts v1.ListOptions) (watch.Interface, erro // Create takes the representation of a workerPodAutoScaler and creates it. Returns the server's representation of the workerPodAutoScaler, and an error, if there is any. func (c *workerPodAutoScalers) Create(workerPodAutoScaler *v1alpha1.WorkerPodAutoScaler) (result *v1alpha1.WorkerPodAutoScaler, err error) { result = &v1alpha1.WorkerPodAutoScaler{} + + err = validate(workerPodAutoScaler) + if err != nil { + return result, err + } + err = c.client.Post(). Namespace(c.ns). Resource("workerpodautoscalers"). Body(workerPodAutoScaler). Do(). Into(result) - return + + return result, err } // Update takes the representation of a workerPodAutoScaler and updates it. Returns the server's representation of the workerPodAutoScaler, and an error, if there is any. func (c *workerPodAutoScalers) Update(workerPodAutoScaler *v1alpha1.WorkerPodAutoScaler) (result *v1alpha1.WorkerPodAutoScaler, err error) { result = &v1alpha1.WorkerPodAutoScaler{} + + err = validate(workerPodAutoScaler) + if err != nil { + return result, err + } + err = c.client.Put(). Namespace(c.ns). Resource("workerpodautoscalers"). @@ -130,7 +156,8 @@ func (c *workerPodAutoScalers) Update(workerPodAutoScaler *v1alpha1.WorkerPodAut Body(workerPodAutoScaler). Do(). Into(result) - return + + return result, err } // UpdateStatus was generated because the type contains a Status member.