From 9bf27067bf2f2d5da27a94a40e5038c79c1de2a4 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Tue, 18 Jun 2024 17:45:01 -0400 Subject: [PATCH] Add missing types (#147) - admissionregistration.k8s.io/v1 ValidatingAdmissionPolicy - admissionregistration.k8s.io/v1 ValidatingAdmissionPolicyBinding - autoscaling/v2 HorizontalPodAutoscaler - storage.k8s.io/v1 CSIStorageCapacity Signed-off-by: Scott Andrews --- .../v1/validatingadmissionpolicy.go | 220 + .../v1/validatingadmissionpolicybinding.go | 55 + .../v1/zz_generated.die.go | 3560 ++++++++++++ .../v1/zz_generated.die_test.go | 126 + .../v1/{userinfo.go => selfsubjectreview.go} | 16 +- apis/authentication/v1/zz_generated.die.go | 896 ++- .../v1/zz_generated.die_test.go | 36 +- .../autoscaling/v2/horizontalpodautoscaler.go | 417 ++ apis/autoscaling/v2/zz_generated.die.go | 4803 +++++++++++++++++ apis/autoscaling/v2/zz_generated.die_test.go | 225 + apis/storage/v1/csistoragecapacities.go | 44 + apis/storage/v1/zz_generated.die.go | 352 ++ apis/storage/v1/zz_generated.die_test.go | 9 + 13 files changed, 10542 insertions(+), 217 deletions(-) create mode 100644 apis/admissionregistration/v1/validatingadmissionpolicy.go create mode 100644 apis/admissionregistration/v1/validatingadmissionpolicybinding.go rename apis/authentication/v1/{userinfo.go => selfsubjectreview.go} (68%) create mode 100644 apis/autoscaling/v2/horizontalpodautoscaler.go create mode 100644 apis/autoscaling/v2/zz_generated.die.go create mode 100644 apis/autoscaling/v2/zz_generated.die_test.go create mode 100644 apis/storage/v1/csistoragecapacities.go diff --git a/apis/admissionregistration/v1/validatingadmissionpolicy.go b/apis/admissionregistration/v1/validatingadmissionpolicy.go new file mode 100644 index 0000000..a8b7121 --- /dev/null +++ b/apis/admissionregistration/v1/validatingadmissionpolicy.go @@ -0,0 +1,220 @@ +/* +Copyright 2024 the original author or authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +import ( + admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + diemetav1 "reconciler.io/dies/apis/meta/v1" +) + +// +die:object=true,apiVersion=admissionregistration.k8s.io/v1,kind=ValidatingAdmissionPolicy +type _ = admissionregistrationv1.ValidatingAdmissionPolicy + +// +die +type _ = admissionregistrationv1.ValidatingAdmissionPolicySpec + +func (d *ValidatingAdmissionPolicySpecDie) ParamKindDie(fn func(d *ParamKindDie)) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + d := ParamKindBlank.DieImmutable(false).DieFeedPtr(r.ParamKind) + fn(d) + r.ParamKind = d.DieReleasePtr() + }) +} + +func (d *ValidatingAdmissionPolicySpecDie) MatchConstraintsDie(fn func(d *MatchResourcesDie)) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + d := MatchResourcesBlank.DieImmutable(false).DieFeedPtr(r.MatchConstraints) + fn(d) + r.MatchConstraints = d.DieReleasePtr() + }) +} + +func (d *ValidatingAdmissionPolicySpecDie) ValidationsDie(validations ...*ValidationDie) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.Validations = make([]admissionregistrationv1.Validation, len(validations)) + for i := range validations { + r.Validations[i] = validations[i].DieRelease() + } + }) +} + +func (d *ValidatingAdmissionPolicySpecDie) AuditAnnotationsDie(annotations ...*AuditAnnotationDie) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.AuditAnnotations = make([]admissionregistrationv1.AuditAnnotation, len(annotations)) + for i := range annotations { + r.AuditAnnotations[i] = annotations[i].DieRelease() + } + }) +} + +func (d *ValidatingAdmissionPolicySpecDie) MatchConditionDie(name string, fn func(d *MatchConditionDie)) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + for i := range r.MatchConditions { + if name == r.MatchConditions[i].Name { + d := MatchConditionBlank.DieImmutable(false).DieFeed(r.MatchConditions[i]) + fn(d) + r.MatchConditions[i] = d.DieRelease() + return + } + } + + d := MatchConditionBlank.DieImmutable(false).DieFeed(admissionregistrationv1.MatchCondition{Name: name}) + fn(d) + r.MatchConditions = append(r.MatchConditions, d.DieRelease()) + }) +} + +func (d *ValidatingAdmissionPolicySpecDie) VariablesDie(name string, fn func(d *VariableDie)) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + for i := range r.Variables { + if name == r.Variables[i].Name { + d := VariableBlank.DieImmutable(false).DieFeed(r.Variables[i]) + fn(d) + r.Variables[i] = d.DieRelease() + return + } + } + + d := VariableBlank.DieImmutable(false).DieFeed(admissionregistrationv1.Variable{Name: name}) + fn(d) + r.Variables = append(r.Variables, d.DieRelease()) + }) +} + +// +die +type _ = admissionregistrationv1.ParamKind + +// +die +type _ = admissionregistrationv1.MatchResources + +func (d *MatchResourcesDie) NamespaceSelectorDie(fn func(d *diemetav1.LabelSelectorDie)) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + d := diemetav1.LabelSelectorBlank.DieImmutable(false).DieFeedPtr(r.NamespaceSelector) + fn(d) + r.NamespaceSelector = d.DieReleasePtr() + }) +} + +func (d *MatchResourcesDie) ObjectSelectorDie(fn func(d *diemetav1.LabelSelectorDie)) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + d := diemetav1.LabelSelectorBlank.DieImmutable(false).DieFeedPtr(r.ObjectSelector) + fn(d) + r.ObjectSelector = d.DieReleasePtr() + }) +} + +func (d *MatchResourcesDie) ResourceRulesDie(rules ...*NamedRuleWithOperationsDie) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + r.ResourceRules = make([]admissionregistrationv1.NamedRuleWithOperations, len(rules)) + for i := range rules { + r.ResourceRules[i] = rules[i].DieRelease() + } + }) +} + +func (d *MatchResourcesDie) ExcludeResourceRulesDie(rules ...*NamedRuleWithOperationsDie) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + r.ExcludeResourceRules = make([]admissionregistrationv1.NamedRuleWithOperations, len(rules)) + for i := range rules { + r.ExcludeResourceRules[i] = rules[i].DieRelease() + } + }) +} + +// +die +type _ = admissionregistrationv1.NamedRuleWithOperations + +func (d *NamedRuleWithOperationsDie) Operations(v ...admissionregistrationv1.OperationType) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + r.Operations = v + }) +} + +func (d *NamedRuleWithOperationsDie) Rule(v admissionregistrationv1.Rule) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + r.Rule = v + }) +} + +func (d *NamedRuleWithOperationsDie) APIGroups(v ...string) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + r.APIGroups = v + }) +} + +func (d *NamedRuleWithOperationsDie) APIVersions(v ...string) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + r.APIVersions = v + }) +} + +func (d *NamedRuleWithOperationsDie) Resources(v ...string) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + r.Resources = v + }) +} + +func (d *NamedRuleWithOperationsDie) Scope(v *admissionregistrationv1.ScopeType) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + r.Scope = v + }) +} + +// +die +type _ = admissionregistrationv1.Validation + +// +die +type _ = admissionregistrationv1.AuditAnnotation + +// +die +type _ = admissionregistrationv1.Variable + +// +die +type _ = admissionregistrationv1.ValidatingAdmissionPolicyStatus + +func (d *ValidatingAdmissionPolicyStatusDie) TypeCheckingDie(fn func(d *TypeCheckingDie)) *ValidatingAdmissionPolicyStatusDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyStatus) { + d := TypeCheckingBlank.DieImmutable(false).DieFeedPtr(r.TypeChecking) + fn(d) + r.TypeChecking = d.DieReleasePtr() + }) +} + +func (d *ValidatingAdmissionPolicyStatusDie) ConditionsDie(conditions ...*diemetav1.ConditionDie) *ValidatingAdmissionPolicyStatusDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyStatus) { + r.Conditions = make([]metav1.Condition, len(conditions)) + for i := range conditions { + r.Conditions[i] = conditions[i].DieRelease() + } + }) +} + +// +die +type _ = admissionregistrationv1.TypeChecking + +func (d *TypeCheckingDie) ExpressionWarningsDie(warnings ...*ExpressionWarningDie) *TypeCheckingDie { + return d.DieStamp(func(r *admissionregistrationv1.TypeChecking) { + r.ExpressionWarnings = make([]admissionregistrationv1.ExpressionWarning, len(warnings)) + for i := range warnings { + r.ExpressionWarnings[i] = warnings[i].DieRelease() + } + }) +} + +// +die +type _ = admissionregistrationv1.ExpressionWarning diff --git a/apis/admissionregistration/v1/validatingadmissionpolicybinding.go b/apis/admissionregistration/v1/validatingadmissionpolicybinding.go new file mode 100644 index 0000000..1292cae --- /dev/null +++ b/apis/admissionregistration/v1/validatingadmissionpolicybinding.go @@ -0,0 +1,55 @@ +/* +Copyright 2024 the original author or authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +import ( + admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + diemetav1 "reconciler.io/dies/apis/meta/v1" +) + +// +die:object=true,apiVersion=admissionregistration.k8s.io/v1,kind=ValidatingAdmissionPolicyBinding +type _ = admissionregistrationv1.ValidatingAdmissionPolicyBinding + +// +die +type _ = admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec + +func (d *ValidatingAdmissionPolicyBindingSpecDie) ParamRefDie(fn func(d *ParamRefDie)) *ValidatingAdmissionPolicyBindingSpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) { + d := ParamRefBlank.DieImmutable(false).DieFeedPtr(r.ParamRef) + fn(d) + r.ParamRef = d.DieReleasePtr() + }) +} + +func (d *ValidatingAdmissionPolicyBindingSpecDie) MatchResourcesDie(fn func(d *MatchResourcesDie)) *ValidatingAdmissionPolicyBindingSpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) { + d := MatchResourcesBlank.DieImmutable(false).DieFeedPtr(r.MatchResources) + fn(d) + r.MatchResources = d.DieReleasePtr() + }) +} + +// +die +type _ = admissionregistrationv1.ParamRef + +func (d *ParamRefDie) SelectorDie(fn func(d *diemetav1.LabelSelectorDie)) *ParamRefDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamRef) { + d := diemetav1.LabelSelectorBlank.DieImmutable(false).DieFeedPtr(r.Selector) + fn(d) + r.Selector = d.DieReleasePtr() + }) +} diff --git a/apis/admissionregistration/v1/zz_generated.die.go b/apis/admissionregistration/v1/zz_generated.die.go index ad5c4ba..93b9069 100644 --- a/apis/admissionregistration/v1/zz_generated.die.go +++ b/apis/admissionregistration/v1/zz_generated.die.go @@ -1949,6 +1949,3566 @@ func (d *MutatingWebhookDie) MatchConditions(v ...admissionregistrationv1.MatchC }) } +var ValidatingAdmissionPolicyBlank = (&ValidatingAdmissionPolicyDie{}).DieFeed(admissionregistrationv1.ValidatingAdmissionPolicy{}) + +type ValidatingAdmissionPolicyDie struct { + metav1.FrozenObjectMeta + mutable bool + r admissionregistrationv1.ValidatingAdmissionPolicy +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ValidatingAdmissionPolicyDie) DieImmutable(immutable bool) *ValidatingAdmissionPolicyDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ValidatingAdmissionPolicyDie) DieFeed(r admissionregistrationv1.ValidatingAdmissionPolicy) *ValidatingAdmissionPolicyDie { + if d.mutable { + d.FrozenObjectMeta = metav1.FreezeObjectMeta(r.ObjectMeta) + d.r = r + return d + } + return &ValidatingAdmissionPolicyDie{ + FrozenObjectMeta: metav1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ValidatingAdmissionPolicyDie) DieFeedPtr(r *admissionregistrationv1.ValidatingAdmissionPolicy) *ValidatingAdmissionPolicyDie { + if r == nil { + r = &admissionregistrationv1.ValidatingAdmissionPolicy{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ValidatingAdmissionPolicyDie) DieFeedJSON(j []byte) *ValidatingAdmissionPolicyDie { + r := admissionregistrationv1.ValidatingAdmissionPolicy{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ValidatingAdmissionPolicyDie) DieFeedYAML(y []byte) *ValidatingAdmissionPolicyDie { + r := admissionregistrationv1.ValidatingAdmissionPolicy{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ValidatingAdmissionPolicyDie) DieFeedYAMLFile(name string) *ValidatingAdmissionPolicyDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicyDie) DieFeedRawExtension(raw runtime.RawExtension) *ValidatingAdmissionPolicyDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ValidatingAdmissionPolicyDie) DieRelease() admissionregistrationv1.ValidatingAdmissionPolicy { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ValidatingAdmissionPolicyDie) DieReleasePtr() *admissionregistrationv1.ValidatingAdmissionPolicy { + r := d.DieRelease() + return &r +} + +// DieReleaseUnstructured returns the resource managed by the die as an unstructured object. Panics on error. +func (d *ValidatingAdmissionPolicyDie) DieReleaseUnstructured() *unstructured.Unstructured { + r := d.DieReleasePtr() + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r) + if err != nil { + panic(err) + } + return &unstructured.Unstructured{ + Object: u, + } +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ValidatingAdmissionPolicyDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ValidatingAdmissionPolicyDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicyDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ValidatingAdmissionPolicyDie) DieStamp(fn func(r *admissionregistrationv1.ValidatingAdmissionPolicy)) *ValidatingAdmissionPolicyDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ValidatingAdmissionPolicyDie) DieStampAt(jp string, fn interface{}) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ValidatingAdmissionPolicyDie) DieWith(fns ...func(d *ValidatingAdmissionPolicyDie)) *ValidatingAdmissionPolicyDie { + nd := ValidatingAdmissionPolicyBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ValidatingAdmissionPolicyDie) DeepCopy() *ValidatingAdmissionPolicyDie { + r := *d.r.DeepCopy() + return &ValidatingAdmissionPolicyDie{ + FrozenObjectMeta: metav1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +var _ runtime.Object = (*ValidatingAdmissionPolicyDie)(nil) + +func (d *ValidatingAdmissionPolicyDie) DeepCopyObject() runtime.Object { + return d.r.DeepCopy() +} + +func (d *ValidatingAdmissionPolicyDie) GetObjectKind() schema.ObjectKind { + r := d.DieRelease() + return r.GetObjectKind() +} + +func (d *ValidatingAdmissionPolicyDie) MarshalJSON() ([]byte, error) { + return json.Marshal(d.r) +} + +func (d *ValidatingAdmissionPolicyDie) UnmarshalJSON(b []byte) error { + if !d.mutable { + return fmtx.Errorf("cannot unmarshal into immutable dies, create a mutable version first") + } + r := &admissionregistrationv1.ValidatingAdmissionPolicy{} + err := json.Unmarshal(b, r) + *d = *d.DieFeed(*r) + return err +} + +// DieDefaultTypeMetadata sets the APIVersion and Kind to "admissionregistration.k8s.io/v1" and "ValidatingAdmissionPolicy" respectively. +func (d *ValidatingAdmissionPolicyDie) DieDefaultTypeMetadata() *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + r.APIVersion = "admissionregistration.k8s.io/v1" + r.Kind = "ValidatingAdmissionPolicy" + }) +} + +// APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources +func (d *ValidatingAdmissionPolicyDie) APIVersion(v string) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + r.APIVersion = v + }) +} + +// Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds +func (d *ValidatingAdmissionPolicyDie) Kind(v string) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + r.Kind = v + }) +} + +// TypeMetadata standard object's type metadata. +func (d *ValidatingAdmissionPolicyDie) TypeMetadata(v apismetav1.TypeMeta) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + r.TypeMeta = v + }) +} + +// TypeMetadataDie stamps the resource's TypeMeta field with a mutable die. +func (d *ValidatingAdmissionPolicyDie) TypeMetadataDie(fn func(d *metav1.TypeMetaDie)) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + d := metav1.TypeMetaBlank.DieImmutable(false).DieFeed(r.TypeMeta) + fn(d) + r.TypeMeta = d.DieRelease() + }) +} + +// Metadata standard object's metadata. +func (d *ValidatingAdmissionPolicyDie) Metadata(v apismetav1.ObjectMeta) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + r.ObjectMeta = v + }) +} + +// MetadataDie stamps the resource's ObjectMeta field with a mutable die. +func (d *ValidatingAdmissionPolicyDie) MetadataDie(fn func(d *metav1.ObjectMetaDie)) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + d := metav1.ObjectMetaBlank.DieImmutable(false).DieFeed(r.ObjectMeta) + fn(d) + r.ObjectMeta = d.DieRelease() + }) +} + +// SpecDie stamps the resource's spec field with a mutable die. +func (d *ValidatingAdmissionPolicyDie) SpecDie(fn func(d *ValidatingAdmissionPolicySpecDie)) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + d := ValidatingAdmissionPolicySpecBlank.DieImmutable(false).DieFeed(r.Spec) + fn(d) + r.Spec = d.DieRelease() + }) +} + +// StatusDie stamps the resource's status field with a mutable die. +func (d *ValidatingAdmissionPolicyDie) StatusDie(fn func(d *ValidatingAdmissionPolicyStatusDie)) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + d := ValidatingAdmissionPolicyStatusBlank.DieImmutable(false).DieFeed(r.Status) + fn(d) + r.Status = d.DieRelease() + }) +} + +// Specification of the desired behavior of the ValidatingAdmissionPolicy. +func (d *ValidatingAdmissionPolicyDie) Spec(v admissionregistrationv1.ValidatingAdmissionPolicySpec) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + r.Spec = v + }) +} + +// The status of the ValidatingAdmissionPolicy, including warnings that are useful to determine if the policy +// +// behaves in the expected way. +// +// Populated by the system. +// +// Read-only. +func (d *ValidatingAdmissionPolicyDie) Status(v admissionregistrationv1.ValidatingAdmissionPolicyStatus) *ValidatingAdmissionPolicyDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicy) { + r.Status = v + }) +} + +var ValidatingAdmissionPolicySpecBlank = (&ValidatingAdmissionPolicySpecDie{}).DieFeed(admissionregistrationv1.ValidatingAdmissionPolicySpec{}) + +type ValidatingAdmissionPolicySpecDie struct { + mutable bool + r admissionregistrationv1.ValidatingAdmissionPolicySpec +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ValidatingAdmissionPolicySpecDie) DieImmutable(immutable bool) *ValidatingAdmissionPolicySpecDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ValidatingAdmissionPolicySpecDie) DieFeed(r admissionregistrationv1.ValidatingAdmissionPolicySpec) *ValidatingAdmissionPolicySpecDie { + if d.mutable { + d.r = r + return d + } + return &ValidatingAdmissionPolicySpecDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ValidatingAdmissionPolicySpecDie) DieFeedPtr(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) *ValidatingAdmissionPolicySpecDie { + if r == nil { + r = &admissionregistrationv1.ValidatingAdmissionPolicySpec{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ValidatingAdmissionPolicySpecDie) DieFeedJSON(j []byte) *ValidatingAdmissionPolicySpecDie { + r := admissionregistrationv1.ValidatingAdmissionPolicySpec{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ValidatingAdmissionPolicySpecDie) DieFeedYAML(y []byte) *ValidatingAdmissionPolicySpecDie { + r := admissionregistrationv1.ValidatingAdmissionPolicySpec{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ValidatingAdmissionPolicySpecDie) DieFeedYAMLFile(name string) *ValidatingAdmissionPolicySpecDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicySpecDie) DieFeedRawExtension(raw runtime.RawExtension) *ValidatingAdmissionPolicySpecDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ValidatingAdmissionPolicySpecDie) DieRelease() admissionregistrationv1.ValidatingAdmissionPolicySpec { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ValidatingAdmissionPolicySpecDie) DieReleasePtr() *admissionregistrationv1.ValidatingAdmissionPolicySpec { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ValidatingAdmissionPolicySpecDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ValidatingAdmissionPolicySpecDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicySpecDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ValidatingAdmissionPolicySpecDie) DieStamp(fn func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec)) *ValidatingAdmissionPolicySpecDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ValidatingAdmissionPolicySpecDie) DieStampAt(jp string, fn interface{}) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ValidatingAdmissionPolicySpecDie) DieWith(fns ...func(d *ValidatingAdmissionPolicySpecDie)) *ValidatingAdmissionPolicySpecDie { + nd := ValidatingAdmissionPolicySpecBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ValidatingAdmissionPolicySpecDie) DeepCopy() *ValidatingAdmissionPolicySpecDie { + r := *d.r.DeepCopy() + return &ValidatingAdmissionPolicySpecDie{ + mutable: d.mutable, + r: r, + } +} + +// ParamKind specifies the kind of resources used to parameterize this policy. +// +// If absent, there are no parameters for this policy and the param CEL variable will not be provided to validation expressions. +// +// If ParamKind refers to a non-existent kind, this policy definition is mis-configured and the FailurePolicy is applied. +// +// If paramKind is specified but paramRef is unset in ValidatingAdmissionPolicyBinding, the params variable will be null. +func (d *ValidatingAdmissionPolicySpecDie) ParamKind(v *admissionregistrationv1.ParamKind) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.ParamKind = v + }) +} + +// MatchConstraints specifies what resources this policy is designed to validate. +// +// The AdmissionPolicy cares about a request if it matches _all_ Constraints. +// +// # However, in order to prevent clusters from being put into an unstable state that cannot be recovered from via the API +// +// ValidatingAdmissionPolicy cannot match ValidatingAdmissionPolicy and ValidatingAdmissionPolicyBinding. +// +// Required. +func (d *ValidatingAdmissionPolicySpecDie) MatchConstraints(v *admissionregistrationv1.MatchResources) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.MatchConstraints = v + }) +} + +// Validations contain CEL expressions which is used to apply the validation. +// +// Validations and AuditAnnotations may not both be empty; a minimum of one Validations or AuditAnnotations is +// +// required. +func (d *ValidatingAdmissionPolicySpecDie) Validations(v ...admissionregistrationv1.Validation) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.Validations = v + }) +} + +// failurePolicy defines how to handle failures for the admission policy. Failures can +// +// occur from CEL expression parse errors, type check errors, runtime errors and invalid +// +// or mis-configured policy definitions or bindings. +// +// A policy is invalid if spec.paramKind refers to a non-existent Kind. +// +// A binding is invalid if spec.paramRef.name refers to a non-existent resource. +// +// failurePolicy does not define how validations that evaluate to false are handled. +// +// # When failurePolicy is set to Fail, ValidatingAdmissionPolicyBinding validationActions +// +// define how failures are enforced. +// +// Allowed values are Ignore or Fail. Defaults to Fail. +func (d *ValidatingAdmissionPolicySpecDie) FailurePolicy(v *admissionregistrationv1.FailurePolicyType) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.FailurePolicy = v + }) +} + +// auditAnnotations contains CEL expressions which are used to produce audit +// +// annotations for the audit event of the API request. +// +// validations and auditAnnotations may not both be empty; a least one of validations or auditAnnotations is +// +// required. +func (d *ValidatingAdmissionPolicySpecDie) AuditAnnotations(v ...admissionregistrationv1.AuditAnnotation) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.AuditAnnotations = v + }) +} + +// MatchConditions is a list of conditions that must be met for a request to be validated. +// +// Match conditions filter requests that have already been matched by the rules, +// +// namespaceSelector, and objectSelector. An empty list of matchConditions matches all requests. +// +// There are a maximum of 64 match conditions allowed. +// +// # If a parameter object is provided, it can be accessed via the `params` handle in the same +// +// manner as validation expressions. +// +// The exact matching logic is (in order): +// +// 1. If ANY matchCondition evaluates to FALSE, the policy is skipped. +// +// 2. If ALL matchConditions evaluate to TRUE, the policy is evaluated. +// +// 3. If any matchCondition evaluates to an error (but none are FALSE): +// +// - If failurePolicy=Fail, reject the request +// +// - If failurePolicy=Ignore, the policy is skipped +func (d *ValidatingAdmissionPolicySpecDie) MatchConditions(v ...admissionregistrationv1.MatchCondition) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.MatchConditions = v + }) +} + +// Variables contain definitions of variables that can be used in composition of other expressions. +// +// Each variable is defined as a named CEL expression. +// +// # The variables defined here will be available under `variables` in other expressions of the policy +// +// except MatchConditions because MatchConditions are evaluated before the rest of the policy. +// +// The expression of a variable can refer to other variables defined earlier in the list but not those after. +// +// Thus, Variables must be sorted by the order of first appearance and acyclic. +func (d *ValidatingAdmissionPolicySpecDie) Variables(v ...admissionregistrationv1.Variable) *ValidatingAdmissionPolicySpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicySpec) { + r.Variables = v + }) +} + +var ParamKindBlank = (&ParamKindDie{}).DieFeed(admissionregistrationv1.ParamKind{}) + +type ParamKindDie struct { + mutable bool + r admissionregistrationv1.ParamKind +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ParamKindDie) DieImmutable(immutable bool) *ParamKindDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ParamKindDie) DieFeed(r admissionregistrationv1.ParamKind) *ParamKindDie { + if d.mutable { + d.r = r + return d + } + return &ParamKindDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ParamKindDie) DieFeedPtr(r *admissionregistrationv1.ParamKind) *ParamKindDie { + if r == nil { + r = &admissionregistrationv1.ParamKind{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ParamKindDie) DieFeedJSON(j []byte) *ParamKindDie { + r := admissionregistrationv1.ParamKind{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ParamKindDie) DieFeedYAML(y []byte) *ParamKindDie { + r := admissionregistrationv1.ParamKind{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ParamKindDie) DieFeedYAMLFile(name string) *ParamKindDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ParamKindDie) DieFeedRawExtension(raw runtime.RawExtension) *ParamKindDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ParamKindDie) DieRelease() admissionregistrationv1.ParamKind { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ParamKindDie) DieReleasePtr() *admissionregistrationv1.ParamKind { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ParamKindDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ParamKindDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ParamKindDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ParamKindDie) DieStamp(fn func(r *admissionregistrationv1.ParamKind)) *ParamKindDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ParamKindDie) DieStampAt(jp string, fn interface{}) *ParamKindDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamKind) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ParamKindDie) DieWith(fns ...func(d *ParamKindDie)) *ParamKindDie { + nd := ParamKindBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ParamKindDie) DeepCopy() *ParamKindDie { + r := *d.r.DeepCopy() + return &ParamKindDie{ + mutable: d.mutable, + r: r, + } +} + +// APIVersion is the API group version the resources belong to. +// +// In format of "group/version". +// +// Required. +func (d *ParamKindDie) APIVersion(v string) *ParamKindDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamKind) { + r.APIVersion = v + }) +} + +// Kind is the API kind the resources belong to. +// +// Required. +func (d *ParamKindDie) Kind(v string) *ParamKindDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamKind) { + r.Kind = v + }) +} + +var MatchResourcesBlank = (&MatchResourcesDie{}).DieFeed(admissionregistrationv1.MatchResources{}) + +type MatchResourcesDie struct { + mutable bool + r admissionregistrationv1.MatchResources +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *MatchResourcesDie) DieImmutable(immutable bool) *MatchResourcesDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *MatchResourcesDie) DieFeed(r admissionregistrationv1.MatchResources) *MatchResourcesDie { + if d.mutable { + d.r = r + return d + } + return &MatchResourcesDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *MatchResourcesDie) DieFeedPtr(r *admissionregistrationv1.MatchResources) *MatchResourcesDie { + if r == nil { + r = &admissionregistrationv1.MatchResources{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *MatchResourcesDie) DieFeedJSON(j []byte) *MatchResourcesDie { + r := admissionregistrationv1.MatchResources{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *MatchResourcesDie) DieFeedYAML(y []byte) *MatchResourcesDie { + r := admissionregistrationv1.MatchResources{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *MatchResourcesDie) DieFeedYAMLFile(name string) *MatchResourcesDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MatchResourcesDie) DieFeedRawExtension(raw runtime.RawExtension) *MatchResourcesDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *MatchResourcesDie) DieRelease() admissionregistrationv1.MatchResources { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *MatchResourcesDie) DieReleasePtr() *admissionregistrationv1.MatchResources { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *MatchResourcesDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *MatchResourcesDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MatchResourcesDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *MatchResourcesDie) DieStamp(fn func(r *admissionregistrationv1.MatchResources)) *MatchResourcesDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *MatchResourcesDie) DieStampAt(jp string, fn interface{}) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *MatchResourcesDie) DieWith(fns ...func(d *MatchResourcesDie)) *MatchResourcesDie { + nd := MatchResourcesBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *MatchResourcesDie) DeepCopy() *MatchResourcesDie { + r := *d.r.DeepCopy() + return &MatchResourcesDie{ + mutable: d.mutable, + r: r, + } +} + +// NamespaceSelector decides whether to run the admission control policy on an object based +// +// on whether the namespace for that object matches the selector. If the +// +// object itself is a namespace, the matching is performed on +// +// object.metadata.labels. If the object is another cluster scoped resource, +// +// it never skips the policy. +// +// # For example, to run the webhook on any objects whose namespace is not +// +// associated with "runlevel" of "0" or "1"; you will set the selector as +// +// follows: +// +// "namespaceSelector": { +// +// "matchExpressions": [ +// +// { +// +// "key": "runlevel", +// +// "operator": "NotIn", +// +// "values": [ +// +// "0", +// +// "1" +// +// ] +// +// } +// +// ] +// +// } +// +// # If instead you want to only run the policy on any objects whose +// +// namespace is associated with the "environment" of "prod" or "staging"; +// +// you will set the selector as follows: +// +// "namespaceSelector": { +// +// "matchExpressions": [ +// +// { +// +// "key": "environment", +// +// "operator": "In", +// +// "values": [ +// +// "prod", +// +// "staging" +// +// ] +// +// } +// +// ] +// +// } +// +// # See +// +// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ +// +// for more examples of label selectors. +// +// Default to the empty LabelSelector, which matches everything. +func (d *MatchResourcesDie) NamespaceSelector(v *apismetav1.LabelSelector) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + r.NamespaceSelector = v + }) +} + +// ObjectSelector decides whether to run the validation based on if the +// +// object has matching labels. objectSelector is evaluated against both +// +// the oldObject and newObject that would be sent to the cel validation, and +// +// is considered to match if either object matches the selector. A null +// +// object (oldObject in the case of create, or newObject in the case of +// +// delete) or an object that cannot have labels (like a +// +// # DeploymentRollback or a PodProxyOptions object) is not considered to +// +// match. +// +// # Use the object selector only if the webhook is opt-in, because end +// +// users may skip the admission webhook by setting the labels. +// +// Default to the empty LabelSelector, which matches everything. +func (d *MatchResourcesDie) ObjectSelector(v *apismetav1.LabelSelector) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + r.ObjectSelector = v + }) +} + +// ResourceRules describes what operations on what resources/subresources the ValidatingAdmissionPolicy matches. +// +// The policy cares about an operation if it matches _any_ Rule. +func (d *MatchResourcesDie) ResourceRules(v ...admissionregistrationv1.NamedRuleWithOperations) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + r.ResourceRules = v + }) +} + +// ExcludeResourceRules describes what operations on what resources/subresources the ValidatingAdmissionPolicy should not care about. +// +// The exclude rules take precedence over include rules (if a resource matches both, it is excluded) +func (d *MatchResourcesDie) ExcludeResourceRules(v ...admissionregistrationv1.NamedRuleWithOperations) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + r.ExcludeResourceRules = v + }) +} + +// matchPolicy defines how the "MatchResources" list is used to match incoming requests. +// +// Allowed values are "Exact" or "Equivalent". +// +// - Exact: match a request only if it exactly matches a specified rule. +// +// For example, if deployments can be modified via apps/v1, apps/v1beta1, and extensions/v1beta1, +// +// but "rules" only included `apiGroups:["apps"], apiVersions:["v1"], resources: ["deployments"]`, +// +// a request to apps/v1beta1 or extensions/v1beta1 would not be sent to the ValidatingAdmissionPolicy. +// +// - Equivalent: match a request if modifies a resource listed in rules, even via another API group or version. +// +// For example, if deployments can be modified via apps/v1, apps/v1beta1, and extensions/v1beta1, +// +// and "rules" only included `apiGroups:["apps"], apiVersions:["v1"], resources: ["deployments"]`, +// +// a request to apps/v1beta1 or extensions/v1beta1 would be converted to apps/v1 and sent to the ValidatingAdmissionPolicy. +// +// Defaults to "Equivalent" +func (d *MatchResourcesDie) MatchPolicy(v *admissionregistrationv1.MatchPolicyType) *MatchResourcesDie { + return d.DieStamp(func(r *admissionregistrationv1.MatchResources) { + r.MatchPolicy = v + }) +} + +var NamedRuleWithOperationsBlank = (&NamedRuleWithOperationsDie{}).DieFeed(admissionregistrationv1.NamedRuleWithOperations{}) + +type NamedRuleWithOperationsDie struct { + mutable bool + r admissionregistrationv1.NamedRuleWithOperations +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *NamedRuleWithOperationsDie) DieImmutable(immutable bool) *NamedRuleWithOperationsDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *NamedRuleWithOperationsDie) DieFeed(r admissionregistrationv1.NamedRuleWithOperations) *NamedRuleWithOperationsDie { + if d.mutable { + d.r = r + return d + } + return &NamedRuleWithOperationsDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *NamedRuleWithOperationsDie) DieFeedPtr(r *admissionregistrationv1.NamedRuleWithOperations) *NamedRuleWithOperationsDie { + if r == nil { + r = &admissionregistrationv1.NamedRuleWithOperations{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *NamedRuleWithOperationsDie) DieFeedJSON(j []byte) *NamedRuleWithOperationsDie { + r := admissionregistrationv1.NamedRuleWithOperations{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *NamedRuleWithOperationsDie) DieFeedYAML(y []byte) *NamedRuleWithOperationsDie { + r := admissionregistrationv1.NamedRuleWithOperations{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *NamedRuleWithOperationsDie) DieFeedYAMLFile(name string) *NamedRuleWithOperationsDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *NamedRuleWithOperationsDie) DieFeedRawExtension(raw runtime.RawExtension) *NamedRuleWithOperationsDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *NamedRuleWithOperationsDie) DieRelease() admissionregistrationv1.NamedRuleWithOperations { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *NamedRuleWithOperationsDie) DieReleasePtr() *admissionregistrationv1.NamedRuleWithOperations { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *NamedRuleWithOperationsDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *NamedRuleWithOperationsDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *NamedRuleWithOperationsDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *NamedRuleWithOperationsDie) DieStamp(fn func(r *admissionregistrationv1.NamedRuleWithOperations)) *NamedRuleWithOperationsDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *NamedRuleWithOperationsDie) DieStampAt(jp string, fn interface{}) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *NamedRuleWithOperationsDie) DieWith(fns ...func(d *NamedRuleWithOperationsDie)) *NamedRuleWithOperationsDie { + nd := NamedRuleWithOperationsBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *NamedRuleWithOperationsDie) DeepCopy() *NamedRuleWithOperationsDie { + r := *d.r.DeepCopy() + return &NamedRuleWithOperationsDie{ + mutable: d.mutable, + r: r, + } +} + +// ResourceNames is an optional white list of names that the rule applies to. An empty set means that everything is allowed. +func (d *NamedRuleWithOperationsDie) ResourceNames(v ...string) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + r.ResourceNames = v + }) +} + +// RuleWithOperations is a tuple of Operations and Resources. +func (d *NamedRuleWithOperationsDie) RuleWithOperations(v admissionregistrationv1.RuleWithOperations) *NamedRuleWithOperationsDie { + return d.DieStamp(func(r *admissionregistrationv1.NamedRuleWithOperations) { + r.RuleWithOperations = v + }) +} + +var ValidationBlank = (&ValidationDie{}).DieFeed(admissionregistrationv1.Validation{}) + +type ValidationDie struct { + mutable bool + r admissionregistrationv1.Validation +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ValidationDie) DieImmutable(immutable bool) *ValidationDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ValidationDie) DieFeed(r admissionregistrationv1.Validation) *ValidationDie { + if d.mutable { + d.r = r + return d + } + return &ValidationDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ValidationDie) DieFeedPtr(r *admissionregistrationv1.Validation) *ValidationDie { + if r == nil { + r = &admissionregistrationv1.Validation{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ValidationDie) DieFeedJSON(j []byte) *ValidationDie { + r := admissionregistrationv1.Validation{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ValidationDie) DieFeedYAML(y []byte) *ValidationDie { + r := admissionregistrationv1.Validation{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ValidationDie) DieFeedYAMLFile(name string) *ValidationDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidationDie) DieFeedRawExtension(raw runtime.RawExtension) *ValidationDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ValidationDie) DieRelease() admissionregistrationv1.Validation { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ValidationDie) DieReleasePtr() *admissionregistrationv1.Validation { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ValidationDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ValidationDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidationDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ValidationDie) DieStamp(fn func(r *admissionregistrationv1.Validation)) *ValidationDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ValidationDie) DieStampAt(jp string, fn interface{}) *ValidationDie { + return d.DieStamp(func(r *admissionregistrationv1.Validation) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ValidationDie) DieWith(fns ...func(d *ValidationDie)) *ValidationDie { + nd := ValidationBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ValidationDie) DeepCopy() *ValidationDie { + r := *d.r.DeepCopy() + return &ValidationDie{ + mutable: d.mutable, + r: r, + } +} + +// Expression represents the expression which will be evaluated by CEL. +// +// ref: https://github.com/google/cel-spec +// +// CEL expressions have access to the contents of the API request/response, organized into CEL variables as well as some other useful variables: +// +// - 'object' - The object from the incoming request. The value is null for DELETE requests. +// +// - 'oldObject' - The existing object. The value is null for CREATE requests. +// +// - 'request' - Attributes of the API request([ref](/pkg/apis/admission/types.go#AdmissionRequest)). +// +// - 'params' - Parameter resource referred to by the policy binding being evaluated. Only populated if the policy has a ParamKind. +// +// - 'namespaceObject' - The namespace object that the incoming object belongs to. The value is null for cluster-scoped resources. +// +// - 'variables' - Map of composited variables, from its name to its lazily evaluated value. +// +// For example, a variable named 'foo' can be accessed as 'variables.foo'. +// +// - 'authorizer' - A CEL Authorizer. May be used to perform authorization checks for the principal (user or service account) of the request. +// +// See https://pkg.go.dev/k8s.io/apiserver/pkg/cel/library#Authz +// +// - 'authorizer.requestResource' - A CEL ResourceCheck constructed from the 'authorizer' and configured with the +// +// request resource. +// +// # The `apiVersion`, `kind`, `metadata.name` and `metadata.generateName` are always accessible from the root of the +// +// object. No other metadata properties are accessible. +// +// Only property names of the form `[a-zA-Z_.-/][a-zA-Z0-9_.-/]*` are accessible. +// +// Accessible property names are escaped according to the following rules when accessed in the expression: +// +// - '__' escapes to '__underscores__' +// +// - '.' escapes to '__dot__' +// +// - '-' escapes to '__dash__' +// +// - '/' escapes to '__slash__' +// +// - Property names that exactly match a CEL RESERVED keyword escape to '__{keyword}__'. The keywords are: +// +// "true", "false", "null", "in", "as", "break", "const", "continue", "else", "for", "function", "if", +// +// "import", "let", "loop", "package", "namespace", "return". +// +// Examples: +// +// - Expression accessing a property named "namespace": {"Expression": "object.__namespace__ > 0"} +// +// - Expression accessing a property named "x-prop": {"Expression": "object.x__dash__prop > 0"} +// +// - Expression accessing a property named "redact__d": {"Expression": "object.redact__underscores__d > 0"} +// +// Equality on arrays with list type of 'set' or 'map' ignores element order, i.e. [1, 2] == [2, 1]. +// +// Concatenation on arrays with x-kubernetes-list-type use the semantics of the list type: +// +// - 'set': `X + Y` performs a union where the array positions of all elements in `X` are preserved and +// +// non-intersecting elements in `Y` are appended, retaining their partial order. +// +// - 'map': `X + Y` performs a merge where the array positions of all keys in `X` are preserved but the values +// +// are overwritten by values in `Y` when the key sets of `X` and `Y` intersect. Elements in `Y` with +// +// non-intersecting keys are appended, retaining their partial order. +// +// Required. +func (d *ValidationDie) Expression(v string) *ValidationDie { + return d.DieStamp(func(r *admissionregistrationv1.Validation) { + r.Expression = v + }) +} + +// Message represents the message displayed when validation fails. The message is required if the Expression contains +// +// line breaks. The message must not contain line breaks. +// +// If unset, the message is "failed rule: {Rule}". +// +// e.g. "must be a URL with the host matching spec.host" +// +// If the Expression contains line breaks. Message is required. +// +// The message must not contain line breaks. +// +// If unset, the message is "failed Expression: {Expression}". +func (d *ValidationDie) Message(v string) *ValidationDie { + return d.DieStamp(func(r *admissionregistrationv1.Validation) { + r.Message = v + }) +} + +// Reason represents a machine-readable description of why this validation failed. +// +// # If this is the first validation in the list to fail, this reason, as well as the +// +// corresponding HTTP response code, are used in the +// +// HTTP response to the client. +// +// The currently supported reasons are: "Unauthorized", "Forbidden", "Invalid", "RequestEntityTooLarge". +// +// If not set, StatusReasonInvalid is used in the response to the client. +func (d *ValidationDie) Reason(v *apismetav1.StatusReason) *ValidationDie { + return d.DieStamp(func(r *admissionregistrationv1.Validation) { + r.Reason = v + }) +} + +// messageExpression declares a CEL expression that evaluates to the validation failure message that is returned when this rule fails. +// +// Since messageExpression is used as a failure message, it must evaluate to a string. +// +// If both message and messageExpression are present on a validation, then messageExpression will be used if validation fails. +// +// # If messageExpression results in a runtime error, the runtime error is logged, and the validation failure message is produced +// +// as if the messageExpression field were unset. If messageExpression evaluates to an empty string, a string with only spaces, or a string +// +// that contains line breaks, then the validation failure message will also be produced as if the messageExpression field were unset, and +// +// the fact that messageExpression produced an empty string/string with only spaces/string with line breaks will be logged. +// +// messageExpression has access to all the same variables as the `expression` except for 'authorizer' and 'authorizer.requestResource'. +// +// Example: +// +// "object.x must be less than max ("+string(params.max)+")" +func (d *ValidationDie) MessageExpression(v string) *ValidationDie { + return d.DieStamp(func(r *admissionregistrationv1.Validation) { + r.MessageExpression = v + }) +} + +var AuditAnnotationBlank = (&AuditAnnotationDie{}).DieFeed(admissionregistrationv1.AuditAnnotation{}) + +type AuditAnnotationDie struct { + mutable bool + r admissionregistrationv1.AuditAnnotation +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *AuditAnnotationDie) DieImmutable(immutable bool) *AuditAnnotationDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *AuditAnnotationDie) DieFeed(r admissionregistrationv1.AuditAnnotation) *AuditAnnotationDie { + if d.mutable { + d.r = r + return d + } + return &AuditAnnotationDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *AuditAnnotationDie) DieFeedPtr(r *admissionregistrationv1.AuditAnnotation) *AuditAnnotationDie { + if r == nil { + r = &admissionregistrationv1.AuditAnnotation{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *AuditAnnotationDie) DieFeedJSON(j []byte) *AuditAnnotationDie { + r := admissionregistrationv1.AuditAnnotation{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *AuditAnnotationDie) DieFeedYAML(y []byte) *AuditAnnotationDie { + r := admissionregistrationv1.AuditAnnotation{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *AuditAnnotationDie) DieFeedYAMLFile(name string) *AuditAnnotationDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *AuditAnnotationDie) DieFeedRawExtension(raw runtime.RawExtension) *AuditAnnotationDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *AuditAnnotationDie) DieRelease() admissionregistrationv1.AuditAnnotation { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *AuditAnnotationDie) DieReleasePtr() *admissionregistrationv1.AuditAnnotation { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *AuditAnnotationDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *AuditAnnotationDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *AuditAnnotationDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *AuditAnnotationDie) DieStamp(fn func(r *admissionregistrationv1.AuditAnnotation)) *AuditAnnotationDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *AuditAnnotationDie) DieStampAt(jp string, fn interface{}) *AuditAnnotationDie { + return d.DieStamp(func(r *admissionregistrationv1.AuditAnnotation) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *AuditAnnotationDie) DieWith(fns ...func(d *AuditAnnotationDie)) *AuditAnnotationDie { + nd := AuditAnnotationBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *AuditAnnotationDie) DeepCopy() *AuditAnnotationDie { + r := *d.r.DeepCopy() + return &AuditAnnotationDie{ + mutable: d.mutable, + r: r, + } +} + +// key specifies the audit annotation key. The audit annotation keys of +// +// a ValidatingAdmissionPolicy must be unique. The key must be a qualified +// +// name ([A-Za-z0-9][-A-Za-z0-9_.]*) no more than 63 bytes in length. +// +// # The key is combined with the resource name of the +// +// ValidatingAdmissionPolicy to construct an audit annotation key: +// +// "{ValidatingAdmissionPolicy name}/{key}". +// +// # If an admission webhook uses the same resource name as this ValidatingAdmissionPolicy +// +// and the same audit annotation key, the annotation key will be identical. +// +// # In this case, the first annotation written with the key will be included +// +// in the audit event and all subsequent annotations with the same key +// +// will be discarded. +// +// Required. +func (d *AuditAnnotationDie) Key(v string) *AuditAnnotationDie { + return d.DieStamp(func(r *admissionregistrationv1.AuditAnnotation) { + r.Key = v + }) +} + +// valueExpression represents the expression which is evaluated by CEL to +// +// produce an audit annotation value. The expression must evaluate to either +// +// a string or null value. If the expression evaluates to a string, the +// +// audit annotation is included with the string value. If the expression +// +// evaluates to null or empty string the audit annotation will be omitted. +// +// The valueExpression may be no longer than 5kb in length. +// +// # If the result of the valueExpression is more than 10kb in length, it +// +// will be truncated to 10kb. +// +// # If multiple ValidatingAdmissionPolicyBinding resources match an +// +// # API request, then the valueExpression will be evaluated for +// +// each binding. All unique values produced by the valueExpressions +// +// will be joined together in a comma-separated list. +// +// Required. +func (d *AuditAnnotationDie) ValueExpression(v string) *AuditAnnotationDie { + return d.DieStamp(func(r *admissionregistrationv1.AuditAnnotation) { + r.ValueExpression = v + }) +} + +var VariableBlank = (&VariableDie{}).DieFeed(admissionregistrationv1.Variable{}) + +type VariableDie struct { + mutable bool + r admissionregistrationv1.Variable +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *VariableDie) DieImmutable(immutable bool) *VariableDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *VariableDie) DieFeed(r admissionregistrationv1.Variable) *VariableDie { + if d.mutable { + d.r = r + return d + } + return &VariableDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *VariableDie) DieFeedPtr(r *admissionregistrationv1.Variable) *VariableDie { + if r == nil { + r = &admissionregistrationv1.Variable{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *VariableDie) DieFeedJSON(j []byte) *VariableDie { + r := admissionregistrationv1.Variable{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *VariableDie) DieFeedYAML(y []byte) *VariableDie { + r := admissionregistrationv1.Variable{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *VariableDie) DieFeedYAMLFile(name string) *VariableDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *VariableDie) DieFeedRawExtension(raw runtime.RawExtension) *VariableDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *VariableDie) DieRelease() admissionregistrationv1.Variable { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *VariableDie) DieReleasePtr() *admissionregistrationv1.Variable { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *VariableDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *VariableDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *VariableDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *VariableDie) DieStamp(fn func(r *admissionregistrationv1.Variable)) *VariableDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *VariableDie) DieStampAt(jp string, fn interface{}) *VariableDie { + return d.DieStamp(func(r *admissionregistrationv1.Variable) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *VariableDie) DieWith(fns ...func(d *VariableDie)) *VariableDie { + nd := VariableBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *VariableDie) DeepCopy() *VariableDie { + r := *d.r.DeepCopy() + return &VariableDie{ + mutable: d.mutable, + r: r, + } +} + +// Name is the name of the variable. The name must be a valid CEL identifier and unique among all variables. +// +// The variable can be accessed in other expressions through `variables` +// +// For example, if name is "foo", the variable will be available as `variables.foo` +func (d *VariableDie) Name(v string) *VariableDie { + return d.DieStamp(func(r *admissionregistrationv1.Variable) { + r.Name = v + }) +} + +// Expression is the expression that will be evaluated as the value of the variable. +// +// The CEL expression has access to the same identifiers as the CEL expressions in Validation. +func (d *VariableDie) Expression(v string) *VariableDie { + return d.DieStamp(func(r *admissionregistrationv1.Variable) { + r.Expression = v + }) +} + +var ValidatingAdmissionPolicyStatusBlank = (&ValidatingAdmissionPolicyStatusDie{}).DieFeed(admissionregistrationv1.ValidatingAdmissionPolicyStatus{}) + +type ValidatingAdmissionPolicyStatusDie struct { + mutable bool + r admissionregistrationv1.ValidatingAdmissionPolicyStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ValidatingAdmissionPolicyStatusDie) DieImmutable(immutable bool) *ValidatingAdmissionPolicyStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ValidatingAdmissionPolicyStatusDie) DieFeed(r admissionregistrationv1.ValidatingAdmissionPolicyStatus) *ValidatingAdmissionPolicyStatusDie { + if d.mutable { + d.r = r + return d + } + return &ValidatingAdmissionPolicyStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ValidatingAdmissionPolicyStatusDie) DieFeedPtr(r *admissionregistrationv1.ValidatingAdmissionPolicyStatus) *ValidatingAdmissionPolicyStatusDie { + if r == nil { + r = &admissionregistrationv1.ValidatingAdmissionPolicyStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ValidatingAdmissionPolicyStatusDie) DieFeedJSON(j []byte) *ValidatingAdmissionPolicyStatusDie { + r := admissionregistrationv1.ValidatingAdmissionPolicyStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ValidatingAdmissionPolicyStatusDie) DieFeedYAML(y []byte) *ValidatingAdmissionPolicyStatusDie { + r := admissionregistrationv1.ValidatingAdmissionPolicyStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ValidatingAdmissionPolicyStatusDie) DieFeedYAMLFile(name string) *ValidatingAdmissionPolicyStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicyStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *ValidatingAdmissionPolicyStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ValidatingAdmissionPolicyStatusDie) DieRelease() admissionregistrationv1.ValidatingAdmissionPolicyStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ValidatingAdmissionPolicyStatusDie) DieReleasePtr() *admissionregistrationv1.ValidatingAdmissionPolicyStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ValidatingAdmissionPolicyStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ValidatingAdmissionPolicyStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicyStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ValidatingAdmissionPolicyStatusDie) DieStamp(fn func(r *admissionregistrationv1.ValidatingAdmissionPolicyStatus)) *ValidatingAdmissionPolicyStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ValidatingAdmissionPolicyStatusDie) DieStampAt(jp string, fn interface{}) *ValidatingAdmissionPolicyStatusDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ValidatingAdmissionPolicyStatusDie) DieWith(fns ...func(d *ValidatingAdmissionPolicyStatusDie)) *ValidatingAdmissionPolicyStatusDie { + nd := ValidatingAdmissionPolicyStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ValidatingAdmissionPolicyStatusDie) DeepCopy() *ValidatingAdmissionPolicyStatusDie { + r := *d.r.DeepCopy() + return &ValidatingAdmissionPolicyStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// The generation observed by the controller. +func (d *ValidatingAdmissionPolicyStatusDie) ObservedGeneration(v int64) *ValidatingAdmissionPolicyStatusDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyStatus) { + r.ObservedGeneration = v + }) +} + +// The results of type checking for each expression. +// +// Presence of this field indicates the completion of the type checking. +func (d *ValidatingAdmissionPolicyStatusDie) TypeChecking(v *admissionregistrationv1.TypeChecking) *ValidatingAdmissionPolicyStatusDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyStatus) { + r.TypeChecking = v + }) +} + +// The conditions represent the latest available observations of a policy's current state. +func (d *ValidatingAdmissionPolicyStatusDie) Conditions(v ...apismetav1.Condition) *ValidatingAdmissionPolicyStatusDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyStatus) { + r.Conditions = v + }) +} + +var TypeCheckingBlank = (&TypeCheckingDie{}).DieFeed(admissionregistrationv1.TypeChecking{}) + +type TypeCheckingDie struct { + mutable bool + r admissionregistrationv1.TypeChecking +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *TypeCheckingDie) DieImmutable(immutable bool) *TypeCheckingDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *TypeCheckingDie) DieFeed(r admissionregistrationv1.TypeChecking) *TypeCheckingDie { + if d.mutable { + d.r = r + return d + } + return &TypeCheckingDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *TypeCheckingDie) DieFeedPtr(r *admissionregistrationv1.TypeChecking) *TypeCheckingDie { + if r == nil { + r = &admissionregistrationv1.TypeChecking{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *TypeCheckingDie) DieFeedJSON(j []byte) *TypeCheckingDie { + r := admissionregistrationv1.TypeChecking{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *TypeCheckingDie) DieFeedYAML(y []byte) *TypeCheckingDie { + r := admissionregistrationv1.TypeChecking{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *TypeCheckingDie) DieFeedYAMLFile(name string) *TypeCheckingDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *TypeCheckingDie) DieFeedRawExtension(raw runtime.RawExtension) *TypeCheckingDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *TypeCheckingDie) DieRelease() admissionregistrationv1.TypeChecking { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *TypeCheckingDie) DieReleasePtr() *admissionregistrationv1.TypeChecking { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *TypeCheckingDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *TypeCheckingDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *TypeCheckingDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *TypeCheckingDie) DieStamp(fn func(r *admissionregistrationv1.TypeChecking)) *TypeCheckingDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *TypeCheckingDie) DieStampAt(jp string, fn interface{}) *TypeCheckingDie { + return d.DieStamp(func(r *admissionregistrationv1.TypeChecking) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *TypeCheckingDie) DieWith(fns ...func(d *TypeCheckingDie)) *TypeCheckingDie { + nd := TypeCheckingBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *TypeCheckingDie) DeepCopy() *TypeCheckingDie { + r := *d.r.DeepCopy() + return &TypeCheckingDie{ + mutable: d.mutable, + r: r, + } +} + +// The type checking warnings for each expression. +func (d *TypeCheckingDie) ExpressionWarnings(v ...admissionregistrationv1.ExpressionWarning) *TypeCheckingDie { + return d.DieStamp(func(r *admissionregistrationv1.TypeChecking) { + r.ExpressionWarnings = v + }) +} + +var ExpressionWarningBlank = (&ExpressionWarningDie{}).DieFeed(admissionregistrationv1.ExpressionWarning{}) + +type ExpressionWarningDie struct { + mutable bool + r admissionregistrationv1.ExpressionWarning +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ExpressionWarningDie) DieImmutable(immutable bool) *ExpressionWarningDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ExpressionWarningDie) DieFeed(r admissionregistrationv1.ExpressionWarning) *ExpressionWarningDie { + if d.mutable { + d.r = r + return d + } + return &ExpressionWarningDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ExpressionWarningDie) DieFeedPtr(r *admissionregistrationv1.ExpressionWarning) *ExpressionWarningDie { + if r == nil { + r = &admissionregistrationv1.ExpressionWarning{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ExpressionWarningDie) DieFeedJSON(j []byte) *ExpressionWarningDie { + r := admissionregistrationv1.ExpressionWarning{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ExpressionWarningDie) DieFeedYAML(y []byte) *ExpressionWarningDie { + r := admissionregistrationv1.ExpressionWarning{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ExpressionWarningDie) DieFeedYAMLFile(name string) *ExpressionWarningDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ExpressionWarningDie) DieFeedRawExtension(raw runtime.RawExtension) *ExpressionWarningDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ExpressionWarningDie) DieRelease() admissionregistrationv1.ExpressionWarning { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ExpressionWarningDie) DieReleasePtr() *admissionregistrationv1.ExpressionWarning { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ExpressionWarningDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ExpressionWarningDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ExpressionWarningDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ExpressionWarningDie) DieStamp(fn func(r *admissionregistrationv1.ExpressionWarning)) *ExpressionWarningDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ExpressionWarningDie) DieStampAt(jp string, fn interface{}) *ExpressionWarningDie { + return d.DieStamp(func(r *admissionregistrationv1.ExpressionWarning) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ExpressionWarningDie) DieWith(fns ...func(d *ExpressionWarningDie)) *ExpressionWarningDie { + nd := ExpressionWarningBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ExpressionWarningDie) DeepCopy() *ExpressionWarningDie { + r := *d.r.DeepCopy() + return &ExpressionWarningDie{ + mutable: d.mutable, + r: r, + } +} + +// The path to the field that refers the expression. +// +// # For example, the reference to the expression of the first item of +// +// validations is "spec.validations[0].expression" +func (d *ExpressionWarningDie) FieldRef(v string) *ExpressionWarningDie { + return d.DieStamp(func(r *admissionregistrationv1.ExpressionWarning) { + r.FieldRef = v + }) +} + +// The content of type checking information in a human-readable form. +// +// # Each line of the warning contains the type that the expression is checked +// +// against, followed by the type check error from the compiler. +func (d *ExpressionWarningDie) Warning(v string) *ExpressionWarningDie { + return d.DieStamp(func(r *admissionregistrationv1.ExpressionWarning) { + r.Warning = v + }) +} + +var ValidatingAdmissionPolicyBindingBlank = (&ValidatingAdmissionPolicyBindingDie{}).DieFeed(admissionregistrationv1.ValidatingAdmissionPolicyBinding{}) + +type ValidatingAdmissionPolicyBindingDie struct { + metav1.FrozenObjectMeta + mutable bool + r admissionregistrationv1.ValidatingAdmissionPolicyBinding +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ValidatingAdmissionPolicyBindingDie) DieImmutable(immutable bool) *ValidatingAdmissionPolicyBindingDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ValidatingAdmissionPolicyBindingDie) DieFeed(r admissionregistrationv1.ValidatingAdmissionPolicyBinding) *ValidatingAdmissionPolicyBindingDie { + if d.mutable { + d.FrozenObjectMeta = metav1.FreezeObjectMeta(r.ObjectMeta) + d.r = r + return d + } + return &ValidatingAdmissionPolicyBindingDie{ + FrozenObjectMeta: metav1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ValidatingAdmissionPolicyBindingDie) DieFeedPtr(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) *ValidatingAdmissionPolicyBindingDie { + if r == nil { + r = &admissionregistrationv1.ValidatingAdmissionPolicyBinding{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ValidatingAdmissionPolicyBindingDie) DieFeedJSON(j []byte) *ValidatingAdmissionPolicyBindingDie { + r := admissionregistrationv1.ValidatingAdmissionPolicyBinding{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ValidatingAdmissionPolicyBindingDie) DieFeedYAML(y []byte) *ValidatingAdmissionPolicyBindingDie { + r := admissionregistrationv1.ValidatingAdmissionPolicyBinding{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ValidatingAdmissionPolicyBindingDie) DieFeedYAMLFile(name string) *ValidatingAdmissionPolicyBindingDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicyBindingDie) DieFeedRawExtension(raw runtime.RawExtension) *ValidatingAdmissionPolicyBindingDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ValidatingAdmissionPolicyBindingDie) DieRelease() admissionregistrationv1.ValidatingAdmissionPolicyBinding { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ValidatingAdmissionPolicyBindingDie) DieReleasePtr() *admissionregistrationv1.ValidatingAdmissionPolicyBinding { + r := d.DieRelease() + return &r +} + +// DieReleaseUnstructured returns the resource managed by the die as an unstructured object. Panics on error. +func (d *ValidatingAdmissionPolicyBindingDie) DieReleaseUnstructured() *unstructured.Unstructured { + r := d.DieReleasePtr() + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r) + if err != nil { + panic(err) + } + return &unstructured.Unstructured{ + Object: u, + } +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ValidatingAdmissionPolicyBindingDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ValidatingAdmissionPolicyBindingDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicyBindingDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ValidatingAdmissionPolicyBindingDie) DieStamp(fn func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding)) *ValidatingAdmissionPolicyBindingDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ValidatingAdmissionPolicyBindingDie) DieStampAt(jp string, fn interface{}) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ValidatingAdmissionPolicyBindingDie) DieWith(fns ...func(d *ValidatingAdmissionPolicyBindingDie)) *ValidatingAdmissionPolicyBindingDie { + nd := ValidatingAdmissionPolicyBindingBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ValidatingAdmissionPolicyBindingDie) DeepCopy() *ValidatingAdmissionPolicyBindingDie { + r := *d.r.DeepCopy() + return &ValidatingAdmissionPolicyBindingDie{ + FrozenObjectMeta: metav1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +var _ runtime.Object = (*ValidatingAdmissionPolicyBindingDie)(nil) + +func (d *ValidatingAdmissionPolicyBindingDie) DeepCopyObject() runtime.Object { + return d.r.DeepCopy() +} + +func (d *ValidatingAdmissionPolicyBindingDie) GetObjectKind() schema.ObjectKind { + r := d.DieRelease() + return r.GetObjectKind() +} + +func (d *ValidatingAdmissionPolicyBindingDie) MarshalJSON() ([]byte, error) { + return json.Marshal(d.r) +} + +func (d *ValidatingAdmissionPolicyBindingDie) UnmarshalJSON(b []byte) error { + if !d.mutable { + return fmtx.Errorf("cannot unmarshal into immutable dies, create a mutable version first") + } + r := &admissionregistrationv1.ValidatingAdmissionPolicyBinding{} + err := json.Unmarshal(b, r) + *d = *d.DieFeed(*r) + return err +} + +// DieDefaultTypeMetadata sets the APIVersion and Kind to "admissionregistration.k8s.io/v1" and "ValidatingAdmissionPolicyBinding" respectively. +func (d *ValidatingAdmissionPolicyBindingDie) DieDefaultTypeMetadata() *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + r.APIVersion = "admissionregistration.k8s.io/v1" + r.Kind = "ValidatingAdmissionPolicyBinding" + }) +} + +// APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources +func (d *ValidatingAdmissionPolicyBindingDie) APIVersion(v string) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + r.APIVersion = v + }) +} + +// Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds +func (d *ValidatingAdmissionPolicyBindingDie) Kind(v string) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + r.Kind = v + }) +} + +// TypeMetadata standard object's type metadata. +func (d *ValidatingAdmissionPolicyBindingDie) TypeMetadata(v apismetav1.TypeMeta) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + r.TypeMeta = v + }) +} + +// TypeMetadataDie stamps the resource's TypeMeta field with a mutable die. +func (d *ValidatingAdmissionPolicyBindingDie) TypeMetadataDie(fn func(d *metav1.TypeMetaDie)) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + d := metav1.TypeMetaBlank.DieImmutable(false).DieFeed(r.TypeMeta) + fn(d) + r.TypeMeta = d.DieRelease() + }) +} + +// Metadata standard object's metadata. +func (d *ValidatingAdmissionPolicyBindingDie) Metadata(v apismetav1.ObjectMeta) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + r.ObjectMeta = v + }) +} + +// MetadataDie stamps the resource's ObjectMeta field with a mutable die. +func (d *ValidatingAdmissionPolicyBindingDie) MetadataDie(fn func(d *metav1.ObjectMetaDie)) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + d := metav1.ObjectMetaBlank.DieImmutable(false).DieFeed(r.ObjectMeta) + fn(d) + r.ObjectMeta = d.DieRelease() + }) +} + +// SpecDie stamps the resource's spec field with a mutable die. +func (d *ValidatingAdmissionPolicyBindingDie) SpecDie(fn func(d *ValidatingAdmissionPolicyBindingSpecDie)) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + d := ValidatingAdmissionPolicyBindingSpecBlank.DieImmutable(false).DieFeed(r.Spec) + fn(d) + r.Spec = d.DieRelease() + }) +} + +// Specification of the desired behavior of the ValidatingAdmissionPolicyBinding. +func (d *ValidatingAdmissionPolicyBindingDie) Spec(v admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) *ValidatingAdmissionPolicyBindingDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBinding) { + r.Spec = v + }) +} + +var ValidatingAdmissionPolicyBindingSpecBlank = (&ValidatingAdmissionPolicyBindingSpecDie{}).DieFeed(admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec{}) + +type ValidatingAdmissionPolicyBindingSpecDie struct { + mutable bool + r admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieImmutable(immutable bool) *ValidatingAdmissionPolicyBindingSpecDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieFeed(r admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) *ValidatingAdmissionPolicyBindingSpecDie { + if d.mutable { + d.r = r + return d + } + return &ValidatingAdmissionPolicyBindingSpecDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieFeedPtr(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) *ValidatingAdmissionPolicyBindingSpecDie { + if r == nil { + r = &admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieFeedJSON(j []byte) *ValidatingAdmissionPolicyBindingSpecDie { + r := admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieFeedYAML(y []byte) *ValidatingAdmissionPolicyBindingSpecDie { + r := admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieFeedYAMLFile(name string) *ValidatingAdmissionPolicyBindingSpecDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieFeedRawExtension(raw runtime.RawExtension) *ValidatingAdmissionPolicyBindingSpecDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieRelease() admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieReleasePtr() *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieStamp(fn func(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec)) *ValidatingAdmissionPolicyBindingSpecDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieStampAt(jp string, fn interface{}) *ValidatingAdmissionPolicyBindingSpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DieWith(fns ...func(d *ValidatingAdmissionPolicyBindingSpecDie)) *ValidatingAdmissionPolicyBindingSpecDie { + nd := ValidatingAdmissionPolicyBindingSpecBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ValidatingAdmissionPolicyBindingSpecDie) DeepCopy() *ValidatingAdmissionPolicyBindingSpecDie { + r := *d.r.DeepCopy() + return &ValidatingAdmissionPolicyBindingSpecDie{ + mutable: d.mutable, + r: r, + } +} + +// PolicyName references a ValidatingAdmissionPolicy name which the ValidatingAdmissionPolicyBinding binds to. +// +// # If the referenced resource does not exist, this binding is considered invalid and will be ignored +// +// Required. +func (d *ValidatingAdmissionPolicyBindingSpecDie) PolicyName(v string) *ValidatingAdmissionPolicyBindingSpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) { + r.PolicyName = v + }) +} + +// paramRef specifies the parameter resource used to configure the admission control policy. +// +// It should point to a resource of the type specified in ParamKind of the bound ValidatingAdmissionPolicy. +// +// If the policy specifies a ParamKind and the resource referred to by ParamRef does not exist, this binding is considered mis-configured and the FailurePolicy of the ValidatingAdmissionPolicy applied. +// +// If the policy does not specify a ParamKind then this field is ignored, and the rules are evaluated without a param. +func (d *ValidatingAdmissionPolicyBindingSpecDie) ParamRef(v *admissionregistrationv1.ParamRef) *ValidatingAdmissionPolicyBindingSpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) { + r.ParamRef = v + }) +} + +// MatchResources declares what resources match this binding and will be validated by it. +// +// Note that this is intersected with the policy's matchConstraints, so only requests that are matched by the policy can be selected by this. +// +// # If this is unset, all resources matched by the policy are validated by this binding +// +// When resourceRules is unset, it does not constrain resource matching. If a resource is matched by the other fields of this object, it will be validated. +// +// Note that this is differs from ValidatingAdmissionPolicy matchConstraints, where resourceRules are required. +func (d *ValidatingAdmissionPolicyBindingSpecDie) MatchResources(v *admissionregistrationv1.MatchResources) *ValidatingAdmissionPolicyBindingSpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) { + r.MatchResources = v + }) +} + +// validationActions declares how Validations of the referenced ValidatingAdmissionPolicy are enforced. +// +// If a validation evaluates to false it is always enforced according to these actions. +// +// # Failures defined by the ValidatingAdmissionPolicy's FailurePolicy are enforced according +// +// to these actions only if the FailurePolicy is set to Fail, otherwise the failures are +// +// ignored. This includes compilation errors, runtime errors and misconfigurations of the policy. +// +// validationActions is declared as a set of action values. Order does +// +// not matter. validationActions may not contain duplicates of the same action. +// +// The supported actions values are: +// +// "Deny" specifies that a validation failure results in a denied request. +// +// "Warn" specifies that a validation failure is reported to the request client +// +// in HTTP Warning headers, with a warning code of 299. Warnings can be sent +// +// both for allowed or denied admission responses. +// +// "Audit" specifies that a validation failure is included in the published +// +// audit event for the request. The audit event will contain a +// +// `validation.policy.admission.k8s.io/validation_failure` audit annotation +// +// with a value containing the details of the validation failures, formatted as +// +// a JSON list of objects, each with the following fields: +// +// - message: The validation failure message string +// +// - policy: The resource name of the ValidatingAdmissionPolicy +// +// - binding: The resource name of the ValidatingAdmissionPolicyBinding +// +// - expressionIndex: The index of the failed validations in the ValidatingAdmissionPolicy +// +// - validationActions: The enforcement actions enacted for the validation failure +// +// Example audit annotation: +// +// `"validation.policy.admission.k8s.io/validation_failure": "[{\"message\": \"Invalid value\", {\"policy\": \"policy.example.com\", {\"binding\": \"policybinding.example.com\", {\"expressionIndex\": \"1\", {\"validationActions\": [\"Audit\"]}]"` +// +// # Clients should expect to handle additional values by ignoring +// +// any values not recognized. +// +// "Deny" and "Warn" may not be used together since this combination +// +// needlessly duplicates the validation failure both in the +// +// API response body and the HTTP warning headers. +// +// Required. +func (d *ValidatingAdmissionPolicyBindingSpecDie) ValidationActions(v ...admissionregistrationv1.ValidationAction) *ValidatingAdmissionPolicyBindingSpecDie { + return d.DieStamp(func(r *admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec) { + r.ValidationActions = v + }) +} + +var ParamRefBlank = (&ParamRefDie{}).DieFeed(admissionregistrationv1.ParamRef{}) + +type ParamRefDie struct { + mutable bool + r admissionregistrationv1.ParamRef +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ParamRefDie) DieImmutable(immutable bool) *ParamRefDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ParamRefDie) DieFeed(r admissionregistrationv1.ParamRef) *ParamRefDie { + if d.mutable { + d.r = r + return d + } + return &ParamRefDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ParamRefDie) DieFeedPtr(r *admissionregistrationv1.ParamRef) *ParamRefDie { + if r == nil { + r = &admissionregistrationv1.ParamRef{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ParamRefDie) DieFeedJSON(j []byte) *ParamRefDie { + r := admissionregistrationv1.ParamRef{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ParamRefDie) DieFeedYAML(y []byte) *ParamRefDie { + r := admissionregistrationv1.ParamRef{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ParamRefDie) DieFeedYAMLFile(name string) *ParamRefDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ParamRefDie) DieFeedRawExtension(raw runtime.RawExtension) *ParamRefDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ParamRefDie) DieRelease() admissionregistrationv1.ParamRef { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ParamRefDie) DieReleasePtr() *admissionregistrationv1.ParamRef { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ParamRefDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ParamRefDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ParamRefDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ParamRefDie) DieStamp(fn func(r *admissionregistrationv1.ParamRef)) *ParamRefDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ParamRefDie) DieStampAt(jp string, fn interface{}) *ParamRefDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamRef) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ParamRefDie) DieWith(fns ...func(d *ParamRefDie)) *ParamRefDie { + nd := ParamRefBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ParamRefDie) DeepCopy() *ParamRefDie { + r := *d.r.DeepCopy() + return &ParamRefDie{ + mutable: d.mutable, + r: r, + } +} + +// name is the name of the resource being referenced. +// +// # One of `name` or `selector` must be set, but `name` and `selector` are +// +// mutually exclusive properties. If one is set, the other must be unset. +// +// # A single parameter used for all admission requests can be configured +// +// by setting the `name` field, leaving `selector` blank, and setting namespace +// +// if `paramKind` is namespace-scoped. +func (d *ParamRefDie) Name(v string) *ParamRefDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamRef) { + r.Name = v + }) +} + +// namespace is the namespace of the referenced resource. Allows limiting +// +// the search for params to a specific namespace. Applies to both `name` and +// +// `selector` fields. +// +// # A per-namespace parameter may be used by specifying a namespace-scoped +// +// `paramKind` in the policy and leaving this field empty. +// +// - If `paramKind` is cluster-scoped, this field MUST be unset. Setting this +// +// field results in a configuration error. +// +// - If `paramKind` is namespace-scoped, the namespace of the object being +// +// evaluated for admission will be used when this field is left unset. Take +// +// care that if this is left empty the binding must not match any cluster-scoped +// +// resources, which will result in an error. +func (d *ParamRefDie) Namespace(v string) *ParamRefDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamRef) { + r.Namespace = v + }) +} + +// selector can be used to match multiple param objects based on their labels. +// +// Supply selector: {} to match all resources of the ParamKind. +// +// # If multiple params are found, they are all evaluated with the policy expressions +// +// and the results are ANDed together. +// +// # One of `name` or `selector` must be set, but `name` and `selector` are +// +// mutually exclusive properties. If one is set, the other must be unset. +func (d *ParamRefDie) Selector(v *apismetav1.LabelSelector) *ParamRefDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamRef) { + r.Selector = v + }) +} + +// `parameterNotFoundAction` controls the behavior of the binding when the resource +// +// exists, and name or selector is valid, but there are no parameters +// +// matched by the binding. If the value is set to `Allow`, then no +// +// matched parameters will be treated as successful validation by the binding. +// +// # If set to `Deny`, then no matched parameters will be subject to the +// +// `failurePolicy` of the policy. +// +// Allowed values are `Allow` or `Deny` +// +// Required +func (d *ParamRefDie) ParameterNotFoundAction(v *admissionregistrationv1.ParameterNotFoundActionType) *ParamRefDie { + return d.DieStamp(func(r *admissionregistrationv1.ParamRef) { + r.ParameterNotFoundAction = v + }) +} + var ValidatingWebhookConfigurationBlank = (&ValidatingWebhookConfigurationDie{}).DieFeed(admissionregistrationv1.ValidatingWebhookConfiguration{}) type ValidatingWebhookConfigurationDie struct { diff --git a/apis/admissionregistration/v1/zz_generated.die_test.go b/apis/admissionregistration/v1/zz_generated.die_test.go index 6401bc9..f971e86 100644 --- a/apis/admissionregistration/v1/zz_generated.die_test.go +++ b/apis/admissionregistration/v1/zz_generated.die_test.go @@ -89,6 +89,132 @@ func TestMutatingWebhookDie_MissingMethods(t *testingx.T) { } } +func TestValidatingAdmissionPolicyDie_MissingMethods(t *testingx.T) { + die := ValidatingAdmissionPolicyBlank + ignore := []string{"TypeMeta", "ObjectMeta"} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ValidatingAdmissionPolicyDie: %s", diff.List()) + } +} + +func TestValidatingAdmissionPolicySpecDie_MissingMethods(t *testingx.T) { + die := ValidatingAdmissionPolicySpecBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ValidatingAdmissionPolicySpecDie: %s", diff.List()) + } +} + +func TestParamKindDie_MissingMethods(t *testingx.T) { + die := ParamKindBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ParamKindDie: %s", diff.List()) + } +} + +func TestMatchResourcesDie_MissingMethods(t *testingx.T) { + die := MatchResourcesBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for MatchResourcesDie: %s", diff.List()) + } +} + +func TestNamedRuleWithOperationsDie_MissingMethods(t *testingx.T) { + die := NamedRuleWithOperationsBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for NamedRuleWithOperationsDie: %s", diff.List()) + } +} + +func TestValidationDie_MissingMethods(t *testingx.T) { + die := ValidationBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ValidationDie: %s", diff.List()) + } +} + +func TestAuditAnnotationDie_MissingMethods(t *testingx.T) { + die := AuditAnnotationBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for AuditAnnotationDie: %s", diff.List()) + } +} + +func TestVariableDie_MissingMethods(t *testingx.T) { + die := VariableBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for VariableDie: %s", diff.List()) + } +} + +func TestValidatingAdmissionPolicyStatusDie_MissingMethods(t *testingx.T) { + die := ValidatingAdmissionPolicyStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ValidatingAdmissionPolicyStatusDie: %s", diff.List()) + } +} + +func TestTypeCheckingDie_MissingMethods(t *testingx.T) { + die := TypeCheckingBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for TypeCheckingDie: %s", diff.List()) + } +} + +func TestExpressionWarningDie_MissingMethods(t *testingx.T) { + die := ExpressionWarningBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ExpressionWarningDie: %s", diff.List()) + } +} + +func TestValidatingAdmissionPolicyBindingDie_MissingMethods(t *testingx.T) { + die := ValidatingAdmissionPolicyBindingBlank + ignore := []string{"TypeMeta", "ObjectMeta"} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ValidatingAdmissionPolicyBindingDie: %s", diff.List()) + } +} + +func TestValidatingAdmissionPolicyBindingSpecDie_MissingMethods(t *testingx.T) { + die := ValidatingAdmissionPolicyBindingSpecBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ValidatingAdmissionPolicyBindingSpecDie: %s", diff.List()) + } +} + +func TestParamRefDie_MissingMethods(t *testingx.T) { + die := ParamRefBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ParamRefDie: %s", diff.List()) + } +} + func TestValidatingWebhookConfigurationDie_MissingMethods(t *testingx.T) { die := ValidatingWebhookConfigurationBlank ignore := []string{"TypeMeta", "ObjectMeta"} diff --git a/apis/authentication/v1/userinfo.go b/apis/authentication/v1/selfsubjectreview.go similarity index 68% rename from apis/authentication/v1/userinfo.go rename to apis/authentication/v1/selfsubjectreview.go index e084735..2923d8e 100644 --- a/apis/authentication/v1/userinfo.go +++ b/apis/authentication/v1/selfsubjectreview.go @@ -1,5 +1,5 @@ /* -Copyright 2022 the original author or authors. +Copyright 2024 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,6 +20,20 @@ import ( authenticationv1 "k8s.io/api/authentication/v1" ) +// +die:object=true,apiVersion=authentication.k8s.io/v1,kind=SelfSubjectReview +type _ = authenticationv1.SelfSubjectReview + +// +die +type _ = authenticationv1.SelfSubjectReviewStatus + +func (d *SelfSubjectReviewStatusDie) UserInfoDie(fn func(d *UserInfoDie)) *SelfSubjectReviewStatusDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReviewStatus) { + d := UserInfoBlank.DieImmutable(false).DieFeed(r.UserInfo) + fn(d) + r.UserInfo = d.DieRelease() + }) +} + // +die:ignore={Extra} type _ = authenticationv1.UserInfo diff --git a/apis/authentication/v1/zz_generated.die.go b/apis/authentication/v1/zz_generated.die.go index dcec637..72d5130 100644 --- a/apis/authentication/v1/zz_generated.die.go +++ b/apis/authentication/v1/zz_generated.die.go @@ -37,6 +37,695 @@ import ( yaml "sigs.k8s.io/yaml" ) +var SelfSubjectReviewBlank = (&SelfSubjectReviewDie{}).DieFeed(authenticationv1.SelfSubjectReview{}) + +type SelfSubjectReviewDie struct { + metav1.FrozenObjectMeta + mutable bool + r authenticationv1.SelfSubjectReview +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *SelfSubjectReviewDie) DieImmutable(immutable bool) *SelfSubjectReviewDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *SelfSubjectReviewDie) DieFeed(r authenticationv1.SelfSubjectReview) *SelfSubjectReviewDie { + if d.mutable { + d.FrozenObjectMeta = metav1.FreezeObjectMeta(r.ObjectMeta) + d.r = r + return d + } + return &SelfSubjectReviewDie{ + FrozenObjectMeta: metav1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *SelfSubjectReviewDie) DieFeedPtr(r *authenticationv1.SelfSubjectReview) *SelfSubjectReviewDie { + if r == nil { + r = &authenticationv1.SelfSubjectReview{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *SelfSubjectReviewDie) DieFeedJSON(j []byte) *SelfSubjectReviewDie { + r := authenticationv1.SelfSubjectReview{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *SelfSubjectReviewDie) DieFeedYAML(y []byte) *SelfSubjectReviewDie { + r := authenticationv1.SelfSubjectReview{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *SelfSubjectReviewDie) DieFeedYAMLFile(name string) *SelfSubjectReviewDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *SelfSubjectReviewDie) DieFeedRawExtension(raw runtime.RawExtension) *SelfSubjectReviewDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *SelfSubjectReviewDie) DieRelease() authenticationv1.SelfSubjectReview { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *SelfSubjectReviewDie) DieReleasePtr() *authenticationv1.SelfSubjectReview { + r := d.DieRelease() + return &r +} + +// DieReleaseUnstructured returns the resource managed by the die as an unstructured object. Panics on error. +func (d *SelfSubjectReviewDie) DieReleaseUnstructured() *unstructured.Unstructured { + r := d.DieReleasePtr() + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r) + if err != nil { + panic(err) + } + return &unstructured.Unstructured{ + Object: u, + } +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *SelfSubjectReviewDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *SelfSubjectReviewDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *SelfSubjectReviewDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *SelfSubjectReviewDie) DieStamp(fn func(r *authenticationv1.SelfSubjectReview)) *SelfSubjectReviewDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *SelfSubjectReviewDie) DieStampAt(jp string, fn interface{}) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *SelfSubjectReviewDie) DieWith(fns ...func(d *SelfSubjectReviewDie)) *SelfSubjectReviewDie { + nd := SelfSubjectReviewBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *SelfSubjectReviewDie) DeepCopy() *SelfSubjectReviewDie { + r := *d.r.DeepCopy() + return &SelfSubjectReviewDie{ + FrozenObjectMeta: metav1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +var _ runtime.Object = (*SelfSubjectReviewDie)(nil) + +func (d *SelfSubjectReviewDie) DeepCopyObject() runtime.Object { + return d.r.DeepCopy() +} + +func (d *SelfSubjectReviewDie) GetObjectKind() schema.ObjectKind { + r := d.DieRelease() + return r.GetObjectKind() +} + +func (d *SelfSubjectReviewDie) MarshalJSON() ([]byte, error) { + return json.Marshal(d.r) +} + +func (d *SelfSubjectReviewDie) UnmarshalJSON(b []byte) error { + if !d.mutable { + return fmtx.Errorf("cannot unmarshal into immutable dies, create a mutable version first") + } + r := &authenticationv1.SelfSubjectReview{} + err := json.Unmarshal(b, r) + *d = *d.DieFeed(*r) + return err +} + +// DieDefaultTypeMetadata sets the APIVersion and Kind to "authentication.k8s.io/v1" and "SelfSubjectReview" respectively. +func (d *SelfSubjectReviewDie) DieDefaultTypeMetadata() *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + r.APIVersion = "authentication.k8s.io/v1" + r.Kind = "SelfSubjectReview" + }) +} + +// APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources +func (d *SelfSubjectReviewDie) APIVersion(v string) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + r.APIVersion = v + }) +} + +// Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds +func (d *SelfSubjectReviewDie) Kind(v string) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + r.Kind = v + }) +} + +// TypeMetadata standard object's type metadata. +func (d *SelfSubjectReviewDie) TypeMetadata(v apismetav1.TypeMeta) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + r.TypeMeta = v + }) +} + +// TypeMetadataDie stamps the resource's TypeMeta field with a mutable die. +func (d *SelfSubjectReviewDie) TypeMetadataDie(fn func(d *metav1.TypeMetaDie)) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + d := metav1.TypeMetaBlank.DieImmutable(false).DieFeed(r.TypeMeta) + fn(d) + r.TypeMeta = d.DieRelease() + }) +} + +// Metadata standard object's metadata. +func (d *SelfSubjectReviewDie) Metadata(v apismetav1.ObjectMeta) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + r.ObjectMeta = v + }) +} + +// MetadataDie stamps the resource's ObjectMeta field with a mutable die. +func (d *SelfSubjectReviewDie) MetadataDie(fn func(d *metav1.ObjectMetaDie)) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + d := metav1.ObjectMetaBlank.DieImmutable(false).DieFeed(r.ObjectMeta) + fn(d) + r.ObjectMeta = d.DieRelease() + }) +} + +// StatusDie stamps the resource's status field with a mutable die. +func (d *SelfSubjectReviewDie) StatusDie(fn func(d *SelfSubjectReviewStatusDie)) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + d := SelfSubjectReviewStatusBlank.DieImmutable(false).DieFeed(r.Status) + fn(d) + r.Status = d.DieRelease() + }) +} + +// Status is filled in by the server with the user attributes. +func (d *SelfSubjectReviewDie) Status(v authenticationv1.SelfSubjectReviewStatus) *SelfSubjectReviewDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReview) { + r.Status = v + }) +} + +var SelfSubjectReviewStatusBlank = (&SelfSubjectReviewStatusDie{}).DieFeed(authenticationv1.SelfSubjectReviewStatus{}) + +type SelfSubjectReviewStatusDie struct { + mutable bool + r authenticationv1.SelfSubjectReviewStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *SelfSubjectReviewStatusDie) DieImmutable(immutable bool) *SelfSubjectReviewStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *SelfSubjectReviewStatusDie) DieFeed(r authenticationv1.SelfSubjectReviewStatus) *SelfSubjectReviewStatusDie { + if d.mutable { + d.r = r + return d + } + return &SelfSubjectReviewStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *SelfSubjectReviewStatusDie) DieFeedPtr(r *authenticationv1.SelfSubjectReviewStatus) *SelfSubjectReviewStatusDie { + if r == nil { + r = &authenticationv1.SelfSubjectReviewStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *SelfSubjectReviewStatusDie) DieFeedJSON(j []byte) *SelfSubjectReviewStatusDie { + r := authenticationv1.SelfSubjectReviewStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *SelfSubjectReviewStatusDie) DieFeedYAML(y []byte) *SelfSubjectReviewStatusDie { + r := authenticationv1.SelfSubjectReviewStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *SelfSubjectReviewStatusDie) DieFeedYAMLFile(name string) *SelfSubjectReviewStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *SelfSubjectReviewStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *SelfSubjectReviewStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *SelfSubjectReviewStatusDie) DieRelease() authenticationv1.SelfSubjectReviewStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *SelfSubjectReviewStatusDie) DieReleasePtr() *authenticationv1.SelfSubjectReviewStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *SelfSubjectReviewStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *SelfSubjectReviewStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *SelfSubjectReviewStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *SelfSubjectReviewStatusDie) DieStamp(fn func(r *authenticationv1.SelfSubjectReviewStatus)) *SelfSubjectReviewStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *SelfSubjectReviewStatusDie) DieStampAt(jp string, fn interface{}) *SelfSubjectReviewStatusDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReviewStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *SelfSubjectReviewStatusDie) DieWith(fns ...func(d *SelfSubjectReviewStatusDie)) *SelfSubjectReviewStatusDie { + nd := SelfSubjectReviewStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *SelfSubjectReviewStatusDie) DeepCopy() *SelfSubjectReviewStatusDie { + r := *d.r.DeepCopy() + return &SelfSubjectReviewStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// User attributes of the user making this request. +func (d *SelfSubjectReviewStatusDie) UserInfo(v authenticationv1.UserInfo) *SelfSubjectReviewStatusDie { + return d.DieStamp(func(r *authenticationv1.SelfSubjectReviewStatus) { + r.UserInfo = v + }) +} + +var UserInfoBlank = (&UserInfoDie{}).DieFeed(authenticationv1.UserInfo{}) + +type UserInfoDie struct { + mutable bool + r authenticationv1.UserInfo +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *UserInfoDie) DieImmutable(immutable bool) *UserInfoDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *UserInfoDie) DieFeed(r authenticationv1.UserInfo) *UserInfoDie { + if d.mutable { + d.r = r + return d + } + return &UserInfoDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *UserInfoDie) DieFeedPtr(r *authenticationv1.UserInfo) *UserInfoDie { + if r == nil { + r = &authenticationv1.UserInfo{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *UserInfoDie) DieFeedJSON(j []byte) *UserInfoDie { + r := authenticationv1.UserInfo{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *UserInfoDie) DieFeedYAML(y []byte) *UserInfoDie { + r := authenticationv1.UserInfo{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *UserInfoDie) DieFeedYAMLFile(name string) *UserInfoDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *UserInfoDie) DieFeedRawExtension(raw runtime.RawExtension) *UserInfoDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *UserInfoDie) DieRelease() authenticationv1.UserInfo { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *UserInfoDie) DieReleasePtr() *authenticationv1.UserInfo { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *UserInfoDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *UserInfoDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *UserInfoDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *UserInfoDie) DieStamp(fn func(r *authenticationv1.UserInfo)) *UserInfoDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *UserInfoDie) DieStampAt(jp string, fn interface{}) *UserInfoDie { + return d.DieStamp(func(r *authenticationv1.UserInfo) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *UserInfoDie) DieWith(fns ...func(d *UserInfoDie)) *UserInfoDie { + nd := UserInfoBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *UserInfoDie) DeepCopy() *UserInfoDie { + r := *d.r.DeepCopy() + return &UserInfoDie{ + mutable: d.mutable, + r: r, + } +} + +// The name that uniquely identifies this user among all active users. +func (d *UserInfoDie) Username(v string) *UserInfoDie { + return d.DieStamp(func(r *authenticationv1.UserInfo) { + r.Username = v + }) +} + +// A unique value that identifies this user across time. If this user is +// +// deleted and another user by the same name is added, they will have +// +// different UIDs. +func (d *UserInfoDie) UID(v string) *UserInfoDie { + return d.DieStamp(func(r *authenticationv1.UserInfo) { + r.UID = v + }) +} + +// The names of groups this user is a part of. +func (d *UserInfoDie) Groups(v ...string) *UserInfoDie { + return d.DieStamp(func(r *authenticationv1.UserInfo) { + r.Groups = v + }) +} + var TokenReviewBlank = (&TokenReviewDie{}).DieFeed(authenticationv1.TokenReview{}) type TokenReviewDie struct { @@ -958,210 +1647,3 @@ func (d *TokenRequestStatusDie) ExpirationTimestamp(v apismetav1.Time) *TokenReq r.ExpirationTimestamp = v }) } - -var UserInfoBlank = (&UserInfoDie{}).DieFeed(authenticationv1.UserInfo{}) - -type UserInfoDie struct { - mutable bool - r authenticationv1.UserInfo -} - -// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). -func (d *UserInfoDie) DieImmutable(immutable bool) *UserInfoDie { - if d.mutable == !immutable { - return d - } - d = d.DeepCopy() - d.mutable = !immutable - return d -} - -// DieFeed returns a new die with the provided resource. -func (d *UserInfoDie) DieFeed(r authenticationv1.UserInfo) *UserInfoDie { - if d.mutable { - d.r = r - return d - } - return &UserInfoDie{ - mutable: d.mutable, - r: r, - } -} - -// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. -func (d *UserInfoDie) DieFeedPtr(r *authenticationv1.UserInfo) *UserInfoDie { - if r == nil { - r = &authenticationv1.UserInfo{} - } - return d.DieFeed(*r) -} - -// DieFeedJSON returns a new die with the provided JSON. Panics on error. -func (d *UserInfoDie) DieFeedJSON(j []byte) *UserInfoDie { - r := authenticationv1.UserInfo{} - if err := json.Unmarshal(j, &r); err != nil { - panic(err) - } - return d.DieFeed(r) -} - -// DieFeedYAML returns a new die with the provided YAML. Panics on error. -func (d *UserInfoDie) DieFeedYAML(y []byte) *UserInfoDie { - r := authenticationv1.UserInfo{} - if err := yaml.Unmarshal(y, &r); err != nil { - panic(err) - } - return d.DieFeed(r) -} - -// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. -func (d *UserInfoDie) DieFeedYAMLFile(name string) *UserInfoDie { - y, err := osx.ReadFile(name) - if err != nil { - panic(err) - } - return d.DieFeedYAML(y) -} - -// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. -func (d *UserInfoDie) DieFeedRawExtension(raw runtime.RawExtension) *UserInfoDie { - j, err := json.Marshal(raw) - if err != nil { - panic(err) - } - return d.DieFeedJSON(j) -} - -// DieRelease returns the resource managed by the die. -func (d *UserInfoDie) DieRelease() authenticationv1.UserInfo { - if d.mutable { - return d.r - } - return *d.r.DeepCopy() -} - -// DieReleasePtr returns a pointer to the resource managed by the die. -func (d *UserInfoDie) DieReleasePtr() *authenticationv1.UserInfo { - r := d.DieRelease() - return &r -} - -// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. -func (d *UserInfoDie) DieReleaseJSON() []byte { - r := d.DieReleasePtr() - j, err := json.Marshal(r) - if err != nil { - panic(err) - } - return j -} - -// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. -func (d *UserInfoDie) DieReleaseYAML() []byte { - r := d.DieReleasePtr() - y, err := yaml.Marshal(r) - if err != nil { - panic(err) - } - return y -} - -// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. -func (d *UserInfoDie) DieReleaseRawExtension() runtime.RawExtension { - j := d.DieReleaseJSON() - raw := runtime.RawExtension{} - if err := json.Unmarshal(j, &raw); err != nil { - panic(err) - } - return raw -} - -// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. -func (d *UserInfoDie) DieStamp(fn func(r *authenticationv1.UserInfo)) *UserInfoDie { - r := d.DieRelease() - fn(&r) - return d.DieFeed(r) -} - -// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. -// -// Future iterations will improve type coercion from the resource to the callback argument. -func (d *UserInfoDie) DieStampAt(jp string, fn interface{}) *UserInfoDie { - return d.DieStamp(func(r *authenticationv1.UserInfo) { - if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { - panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) - } - if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { - panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) - } - - cp := jsonpath.New("") - if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { - panic(err) - } - cr, err := cp.FindResults(r) - if err != nil { - // errors are expected if a path is not found - return - } - for _, cv := range cr[0] { - arg0t := reflectx.ValueOf(fn).Type().In(0) - - var args []reflectx.Value - if cv.Type().AssignableTo(arg0t) { - args = []reflectx.Value{cv} - } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { - args = []reflectx.Value{cv.Addr()} - } else { - panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) - } - - reflectx.ValueOf(fn).Call(args) - } - }) -} - -// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. -func (d *UserInfoDie) DieWith(fns ...func(d *UserInfoDie)) *UserInfoDie { - nd := UserInfoBlank.DieFeed(d.DieRelease()).DieImmutable(false) - for _, fn := range fns { - if fn != nil { - fn(nd) - } - } - return d.DieFeed(nd.DieRelease()) -} - -// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. -func (d *UserInfoDie) DeepCopy() *UserInfoDie { - r := *d.r.DeepCopy() - return &UserInfoDie{ - mutable: d.mutable, - r: r, - } -} - -// The name that uniquely identifies this user among all active users. -func (d *UserInfoDie) Username(v string) *UserInfoDie { - return d.DieStamp(func(r *authenticationv1.UserInfo) { - r.Username = v - }) -} - -// A unique value that identifies this user across time. If this user is -// -// deleted and another user by the same name is added, they will have -// -// different UIDs. -func (d *UserInfoDie) UID(v string) *UserInfoDie { - return d.DieStamp(func(r *authenticationv1.UserInfo) { - r.UID = v - }) -} - -// The names of groups this user is a part of. -func (d *UserInfoDie) Groups(v ...string) *UserInfoDie { - return d.DieStamp(func(r *authenticationv1.UserInfo) { - r.Groups = v - }) -} diff --git a/apis/authentication/v1/zz_generated.die_test.go b/apis/authentication/v1/zz_generated.die_test.go index 635d669..cbbcbdd 100644 --- a/apis/authentication/v1/zz_generated.die_test.go +++ b/apis/authentication/v1/zz_generated.die_test.go @@ -26,6 +26,33 @@ import ( testingx "testing" ) +func TestSelfSubjectReviewDie_MissingMethods(t *testingx.T) { + die := SelfSubjectReviewBlank + ignore := []string{"TypeMeta", "ObjectMeta"} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for SelfSubjectReviewDie: %s", diff.List()) + } +} + +func TestSelfSubjectReviewStatusDie_MissingMethods(t *testingx.T) { + die := SelfSubjectReviewStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for SelfSubjectReviewStatusDie: %s", diff.List()) + } +} + +func TestUserInfoDie_MissingMethods(t *testingx.T) { + die := UserInfoBlank + ignore := []string{"Extra"} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for UserInfoDie: %s", diff.List()) + } +} + func TestTokenReviewDie_MissingMethods(t *testingx.T) { die := TokenReviewBlank ignore := []string{"TypeMeta", "ObjectMeta"} @@ -61,12 +88,3 @@ func TestTokenRequestStatusDie_MissingMethods(t *testingx.T) { t.Errorf("found missing fields for TokenRequestStatusDie: %s", diff.List()) } } - -func TestUserInfoDie_MissingMethods(t *testingx.T) { - die := UserInfoBlank - ignore := []string{"Extra"} - diff := testing.DieFieldDiff(die).Delete(ignore...) - if diff.Len() != 0 { - t.Errorf("found missing fields for UserInfoDie: %s", diff.List()) - } -} diff --git a/apis/autoscaling/v2/horizontalpodautoscaler.go b/apis/autoscaling/v2/horizontalpodautoscaler.go new file mode 100644 index 0000000..12e08cf --- /dev/null +++ b/apis/autoscaling/v2/horizontalpodautoscaler.go @@ -0,0 +1,417 @@ +/* +Copyright 2024 the original author or authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v2 + +import ( + autoscalingv2 "k8s.io/api/autoscaling/v2" + corev1 "k8s.io/api/core/v1" + resource "k8s.io/apimachinery/pkg/api/resource" + diemetav1 "reconciler.io/dies/apis/meta/v1" +) + +// +die:object=true,apiVersion=autoscaling/v2,kind=HorizontalPodAutoscaler +type _ = autoscalingv2.HorizontalPodAutoscaler + +// +die +type _ = autoscalingv2.HorizontalPodAutoscalerSpec + +func (d *HorizontalPodAutoscalerSpecDie) ScaleTargetRefDie(fn func(d *CrossVersionObjectReferenceDie)) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + d := CrossVersionObjectReferenceBlank.DieImmutable(false).DieFeed(r.ScaleTargetRef) + fn(d) + r.ScaleTargetRef = d.DieRelease() + }) +} + +func (d *HorizontalPodAutoscalerSpecDie) ConditionsDie(metrics ...*MetricSpecDie) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + r.Metrics = make([]autoscalingv2.MetricSpec, len(metrics)) + for i := range metrics { + r.Metrics[i] = metrics[i].DieRelease() + } + }) +} + +func (d *HorizontalPodAutoscalerSpecDie) BehaviorDie(fn func(d *HorizontalPodAutoscalerBehaviorDie)) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + d := HorizontalPodAutoscalerBehaviorBlank.DieImmutable(false).DieFeedPtr(r.Behavior) + fn(d) + r.Behavior = d.DieReleasePtr() + }) +} + +// +die +type _ = autoscalingv2.CrossVersionObjectReference + +// +die +type _ = autoscalingv2.MetricSpec + +func (d *MetricSpecDie) BehaviorDie(fn func(d *ObjectMetricSourceDie)) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + d := ObjectMetricSourceBlank.DieImmutable(false).DieFeedPtr(r.Object) + fn(d) + r.Object = d.DieReleasePtr() + }) +} + +func (d *MetricSpecDie) PodsDie(fn func(d *PodsMetricSourceDie)) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + d := PodsMetricSourceBlank.DieImmutable(false).DieFeedPtr(r.Pods) + fn(d) + r.Pods = d.DieReleasePtr() + }) +} + +func (d *MetricSpecDie) ResourceDie(fn func(d *ResourceMetricSourceDie)) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + d := ResourceMetricSourceBlank.DieImmutable(false).DieFeedPtr(r.Resource) + fn(d) + r.Resource = d.DieReleasePtr() + }) +} + +func (d *MetricSpecDie) ContainerResourceDie(fn func(d *ContainerResourceMetricSourceDie)) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + d := ContainerResourceMetricSourceBlank.DieImmutable(false).DieFeedPtr(r.ContainerResource) + fn(d) + r.ContainerResource = d.DieReleasePtr() + }) +} + +func (d *MetricSpecDie) ExternalDie(fn func(d *ExternalMetricSourceDie)) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + d := ExternalMetricSourceBlank.DieImmutable(false).DieFeedPtr(r.External) + fn(d) + r.External = d.DieReleasePtr() + }) +} + +// +die +type _ = autoscalingv2.ObjectMetricSource + +func (d *ObjectMetricSourceDie) DescribedObjectDie(fn func(d *CrossVersionObjectReferenceDie)) *ObjectMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricSource) { + d := CrossVersionObjectReferenceBlank.DieImmutable(false).DieFeed(r.DescribedObject) + fn(d) + r.DescribedObject = d.DieRelease() + }) +} + +func (d *ObjectMetricSourceDie) TargetDie(fn func(d *MetricTargetDie)) *ObjectMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricSource) { + d := MetricTargetBlank.DieImmutable(false).DieFeed(r.Target) + fn(d) + r.Target = d.DieRelease() + }) +} + +func (d *ObjectMetricSourceDie) MetricDie(fn func(d *MetricIdentifierDie)) *ObjectMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricSource) { + d := MetricIdentifierBlank.DieImmutable(false).DieFeed(r.Metric) + fn(d) + r.Metric = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.MetricTarget + +func (d *MetricTargetDie) ValueString(quantity string) *MetricTargetDie { + q := resource.MustParse(quantity) + return d.Value(&q) +} + +func (d *MetricTargetDie) AverageValueString(quantity string) *MetricTargetDie { + q := resource.MustParse(quantity) + return d.AverageValue(&q) +} + +// +die +type _ = autoscalingv2.MetricIdentifier + +func (d *MetricIdentifierDie) SelectorDie(fn func(d *diemetav1.LabelSelectorDie)) *MetricIdentifierDie { + return d.DieStamp(func(r *autoscalingv2.MetricIdentifier) { + d := diemetav1.LabelSelectorBlank.DieImmutable(false).DieFeedPtr(r.Selector) + fn(d) + r.Selector = d.DieReleasePtr() + }) +} + +// +die +type _ = autoscalingv2.PodsMetricSource + +func (d *PodsMetricSourceDie) MetricDie(fn func(d *MetricIdentifierDie)) *PodsMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricSource) { + d := MetricIdentifierBlank.DieImmutable(false).DieFeed(r.Metric) + fn(d) + r.Metric = d.DieRelease() + }) +} + +func (d *PodsMetricSourceDie) TargetDie(fn func(d *MetricTargetDie)) *PodsMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricSource) { + d := MetricTargetBlank.DieImmutable(false).DieFeed(r.Target) + fn(d) + r.Target = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.ResourceMetricSource + +func (d *ResourceMetricSourceDie) TargetDie(fn func(d *MetricTargetDie)) *ResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ResourceMetricSource) { + d := MetricTargetBlank.DieImmutable(false).DieFeed(r.Target) + fn(d) + r.Target = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.ContainerResourceMetricSource + +func (d *ContainerResourceMetricSourceDie) TargetDie(fn func(d *MetricTargetDie)) *ContainerResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricSource) { + d := MetricTargetBlank.DieImmutable(false).DieFeed(r.Target) + fn(d) + r.Target = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.ExternalMetricSource + +func (d *ExternalMetricSourceDie) MetricDie(fn func(d *MetricIdentifierDie)) *ExternalMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricSource) { + d := MetricIdentifierBlank.DieImmutable(false).DieFeed(r.Metric) + fn(d) + r.Metric = d.DieRelease() + }) +} + +func (d *ExternalMetricSourceDie) TargetDie(fn func(d *MetricTargetDie)) *ExternalMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricSource) { + d := MetricTargetBlank.DieImmutable(false).DieFeed(r.Target) + fn(d) + r.Target = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.HorizontalPodAutoscalerBehavior + +func (d *HorizontalPodAutoscalerBehaviorDie) ScaleUpDie(fn func(d *HPAScalingRulesDie)) *HorizontalPodAutoscalerBehaviorDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerBehavior) { + d := HPAScalingRulesBlank.DieImmutable(false).DieFeedPtr(r.ScaleUp) + fn(d) + r.ScaleUp = d.DieReleasePtr() + }) +} + +func (d *HorizontalPodAutoscalerBehaviorDie) ScaleDownDie(fn func(d *HPAScalingRulesDie)) *HorizontalPodAutoscalerBehaviorDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerBehavior) { + d := HPAScalingRulesBlank.DieImmutable(false).DieFeedPtr(r.ScaleDown) + fn(d) + r.ScaleDown = d.DieReleasePtr() + }) +} + +// +die +type _ = autoscalingv2.HPAScalingRules + +func (d *HPAScalingRulesDie) PoliciesDie(policies ...*HPAScalingPolicyDie) *HPAScalingRulesDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingRules) { + r.Policies = make([]autoscalingv2.HPAScalingPolicy, len(policies)) + for i := range policies { + r.Policies[i] = policies[i].DieRelease() + } + }) +} + +// +die +type _ = autoscalingv2.HPAScalingPolicy + +// +die +type _ = autoscalingv2.HorizontalPodAutoscalerStatus + +func (d *HorizontalPodAutoscalerStatusDie) CurrentMetricsDie(metrics ...*MetricStatusDie) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + r.CurrentMetrics = make([]autoscalingv2.MetricStatus, len(metrics)) + for i := range metrics { + r.CurrentMetrics[i] = metrics[i].DieRelease() + } + }) +} + +func (d *HorizontalPodAutoscalerStatusDie) ConditionsDie(conditions ...*diemetav1.ConditionDie) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + r.Conditions = make([]autoscalingv2.HorizontalPodAutoscalerCondition, len(conditions)) + for i := range conditions { + c := conditions[i].DieRelease() + r.Conditions[i] = autoscalingv2.HorizontalPodAutoscalerCondition{ + Type: autoscalingv2.HorizontalPodAutoscalerConditionType(c.Type), + Status: corev1.ConditionStatus(c.Status), + LastTransitionTime: c.LastTransitionTime, + Reason: c.Reason, + Message: c.Message, + } + } + }) +} + +// +die +type _ = autoscalingv2.MetricStatus + +func (d *MetricStatusDie) ObjectDie(fn func(d *ObjectMetricStatusDie)) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + d := ObjectMetricStatusBlank.DieImmutable(false).DieFeedPtr(r.Object) + fn(d) + r.Object = d.DieReleasePtr() + }) +} + +func (d *MetricStatusDie) PodDie(fn func(d *PodsMetricStatusDie)) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + d := PodsMetricStatusBlank.DieImmutable(false).DieFeedPtr(r.Pods) + fn(d) + r.Pods = d.DieReleasePtr() + }) +} + +func (d *MetricStatusDie) ResourceDie(fn func(d *ResourceMetricStatusDie)) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + d := ResourceMetricStatusBlank.DieImmutable(false).DieFeedPtr(r.Resource) + fn(d) + r.Resource = d.DieReleasePtr() + }) +} + +func (d *MetricStatusDie) ContainerResourceDie(fn func(d *ContainerResourceMetricStatusDie)) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + d := ContainerResourceMetricStatusBlank.DieImmutable(false).DieFeedPtr(r.ContainerResource) + fn(d) + r.ContainerResource = d.DieReleasePtr() + }) +} + +func (d *MetricStatusDie) ExternalDie(fn func(d *ExternalMetricStatusDie)) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + d := ExternalMetricStatusBlank.DieImmutable(false).DieFeedPtr(r.External) + fn(d) + r.External = d.DieReleasePtr() + }) +} + +// +die +type _ = autoscalingv2.ObjectMetricStatus + +func (d *ObjectMetricStatusDie) MetricDie(fn func(d *MetricIdentifierDie)) *ObjectMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricStatus) { + d := MetricIdentifierBlank.DieImmutable(false).DieFeed(r.Metric) + fn(d) + r.Metric = d.DieRelease() + }) +} + +func (d *ObjectMetricStatusDie) CurrentDie(fn func(d *MetricValueStatusDie)) *ObjectMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricStatus) { + d := MetricValueStatusBlank.DieImmutable(false).DieFeed(r.Current) + fn(d) + r.Current = d.DieRelease() + }) +} + +func (d *ObjectMetricStatusDie) DescribedObjectDie(fn func(d *CrossVersionObjectReferenceDie)) *ObjectMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricStatus) { + d := CrossVersionObjectReferenceBlank.DieImmutable(false).DieFeed(r.DescribedObject) + fn(d) + r.DescribedObject = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.MetricValueStatus + +func (d *MetricValueStatusDie) ValueString(quantity string) *MetricValueStatusDie { + q := resource.MustParse(quantity) + return d.Value(&q) +} + +func (d *MetricValueStatusDie) AverageValueString(quantity string) *MetricValueStatusDie { + q := resource.MustParse(quantity) + return d.AverageValue(&q) +} + +// +die +type _ = autoscalingv2.PodsMetricStatus + +func (d *PodsMetricStatusDie) MetricDie(fn func(d *MetricIdentifierDie)) *PodsMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricStatus) { + d := MetricIdentifierBlank.DieImmutable(false).DieFeed(r.Metric) + fn(d) + r.Metric = d.DieRelease() + }) +} + +func (d *PodsMetricStatusDie) CurrentDie(fn func(d *MetricValueStatusDie)) *PodsMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricStatus) { + d := MetricValueStatusBlank.DieImmutable(false).DieFeed(r.Current) + fn(d) + r.Current = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.ResourceMetricStatus + +func (d *ResourceMetricStatusDie) CurrentDie(fn func(d *MetricValueStatusDie)) *ResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ResourceMetricStatus) { + d := MetricValueStatusBlank.DieImmutable(false).DieFeed(r.Current) + fn(d) + r.Current = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.ContainerResourceMetricStatus + +func (d *ContainerResourceMetricStatusDie) CurrentDie(fn func(d *MetricValueStatusDie)) *ContainerResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricStatus) { + d := MetricValueStatusBlank.DieImmutable(false).DieFeed(r.Current) + fn(d) + r.Current = d.DieRelease() + }) +} + +// +die +type _ = autoscalingv2.ExternalMetricStatus + +func (d *ExternalMetricStatusDie) MetricDie(fn func(d *MetricIdentifierDie)) *ExternalMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricStatus) { + d := MetricIdentifierBlank.DieImmutable(false).DieFeed(r.Metric) + fn(d) + r.Metric = d.DieRelease() + }) +} + +func (d *ExternalMetricStatusDie) CurrentDie(fn func(d *MetricValueStatusDie)) *ExternalMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricStatus) { + d := MetricValueStatusBlank.DieImmutable(false).DieFeed(r.Current) + fn(d) + r.Current = d.DieRelease() + }) +} diff --git a/apis/autoscaling/v2/zz_generated.die.go b/apis/autoscaling/v2/zz_generated.die.go new file mode 100644 index 0000000..ed6da60 --- /dev/null +++ b/apis/autoscaling/v2/zz_generated.die.go @@ -0,0 +1,4803 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright 2021-2022 the original author or authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by diegen. DO NOT EDIT. + +package v2 + +import ( + json "encoding/json" + fmtx "fmt" + autoscalingv2 "k8s.io/api/autoscaling/v2" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + runtime "k8s.io/apimachinery/pkg/runtime" + schema "k8s.io/apimachinery/pkg/runtime/schema" + jsonpath "k8s.io/client-go/util/jsonpath" + osx "os" + "reconciler.io/dies/apis/meta/v1" + reflectx "reflect" + yaml "sigs.k8s.io/yaml" +) + +var HorizontalPodAutoscalerBlank = (&HorizontalPodAutoscalerDie{}).DieFeed(autoscalingv2.HorizontalPodAutoscaler{}) + +type HorizontalPodAutoscalerDie struct { + v1.FrozenObjectMeta + mutable bool + r autoscalingv2.HorizontalPodAutoscaler +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *HorizontalPodAutoscalerDie) DieImmutable(immutable bool) *HorizontalPodAutoscalerDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *HorizontalPodAutoscalerDie) DieFeed(r autoscalingv2.HorizontalPodAutoscaler) *HorizontalPodAutoscalerDie { + if d.mutable { + d.FrozenObjectMeta = v1.FreezeObjectMeta(r.ObjectMeta) + d.r = r + return d + } + return &HorizontalPodAutoscalerDie{ + FrozenObjectMeta: v1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *HorizontalPodAutoscalerDie) DieFeedPtr(r *autoscalingv2.HorizontalPodAutoscaler) *HorizontalPodAutoscalerDie { + if r == nil { + r = &autoscalingv2.HorizontalPodAutoscaler{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *HorizontalPodAutoscalerDie) DieFeedJSON(j []byte) *HorizontalPodAutoscalerDie { + r := autoscalingv2.HorizontalPodAutoscaler{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *HorizontalPodAutoscalerDie) DieFeedYAML(y []byte) *HorizontalPodAutoscalerDie { + r := autoscalingv2.HorizontalPodAutoscaler{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *HorizontalPodAutoscalerDie) DieFeedYAMLFile(name string) *HorizontalPodAutoscalerDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HorizontalPodAutoscalerDie) DieFeedRawExtension(raw runtime.RawExtension) *HorizontalPodAutoscalerDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *HorizontalPodAutoscalerDie) DieRelease() autoscalingv2.HorizontalPodAutoscaler { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *HorizontalPodAutoscalerDie) DieReleasePtr() *autoscalingv2.HorizontalPodAutoscaler { + r := d.DieRelease() + return &r +} + +// DieReleaseUnstructured returns the resource managed by the die as an unstructured object. Panics on error. +func (d *HorizontalPodAutoscalerDie) DieReleaseUnstructured() *unstructured.Unstructured { + r := d.DieReleasePtr() + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r) + if err != nil { + panic(err) + } + return &unstructured.Unstructured{ + Object: u, + } +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *HorizontalPodAutoscalerDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *HorizontalPodAutoscalerDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HorizontalPodAutoscalerDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *HorizontalPodAutoscalerDie) DieStamp(fn func(r *autoscalingv2.HorizontalPodAutoscaler)) *HorizontalPodAutoscalerDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *HorizontalPodAutoscalerDie) DieStampAt(jp string, fn interface{}) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *HorizontalPodAutoscalerDie) DieWith(fns ...func(d *HorizontalPodAutoscalerDie)) *HorizontalPodAutoscalerDie { + nd := HorizontalPodAutoscalerBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *HorizontalPodAutoscalerDie) DeepCopy() *HorizontalPodAutoscalerDie { + r := *d.r.DeepCopy() + return &HorizontalPodAutoscalerDie{ + FrozenObjectMeta: v1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +var _ runtime.Object = (*HorizontalPodAutoscalerDie)(nil) + +func (d *HorizontalPodAutoscalerDie) DeepCopyObject() runtime.Object { + return d.r.DeepCopy() +} + +func (d *HorizontalPodAutoscalerDie) GetObjectKind() schema.ObjectKind { + r := d.DieRelease() + return r.GetObjectKind() +} + +func (d *HorizontalPodAutoscalerDie) MarshalJSON() ([]byte, error) { + return json.Marshal(d.r) +} + +func (d *HorizontalPodAutoscalerDie) UnmarshalJSON(b []byte) error { + if !d.mutable { + return fmtx.Errorf("cannot unmarshal into immutable dies, create a mutable version first") + } + r := &autoscalingv2.HorizontalPodAutoscaler{} + err := json.Unmarshal(b, r) + *d = *d.DieFeed(*r) + return err +} + +// DieDefaultTypeMetadata sets the APIVersion and Kind to "autoscaling/v2" and "HorizontalPodAutoscaler" respectively. +func (d *HorizontalPodAutoscalerDie) DieDefaultTypeMetadata() *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + r.APIVersion = "autoscaling/v2" + r.Kind = "HorizontalPodAutoscaler" + }) +} + +// APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources +func (d *HorizontalPodAutoscalerDie) APIVersion(v string) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + r.APIVersion = v + }) +} + +// Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds +func (d *HorizontalPodAutoscalerDie) Kind(v string) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + r.Kind = v + }) +} + +// TypeMetadata standard object's type metadata. +func (d *HorizontalPodAutoscalerDie) TypeMetadata(v metav1.TypeMeta) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + r.TypeMeta = v + }) +} + +// TypeMetadataDie stamps the resource's TypeMeta field with a mutable die. +func (d *HorizontalPodAutoscalerDie) TypeMetadataDie(fn func(d *v1.TypeMetaDie)) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + d := v1.TypeMetaBlank.DieImmutable(false).DieFeed(r.TypeMeta) + fn(d) + r.TypeMeta = d.DieRelease() + }) +} + +// Metadata standard object's metadata. +func (d *HorizontalPodAutoscalerDie) Metadata(v metav1.ObjectMeta) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + r.ObjectMeta = v + }) +} + +// MetadataDie stamps the resource's ObjectMeta field with a mutable die. +func (d *HorizontalPodAutoscalerDie) MetadataDie(fn func(d *v1.ObjectMetaDie)) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + d := v1.ObjectMetaBlank.DieImmutable(false).DieFeed(r.ObjectMeta) + fn(d) + r.ObjectMeta = d.DieRelease() + }) +} + +// SpecDie stamps the resource's spec field with a mutable die. +func (d *HorizontalPodAutoscalerDie) SpecDie(fn func(d *HorizontalPodAutoscalerSpecDie)) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + d := HorizontalPodAutoscalerSpecBlank.DieImmutable(false).DieFeed(r.Spec) + fn(d) + r.Spec = d.DieRelease() + }) +} + +// StatusDie stamps the resource's status field with a mutable die. +func (d *HorizontalPodAutoscalerDie) StatusDie(fn func(d *HorizontalPodAutoscalerStatusDie)) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + d := HorizontalPodAutoscalerStatusBlank.DieImmutable(false).DieFeed(r.Status) + fn(d) + r.Status = d.DieRelease() + }) +} + +// spec is the specification for the behaviour of the autoscaler. +// +// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status. +func (d *HorizontalPodAutoscalerDie) Spec(v autoscalingv2.HorizontalPodAutoscalerSpec) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + r.Spec = v + }) +} + +// status is the current information about the autoscaler. +func (d *HorizontalPodAutoscalerDie) Status(v autoscalingv2.HorizontalPodAutoscalerStatus) *HorizontalPodAutoscalerDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscaler) { + r.Status = v + }) +} + +var HorizontalPodAutoscalerSpecBlank = (&HorizontalPodAutoscalerSpecDie{}).DieFeed(autoscalingv2.HorizontalPodAutoscalerSpec{}) + +type HorizontalPodAutoscalerSpecDie struct { + mutable bool + r autoscalingv2.HorizontalPodAutoscalerSpec +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *HorizontalPodAutoscalerSpecDie) DieImmutable(immutable bool) *HorizontalPodAutoscalerSpecDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *HorizontalPodAutoscalerSpecDie) DieFeed(r autoscalingv2.HorizontalPodAutoscalerSpec) *HorizontalPodAutoscalerSpecDie { + if d.mutable { + d.r = r + return d + } + return &HorizontalPodAutoscalerSpecDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *HorizontalPodAutoscalerSpecDie) DieFeedPtr(r *autoscalingv2.HorizontalPodAutoscalerSpec) *HorizontalPodAutoscalerSpecDie { + if r == nil { + r = &autoscalingv2.HorizontalPodAutoscalerSpec{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *HorizontalPodAutoscalerSpecDie) DieFeedJSON(j []byte) *HorizontalPodAutoscalerSpecDie { + r := autoscalingv2.HorizontalPodAutoscalerSpec{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *HorizontalPodAutoscalerSpecDie) DieFeedYAML(y []byte) *HorizontalPodAutoscalerSpecDie { + r := autoscalingv2.HorizontalPodAutoscalerSpec{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *HorizontalPodAutoscalerSpecDie) DieFeedYAMLFile(name string) *HorizontalPodAutoscalerSpecDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HorizontalPodAutoscalerSpecDie) DieFeedRawExtension(raw runtime.RawExtension) *HorizontalPodAutoscalerSpecDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *HorizontalPodAutoscalerSpecDie) DieRelease() autoscalingv2.HorizontalPodAutoscalerSpec { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *HorizontalPodAutoscalerSpecDie) DieReleasePtr() *autoscalingv2.HorizontalPodAutoscalerSpec { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *HorizontalPodAutoscalerSpecDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *HorizontalPodAutoscalerSpecDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HorizontalPodAutoscalerSpecDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *HorizontalPodAutoscalerSpecDie) DieStamp(fn func(r *autoscalingv2.HorizontalPodAutoscalerSpec)) *HorizontalPodAutoscalerSpecDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *HorizontalPodAutoscalerSpecDie) DieStampAt(jp string, fn interface{}) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *HorizontalPodAutoscalerSpecDie) DieWith(fns ...func(d *HorizontalPodAutoscalerSpecDie)) *HorizontalPodAutoscalerSpecDie { + nd := HorizontalPodAutoscalerSpecBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *HorizontalPodAutoscalerSpecDie) DeepCopy() *HorizontalPodAutoscalerSpecDie { + r := *d.r.DeepCopy() + return &HorizontalPodAutoscalerSpecDie{ + mutable: d.mutable, + r: r, + } +} + +// scaleTargetRef points to the target resource to scale, and is used to the pods for which metrics +// +// should be collected, as well as to actually change the replica count. +func (d *HorizontalPodAutoscalerSpecDie) ScaleTargetRef(v autoscalingv2.CrossVersionObjectReference) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + r.ScaleTargetRef = v + }) +} + +// minReplicas is the lower limit for the number of replicas to which the autoscaler +// +// can scale down. It defaults to 1 pod. minReplicas is allowed to be 0 if the +// +// alpha feature gate HPAScaleToZero is enabled and at least one Object or External +// +// metric is configured. Scaling is active as long as at least one metric value is +// +// available. +func (d *HorizontalPodAutoscalerSpecDie) MinReplicas(v *int32) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + r.MinReplicas = v + }) +} + +// maxReplicas is the upper limit for the number of replicas to which the autoscaler can scale up. +// +// It cannot be less that minReplicas. +func (d *HorizontalPodAutoscalerSpecDie) MaxReplicas(v int32) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + r.MaxReplicas = v + }) +} + +// metrics contains the specifications for which to use to calculate the +// +// desired replica count (the maximum replica count across all metrics will +// +// be used). The desired replica count is calculated multiplying the +// +// ratio between the target value and the current value by the current +// +// number of pods. Ergo, metrics used must decrease as the pod count is +// +// increased, and vice-versa. See the individual metric source types for +// +// more information about how each type of metric must respond. +// +// If not set, the default metric will be set to 80% average CPU utilization. +func (d *HorizontalPodAutoscalerSpecDie) Metrics(v ...autoscalingv2.MetricSpec) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + r.Metrics = v + }) +} + +// behavior configures the scaling behavior of the target +// +// in both Up and Down directions (scaleUp and scaleDown fields respectively). +// +// If not set, the default HPAScalingRules for scale up and scale down are used. +func (d *HorizontalPodAutoscalerSpecDie) Behavior(v *autoscalingv2.HorizontalPodAutoscalerBehavior) *HorizontalPodAutoscalerSpecDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerSpec) { + r.Behavior = v + }) +} + +var CrossVersionObjectReferenceBlank = (&CrossVersionObjectReferenceDie{}).DieFeed(autoscalingv2.CrossVersionObjectReference{}) + +type CrossVersionObjectReferenceDie struct { + mutable bool + r autoscalingv2.CrossVersionObjectReference +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *CrossVersionObjectReferenceDie) DieImmutable(immutable bool) *CrossVersionObjectReferenceDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *CrossVersionObjectReferenceDie) DieFeed(r autoscalingv2.CrossVersionObjectReference) *CrossVersionObjectReferenceDie { + if d.mutable { + d.r = r + return d + } + return &CrossVersionObjectReferenceDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *CrossVersionObjectReferenceDie) DieFeedPtr(r *autoscalingv2.CrossVersionObjectReference) *CrossVersionObjectReferenceDie { + if r == nil { + r = &autoscalingv2.CrossVersionObjectReference{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *CrossVersionObjectReferenceDie) DieFeedJSON(j []byte) *CrossVersionObjectReferenceDie { + r := autoscalingv2.CrossVersionObjectReference{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *CrossVersionObjectReferenceDie) DieFeedYAML(y []byte) *CrossVersionObjectReferenceDie { + r := autoscalingv2.CrossVersionObjectReference{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *CrossVersionObjectReferenceDie) DieFeedYAMLFile(name string) *CrossVersionObjectReferenceDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *CrossVersionObjectReferenceDie) DieFeedRawExtension(raw runtime.RawExtension) *CrossVersionObjectReferenceDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *CrossVersionObjectReferenceDie) DieRelease() autoscalingv2.CrossVersionObjectReference { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *CrossVersionObjectReferenceDie) DieReleasePtr() *autoscalingv2.CrossVersionObjectReference { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *CrossVersionObjectReferenceDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *CrossVersionObjectReferenceDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *CrossVersionObjectReferenceDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *CrossVersionObjectReferenceDie) DieStamp(fn func(r *autoscalingv2.CrossVersionObjectReference)) *CrossVersionObjectReferenceDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *CrossVersionObjectReferenceDie) DieStampAt(jp string, fn interface{}) *CrossVersionObjectReferenceDie { + return d.DieStamp(func(r *autoscalingv2.CrossVersionObjectReference) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *CrossVersionObjectReferenceDie) DieWith(fns ...func(d *CrossVersionObjectReferenceDie)) *CrossVersionObjectReferenceDie { + nd := CrossVersionObjectReferenceBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *CrossVersionObjectReferenceDie) DeepCopy() *CrossVersionObjectReferenceDie { + r := *d.r.DeepCopy() + return &CrossVersionObjectReferenceDie{ + mutable: d.mutable, + r: r, + } +} + +// kind is the kind of the referent; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds +func (d *CrossVersionObjectReferenceDie) Kind(v string) *CrossVersionObjectReferenceDie { + return d.DieStamp(func(r *autoscalingv2.CrossVersionObjectReference) { + r.Kind = v + }) +} + +// name is the name of the referent; More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names +func (d *CrossVersionObjectReferenceDie) Name(v string) *CrossVersionObjectReferenceDie { + return d.DieStamp(func(r *autoscalingv2.CrossVersionObjectReference) { + r.Name = v + }) +} + +// apiVersion is the API version of the referent +func (d *CrossVersionObjectReferenceDie) APIVersion(v string) *CrossVersionObjectReferenceDie { + return d.DieStamp(func(r *autoscalingv2.CrossVersionObjectReference) { + r.APIVersion = v + }) +} + +var MetricSpecBlank = (&MetricSpecDie{}).DieFeed(autoscalingv2.MetricSpec{}) + +type MetricSpecDie struct { + mutable bool + r autoscalingv2.MetricSpec +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *MetricSpecDie) DieImmutable(immutable bool) *MetricSpecDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *MetricSpecDie) DieFeed(r autoscalingv2.MetricSpec) *MetricSpecDie { + if d.mutable { + d.r = r + return d + } + return &MetricSpecDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *MetricSpecDie) DieFeedPtr(r *autoscalingv2.MetricSpec) *MetricSpecDie { + if r == nil { + r = &autoscalingv2.MetricSpec{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *MetricSpecDie) DieFeedJSON(j []byte) *MetricSpecDie { + r := autoscalingv2.MetricSpec{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *MetricSpecDie) DieFeedYAML(y []byte) *MetricSpecDie { + r := autoscalingv2.MetricSpec{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *MetricSpecDie) DieFeedYAMLFile(name string) *MetricSpecDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricSpecDie) DieFeedRawExtension(raw runtime.RawExtension) *MetricSpecDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *MetricSpecDie) DieRelease() autoscalingv2.MetricSpec { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *MetricSpecDie) DieReleasePtr() *autoscalingv2.MetricSpec { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *MetricSpecDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *MetricSpecDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricSpecDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *MetricSpecDie) DieStamp(fn func(r *autoscalingv2.MetricSpec)) *MetricSpecDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *MetricSpecDie) DieStampAt(jp string, fn interface{}) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *MetricSpecDie) DieWith(fns ...func(d *MetricSpecDie)) *MetricSpecDie { + nd := MetricSpecBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *MetricSpecDie) DeepCopy() *MetricSpecDie { + r := *d.r.DeepCopy() + return &MetricSpecDie{ + mutable: d.mutable, + r: r, + } +} + +// type is the type of metric source. It should be one of "ContainerResource", "External", +// +// "Object", "Pods" or "Resource", each mapping to a matching field in the object. +// +// Note: "ContainerResource" type is available on when the feature-gate +// +// HPAContainerMetrics is enabled +func (d *MetricSpecDie) Type(v autoscalingv2.MetricSourceType) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + r.Type = v + }) +} + +// object refers to a metric describing a single kubernetes object +// +// (for example, hits-per-second on an Ingress object). +func (d *MetricSpecDie) Object(v *autoscalingv2.ObjectMetricSource) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + r.Object = v + }) +} + +// pods refers to a metric describing each pod in the current scale target +// +// (for example, transactions-processed-per-second). The values will be +// +// averaged together before being compared to the target value. +func (d *MetricSpecDie) Pods(v *autoscalingv2.PodsMetricSource) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + r.Pods = v + }) +} + +// resource refers to a resource metric (such as those specified in +// +// requests and limits) known to Kubernetes describing each pod in the +// +// current scale target (e.g. CPU or memory). Such metrics are built in to +// +// # Kubernetes, and have special scaling options on top of those available +// +// to normal per-pod metrics using the "pods" source. +func (d *MetricSpecDie) Resource(v *autoscalingv2.ResourceMetricSource) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + r.Resource = v + }) +} + +// containerResource refers to a resource metric (such as those specified in +// +// requests and limits) known to Kubernetes describing a single container in +// +// each pod of the current scale target (e.g. CPU or memory). Such metrics are +// +// built in to Kubernetes, and have special scaling options on top of those +// +// available to normal per-pod metrics using the "pods" source. +// +// This is an alpha feature and can be enabled by the HPAContainerMetrics feature flag. +func (d *MetricSpecDie) ContainerResource(v *autoscalingv2.ContainerResourceMetricSource) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + r.ContainerResource = v + }) +} + +// external refers to a global metric that is not associated +// +// with any Kubernetes object. It allows autoscaling based on information +// +// coming from components running outside of cluster +// +// (for example length of queue in cloud messaging service, or +// +// QPS from loadbalancer running outside of cluster). +func (d *MetricSpecDie) External(v *autoscalingv2.ExternalMetricSource) *MetricSpecDie { + return d.DieStamp(func(r *autoscalingv2.MetricSpec) { + r.External = v + }) +} + +var ObjectMetricSourceBlank = (&ObjectMetricSourceDie{}).DieFeed(autoscalingv2.ObjectMetricSource{}) + +type ObjectMetricSourceDie struct { + mutable bool + r autoscalingv2.ObjectMetricSource +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ObjectMetricSourceDie) DieImmutable(immutable bool) *ObjectMetricSourceDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ObjectMetricSourceDie) DieFeed(r autoscalingv2.ObjectMetricSource) *ObjectMetricSourceDie { + if d.mutable { + d.r = r + return d + } + return &ObjectMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ObjectMetricSourceDie) DieFeedPtr(r *autoscalingv2.ObjectMetricSource) *ObjectMetricSourceDie { + if r == nil { + r = &autoscalingv2.ObjectMetricSource{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ObjectMetricSourceDie) DieFeedJSON(j []byte) *ObjectMetricSourceDie { + r := autoscalingv2.ObjectMetricSource{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ObjectMetricSourceDie) DieFeedYAML(y []byte) *ObjectMetricSourceDie { + r := autoscalingv2.ObjectMetricSource{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ObjectMetricSourceDie) DieFeedYAMLFile(name string) *ObjectMetricSourceDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ObjectMetricSourceDie) DieFeedRawExtension(raw runtime.RawExtension) *ObjectMetricSourceDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ObjectMetricSourceDie) DieRelease() autoscalingv2.ObjectMetricSource { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ObjectMetricSourceDie) DieReleasePtr() *autoscalingv2.ObjectMetricSource { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ObjectMetricSourceDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ObjectMetricSourceDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ObjectMetricSourceDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ObjectMetricSourceDie) DieStamp(fn func(r *autoscalingv2.ObjectMetricSource)) *ObjectMetricSourceDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ObjectMetricSourceDie) DieStampAt(jp string, fn interface{}) *ObjectMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricSource) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ObjectMetricSourceDie) DieWith(fns ...func(d *ObjectMetricSourceDie)) *ObjectMetricSourceDie { + nd := ObjectMetricSourceBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ObjectMetricSourceDie) DeepCopy() *ObjectMetricSourceDie { + r := *d.r.DeepCopy() + return &ObjectMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// describedObject specifies the descriptions of a object,such as kind,name apiVersion +func (d *ObjectMetricSourceDie) DescribedObject(v autoscalingv2.CrossVersionObjectReference) *ObjectMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricSource) { + r.DescribedObject = v + }) +} + +// target specifies the target value for the given metric +func (d *ObjectMetricSourceDie) Target(v autoscalingv2.MetricTarget) *ObjectMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricSource) { + r.Target = v + }) +} + +// metric identifies the target metric by name and selector +func (d *ObjectMetricSourceDie) Metric(v autoscalingv2.MetricIdentifier) *ObjectMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricSource) { + r.Metric = v + }) +} + +var MetricTargetBlank = (&MetricTargetDie{}).DieFeed(autoscalingv2.MetricTarget{}) + +type MetricTargetDie struct { + mutable bool + r autoscalingv2.MetricTarget +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *MetricTargetDie) DieImmutable(immutable bool) *MetricTargetDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *MetricTargetDie) DieFeed(r autoscalingv2.MetricTarget) *MetricTargetDie { + if d.mutable { + d.r = r + return d + } + return &MetricTargetDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *MetricTargetDie) DieFeedPtr(r *autoscalingv2.MetricTarget) *MetricTargetDie { + if r == nil { + r = &autoscalingv2.MetricTarget{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *MetricTargetDie) DieFeedJSON(j []byte) *MetricTargetDie { + r := autoscalingv2.MetricTarget{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *MetricTargetDie) DieFeedYAML(y []byte) *MetricTargetDie { + r := autoscalingv2.MetricTarget{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *MetricTargetDie) DieFeedYAMLFile(name string) *MetricTargetDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricTargetDie) DieFeedRawExtension(raw runtime.RawExtension) *MetricTargetDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *MetricTargetDie) DieRelease() autoscalingv2.MetricTarget { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *MetricTargetDie) DieReleasePtr() *autoscalingv2.MetricTarget { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *MetricTargetDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *MetricTargetDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricTargetDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *MetricTargetDie) DieStamp(fn func(r *autoscalingv2.MetricTarget)) *MetricTargetDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *MetricTargetDie) DieStampAt(jp string, fn interface{}) *MetricTargetDie { + return d.DieStamp(func(r *autoscalingv2.MetricTarget) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *MetricTargetDie) DieWith(fns ...func(d *MetricTargetDie)) *MetricTargetDie { + nd := MetricTargetBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *MetricTargetDie) DeepCopy() *MetricTargetDie { + r := *d.r.DeepCopy() + return &MetricTargetDie{ + mutable: d.mutable, + r: r, + } +} + +// type represents whether the metric type is Utilization, Value, or AverageValue +func (d *MetricTargetDie) Type(v autoscalingv2.MetricTargetType) *MetricTargetDie { + return d.DieStamp(func(r *autoscalingv2.MetricTarget) { + r.Type = v + }) +} + +// value is the target value of the metric (as a quantity). +func (d *MetricTargetDie) Value(v *resource.Quantity) *MetricTargetDie { + return d.DieStamp(func(r *autoscalingv2.MetricTarget) { + r.Value = v + }) +} + +// averageValue is the target value of the average of the +// +// metric across all relevant pods (as a quantity) +func (d *MetricTargetDie) AverageValue(v *resource.Quantity) *MetricTargetDie { + return d.DieStamp(func(r *autoscalingv2.MetricTarget) { + r.AverageValue = v + }) +} + +// averageUtilization is the target value of the average of the +// +// resource metric across all relevant pods, represented as a percentage of +// +// the requested value of the resource for the pods. +// +// Currently only valid for Resource metric source type +func (d *MetricTargetDie) AverageUtilization(v *int32) *MetricTargetDie { + return d.DieStamp(func(r *autoscalingv2.MetricTarget) { + r.AverageUtilization = v + }) +} + +var MetricIdentifierBlank = (&MetricIdentifierDie{}).DieFeed(autoscalingv2.MetricIdentifier{}) + +type MetricIdentifierDie struct { + mutable bool + r autoscalingv2.MetricIdentifier +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *MetricIdentifierDie) DieImmutable(immutable bool) *MetricIdentifierDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *MetricIdentifierDie) DieFeed(r autoscalingv2.MetricIdentifier) *MetricIdentifierDie { + if d.mutable { + d.r = r + return d + } + return &MetricIdentifierDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *MetricIdentifierDie) DieFeedPtr(r *autoscalingv2.MetricIdentifier) *MetricIdentifierDie { + if r == nil { + r = &autoscalingv2.MetricIdentifier{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *MetricIdentifierDie) DieFeedJSON(j []byte) *MetricIdentifierDie { + r := autoscalingv2.MetricIdentifier{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *MetricIdentifierDie) DieFeedYAML(y []byte) *MetricIdentifierDie { + r := autoscalingv2.MetricIdentifier{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *MetricIdentifierDie) DieFeedYAMLFile(name string) *MetricIdentifierDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricIdentifierDie) DieFeedRawExtension(raw runtime.RawExtension) *MetricIdentifierDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *MetricIdentifierDie) DieRelease() autoscalingv2.MetricIdentifier { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *MetricIdentifierDie) DieReleasePtr() *autoscalingv2.MetricIdentifier { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *MetricIdentifierDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *MetricIdentifierDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricIdentifierDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *MetricIdentifierDie) DieStamp(fn func(r *autoscalingv2.MetricIdentifier)) *MetricIdentifierDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *MetricIdentifierDie) DieStampAt(jp string, fn interface{}) *MetricIdentifierDie { + return d.DieStamp(func(r *autoscalingv2.MetricIdentifier) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *MetricIdentifierDie) DieWith(fns ...func(d *MetricIdentifierDie)) *MetricIdentifierDie { + nd := MetricIdentifierBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *MetricIdentifierDie) DeepCopy() *MetricIdentifierDie { + r := *d.r.DeepCopy() + return &MetricIdentifierDie{ + mutable: d.mutable, + r: r, + } +} + +// name is the name of the given metric +func (d *MetricIdentifierDie) Name(v string) *MetricIdentifierDie { + return d.DieStamp(func(r *autoscalingv2.MetricIdentifier) { + r.Name = v + }) +} + +// selector is the string-encoded form of a standard kubernetes label selector for the given metric +// +// When set, it is passed as an additional parameter to the metrics server for more specific metrics scoping. +// +// When unset, just the metricName will be used to gather metrics. +func (d *MetricIdentifierDie) Selector(v *metav1.LabelSelector) *MetricIdentifierDie { + return d.DieStamp(func(r *autoscalingv2.MetricIdentifier) { + r.Selector = v + }) +} + +var PodsMetricSourceBlank = (&PodsMetricSourceDie{}).DieFeed(autoscalingv2.PodsMetricSource{}) + +type PodsMetricSourceDie struct { + mutable bool + r autoscalingv2.PodsMetricSource +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *PodsMetricSourceDie) DieImmutable(immutable bool) *PodsMetricSourceDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *PodsMetricSourceDie) DieFeed(r autoscalingv2.PodsMetricSource) *PodsMetricSourceDie { + if d.mutable { + d.r = r + return d + } + return &PodsMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *PodsMetricSourceDie) DieFeedPtr(r *autoscalingv2.PodsMetricSource) *PodsMetricSourceDie { + if r == nil { + r = &autoscalingv2.PodsMetricSource{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *PodsMetricSourceDie) DieFeedJSON(j []byte) *PodsMetricSourceDie { + r := autoscalingv2.PodsMetricSource{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *PodsMetricSourceDie) DieFeedYAML(y []byte) *PodsMetricSourceDie { + r := autoscalingv2.PodsMetricSource{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *PodsMetricSourceDie) DieFeedYAMLFile(name string) *PodsMetricSourceDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *PodsMetricSourceDie) DieFeedRawExtension(raw runtime.RawExtension) *PodsMetricSourceDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *PodsMetricSourceDie) DieRelease() autoscalingv2.PodsMetricSource { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *PodsMetricSourceDie) DieReleasePtr() *autoscalingv2.PodsMetricSource { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *PodsMetricSourceDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *PodsMetricSourceDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *PodsMetricSourceDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *PodsMetricSourceDie) DieStamp(fn func(r *autoscalingv2.PodsMetricSource)) *PodsMetricSourceDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *PodsMetricSourceDie) DieStampAt(jp string, fn interface{}) *PodsMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricSource) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *PodsMetricSourceDie) DieWith(fns ...func(d *PodsMetricSourceDie)) *PodsMetricSourceDie { + nd := PodsMetricSourceBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *PodsMetricSourceDie) DeepCopy() *PodsMetricSourceDie { + r := *d.r.DeepCopy() + return &PodsMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// metric identifies the target metric by name and selector +func (d *PodsMetricSourceDie) Metric(v autoscalingv2.MetricIdentifier) *PodsMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricSource) { + r.Metric = v + }) +} + +// target specifies the target value for the given metric +func (d *PodsMetricSourceDie) Target(v autoscalingv2.MetricTarget) *PodsMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricSource) { + r.Target = v + }) +} + +var ResourceMetricSourceBlank = (&ResourceMetricSourceDie{}).DieFeed(autoscalingv2.ResourceMetricSource{}) + +type ResourceMetricSourceDie struct { + mutable bool + r autoscalingv2.ResourceMetricSource +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ResourceMetricSourceDie) DieImmutable(immutable bool) *ResourceMetricSourceDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ResourceMetricSourceDie) DieFeed(r autoscalingv2.ResourceMetricSource) *ResourceMetricSourceDie { + if d.mutable { + d.r = r + return d + } + return &ResourceMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ResourceMetricSourceDie) DieFeedPtr(r *autoscalingv2.ResourceMetricSource) *ResourceMetricSourceDie { + if r == nil { + r = &autoscalingv2.ResourceMetricSource{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ResourceMetricSourceDie) DieFeedJSON(j []byte) *ResourceMetricSourceDie { + r := autoscalingv2.ResourceMetricSource{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ResourceMetricSourceDie) DieFeedYAML(y []byte) *ResourceMetricSourceDie { + r := autoscalingv2.ResourceMetricSource{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ResourceMetricSourceDie) DieFeedYAMLFile(name string) *ResourceMetricSourceDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ResourceMetricSourceDie) DieFeedRawExtension(raw runtime.RawExtension) *ResourceMetricSourceDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ResourceMetricSourceDie) DieRelease() autoscalingv2.ResourceMetricSource { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ResourceMetricSourceDie) DieReleasePtr() *autoscalingv2.ResourceMetricSource { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ResourceMetricSourceDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ResourceMetricSourceDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ResourceMetricSourceDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ResourceMetricSourceDie) DieStamp(fn func(r *autoscalingv2.ResourceMetricSource)) *ResourceMetricSourceDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ResourceMetricSourceDie) DieStampAt(jp string, fn interface{}) *ResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ResourceMetricSource) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ResourceMetricSourceDie) DieWith(fns ...func(d *ResourceMetricSourceDie)) *ResourceMetricSourceDie { + nd := ResourceMetricSourceBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ResourceMetricSourceDie) DeepCopy() *ResourceMetricSourceDie { + r := *d.r.DeepCopy() + return &ResourceMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// name is the name of the resource in question. +func (d *ResourceMetricSourceDie) Name(v corev1.ResourceName) *ResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ResourceMetricSource) { + r.Name = v + }) +} + +// target specifies the target value for the given metric +func (d *ResourceMetricSourceDie) Target(v autoscalingv2.MetricTarget) *ResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ResourceMetricSource) { + r.Target = v + }) +} + +var ContainerResourceMetricSourceBlank = (&ContainerResourceMetricSourceDie{}).DieFeed(autoscalingv2.ContainerResourceMetricSource{}) + +type ContainerResourceMetricSourceDie struct { + mutable bool + r autoscalingv2.ContainerResourceMetricSource +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ContainerResourceMetricSourceDie) DieImmutable(immutable bool) *ContainerResourceMetricSourceDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ContainerResourceMetricSourceDie) DieFeed(r autoscalingv2.ContainerResourceMetricSource) *ContainerResourceMetricSourceDie { + if d.mutable { + d.r = r + return d + } + return &ContainerResourceMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ContainerResourceMetricSourceDie) DieFeedPtr(r *autoscalingv2.ContainerResourceMetricSource) *ContainerResourceMetricSourceDie { + if r == nil { + r = &autoscalingv2.ContainerResourceMetricSource{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ContainerResourceMetricSourceDie) DieFeedJSON(j []byte) *ContainerResourceMetricSourceDie { + r := autoscalingv2.ContainerResourceMetricSource{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ContainerResourceMetricSourceDie) DieFeedYAML(y []byte) *ContainerResourceMetricSourceDie { + r := autoscalingv2.ContainerResourceMetricSource{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ContainerResourceMetricSourceDie) DieFeedYAMLFile(name string) *ContainerResourceMetricSourceDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ContainerResourceMetricSourceDie) DieFeedRawExtension(raw runtime.RawExtension) *ContainerResourceMetricSourceDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ContainerResourceMetricSourceDie) DieRelease() autoscalingv2.ContainerResourceMetricSource { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ContainerResourceMetricSourceDie) DieReleasePtr() *autoscalingv2.ContainerResourceMetricSource { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ContainerResourceMetricSourceDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ContainerResourceMetricSourceDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ContainerResourceMetricSourceDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ContainerResourceMetricSourceDie) DieStamp(fn func(r *autoscalingv2.ContainerResourceMetricSource)) *ContainerResourceMetricSourceDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ContainerResourceMetricSourceDie) DieStampAt(jp string, fn interface{}) *ContainerResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricSource) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ContainerResourceMetricSourceDie) DieWith(fns ...func(d *ContainerResourceMetricSourceDie)) *ContainerResourceMetricSourceDie { + nd := ContainerResourceMetricSourceBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ContainerResourceMetricSourceDie) DeepCopy() *ContainerResourceMetricSourceDie { + r := *d.r.DeepCopy() + return &ContainerResourceMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// name is the name of the resource in question. +func (d *ContainerResourceMetricSourceDie) Name(v corev1.ResourceName) *ContainerResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricSource) { + r.Name = v + }) +} + +// target specifies the target value for the given metric +func (d *ContainerResourceMetricSourceDie) Target(v autoscalingv2.MetricTarget) *ContainerResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricSource) { + r.Target = v + }) +} + +// container is the name of the container in the pods of the scaling target +func (d *ContainerResourceMetricSourceDie) Container(v string) *ContainerResourceMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricSource) { + r.Container = v + }) +} + +var ExternalMetricSourceBlank = (&ExternalMetricSourceDie{}).DieFeed(autoscalingv2.ExternalMetricSource{}) + +type ExternalMetricSourceDie struct { + mutable bool + r autoscalingv2.ExternalMetricSource +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ExternalMetricSourceDie) DieImmutable(immutable bool) *ExternalMetricSourceDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ExternalMetricSourceDie) DieFeed(r autoscalingv2.ExternalMetricSource) *ExternalMetricSourceDie { + if d.mutable { + d.r = r + return d + } + return &ExternalMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ExternalMetricSourceDie) DieFeedPtr(r *autoscalingv2.ExternalMetricSource) *ExternalMetricSourceDie { + if r == nil { + r = &autoscalingv2.ExternalMetricSource{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ExternalMetricSourceDie) DieFeedJSON(j []byte) *ExternalMetricSourceDie { + r := autoscalingv2.ExternalMetricSource{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ExternalMetricSourceDie) DieFeedYAML(y []byte) *ExternalMetricSourceDie { + r := autoscalingv2.ExternalMetricSource{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ExternalMetricSourceDie) DieFeedYAMLFile(name string) *ExternalMetricSourceDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ExternalMetricSourceDie) DieFeedRawExtension(raw runtime.RawExtension) *ExternalMetricSourceDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ExternalMetricSourceDie) DieRelease() autoscalingv2.ExternalMetricSource { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ExternalMetricSourceDie) DieReleasePtr() *autoscalingv2.ExternalMetricSource { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ExternalMetricSourceDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ExternalMetricSourceDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ExternalMetricSourceDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ExternalMetricSourceDie) DieStamp(fn func(r *autoscalingv2.ExternalMetricSource)) *ExternalMetricSourceDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ExternalMetricSourceDie) DieStampAt(jp string, fn interface{}) *ExternalMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricSource) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ExternalMetricSourceDie) DieWith(fns ...func(d *ExternalMetricSourceDie)) *ExternalMetricSourceDie { + nd := ExternalMetricSourceBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ExternalMetricSourceDie) DeepCopy() *ExternalMetricSourceDie { + r := *d.r.DeepCopy() + return &ExternalMetricSourceDie{ + mutable: d.mutable, + r: r, + } +} + +// metric identifies the target metric by name and selector +func (d *ExternalMetricSourceDie) Metric(v autoscalingv2.MetricIdentifier) *ExternalMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricSource) { + r.Metric = v + }) +} + +// target specifies the target value for the given metric +func (d *ExternalMetricSourceDie) Target(v autoscalingv2.MetricTarget) *ExternalMetricSourceDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricSource) { + r.Target = v + }) +} + +var HorizontalPodAutoscalerBehaviorBlank = (&HorizontalPodAutoscalerBehaviorDie{}).DieFeed(autoscalingv2.HorizontalPodAutoscalerBehavior{}) + +type HorizontalPodAutoscalerBehaviorDie struct { + mutable bool + r autoscalingv2.HorizontalPodAutoscalerBehavior +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *HorizontalPodAutoscalerBehaviorDie) DieImmutable(immutable bool) *HorizontalPodAutoscalerBehaviorDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *HorizontalPodAutoscalerBehaviorDie) DieFeed(r autoscalingv2.HorizontalPodAutoscalerBehavior) *HorizontalPodAutoscalerBehaviorDie { + if d.mutable { + d.r = r + return d + } + return &HorizontalPodAutoscalerBehaviorDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *HorizontalPodAutoscalerBehaviorDie) DieFeedPtr(r *autoscalingv2.HorizontalPodAutoscalerBehavior) *HorizontalPodAutoscalerBehaviorDie { + if r == nil { + r = &autoscalingv2.HorizontalPodAutoscalerBehavior{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *HorizontalPodAutoscalerBehaviorDie) DieFeedJSON(j []byte) *HorizontalPodAutoscalerBehaviorDie { + r := autoscalingv2.HorizontalPodAutoscalerBehavior{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *HorizontalPodAutoscalerBehaviorDie) DieFeedYAML(y []byte) *HorizontalPodAutoscalerBehaviorDie { + r := autoscalingv2.HorizontalPodAutoscalerBehavior{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *HorizontalPodAutoscalerBehaviorDie) DieFeedYAMLFile(name string) *HorizontalPodAutoscalerBehaviorDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HorizontalPodAutoscalerBehaviorDie) DieFeedRawExtension(raw runtime.RawExtension) *HorizontalPodAutoscalerBehaviorDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *HorizontalPodAutoscalerBehaviorDie) DieRelease() autoscalingv2.HorizontalPodAutoscalerBehavior { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *HorizontalPodAutoscalerBehaviorDie) DieReleasePtr() *autoscalingv2.HorizontalPodAutoscalerBehavior { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *HorizontalPodAutoscalerBehaviorDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *HorizontalPodAutoscalerBehaviorDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HorizontalPodAutoscalerBehaviorDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *HorizontalPodAutoscalerBehaviorDie) DieStamp(fn func(r *autoscalingv2.HorizontalPodAutoscalerBehavior)) *HorizontalPodAutoscalerBehaviorDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *HorizontalPodAutoscalerBehaviorDie) DieStampAt(jp string, fn interface{}) *HorizontalPodAutoscalerBehaviorDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerBehavior) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *HorizontalPodAutoscalerBehaviorDie) DieWith(fns ...func(d *HorizontalPodAutoscalerBehaviorDie)) *HorizontalPodAutoscalerBehaviorDie { + nd := HorizontalPodAutoscalerBehaviorBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *HorizontalPodAutoscalerBehaviorDie) DeepCopy() *HorizontalPodAutoscalerBehaviorDie { + r := *d.r.DeepCopy() + return &HorizontalPodAutoscalerBehaviorDie{ + mutable: d.mutable, + r: r, + } +} + +// scaleUp is scaling policy for scaling Up. +// +// If not set, the default value is the higher of: +// +// * increase no more than 4 pods per 60 seconds +// +// * double the number of pods per 60 seconds +// +// No stabilization is used. +func (d *HorizontalPodAutoscalerBehaviorDie) ScaleUp(v *autoscalingv2.HPAScalingRules) *HorizontalPodAutoscalerBehaviorDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerBehavior) { + r.ScaleUp = v + }) +} + +// scaleDown is scaling policy for scaling Down. +// +// # If not set, the default value is to allow to scale down to minReplicas pods, with a +// +// 300 second stabilization window (i.e., the highest recommendation for +// +// the last 300sec is used). +func (d *HorizontalPodAutoscalerBehaviorDie) ScaleDown(v *autoscalingv2.HPAScalingRules) *HorizontalPodAutoscalerBehaviorDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerBehavior) { + r.ScaleDown = v + }) +} + +var HPAScalingRulesBlank = (&HPAScalingRulesDie{}).DieFeed(autoscalingv2.HPAScalingRules{}) + +type HPAScalingRulesDie struct { + mutable bool + r autoscalingv2.HPAScalingRules +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *HPAScalingRulesDie) DieImmutable(immutable bool) *HPAScalingRulesDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *HPAScalingRulesDie) DieFeed(r autoscalingv2.HPAScalingRules) *HPAScalingRulesDie { + if d.mutable { + d.r = r + return d + } + return &HPAScalingRulesDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *HPAScalingRulesDie) DieFeedPtr(r *autoscalingv2.HPAScalingRules) *HPAScalingRulesDie { + if r == nil { + r = &autoscalingv2.HPAScalingRules{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *HPAScalingRulesDie) DieFeedJSON(j []byte) *HPAScalingRulesDie { + r := autoscalingv2.HPAScalingRules{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *HPAScalingRulesDie) DieFeedYAML(y []byte) *HPAScalingRulesDie { + r := autoscalingv2.HPAScalingRules{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *HPAScalingRulesDie) DieFeedYAMLFile(name string) *HPAScalingRulesDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HPAScalingRulesDie) DieFeedRawExtension(raw runtime.RawExtension) *HPAScalingRulesDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *HPAScalingRulesDie) DieRelease() autoscalingv2.HPAScalingRules { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *HPAScalingRulesDie) DieReleasePtr() *autoscalingv2.HPAScalingRules { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *HPAScalingRulesDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *HPAScalingRulesDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HPAScalingRulesDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *HPAScalingRulesDie) DieStamp(fn func(r *autoscalingv2.HPAScalingRules)) *HPAScalingRulesDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *HPAScalingRulesDie) DieStampAt(jp string, fn interface{}) *HPAScalingRulesDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingRules) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *HPAScalingRulesDie) DieWith(fns ...func(d *HPAScalingRulesDie)) *HPAScalingRulesDie { + nd := HPAScalingRulesBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *HPAScalingRulesDie) DeepCopy() *HPAScalingRulesDie { + r := *d.r.DeepCopy() + return &HPAScalingRulesDie{ + mutable: d.mutable, + r: r, + } +} + +// stabilizationWindowSeconds is the number of seconds for which past recommendations should be +// +// considered while scaling up or scaling down. +// +// StabilizationWindowSeconds must be greater than or equal to zero and less than or equal to 3600 (one hour). +// +// If not set, use the default values: +// +// - For scale up: 0 (i.e. no stabilization is done). +// +// - For scale down: 300 (i.e. the stabilization window is 300 seconds long). +func (d *HPAScalingRulesDie) StabilizationWindowSeconds(v *int32) *HPAScalingRulesDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingRules) { + r.StabilizationWindowSeconds = v + }) +} + +// selectPolicy is used to specify which policy should be used. +// +// If not set, the default value Max is used. +func (d *HPAScalingRulesDie) SelectPolicy(v *autoscalingv2.ScalingPolicySelect) *HPAScalingRulesDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingRules) { + r.SelectPolicy = v + }) +} + +// policies is a list of potential scaling polices which can be used during scaling. +// +// At least one policy must be specified, otherwise the HPAScalingRules will be discarded as invalid +func (d *HPAScalingRulesDie) Policies(v ...autoscalingv2.HPAScalingPolicy) *HPAScalingRulesDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingRules) { + r.Policies = v + }) +} + +var HPAScalingPolicyBlank = (&HPAScalingPolicyDie{}).DieFeed(autoscalingv2.HPAScalingPolicy{}) + +type HPAScalingPolicyDie struct { + mutable bool + r autoscalingv2.HPAScalingPolicy +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *HPAScalingPolicyDie) DieImmutable(immutable bool) *HPAScalingPolicyDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *HPAScalingPolicyDie) DieFeed(r autoscalingv2.HPAScalingPolicy) *HPAScalingPolicyDie { + if d.mutable { + d.r = r + return d + } + return &HPAScalingPolicyDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *HPAScalingPolicyDie) DieFeedPtr(r *autoscalingv2.HPAScalingPolicy) *HPAScalingPolicyDie { + if r == nil { + r = &autoscalingv2.HPAScalingPolicy{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *HPAScalingPolicyDie) DieFeedJSON(j []byte) *HPAScalingPolicyDie { + r := autoscalingv2.HPAScalingPolicy{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *HPAScalingPolicyDie) DieFeedYAML(y []byte) *HPAScalingPolicyDie { + r := autoscalingv2.HPAScalingPolicy{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *HPAScalingPolicyDie) DieFeedYAMLFile(name string) *HPAScalingPolicyDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HPAScalingPolicyDie) DieFeedRawExtension(raw runtime.RawExtension) *HPAScalingPolicyDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *HPAScalingPolicyDie) DieRelease() autoscalingv2.HPAScalingPolicy { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *HPAScalingPolicyDie) DieReleasePtr() *autoscalingv2.HPAScalingPolicy { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *HPAScalingPolicyDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *HPAScalingPolicyDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HPAScalingPolicyDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *HPAScalingPolicyDie) DieStamp(fn func(r *autoscalingv2.HPAScalingPolicy)) *HPAScalingPolicyDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *HPAScalingPolicyDie) DieStampAt(jp string, fn interface{}) *HPAScalingPolicyDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingPolicy) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *HPAScalingPolicyDie) DieWith(fns ...func(d *HPAScalingPolicyDie)) *HPAScalingPolicyDie { + nd := HPAScalingPolicyBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *HPAScalingPolicyDie) DeepCopy() *HPAScalingPolicyDie { + r := *d.r.DeepCopy() + return &HPAScalingPolicyDie{ + mutable: d.mutable, + r: r, + } +} + +// type is used to specify the scaling policy. +func (d *HPAScalingPolicyDie) Type(v autoscalingv2.HPAScalingPolicyType) *HPAScalingPolicyDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingPolicy) { + r.Type = v + }) +} + +// value contains the amount of change which is permitted by the policy. +// +// It must be greater than zero +func (d *HPAScalingPolicyDie) Value(v int32) *HPAScalingPolicyDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingPolicy) { + r.Value = v + }) +} + +// periodSeconds specifies the window of time for which the policy should hold true. +// +// PeriodSeconds must be greater than zero and less than or equal to 1800 (30 min). +func (d *HPAScalingPolicyDie) PeriodSeconds(v int32) *HPAScalingPolicyDie { + return d.DieStamp(func(r *autoscalingv2.HPAScalingPolicy) { + r.PeriodSeconds = v + }) +} + +var HorizontalPodAutoscalerStatusBlank = (&HorizontalPodAutoscalerStatusDie{}).DieFeed(autoscalingv2.HorizontalPodAutoscalerStatus{}) + +type HorizontalPodAutoscalerStatusDie struct { + mutable bool + r autoscalingv2.HorizontalPodAutoscalerStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *HorizontalPodAutoscalerStatusDie) DieImmutable(immutable bool) *HorizontalPodAutoscalerStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *HorizontalPodAutoscalerStatusDie) DieFeed(r autoscalingv2.HorizontalPodAutoscalerStatus) *HorizontalPodAutoscalerStatusDie { + if d.mutable { + d.r = r + return d + } + return &HorizontalPodAutoscalerStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *HorizontalPodAutoscalerStatusDie) DieFeedPtr(r *autoscalingv2.HorizontalPodAutoscalerStatus) *HorizontalPodAutoscalerStatusDie { + if r == nil { + r = &autoscalingv2.HorizontalPodAutoscalerStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *HorizontalPodAutoscalerStatusDie) DieFeedJSON(j []byte) *HorizontalPodAutoscalerStatusDie { + r := autoscalingv2.HorizontalPodAutoscalerStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *HorizontalPodAutoscalerStatusDie) DieFeedYAML(y []byte) *HorizontalPodAutoscalerStatusDie { + r := autoscalingv2.HorizontalPodAutoscalerStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *HorizontalPodAutoscalerStatusDie) DieFeedYAMLFile(name string) *HorizontalPodAutoscalerStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HorizontalPodAutoscalerStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *HorizontalPodAutoscalerStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *HorizontalPodAutoscalerStatusDie) DieRelease() autoscalingv2.HorizontalPodAutoscalerStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *HorizontalPodAutoscalerStatusDie) DieReleasePtr() *autoscalingv2.HorizontalPodAutoscalerStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *HorizontalPodAutoscalerStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *HorizontalPodAutoscalerStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *HorizontalPodAutoscalerStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *HorizontalPodAutoscalerStatusDie) DieStamp(fn func(r *autoscalingv2.HorizontalPodAutoscalerStatus)) *HorizontalPodAutoscalerStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *HorizontalPodAutoscalerStatusDie) DieStampAt(jp string, fn interface{}) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *HorizontalPodAutoscalerStatusDie) DieWith(fns ...func(d *HorizontalPodAutoscalerStatusDie)) *HorizontalPodAutoscalerStatusDie { + nd := HorizontalPodAutoscalerStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *HorizontalPodAutoscalerStatusDie) DeepCopy() *HorizontalPodAutoscalerStatusDie { + r := *d.r.DeepCopy() + return &HorizontalPodAutoscalerStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// observedGeneration is the most recent generation observed by this autoscaler. +func (d *HorizontalPodAutoscalerStatusDie) ObservedGeneration(v *int64) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + r.ObservedGeneration = v + }) +} + +// lastScaleTime is the last time the HorizontalPodAutoscaler scaled the number of pods, +// +// used by the autoscaler to control how often the number of pods is changed. +func (d *HorizontalPodAutoscalerStatusDie) LastScaleTime(v *metav1.Time) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + r.LastScaleTime = v + }) +} + +// currentReplicas is current number of replicas of pods managed by this autoscaler, +// +// as last seen by the autoscaler. +func (d *HorizontalPodAutoscalerStatusDie) CurrentReplicas(v int32) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + r.CurrentReplicas = v + }) +} + +// desiredReplicas is the desired number of replicas of pods managed by this autoscaler, +// +// as last calculated by the autoscaler. +func (d *HorizontalPodAutoscalerStatusDie) DesiredReplicas(v int32) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + r.DesiredReplicas = v + }) +} + +// currentMetrics is the last read state of the metrics used by this autoscaler. +func (d *HorizontalPodAutoscalerStatusDie) CurrentMetrics(v ...autoscalingv2.MetricStatus) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + r.CurrentMetrics = v + }) +} + +// conditions is the set of conditions required for this autoscaler to scale its target, +// +// and indicates whether or not those conditions are met. +func (d *HorizontalPodAutoscalerStatusDie) Conditions(v ...autoscalingv2.HorizontalPodAutoscalerCondition) *HorizontalPodAutoscalerStatusDie { + return d.DieStamp(func(r *autoscalingv2.HorizontalPodAutoscalerStatus) { + r.Conditions = v + }) +} + +var MetricStatusBlank = (&MetricStatusDie{}).DieFeed(autoscalingv2.MetricStatus{}) + +type MetricStatusDie struct { + mutable bool + r autoscalingv2.MetricStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *MetricStatusDie) DieImmutable(immutable bool) *MetricStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *MetricStatusDie) DieFeed(r autoscalingv2.MetricStatus) *MetricStatusDie { + if d.mutable { + d.r = r + return d + } + return &MetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *MetricStatusDie) DieFeedPtr(r *autoscalingv2.MetricStatus) *MetricStatusDie { + if r == nil { + r = &autoscalingv2.MetricStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *MetricStatusDie) DieFeedJSON(j []byte) *MetricStatusDie { + r := autoscalingv2.MetricStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *MetricStatusDie) DieFeedYAML(y []byte) *MetricStatusDie { + r := autoscalingv2.MetricStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *MetricStatusDie) DieFeedYAMLFile(name string) *MetricStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *MetricStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *MetricStatusDie) DieRelease() autoscalingv2.MetricStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *MetricStatusDie) DieReleasePtr() *autoscalingv2.MetricStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *MetricStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *MetricStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *MetricStatusDie) DieStamp(fn func(r *autoscalingv2.MetricStatus)) *MetricStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *MetricStatusDie) DieStampAt(jp string, fn interface{}) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *MetricStatusDie) DieWith(fns ...func(d *MetricStatusDie)) *MetricStatusDie { + nd := MetricStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *MetricStatusDie) DeepCopy() *MetricStatusDie { + r := *d.r.DeepCopy() + return &MetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// type is the type of metric source. It will be one of "ContainerResource", "External", +// +// "Object", "Pods" or "Resource", each corresponds to a matching field in the object. +// +// Note: "ContainerResource" type is available on when the feature-gate +// +// HPAContainerMetrics is enabled +func (d *MetricStatusDie) Type(v autoscalingv2.MetricSourceType) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + r.Type = v + }) +} + +// object refers to a metric describing a single kubernetes object +// +// (for example, hits-per-second on an Ingress object). +func (d *MetricStatusDie) Object(v *autoscalingv2.ObjectMetricStatus) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + r.Object = v + }) +} + +// pods refers to a metric describing each pod in the current scale target +// +// (for example, transactions-processed-per-second). The values will be +// +// averaged together before being compared to the target value. +func (d *MetricStatusDie) Pods(v *autoscalingv2.PodsMetricStatus) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + r.Pods = v + }) +} + +// resource refers to a resource metric (such as those specified in +// +// requests and limits) known to Kubernetes describing each pod in the +// +// current scale target (e.g. CPU or memory). Such metrics are built in to +// +// # Kubernetes, and have special scaling options on top of those available +// +// to normal per-pod metrics using the "pods" source. +func (d *MetricStatusDie) Resource(v *autoscalingv2.ResourceMetricStatus) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + r.Resource = v + }) +} + +// container resource refers to a resource metric (such as those specified in +// +// requests and limits) known to Kubernetes describing a single container in each pod in the +// +// current scale target (e.g. CPU or memory). Such metrics are built in to +// +// # Kubernetes, and have special scaling options on top of those available +// +// to normal per-pod metrics using the "pods" source. +func (d *MetricStatusDie) ContainerResource(v *autoscalingv2.ContainerResourceMetricStatus) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + r.ContainerResource = v + }) +} + +// external refers to a global metric that is not associated +// +// with any Kubernetes object. It allows autoscaling based on information +// +// coming from components running outside of cluster +// +// (for example length of queue in cloud messaging service, or +// +// QPS from loadbalancer running outside of cluster). +func (d *MetricStatusDie) External(v *autoscalingv2.ExternalMetricStatus) *MetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricStatus) { + r.External = v + }) +} + +var ObjectMetricStatusBlank = (&ObjectMetricStatusDie{}).DieFeed(autoscalingv2.ObjectMetricStatus{}) + +type ObjectMetricStatusDie struct { + mutable bool + r autoscalingv2.ObjectMetricStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ObjectMetricStatusDie) DieImmutable(immutable bool) *ObjectMetricStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ObjectMetricStatusDie) DieFeed(r autoscalingv2.ObjectMetricStatus) *ObjectMetricStatusDie { + if d.mutable { + d.r = r + return d + } + return &ObjectMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ObjectMetricStatusDie) DieFeedPtr(r *autoscalingv2.ObjectMetricStatus) *ObjectMetricStatusDie { + if r == nil { + r = &autoscalingv2.ObjectMetricStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ObjectMetricStatusDie) DieFeedJSON(j []byte) *ObjectMetricStatusDie { + r := autoscalingv2.ObjectMetricStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ObjectMetricStatusDie) DieFeedYAML(y []byte) *ObjectMetricStatusDie { + r := autoscalingv2.ObjectMetricStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ObjectMetricStatusDie) DieFeedYAMLFile(name string) *ObjectMetricStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ObjectMetricStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *ObjectMetricStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ObjectMetricStatusDie) DieRelease() autoscalingv2.ObjectMetricStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ObjectMetricStatusDie) DieReleasePtr() *autoscalingv2.ObjectMetricStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ObjectMetricStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ObjectMetricStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ObjectMetricStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ObjectMetricStatusDie) DieStamp(fn func(r *autoscalingv2.ObjectMetricStatus)) *ObjectMetricStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ObjectMetricStatusDie) DieStampAt(jp string, fn interface{}) *ObjectMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ObjectMetricStatusDie) DieWith(fns ...func(d *ObjectMetricStatusDie)) *ObjectMetricStatusDie { + nd := ObjectMetricStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ObjectMetricStatusDie) DeepCopy() *ObjectMetricStatusDie { + r := *d.r.DeepCopy() + return &ObjectMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// metric identifies the target metric by name and selector +func (d *ObjectMetricStatusDie) Metric(v autoscalingv2.MetricIdentifier) *ObjectMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricStatus) { + r.Metric = v + }) +} + +// current contains the current value for the given metric +func (d *ObjectMetricStatusDie) Current(v autoscalingv2.MetricValueStatus) *ObjectMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricStatus) { + r.Current = v + }) +} + +// DescribedObject specifies the descriptions of a object,such as kind,name apiVersion +func (d *ObjectMetricStatusDie) DescribedObject(v autoscalingv2.CrossVersionObjectReference) *ObjectMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ObjectMetricStatus) { + r.DescribedObject = v + }) +} + +var MetricValueStatusBlank = (&MetricValueStatusDie{}).DieFeed(autoscalingv2.MetricValueStatus{}) + +type MetricValueStatusDie struct { + mutable bool + r autoscalingv2.MetricValueStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *MetricValueStatusDie) DieImmutable(immutable bool) *MetricValueStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *MetricValueStatusDie) DieFeed(r autoscalingv2.MetricValueStatus) *MetricValueStatusDie { + if d.mutable { + d.r = r + return d + } + return &MetricValueStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *MetricValueStatusDie) DieFeedPtr(r *autoscalingv2.MetricValueStatus) *MetricValueStatusDie { + if r == nil { + r = &autoscalingv2.MetricValueStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *MetricValueStatusDie) DieFeedJSON(j []byte) *MetricValueStatusDie { + r := autoscalingv2.MetricValueStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *MetricValueStatusDie) DieFeedYAML(y []byte) *MetricValueStatusDie { + r := autoscalingv2.MetricValueStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *MetricValueStatusDie) DieFeedYAMLFile(name string) *MetricValueStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricValueStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *MetricValueStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *MetricValueStatusDie) DieRelease() autoscalingv2.MetricValueStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *MetricValueStatusDie) DieReleasePtr() *autoscalingv2.MetricValueStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *MetricValueStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *MetricValueStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *MetricValueStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *MetricValueStatusDie) DieStamp(fn func(r *autoscalingv2.MetricValueStatus)) *MetricValueStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *MetricValueStatusDie) DieStampAt(jp string, fn interface{}) *MetricValueStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricValueStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *MetricValueStatusDie) DieWith(fns ...func(d *MetricValueStatusDie)) *MetricValueStatusDie { + nd := MetricValueStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *MetricValueStatusDie) DeepCopy() *MetricValueStatusDie { + r := *d.r.DeepCopy() + return &MetricValueStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// value is the current value of the metric (as a quantity). +func (d *MetricValueStatusDie) Value(v *resource.Quantity) *MetricValueStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricValueStatus) { + r.Value = v + }) +} + +// averageValue is the current value of the average of the +// +// metric across all relevant pods (as a quantity) +func (d *MetricValueStatusDie) AverageValue(v *resource.Quantity) *MetricValueStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricValueStatus) { + r.AverageValue = v + }) +} + +// currentAverageUtilization is the current value of the average of the +// +// resource metric across all relevant pods, represented as a percentage of +// +// the requested value of the resource for the pods. +func (d *MetricValueStatusDie) AverageUtilization(v *int32) *MetricValueStatusDie { + return d.DieStamp(func(r *autoscalingv2.MetricValueStatus) { + r.AverageUtilization = v + }) +} + +var PodsMetricStatusBlank = (&PodsMetricStatusDie{}).DieFeed(autoscalingv2.PodsMetricStatus{}) + +type PodsMetricStatusDie struct { + mutable bool + r autoscalingv2.PodsMetricStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *PodsMetricStatusDie) DieImmutable(immutable bool) *PodsMetricStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *PodsMetricStatusDie) DieFeed(r autoscalingv2.PodsMetricStatus) *PodsMetricStatusDie { + if d.mutable { + d.r = r + return d + } + return &PodsMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *PodsMetricStatusDie) DieFeedPtr(r *autoscalingv2.PodsMetricStatus) *PodsMetricStatusDie { + if r == nil { + r = &autoscalingv2.PodsMetricStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *PodsMetricStatusDie) DieFeedJSON(j []byte) *PodsMetricStatusDie { + r := autoscalingv2.PodsMetricStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *PodsMetricStatusDie) DieFeedYAML(y []byte) *PodsMetricStatusDie { + r := autoscalingv2.PodsMetricStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *PodsMetricStatusDie) DieFeedYAMLFile(name string) *PodsMetricStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *PodsMetricStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *PodsMetricStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *PodsMetricStatusDie) DieRelease() autoscalingv2.PodsMetricStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *PodsMetricStatusDie) DieReleasePtr() *autoscalingv2.PodsMetricStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *PodsMetricStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *PodsMetricStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *PodsMetricStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *PodsMetricStatusDie) DieStamp(fn func(r *autoscalingv2.PodsMetricStatus)) *PodsMetricStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *PodsMetricStatusDie) DieStampAt(jp string, fn interface{}) *PodsMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *PodsMetricStatusDie) DieWith(fns ...func(d *PodsMetricStatusDie)) *PodsMetricStatusDie { + nd := PodsMetricStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *PodsMetricStatusDie) DeepCopy() *PodsMetricStatusDie { + r := *d.r.DeepCopy() + return &PodsMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// metric identifies the target metric by name and selector +func (d *PodsMetricStatusDie) Metric(v autoscalingv2.MetricIdentifier) *PodsMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricStatus) { + r.Metric = v + }) +} + +// current contains the current value for the given metric +func (d *PodsMetricStatusDie) Current(v autoscalingv2.MetricValueStatus) *PodsMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.PodsMetricStatus) { + r.Current = v + }) +} + +var ResourceMetricStatusBlank = (&ResourceMetricStatusDie{}).DieFeed(autoscalingv2.ResourceMetricStatus{}) + +type ResourceMetricStatusDie struct { + mutable bool + r autoscalingv2.ResourceMetricStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ResourceMetricStatusDie) DieImmutable(immutable bool) *ResourceMetricStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ResourceMetricStatusDie) DieFeed(r autoscalingv2.ResourceMetricStatus) *ResourceMetricStatusDie { + if d.mutable { + d.r = r + return d + } + return &ResourceMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ResourceMetricStatusDie) DieFeedPtr(r *autoscalingv2.ResourceMetricStatus) *ResourceMetricStatusDie { + if r == nil { + r = &autoscalingv2.ResourceMetricStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ResourceMetricStatusDie) DieFeedJSON(j []byte) *ResourceMetricStatusDie { + r := autoscalingv2.ResourceMetricStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ResourceMetricStatusDie) DieFeedYAML(y []byte) *ResourceMetricStatusDie { + r := autoscalingv2.ResourceMetricStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ResourceMetricStatusDie) DieFeedYAMLFile(name string) *ResourceMetricStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ResourceMetricStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *ResourceMetricStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ResourceMetricStatusDie) DieRelease() autoscalingv2.ResourceMetricStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ResourceMetricStatusDie) DieReleasePtr() *autoscalingv2.ResourceMetricStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ResourceMetricStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ResourceMetricStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ResourceMetricStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ResourceMetricStatusDie) DieStamp(fn func(r *autoscalingv2.ResourceMetricStatus)) *ResourceMetricStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ResourceMetricStatusDie) DieStampAt(jp string, fn interface{}) *ResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ResourceMetricStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ResourceMetricStatusDie) DieWith(fns ...func(d *ResourceMetricStatusDie)) *ResourceMetricStatusDie { + nd := ResourceMetricStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ResourceMetricStatusDie) DeepCopy() *ResourceMetricStatusDie { + r := *d.r.DeepCopy() + return &ResourceMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// name is the name of the resource in question. +func (d *ResourceMetricStatusDie) Name(v corev1.ResourceName) *ResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ResourceMetricStatus) { + r.Name = v + }) +} + +// current contains the current value for the given metric +func (d *ResourceMetricStatusDie) Current(v autoscalingv2.MetricValueStatus) *ResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ResourceMetricStatus) { + r.Current = v + }) +} + +var ContainerResourceMetricStatusBlank = (&ContainerResourceMetricStatusDie{}).DieFeed(autoscalingv2.ContainerResourceMetricStatus{}) + +type ContainerResourceMetricStatusDie struct { + mutable bool + r autoscalingv2.ContainerResourceMetricStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ContainerResourceMetricStatusDie) DieImmutable(immutable bool) *ContainerResourceMetricStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ContainerResourceMetricStatusDie) DieFeed(r autoscalingv2.ContainerResourceMetricStatus) *ContainerResourceMetricStatusDie { + if d.mutable { + d.r = r + return d + } + return &ContainerResourceMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ContainerResourceMetricStatusDie) DieFeedPtr(r *autoscalingv2.ContainerResourceMetricStatus) *ContainerResourceMetricStatusDie { + if r == nil { + r = &autoscalingv2.ContainerResourceMetricStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ContainerResourceMetricStatusDie) DieFeedJSON(j []byte) *ContainerResourceMetricStatusDie { + r := autoscalingv2.ContainerResourceMetricStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ContainerResourceMetricStatusDie) DieFeedYAML(y []byte) *ContainerResourceMetricStatusDie { + r := autoscalingv2.ContainerResourceMetricStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ContainerResourceMetricStatusDie) DieFeedYAMLFile(name string) *ContainerResourceMetricStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ContainerResourceMetricStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *ContainerResourceMetricStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ContainerResourceMetricStatusDie) DieRelease() autoscalingv2.ContainerResourceMetricStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ContainerResourceMetricStatusDie) DieReleasePtr() *autoscalingv2.ContainerResourceMetricStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ContainerResourceMetricStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ContainerResourceMetricStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ContainerResourceMetricStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ContainerResourceMetricStatusDie) DieStamp(fn func(r *autoscalingv2.ContainerResourceMetricStatus)) *ContainerResourceMetricStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ContainerResourceMetricStatusDie) DieStampAt(jp string, fn interface{}) *ContainerResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ContainerResourceMetricStatusDie) DieWith(fns ...func(d *ContainerResourceMetricStatusDie)) *ContainerResourceMetricStatusDie { + nd := ContainerResourceMetricStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ContainerResourceMetricStatusDie) DeepCopy() *ContainerResourceMetricStatusDie { + r := *d.r.DeepCopy() + return &ContainerResourceMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// name is the name of the resource in question. +func (d *ContainerResourceMetricStatusDie) Name(v corev1.ResourceName) *ContainerResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricStatus) { + r.Name = v + }) +} + +// current contains the current value for the given metric +func (d *ContainerResourceMetricStatusDie) Current(v autoscalingv2.MetricValueStatus) *ContainerResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricStatus) { + r.Current = v + }) +} + +// container is the name of the container in the pods of the scaling target +func (d *ContainerResourceMetricStatusDie) Container(v string) *ContainerResourceMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ContainerResourceMetricStatus) { + r.Container = v + }) +} + +var ExternalMetricStatusBlank = (&ExternalMetricStatusDie{}).DieFeed(autoscalingv2.ExternalMetricStatus{}) + +type ExternalMetricStatusDie struct { + mutable bool + r autoscalingv2.ExternalMetricStatus +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *ExternalMetricStatusDie) DieImmutable(immutable bool) *ExternalMetricStatusDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *ExternalMetricStatusDie) DieFeed(r autoscalingv2.ExternalMetricStatus) *ExternalMetricStatusDie { + if d.mutable { + d.r = r + return d + } + return &ExternalMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *ExternalMetricStatusDie) DieFeedPtr(r *autoscalingv2.ExternalMetricStatus) *ExternalMetricStatusDie { + if r == nil { + r = &autoscalingv2.ExternalMetricStatus{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *ExternalMetricStatusDie) DieFeedJSON(j []byte) *ExternalMetricStatusDie { + r := autoscalingv2.ExternalMetricStatus{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *ExternalMetricStatusDie) DieFeedYAML(y []byte) *ExternalMetricStatusDie { + r := autoscalingv2.ExternalMetricStatus{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *ExternalMetricStatusDie) DieFeedYAMLFile(name string) *ExternalMetricStatusDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ExternalMetricStatusDie) DieFeedRawExtension(raw runtime.RawExtension) *ExternalMetricStatusDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *ExternalMetricStatusDie) DieRelease() autoscalingv2.ExternalMetricStatus { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *ExternalMetricStatusDie) DieReleasePtr() *autoscalingv2.ExternalMetricStatus { + r := d.DieRelease() + return &r +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *ExternalMetricStatusDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *ExternalMetricStatusDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *ExternalMetricStatusDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *ExternalMetricStatusDie) DieStamp(fn func(r *autoscalingv2.ExternalMetricStatus)) *ExternalMetricStatusDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *ExternalMetricStatusDie) DieStampAt(jp string, fn interface{}) *ExternalMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricStatus) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *ExternalMetricStatusDie) DieWith(fns ...func(d *ExternalMetricStatusDie)) *ExternalMetricStatusDie { + nd := ExternalMetricStatusBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *ExternalMetricStatusDie) DeepCopy() *ExternalMetricStatusDie { + r := *d.r.DeepCopy() + return &ExternalMetricStatusDie{ + mutable: d.mutable, + r: r, + } +} + +// metric identifies the target metric by name and selector +func (d *ExternalMetricStatusDie) Metric(v autoscalingv2.MetricIdentifier) *ExternalMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricStatus) { + r.Metric = v + }) +} + +// current contains the current value for the given metric +func (d *ExternalMetricStatusDie) Current(v autoscalingv2.MetricValueStatus) *ExternalMetricStatusDie { + return d.DieStamp(func(r *autoscalingv2.ExternalMetricStatus) { + r.Current = v + }) +} diff --git a/apis/autoscaling/v2/zz_generated.die_test.go b/apis/autoscaling/v2/zz_generated.die_test.go new file mode 100644 index 0000000..5866ee5 --- /dev/null +++ b/apis/autoscaling/v2/zz_generated.die_test.go @@ -0,0 +1,225 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright 2021-2022 the original author or authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by diegen. DO NOT EDIT. + +package v2 + +import ( + testing "reconciler.io/dies/testing" + testingx "testing" +) + +func TestHorizontalPodAutoscalerDie_MissingMethods(t *testingx.T) { + die := HorizontalPodAutoscalerBlank + ignore := []string{"TypeMeta", "ObjectMeta"} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for HorizontalPodAutoscalerDie: %s", diff.List()) + } +} + +func TestHorizontalPodAutoscalerSpecDie_MissingMethods(t *testingx.T) { + die := HorizontalPodAutoscalerSpecBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for HorizontalPodAutoscalerSpecDie: %s", diff.List()) + } +} + +func TestCrossVersionObjectReferenceDie_MissingMethods(t *testingx.T) { + die := CrossVersionObjectReferenceBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for CrossVersionObjectReferenceDie: %s", diff.List()) + } +} + +func TestMetricSpecDie_MissingMethods(t *testingx.T) { + die := MetricSpecBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for MetricSpecDie: %s", diff.List()) + } +} + +func TestObjectMetricSourceDie_MissingMethods(t *testingx.T) { + die := ObjectMetricSourceBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ObjectMetricSourceDie: %s", diff.List()) + } +} + +func TestMetricTargetDie_MissingMethods(t *testingx.T) { + die := MetricTargetBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for MetricTargetDie: %s", diff.List()) + } +} + +func TestMetricIdentifierDie_MissingMethods(t *testingx.T) { + die := MetricIdentifierBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for MetricIdentifierDie: %s", diff.List()) + } +} + +func TestPodsMetricSourceDie_MissingMethods(t *testingx.T) { + die := PodsMetricSourceBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for PodsMetricSourceDie: %s", diff.List()) + } +} + +func TestResourceMetricSourceDie_MissingMethods(t *testingx.T) { + die := ResourceMetricSourceBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ResourceMetricSourceDie: %s", diff.List()) + } +} + +func TestContainerResourceMetricSourceDie_MissingMethods(t *testingx.T) { + die := ContainerResourceMetricSourceBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ContainerResourceMetricSourceDie: %s", diff.List()) + } +} + +func TestExternalMetricSourceDie_MissingMethods(t *testingx.T) { + die := ExternalMetricSourceBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ExternalMetricSourceDie: %s", diff.List()) + } +} + +func TestHorizontalPodAutoscalerBehaviorDie_MissingMethods(t *testingx.T) { + die := HorizontalPodAutoscalerBehaviorBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for HorizontalPodAutoscalerBehaviorDie: %s", diff.List()) + } +} + +func TestHPAScalingRulesDie_MissingMethods(t *testingx.T) { + die := HPAScalingRulesBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for HPAScalingRulesDie: %s", diff.List()) + } +} + +func TestHPAScalingPolicyDie_MissingMethods(t *testingx.T) { + die := HPAScalingPolicyBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for HPAScalingPolicyDie: %s", diff.List()) + } +} + +func TestHorizontalPodAutoscalerStatusDie_MissingMethods(t *testingx.T) { + die := HorizontalPodAutoscalerStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for HorizontalPodAutoscalerStatusDie: %s", diff.List()) + } +} + +func TestMetricStatusDie_MissingMethods(t *testingx.T) { + die := MetricStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for MetricStatusDie: %s", diff.List()) + } +} + +func TestObjectMetricStatusDie_MissingMethods(t *testingx.T) { + die := ObjectMetricStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ObjectMetricStatusDie: %s", diff.List()) + } +} + +func TestMetricValueStatusDie_MissingMethods(t *testingx.T) { + die := MetricValueStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for MetricValueStatusDie: %s", diff.List()) + } +} + +func TestPodsMetricStatusDie_MissingMethods(t *testingx.T) { + die := PodsMetricStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for PodsMetricStatusDie: %s", diff.List()) + } +} + +func TestResourceMetricStatusDie_MissingMethods(t *testingx.T) { + die := ResourceMetricStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ResourceMetricStatusDie: %s", diff.List()) + } +} + +func TestContainerResourceMetricStatusDie_MissingMethods(t *testingx.T) { + die := ContainerResourceMetricStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ContainerResourceMetricStatusDie: %s", diff.List()) + } +} + +func TestExternalMetricStatusDie_MissingMethods(t *testingx.T) { + die := ExternalMetricStatusBlank + ignore := []string{} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for ExternalMetricStatusDie: %s", diff.List()) + } +} diff --git a/apis/storage/v1/csistoragecapacities.go b/apis/storage/v1/csistoragecapacities.go new file mode 100644 index 0000000..230839a --- /dev/null +++ b/apis/storage/v1/csistoragecapacities.go @@ -0,0 +1,44 @@ +/* +Copyright 2024 the original author or authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +import ( + storagev1 "k8s.io/api/storage/v1" + resource "k8s.io/apimachinery/pkg/api/resource" + diemetav1 "reconciler.io/dies/apis/meta/v1" +) + +// +die:object=true,apiVersion=storage.k8s.io/v1,kind=CSIStorageCapacity +type _ = storagev1.CSIStorageCapacity + +func (d *CSIStorageCapacityDie) NodeTopologyDie(fn func(d *diemetav1.LabelSelectorDie)) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + d := diemetav1.LabelSelectorBlank.DieImmutable(false).DieFeedPtr(r.NodeTopology) + fn(d) + r.NodeTopology = d.DieReleasePtr() + }) +} + +func (d *CSIStorageCapacityDie) CapacityString(quantity string) *CSIStorageCapacityDie { + q := resource.MustParse(quantity) + return d.Capacity(&q) +} + +func (d *CSIStorageCapacityDie) MaximumVolumeSizeString(quantity string) *CSIStorageCapacityDie { + q := resource.MustParse(quantity) + return d.MaximumVolumeSize(&q) +} diff --git a/apis/storage/v1/zz_generated.die.go b/apis/storage/v1/zz_generated.die.go index 798e4a1..277fcab 100644 --- a/apis/storage/v1/zz_generated.die.go +++ b/apis/storage/v1/zz_generated.die.go @@ -26,6 +26,7 @@ import ( fmtx "fmt" corev1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" + "k8s.io/apimachinery/pkg/api/resource" apismetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" runtime "k8s.io/apimachinery/pkg/runtime" @@ -1873,6 +1874,357 @@ func (d *VolumeNodeResourcesDie) Count(v *int32) *VolumeNodeResourcesDie { }) } +var CSIStorageCapacityBlank = (&CSIStorageCapacityDie{}).DieFeed(storagev1.CSIStorageCapacity{}) + +type CSIStorageCapacityDie struct { + metav1.FrozenObjectMeta + mutable bool + r storagev1.CSIStorageCapacity +} + +// DieImmutable returns a new die for the current die's state that is either mutable (`false`) or immutable (`true`). +func (d *CSIStorageCapacityDie) DieImmutable(immutable bool) *CSIStorageCapacityDie { + if d.mutable == !immutable { + return d + } + d = d.DeepCopy() + d.mutable = !immutable + return d +} + +// DieFeed returns a new die with the provided resource. +func (d *CSIStorageCapacityDie) DieFeed(r storagev1.CSIStorageCapacity) *CSIStorageCapacityDie { + if d.mutable { + d.FrozenObjectMeta = metav1.FreezeObjectMeta(r.ObjectMeta) + d.r = r + return d + } + return &CSIStorageCapacityDie{ + FrozenObjectMeta: metav1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +// DieFeedPtr returns a new die with the provided resource pointer. If the resource is nil, the empty value is used instead. +func (d *CSIStorageCapacityDie) DieFeedPtr(r *storagev1.CSIStorageCapacity) *CSIStorageCapacityDie { + if r == nil { + r = &storagev1.CSIStorageCapacity{} + } + return d.DieFeed(*r) +} + +// DieFeedJSON returns a new die with the provided JSON. Panics on error. +func (d *CSIStorageCapacityDie) DieFeedJSON(j []byte) *CSIStorageCapacityDie { + r := storagev1.CSIStorageCapacity{} + if err := json.Unmarshal(j, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAML returns a new die with the provided YAML. Panics on error. +func (d *CSIStorageCapacityDie) DieFeedYAML(y []byte) *CSIStorageCapacityDie { + r := storagev1.CSIStorageCapacity{} + if err := yaml.Unmarshal(y, &r); err != nil { + panic(err) + } + return d.DieFeed(r) +} + +// DieFeedYAMLFile returns a new die loading YAML from a file path. Panics on error. +func (d *CSIStorageCapacityDie) DieFeedYAMLFile(name string) *CSIStorageCapacityDie { + y, err := osx.ReadFile(name) + if err != nil { + panic(err) + } + return d.DieFeedYAML(y) +} + +// DieFeedRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *CSIStorageCapacityDie) DieFeedRawExtension(raw runtime.RawExtension) *CSIStorageCapacityDie { + j, err := json.Marshal(raw) + if err != nil { + panic(err) + } + return d.DieFeedJSON(j) +} + +// DieRelease returns the resource managed by the die. +func (d *CSIStorageCapacityDie) DieRelease() storagev1.CSIStorageCapacity { + if d.mutable { + return d.r + } + return *d.r.DeepCopy() +} + +// DieReleasePtr returns a pointer to the resource managed by the die. +func (d *CSIStorageCapacityDie) DieReleasePtr() *storagev1.CSIStorageCapacity { + r := d.DieRelease() + return &r +} + +// DieReleaseUnstructured returns the resource managed by the die as an unstructured object. Panics on error. +func (d *CSIStorageCapacityDie) DieReleaseUnstructured() *unstructured.Unstructured { + r := d.DieReleasePtr() + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r) + if err != nil { + panic(err) + } + return &unstructured.Unstructured{ + Object: u, + } +} + +// DieReleaseJSON returns the resource managed by the die as JSON. Panics on error. +func (d *CSIStorageCapacityDie) DieReleaseJSON() []byte { + r := d.DieReleasePtr() + j, err := json.Marshal(r) + if err != nil { + panic(err) + } + return j +} + +// DieReleaseYAML returns the resource managed by the die as YAML. Panics on error. +func (d *CSIStorageCapacityDie) DieReleaseYAML() []byte { + r := d.DieReleasePtr() + y, err := yaml.Marshal(r) + if err != nil { + panic(err) + } + return y +} + +// DieReleaseRawExtension returns the resource managed by the die as an raw extension. Panics on error. +func (d *CSIStorageCapacityDie) DieReleaseRawExtension() runtime.RawExtension { + j := d.DieReleaseJSON() + raw := runtime.RawExtension{} + if err := json.Unmarshal(j, &raw); err != nil { + panic(err) + } + return raw +} + +// DieStamp returns a new die with the resource passed to the callback function. The resource is mutable. +func (d *CSIStorageCapacityDie) DieStamp(fn func(r *storagev1.CSIStorageCapacity)) *CSIStorageCapacityDie { + r := d.DieRelease() + fn(&r) + return d.DieFeed(r) +} + +// Experimental: DieStampAt uses a JSON path (http://goessner.net/articles/JsonPath/) expression to stamp portions of the resource. The callback is invoked with each JSON path match. Panics if the callback function does not accept a single argument of the same type or a pointer to that type as found on the resource at the target location. +// +// Future iterations will improve type coercion from the resource to the callback argument. +func (d *CSIStorageCapacityDie) DieStampAt(jp string, fn interface{}) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + if ni := reflectx.ValueOf(fn).Type().NumIn(); ni != 1 { + panic(fmtx.Errorf("callback function must have 1 input parameters, found %d", ni)) + } + if no := reflectx.ValueOf(fn).Type().NumOut(); no != 0 { + panic(fmtx.Errorf("callback function must have 0 output parameters, found %d", no)) + } + + cp := jsonpath.New("") + if err := cp.Parse(fmtx.Sprintf("{%s}", jp)); err != nil { + panic(err) + } + cr, err := cp.FindResults(r) + if err != nil { + // errors are expected if a path is not found + return + } + for _, cv := range cr[0] { + arg0t := reflectx.ValueOf(fn).Type().In(0) + + var args []reflectx.Value + if cv.Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv} + } else if cv.CanAddr() && cv.Addr().Type().AssignableTo(arg0t) { + args = []reflectx.Value{cv.Addr()} + } else { + panic(fmtx.Errorf("callback function must accept value of type %q, found type %q", cv.Type(), arg0t)) + } + + reflectx.ValueOf(fn).Call(args) + } + }) +} + +// DieWith returns a new die after passing the current die to the callback function. The passed die is mutable. +func (d *CSIStorageCapacityDie) DieWith(fns ...func(d *CSIStorageCapacityDie)) *CSIStorageCapacityDie { + nd := CSIStorageCapacityBlank.DieFeed(d.DieRelease()).DieImmutable(false) + for _, fn := range fns { + if fn != nil { + fn(nd) + } + } + return d.DieFeed(nd.DieRelease()) +} + +// DeepCopy returns a new die with equivalent state. Useful for snapshotting a mutable die. +func (d *CSIStorageCapacityDie) DeepCopy() *CSIStorageCapacityDie { + r := *d.r.DeepCopy() + return &CSIStorageCapacityDie{ + FrozenObjectMeta: metav1.FreezeObjectMeta(r.ObjectMeta), + mutable: d.mutable, + r: r, + } +} + +var _ runtime.Object = (*CSIStorageCapacityDie)(nil) + +func (d *CSIStorageCapacityDie) DeepCopyObject() runtime.Object { + return d.r.DeepCopy() +} + +func (d *CSIStorageCapacityDie) GetObjectKind() schema.ObjectKind { + r := d.DieRelease() + return r.GetObjectKind() +} + +func (d *CSIStorageCapacityDie) MarshalJSON() ([]byte, error) { + return json.Marshal(d.r) +} + +func (d *CSIStorageCapacityDie) UnmarshalJSON(b []byte) error { + if !d.mutable { + return fmtx.Errorf("cannot unmarshal into immutable dies, create a mutable version first") + } + r := &storagev1.CSIStorageCapacity{} + err := json.Unmarshal(b, r) + *d = *d.DieFeed(*r) + return err +} + +// DieDefaultTypeMetadata sets the APIVersion and Kind to "storage.k8s.io/v1" and "CSIStorageCapacity" respectively. +func (d *CSIStorageCapacityDie) DieDefaultTypeMetadata() *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.APIVersion = "storage.k8s.io/v1" + r.Kind = "CSIStorageCapacity" + }) +} + +// APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources +func (d *CSIStorageCapacityDie) APIVersion(v string) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.APIVersion = v + }) +} + +// Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds +func (d *CSIStorageCapacityDie) Kind(v string) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.Kind = v + }) +} + +// TypeMetadata standard object's type metadata. +func (d *CSIStorageCapacityDie) TypeMetadata(v apismetav1.TypeMeta) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.TypeMeta = v + }) +} + +// TypeMetadataDie stamps the resource's TypeMeta field with a mutable die. +func (d *CSIStorageCapacityDie) TypeMetadataDie(fn func(d *metav1.TypeMetaDie)) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + d := metav1.TypeMetaBlank.DieImmutable(false).DieFeed(r.TypeMeta) + fn(d) + r.TypeMeta = d.DieRelease() + }) +} + +// Metadata standard object's metadata. +func (d *CSIStorageCapacityDie) Metadata(v apismetav1.ObjectMeta) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.ObjectMeta = v + }) +} + +// MetadataDie stamps the resource's ObjectMeta field with a mutable die. +func (d *CSIStorageCapacityDie) MetadataDie(fn func(d *metav1.ObjectMetaDie)) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + d := metav1.ObjectMetaBlank.DieImmutable(false).DieFeed(r.ObjectMeta) + fn(d) + r.ObjectMeta = d.DieRelease() + }) +} + +// nodeTopology defines which nodes have access to the storage +// +// for which capacity was reported. If not set, the storage is +// +// not accessible from any node in the cluster. If empty, the +// +// storage is accessible from all nodes. This field is +// +// immutable. +func (d *CSIStorageCapacityDie) NodeTopology(v *apismetav1.LabelSelector) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.NodeTopology = v + }) +} + +// storageClassName represents the name of the StorageClass that the reported capacity applies to. +// +// # It must meet the same requirements as the name of a StorageClass +// +// object (non-empty, DNS subdomain). If that object no longer exists, +// +// the CSIStorageCapacity object is obsolete and should be removed by its +// +// creator. +// +// This field is immutable. +func (d *CSIStorageCapacityDie) StorageClassName(v string) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.StorageClassName = v + }) +} + +// capacity is the value reported by the CSI driver in its GetCapacityResponse +// +// for a GetCapacityRequest with topology and parameters that match the +// +// previous fields. +// +// The semantic is currently (CSI spec 1.2) defined as: +// +// # The available capacity, in bytes, of the storage that can be used +// +// to provision volumes. If not set, that information is currently +// +// unavailable. +func (d *CSIStorageCapacityDie) Capacity(v *resource.Quantity) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.Capacity = v + }) +} + +// maximumVolumeSize is the value reported by the CSI driver in its GetCapacityResponse +// +// for a GetCapacityRequest with topology and parameters that match the +// +// previous fields. +// +// # This is defined since CSI spec 1.4.0 as the largest size +// +// that may be used in a +// +// CreateVolumeRequest.capacity_range.required_bytes field to +// +// create a volume with the same parameters as those in +// +// GetCapacityRequest. The corresponding value in the Kubernetes +// +// API is ResourceRequirements.Requests in a volume claim. +func (d *CSIStorageCapacityDie) MaximumVolumeSize(v *resource.Quantity) *CSIStorageCapacityDie { + return d.DieStamp(func(r *storagev1.CSIStorageCapacity) { + r.MaximumVolumeSize = v + }) +} + var StorageClassBlank = (&StorageClassDie{}).DieFeed(storagev1.StorageClass{}) type StorageClassDie struct { diff --git a/apis/storage/v1/zz_generated.die_test.go b/apis/storage/v1/zz_generated.die_test.go index c0fe536..f426ba6 100644 --- a/apis/storage/v1/zz_generated.die_test.go +++ b/apis/storage/v1/zz_generated.die_test.go @@ -89,6 +89,15 @@ func TestVolumeNodeResourcesDie_MissingMethods(t *testingx.T) { } } +func TestCSIStorageCapacityDie_MissingMethods(t *testingx.T) { + die := CSIStorageCapacityBlank + ignore := []string{"TypeMeta", "ObjectMeta"} + diff := testing.DieFieldDiff(die).Delete(ignore...) + if diff.Len() != 0 { + t.Errorf("found missing fields for CSIStorageCapacityDie: %s", diff.List()) + } +} + func TestStorageClassDie_MissingMethods(t *testingx.T) { die := StorageClassBlank ignore := []string{"TypeMeta", "ObjectMeta"}