Skip to content

feat: simplify #66

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion api/clusters/v1alpha1/accessrequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ type PermissionsRequest struct {

// AccessRequestStatus defines the observed state of AccessRequest
type AccessRequestStatus struct {
CommonStatus `json:",inline"`
// ObservedGeneration is the generation of this resource that was last reconciled by the controller.
ObservedGeneration int64 `json:"observedGeneration"`

// LastReconcileTime is the time when the resource was last reconciled by the controller.
LastReconcileTime metav1.Time `json:"lastReconcileTime"`

// Reason is expected to contain a CamelCased string that provides further information in a machine-readable format.
// +optional
Reason string `json:"reason,omitempty"`

// Message contains further details in a human-readable format.
// +optional
Message string `json:"message,omitempty"`

// Conditions contains the conditions of this resource using the standard Kubernetes condition format.
// +optional
Conditions []metav1.Condition `json:"conditions,omitempty"`

// Phase is the current phase of the request.
// +kubebuilder:default=Pending
Expand Down
18 changes: 17 additions & 1 deletion api/clusters/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ type K8sConfiguration struct {

// ClusterStatus defines the observed state of Cluster
type ClusterStatus struct {
CommonStatus `json:",inline"`
// ObservedGeneration is the generation of this resource that was last reconciled by the controller.
ObservedGeneration int64 `json:"observedGeneration"`

// LastReconcileTime is the time when the resource was last reconciled by the controller.
LastReconcileTime metav1.Time `json:"lastReconcileTime"`

// Reason is expected to contain a CamelCased string that provides further information in a machine-readable format.
// +optional
Reason string `json:"reason,omitempty"`

// Message contains further details in a human-readable format.
// +optional
Message string `json:"message,omitempty"`

// Conditions contains the conditions of this resource using the standard Kubernetes condition format.
// +optional
Conditions []metav1.Condition `json:"conditions,omitempty"`

// Phase is the current phase of the cluster.
Phase ClusterPhase `json:"phase"`
Expand Down
18 changes: 17 additions & 1 deletion api/clusters/v1alpha1/clusterrequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@ type ClusterRequestSpec struct {

// +kubebuilder:validation:XValidation:rule="!has(oldSelf.cluster) || has(self.cluster)", message="cluster may not be removed once set"
type ClusterRequestStatus struct {
CommonStatus `json:",inline"`
// ObservedGeneration is the generation of this resource that was last reconciled by the controller.
ObservedGeneration int64 `json:"observedGeneration"`

// LastReconcileTime is the time when the resource was last reconciled by the controller.
LastReconcileTime metav1.Time `json:"lastReconcileTime"`

// Reason is expected to contain a CamelCased string that provides further information in a machine-readable format.
// +optional
Reason string `json:"reason,omitempty"`

// Message contains further details in a human-readable format.
// +optional
Message string `json:"message,omitempty"`

// Conditions contains the conditions of this resource using the standard Kubernetes condition format.
// +optional
Conditions []metav1.Condition `json:"conditions,omitempty"`

// Phase is the current phase of the request.
// +kubebuilder:default=Pending
Expand Down
9 changes: 0 additions & 9 deletions api/clusters/v1alpha1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ const (
PURPOSE_MCP = "mcp"
)

const (
// CONDITION_UNKNOWN represents an unknown status for the condition.
CONDITION_UNKNOWN ConditionStatus = "Unknown"
// CONDITION_TRUE marks the condition as true.
CONDITION_TRUE ConditionStatus = "True"
// CONDITION_FALSE marks the condition as false.
CONDITION_FALSE ConditionStatus = "False"
)

const (
// PHASE_UNKNOWN represents an unknown phase for the cluster.
PHASE_UNKNOWN ClusterPhase = "Unknown"
Expand Down
140 changes: 19 additions & 121 deletions api/clusters/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,26 @@
package v1alpha1

import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// These are standard condition types that can be used across resources
const (
// ReadyConditionType indicates whether the resource is ready
ReadyConditionType = "Ready"
// AvailableConditionType indicates whether the resource is available
AvailableConditionType = "Available"
// ReconcileSuccessConditionType indicates whether the last reconciliation was successful
ReconcileSuccessConditionType = "ReconcileSuccess"
)

type ConditionStatus string

type Condition struct {
// Type is the type of the condition.
// Must be unique within the resource.
Type string `json:"type"`

// Status is the status of the condition.
Status ConditionStatus `json:"status"`

// Reason is expected to contain a CamelCased string that provides further information regarding the condition.
// It should have a fixed value set (like an enum) to be machine-readable. The value set depends on the condition type.
// It is optional, but should be filled at least when Status is not "True".
// +optional
Reason string `json:"reason,omitempty"`

// Message contains further details regarding the condition.
// It is meant for human users, Reason should be used for programmatic evaluation instead.
// It is optional, but should be filled at least when Status is not "True".
// +optional
Message string `json:"message,omitempty"`

// LastTransitionTime specifies the time when this condition's status last changed.
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
}

// Implement the Condition interface from our controller-utils library
func (c *Condition) GetType() string {
return c.Type
}
func (c *Condition) SetType(t string) {
c.Type = t
}
func (c *Condition) GetStatus() ConditionStatus {
return c.Status
}
func (c *Condition) SetStatus(s ConditionStatus) {
c.Status = s
}
func (c *Condition) GetReason() string {
return c.Reason
}
func (c *Condition) SetReason(r string) {
c.Reason = r
}
func (c *Condition) GetMessage() string {
return c.Message
}
func (c *Condition) SetMessage(m string) {
c.Message = m
}
func (c *Condition) GetLastTransitionTime() time.Time {
return c.LastTransitionTime.Time
}
func (c *Condition) SetLastTransitionTime(t time.Time) {
c.LastTransitionTime = metav1.NewTime(t)
}

// ConditionList is a list of Conditions.
type ConditionList []Condition

// ConditionStatusFromBoolPtr converts a bool pointer into the corresponding ConditionStatus.
// If nil, "Unknown" is returned.
func ConditionStatusFromBoolPtr(src *bool) ConditionStatus {
if src == nil {
return CONDITION_UNKNOWN
}
return ConditionStatusFromBool(*src)
}

// ConditionStatusFromBool converts a bool into the corresponding ConditionStatus.
func ConditionStatusFromBool(src bool) ConditionStatus {
if src {
return CONDITION_TRUE
}
return CONDITION_FALSE
}

// IsTrue returns true if the Condition's status is "True".
// Note that the status can be "Unknown", so !IsTrue() is not the same as IsFalse().
func (cc Condition) IsTrue() bool {
return cc.Status == CONDITION_TRUE
}

// IsFalse returns true if the Condition's status is "False".
// Note that the status can be "Unknown", so !IsFalse() is not the same as IsTrue().
func (cc Condition) IsFalse() bool {
return cc.Status == CONDITION_FALSE
}

// IsUnknown returns true if the Condition's status is "Unknown".
func (cc Condition) IsUnknown() bool {
return cc.Status == CONDITION_UNKNOWN
}
// These are standard condition reasons that can be used across resources
const (
// ReconcileSuccessReason indicates that the reconciliation was successful
ReconcileSuccessReason = "ReconcileSuccess"
// ReconcileErrorReason indicates that there was an error during reconciliation
ReconcileErrorReason = "ReconcileError"
// ResourceAvailableReason indicates that the resource is available
ResourceAvailableReason = "ResourceAvailable"
// ResourceUnavailableReason indicates that the resource is not available
ResourceUnavailableReason = "ResourceUnavailable"
)

type ObjectReference struct {
// Name is the name of the referenced resource.
Expand All @@ -113,26 +34,3 @@ type NamespacedObjectReference struct {
// Namespace is the namespace of the referenced resource.
Namespace string `json:"namespace"`
}

// CommonStatus is a status shared by multiple resource.
// Note that a 'phase' is also part of the status, but it cannot be included in this struct.
// The reason is that we want to use string-like types for the phase, but the goddamn code generation does not support generics, no matter which annotations are added.
type CommonStatus struct {
// ObservedGeneration is the generation of this resource that was last reconciled by the controller.
ObservedGeneration int64 `json:"observedGeneration"`

// LastReconcileTime is the time when the resource was last reconciled by the controller.
LastReconcileTime metav1.Time `json:"lastReconcileTime"`

// Reason is expected to contain a CamelCased string that provides further information in a machine-readable format.
// +optional
Reason string `json:"reason,omitempty"`

// Message contains further details in a human-readable format.
// +optional
Message string `json:"message,omitempty"`

// Conditions contains the conditions.
// +optional
Conditions ConditionList `json:"conditions,omitempty"`
}
88 changes: 25 additions & 63 deletions api/clusters/v1alpha1/zz_generated.deepcopy.go

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

Loading
Loading