Skip to content

Commit

Permalink
feat: add conditions to status subresource on CRs (#86)
Browse files Browse the repository at this point in the history
Adds []metav1.Condition property to the status subresource of pools, enterprise, org and repo
  • Loading branch information
rafalgalaw authored Feb 28, 2024
1 parent 459072c commit 1e194e5
Show file tree
Hide file tree
Showing 25 changed files with 1,581 additions and 172 deletions.
33 changes: 28 additions & 5 deletions api/v1alpha1/enterprise_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/mercedes-benz/garm-operator/pkg/util/conditions"
)

// EnterpriseSpec defines the desired state of Enterprise
Expand All @@ -16,9 +18,8 @@ type EnterpriseSpec struct {

// EnterpriseStatus defines the observed state of Enterprise
type EnterpriseStatus struct {
ID string `json:"id"`
PoolManagerIsRunning bool `json:"poolManagerIsRunning"`
PoolManagerFailureReason string `json:"poolManagerFailureReason,omitempty"`
ID string `json:"id"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
Expand All @@ -38,6 +39,14 @@ type Enterprise struct {
Status EnterpriseStatus `json:"status,omitempty"`
}

func (e *Enterprise) SetConditions(conditions []metav1.Condition) {
e.Status.Conditions = conditions
}

func (e *Enterprise) GetConditions() []metav1.Condition {
return e.Status.Conditions
}

func (e *Enterprise) GetCredentialsName() string {
return e.Spec.CredentialsName
}
Expand All @@ -51,11 +60,25 @@ func (e *Enterprise) GetName() string {
}

func (e *Enterprise) GetPoolManagerIsRunning() bool {
return e.Status.PoolManagerIsRunning
condition := conditions.Get(e, conditions.PoolManager)
if condition == nil {
return false
}

return condition.Status == TrueAsString
}

func (e *Enterprise) GetPoolManagerFailureReason() string {
return e.Status.PoolManagerFailureReason
condition := conditions.Get(e, conditions.PoolManager)
if condition == nil {
return ""
}

if condition.Reason == string(conditions.PoolManagerFailureReason) {
return condition.Message
}

return ""
}

func (e *Enterprise) GetKind() string {
Expand Down
33 changes: 28 additions & 5 deletions api/v1alpha1/organization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/mercedes-benz/garm-operator/pkg/util/conditions"
)

// OrganizationSpec defines the desired state of Organization
Expand All @@ -16,9 +18,8 @@ type OrganizationSpec struct {

// OrganizationStatus defines the observed state of Organization
type OrganizationStatus struct {
ID string `json:"id"`
PoolManagerIsRunning bool `json:"poolManagerIsRunning"`
PoolManagerFailureReason string `json:"poolManagerFailureReason,omitempty"`
ID string `json:"id"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
Expand All @@ -38,6 +39,14 @@ type Organization struct {
Status OrganizationStatus `json:"status,omitempty"`
}

func (o *Organization) SetConditions(conditions []metav1.Condition) {
o.Status.Conditions = conditions
}

func (o *Organization) GetConditions() []metav1.Condition {
return o.Status.Conditions
}

func (o *Organization) GetCredentialsName() string {
return o.Spec.CredentialsName
}
Expand All @@ -51,11 +60,25 @@ func (o *Organization) GetName() string {
}

func (o *Organization) GetPoolManagerIsRunning() bool {
return o.Status.PoolManagerIsRunning
condition := conditions.Get(o, conditions.PoolManager)
if condition == nil {
return false
}

return condition.Status == TrueAsString
}

func (o *Organization) GetPoolManagerFailureReason() string {
return o.Status.PoolManagerFailureReason
condition := conditions.Get(o, conditions.PoolManager)
if condition == nil {
return ""
}

if condition.Reason == string(conditions.PoolManagerFailureReason) {
return condition.Message
}

return ""
}

func (o *Organization) GetKind() string {
Expand Down
16 changes: 12 additions & 4 deletions api/v1alpha1/pool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ type PoolStatus struct {
LongRunningIdleRunners uint `json:"longRunningIdleRunners"`
Selector string `json:"selector"`

LastSyncError string `json:"lastSyncError,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

func (p *Pool) SetConditions(conditions []metav1.Condition) {
p.Status.Conditions = conditions
}

func (p *Pool) GetConditions() []metav1.Condition {
return p.Status.Conditions
}

//+kubebuilder:object:root=true
Expand All @@ -60,7 +68,7 @@ type PoolStatus struct {
//+kubebuilder:printcolumn:name="MinIdleRunners",type=string,JSONPath=`.spec.minIdleRunners`
//+kubebuilder:printcolumn:name="MaxRunners",type=string,JSONPath=`.spec.maxRunners`
//+kubebuilder:printcolumn:name="ImageName",type=string,JSONPath=`.spec.imageName`,priority=1
//+kubebuilder:printcolumn:name="Flavour",type=string,JSONPath=`.spec.flavor`,priority=1
//+kubebuilder:printcolumn:name="Flavor",type=string,JSONPath=`.spec.flavor`,priority=1
//+kubebuilder:printcolumn:name="Provider",type=string,JSONPath=`.spec.providerName`,priority=1
//+kubebuilder:printcolumn:name="ScopeType",type=string,JSONPath=`.spec.githubScopeRef.kind`,priority=1
//+kubebuilder:printcolumn:name="ScopeName",type=string,JSONPath=`.spec.githubScopeRef.name`,priority=1
Expand Down Expand Up @@ -99,9 +107,9 @@ func MatchesImage(image string) Predicate {
}
}

func MatchesFlavour(flavour string) Predicate {
func MatchesFlavor(flavor string) Predicate {
return func(p Pool) bool {
return p.Spec.Flavor == flavour
return p.Spec.Flavor == flavor
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/pool_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestPoolList_FilterByFields(t *testing.T) {
args: args{
predicates: []Predicate{
MatchesImage("ubuntu-2204"),
MatchesFlavour("large"),
MatchesFlavor("large"),
MatchesProvider("openstack"),
MatchesGitHubScope("test", "Enterprise"),
},
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestPoolList_FilterByFields(t *testing.T) {
args: args{
predicates: []Predicate{
MatchesImage("ubuntu-2404"),
MatchesFlavour("large"),
MatchesFlavor("large"),
MatchesProvider("openstack"),
MatchesGitHubScope("test", "Enterprise"),
},
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/pool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (r *Pool) ValidateCreate() (admission.Warnings, error) {
}

poolList.FilterByFields(
MatchesFlavour(r.Spec.Flavor),
MatchesFlavor(r.Spec.Flavor),
MatchesImage(r.Spec.ImageName),
MatchesProvider(r.Spec.ProviderName),
MatchesGitHubScope(r.Spec.GitHubScopeRef.Name, r.Spec.GitHubScopeRef.Kind),
Expand Down
33 changes: 28 additions & 5 deletions api/v1alpha1/repository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/mercedes-benz/garm-operator/pkg/util/conditions"
)

// RepositorySpec defines the desired state of Repository
Expand All @@ -17,9 +19,8 @@ type RepositorySpec struct {

// RepositoryStatus defines the observed state of Repository
type RepositoryStatus struct {
ID string `json:"id"`
PoolManagerIsRunning bool `json:"poolManagerIsRunning"`
PoolManagerFailureReason string `json:"poolManagerFailureReason,omitempty"`
ID string `json:"id"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
Expand All @@ -39,6 +40,14 @@ type Repository struct {
Status RepositoryStatus `json:"status,omitempty"`
}

func (r *Repository) SetConditions(conditions []metav1.Condition) {
r.Status.Conditions = conditions
}

func (r *Repository) GetConditions() []metav1.Condition {
return r.Status.Conditions
}

func (r *Repository) GetCredentialsName() string {
return r.Spec.CredentialsName
}
Expand All @@ -52,11 +61,25 @@ func (r *Repository) GetName() string {
}

func (r *Repository) GetPoolManagerIsRunning() bool {
return r.Status.PoolManagerIsRunning
condition := conditions.Get(r, conditions.PoolManager)
if condition == nil {
return false
}

return condition.Status == TrueAsString
}

func (r *Repository) GetPoolManagerFailureReason() string {
return r.Status.PoolManagerFailureReason
condition := conditions.Get(r, conditions.PoolManager)
if condition == nil {
return ""
}

if condition.Reason == string(conditions.PoolManagerFailureReason) {
return condition.Message
}

return ""
}

func (r *Repository) GetKind() string {
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ type SecretRef struct {
// Key is the key in the secret's data map for this value
Key string `json:"key"`
}

const (
TrueAsString = "True"
FalseAsString = "False"
)
37 changes: 33 additions & 4 deletions api/v1alpha1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 1e194e5

Please sign in to comment.