From d26d4b84ecec1214149f4a51244f2b8a9abf40ef Mon Sep 17 00:00:00 2001 From: njtran Date: Fri, 5 Feb 2021 12:24:15 -0800 Subject: [PATCH] Defaults for Metrics Producers --- .../v1alpha1/metricsproducer_defaults.go | 50 +++++++++++++++++-- .../producers/scheduledcapacity/producer.go | 12 ++--- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/pkg/apis/autoscaling/v1alpha1/metricsproducer_defaults.go b/pkg/apis/autoscaling/v1alpha1/metricsproducer_defaults.go index 33a69ed018da..d3830fa67410 100644 --- a/pkg/apis/autoscaling/v1alpha1/metricsproducer_defaults.go +++ b/pkg/apis/autoscaling/v1alpha1/metricsproducer_defaults.go @@ -12,10 +12,54 @@ See the License for the specific language governing permissions and limitations under the License. */ -// +kubebuilder:webhook:path=/mutate-autoscaling-karpenter-sh-v1alpha1-metricsproducer,mutating=true,sideEffects=None,failurePolicy=fail,groups=autoscaling.karpenter.sh,resources=metricsproducers,verbs=create;update,versions=v1alpha1,name=mmetricsproducer.kb.io +// +kubebuilder:webhook:verbs=create;update,path=/mutate-autoscaling-karpenter-sh-v1alpha1-metricsproducer,mutating=true,sideEffects=None,failurePolicy=fail,groups=autoscaling.karpenter.sh,resources=metricsproducers,versions=v1alpha1,name=mmetricsproducer.kb.io package v1alpha1 -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *MetricsProducer) Default() { +import ( + "knative.dev/pkg/ptr" + "reflect" + "sigs.k8s.io/controller-runtime/pkg/webhook" +) + +var _ webhook.Defaulter = &MetricsProducer{} + +type specDefaulter interface { + defaultValues() +} + +// Default implements webhook.Defaulter so a webhook will be registered +func (m *MetricsProducer) Default() { + m.defaultValues() +} + +// Default PendingCapacity +func (s *PendingCapacitySpec) defaultValues() { +} + +// Default ReservedCapacity +func (s *ReservedCapacitySpec) defaultValues() { +} + +// Default ScheduleSpec +func (s *ScheduleSpec) defaultValues() { + if s.Timezone == nil { + s.Timezone = ptr.String("UTC") + } +} + +func (s *QueueSpec) defaultValues() { +} + +func (m *MetricsProducer) defaultValues() { + for _, defaulter := range []specDefaulter{ + m.Spec.PendingCapacity, + m.Spec.ReservedCapacity, + m.Spec.Schedule, + m.Spec.Queue, + } { + if !reflect.ValueOf(defaulter).IsNil() { + defaulter.defaultValues() + } + } } diff --git a/pkg/metrics/producers/scheduledcapacity/producer.go b/pkg/metrics/producers/scheduledcapacity/producer.go index 77e43b7de44d..05242f545c27 100644 --- a/pkg/metrics/producers/scheduledcapacity/producer.go +++ b/pkg/metrics/producers/scheduledcapacity/producer.go @@ -32,14 +32,10 @@ func (p *Producer) Reconcile() error { loc *time.Location err error ) - if p.Spec.Schedule.Timezone != nil { - loc, err = time.LoadLocation(*p.Spec.Schedule.Timezone) - if err != nil { - return fmt.Errorf("timezone was not a valid input") - } - } else { - // Set Default location to UTC if not specified - loc = time.UTC + // defaulting webhook ensures this is always defined + loc, err = time.LoadLocation(*p.Spec.Schedule.Timezone) + if err != nil { + return fmt.Errorf("timezone was not a valid input") } now := time.Now().In(loc)