From aa2c28c73312e6e6af32c5e16867c968af0b9f16 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Fri, 11 Jan 2019 13:42:32 +0200 Subject: [PATCH] Make autoscalerRef optional - use anyOf as a workaround for the openAPI object validation not accepting empty values - fix #23 --- artifacts/flagger/crd.yaml | 4 +++- charts/flagger/templates/crd.yaml | 5 +++-- pkg/apis/flagger/v1alpha3/types.go | 3 ++- pkg/apis/flagger/v1alpha3/zz_generated.deepcopy.go | 7 ++++++- pkg/controller/deployer.go | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/artifacts/flagger/crd.yaml b/artifacts/flagger/crd.yaml index 2d6339d0a..9acfa09b0 100644 --- a/artifacts/flagger/crd.yaml +++ b/artifacts/flagger/crd.yaml @@ -42,7 +42,9 @@ spec: name: type: string autoscalerRef: - type: object + anyOf: + - type: string + - type: object required: ['apiVersion', 'kind', 'name'] properties: apiVersion: diff --git a/charts/flagger/templates/crd.yaml b/charts/flagger/templates/crd.yaml index 9d41a35da..c9789f0f6 100644 --- a/charts/flagger/templates/crd.yaml +++ b/charts/flagger/templates/crd.yaml @@ -43,7 +43,9 @@ spec: name: type: string autoscalerRef: - type: object + anyOf: + - type: string + - type: object required: ['apiVersion', 'kind', 'name'] properties: apiVersion: @@ -98,5 +100,4 @@ spec: timeout: type: string pattern: "^[0-9]+(m|s)" - {{- end }} diff --git a/pkg/apis/flagger/v1alpha3/types.go b/pkg/apis/flagger/v1alpha3/types.go index bc0abe4ec..944f9efa1 100755 --- a/pkg/apis/flagger/v1alpha3/types.go +++ b/pkg/apis/flagger/v1alpha3/types.go @@ -46,7 +46,8 @@ type CanarySpec struct { TargetRef hpav1.CrossVersionObjectReference `json:"targetRef"` // reference to autoscaling resource - AutoscalerRef hpav1.CrossVersionObjectReference `json:"autoscalerRef"` + // +optional + AutoscalerRef *hpav1.CrossVersionObjectReference `json:"autoscalerRef,omitempty"` // virtual service spec Service CanaryService `json:"service"` diff --git a/pkg/apis/flagger/v1alpha3/zz_generated.deepcopy.go b/pkg/apis/flagger/v1alpha3/zz_generated.deepcopy.go index ff1469663..34b134d7a 100644 --- a/pkg/apis/flagger/v1alpha3/zz_generated.deepcopy.go +++ b/pkg/apis/flagger/v1alpha3/zz_generated.deepcopy.go @@ -21,6 +21,7 @@ limitations under the License. package v1alpha3 import ( + v1 "k8s.io/api/autoscaling/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -159,7 +160,11 @@ func (in *CanaryService) DeepCopy() *CanaryService { func (in *CanarySpec) DeepCopyInto(out *CanarySpec) { *out = *in out.TargetRef = in.TargetRef - out.AutoscalerRef = in.AutoscalerRef + if in.AutoscalerRef != nil { + in, out := &in.AutoscalerRef, &out.AutoscalerRef + *out = new(v1.CrossVersionObjectReference) + **out = **in + } in.Service.DeepCopyInto(&out.Service) in.CanaryAnalysis.DeepCopyInto(&out.CanaryAnalysis) if in.ProgressDeadlineSeconds != nil { diff --git a/pkg/controller/deployer.go b/pkg/controller/deployer.go index 39e8e431f..d034acaf3 100644 --- a/pkg/controller/deployer.go +++ b/pkg/controller/deployer.go @@ -231,7 +231,7 @@ func (c *CanaryDeployer) Sync(cd *flaggerv1.Canary) error { } } - if cd.Spec.AutoscalerRef.Kind == "HorizontalPodAutoscaler" { + if cd.Spec.AutoscalerRef != nil && cd.Spec.AutoscalerRef.Kind == "HorizontalPodAutoscaler" { if err := c.createPrimaryHpa(cd); err != nil { return fmt.Errorf("creating hpa %s.%s failed: %v", primaryName, cd.Namespace, err) }