knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/cronjob_defaults.go (about)

     1  /*
     2  Copyright 2021 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1
    18  
    19  import (
    20  	"context"
    21  )
    22  
    23  // CronJobDefaulter is a callback to validate a CronJob.
    24  type CronJobDefaulter func(context.Context, *CronJob)
    25  
    26  // SetDefaults implements apis.Defaultable
    27  func (c *CronJob) SetDefaults(ctx context.Context) {
    28  	if cd := GetCronJobDefaulter(ctx); cd != nil {
    29  		cd(ctx, c)
    30  	}
    31  }
    32  
    33  // cdKey is used for associating a CronJobDefaulter with a context.Context
    34  type cdKey struct{}
    35  
    36  func WithCronJobDefaulter(ctx context.Context, cd CronJobDefaulter) context.Context {
    37  	return context.WithValue(ctx, cdKey{}, cd)
    38  }
    39  
    40  // GetCronJobDefaulter extracts the CronJobDefaulter from the context.
    41  func GetCronJobDefaulter(ctx context.Context) CronJobDefaulter {
    42  	untyped := ctx.Value(cdKey{})
    43  	if untyped == nil {
    44  		return nil
    45  	}
    46  	return untyped.(CronJobDefaulter)
    47  }