Skip to content

Commit

Permalink
refactor: replace custom retries w/ patch helpers; make SinkState a c…
Browse files Browse the repository at this point in the history
…ondition (#233)

* refactor: replace custom retries w/ patch helpers; make SinkState a condition

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

* test: add coverage

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

* fix: return patch errors

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

---------

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson authored Mar 11, 2024
1 parent 4c9d3f2 commit 38f1bc5
Show file tree
Hide file tree
Showing 19 changed files with 490 additions and 378 deletions.
14 changes: 11 additions & 3 deletions api/v1alpha1/validationresult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func TestHash(t *testing.T) {
Expand All @@ -25,9 +26,16 @@ func TestHash(t *testing.T) {
ExpectedResults: 1,
},
Status: ValidationResultStatus{
State: ValidationSucceeded,
SinkState: SinkEmitSucceeded,
Conditions: []ValidationCondition{
State: ValidationSucceeded,
Conditions: []clusterv1beta1.Condition{
{
Type: SinkEmission,
Reason: string(SinkEmitSucceeded),
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Now(),
},
},
ValidationConditions: []ValidationCondition{
{
ValidationType: "foo",
ValidationRule: "bar",
Expand Down
25 changes: 16 additions & 9 deletions api/v1alpha1/validationresult_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

// ValidationResultSpec defines the desired state of ValidationResult
Expand All @@ -41,23 +42,29 @@ const (
ValidationSucceeded ValidationState = "Succeeded"
)

type SinkState string
const SinkEmission clusterv1beta1.ConditionType = "SinkEmission"

type SinkEmitState string

const (
SinkEmitNone SinkState = "N/A"
SinkEmitFailed SinkState = "Failed"
SinkEmitSucceeded SinkState = "Succeeded"
SinkEmitNA SinkEmitState = "SinkEmitNA"
SinkEmitFailed SinkEmitState = "SinkEmitFailed"
SinkEmitSucceeded SinkEmitState = "SinkEmitSucceeded"
)

// ValidationResultStatus defines the observed state of ValidationResult
type ValidationResultStatus struct {
State ValidationState `json:"state"`
SinkState SinkState `json:"sinkState,omitempty"`
State ValidationState `json:"state"`

// +optional
// +patchMergeKey=type
// +patchStrategy=merge
Conditions []clusterv1beta1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`

// +optional
// +patchMergeKey=type
// +patchStrategy=merge
Conditions []ValidationCondition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
ValidationConditions []ValidationCondition `json:"validationConditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
}

type ValidationCondition struct {
Expand All @@ -71,7 +78,7 @@ type ValidationCondition struct {
Details []string `json:"details,omitempty"`
// Human-readable messages indicating additional failure details for the last transition.
Failures []string `json:"failures,omitempty"`
// True if the validation rule succeeded, otherwise False
// True if the validation rule succeeded, otherwise False.
Status corev1.ConditionStatus `json:"status"`
// Timestamp of most recent execution of the validation rule associated with the condition.
LastValidationTime metav1.Time `json:"lastValidationTime"`
Expand Down Expand Up @@ -108,7 +115,7 @@ func (r *ValidationResult) Hash() string {
fmt.Fprint(digester, r.Spec)
fmt.Fprint(digester, r.Status.State)

for _, condition := range r.Status.Conditions {
for _, condition := range r.Status.ValidationConditions {
c := condition.DeepCopy()
c.LastValidationTime = metav1.Time{}
fmt.Fprint(digester, c)
Expand Down
8 changes: 8 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,52 @@ spec:
description: ValidationResultStatus defines the observed state of ValidationResult
properties:
conditions:
items:
description: Condition defines an observation of a Cluster API resource
operational state.
properties:
lastTransitionTime:
description: Last time the condition transitioned from one status
to another. This should be when the underlying condition changed.
If that is not known, then using the time when the API field
changed is acceptable.
format: date-time
type: string
message:
description: A human readable message indicating details about
the transition. This field may be empty.
type: string
reason:
description: The reason for the condition's last transition
in CamelCase. The specific API may choose whether or not this
field is considered a guaranteed API. This field may not be
empty.
type: string
severity:
description: Severity provides an explicit classification of
Reason code, so the users or machines can immediately understand
the current situation and act accordingly. The Severity field
MUST be set only when Status=False.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: Type of condition in CamelCase or in foo.example.com/CamelCase.
Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important.
type: string
required:
- lastTransitionTime
- status
- type
type: object
type: array
state:
type: string
validationConditions:
items:
properties:
details:
Expand All @@ -87,7 +133,7 @@ spec:
type: string
status:
description: True if the validation rule succeeded, otherwise
False
False.
type: string
validationRule:
description: Unique, one-word description of the validation
Expand All @@ -104,10 +150,6 @@ spec:
- validationType
type: object
type: array
sinkState:
type: string
state:
type: string
required:
- state
type: object
Expand Down
25 changes: 14 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,51 @@ require (
k8s.io/apimachinery v0.29.2
k8s.io/client-go v0.29.2
k8s.io/klog/v2 v2.120.1
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/cluster-api v1.6.2
sigs.k8s.io/controller-runtime v0.16.5
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/zapr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gobuffalo/flect v1.0.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand All @@ -69,8 +72,8 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.28.3 // indirect
k8s.io/component-base v0.28.3 // indirect
k8s.io/apiextensions-apiserver v0.28.4 // indirect
k8s.io/component-base v0.28.4 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
Loading

0 comments on commit 38f1bc5

Please sign in to comment.