Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defaults for Metrics Producers #222

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions pkg/apis/autoscaling/v1alpha1/metricsproducer_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
12 changes: 4 additions & 8 deletions pkg/metrics/producers/scheduledcapacity/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down