Skip to content

Commit

Permalink
Make settings copyable (aws#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Jan 26, 2023
1 parent bd476c4 commit 08b676a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/apis/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var defaultSettings = &Settings{
DriftEnabled: false,
}

// +k8s:deepcopy-gen=true
type Settings struct {
BatchMaxDuration metav1.Duration
BatchIdleDuration metav1.Duration
Expand All @@ -49,7 +50,7 @@ func (*Settings) ConfigMap() string {

// Inject creates a Settings from the supplied ConfigMap
func (*Settings) Inject(ctx context.Context, cm *v1.ConfigMap) (context.Context, error) {
s := defaultSettings
s := defaultSettings.DeepCopy()

if err := configmap.Parse(cm.Data,
AsMetaDuration("batchMaxDuration", &s.BatchMaxDuration),
Expand All @@ -70,15 +71,15 @@ func (*Settings) Inject(ctx context.Context, cm *v1.ConfigMap) (context.Context,
// type ExampleStruct struct {
// Example metav1.Duration `json:"example" validate:"required,min=10m"`
// }
func (s *Settings) Validate() (err error) {
func (in *Settings) Validate() (err error) {
validate := validator.New()
if s.BatchMaxDuration.Duration <= 0 {
if in.BatchMaxDuration.Duration <= 0 {
err = multierr.Append(err, fmt.Errorf("batchMaxDuration cannot be negative"))
}
if s.BatchIdleDuration.Duration <= 0 {
if in.BatchIdleDuration.Duration <= 0 {
err = multierr.Append(err, fmt.Errorf("batchMaxDuration cannot be negative"))
}
return multierr.Append(err, validate.Struct(s))
return multierr.Append(err, validate.Struct(in))
}

// AsMetaDuration parses the value at key as a time.Duration into the target, if it exists.
Expand Down
39 changes: 39 additions & 0 deletions pkg/apis/settings/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 08b676a

Please sign in to comment.