Skip to content

Commit

Permalink
refactor: use status conditions to store build status
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Oct 2, 2024
1 parent 6fcd8f6 commit c10a43f
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 134 deletions.
20 changes: 20 additions & 0 deletions apis/lagoon/v1beta1/lagoontask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ func init() {
SchemeBuilder.Register(&LagoonTask{}, &LagoonTaskList{})
}

// convert to string as required for environment ID for backwards compatability
func (a *LagoonTaskEnvironment) UnmarshalJSON(data []byte) error {
tmpMap := map[string]interface{}{}
json.Unmarshal(data, &tmpMap)
if value, ok := tmpMap["id"]; ok {
a.ID = fmt.Sprintf("%v", value)
}
return nil
}

// convert to string as required for project ID for backwards compatability
func (a *LagoonTaskProject) UnmarshalJSON(data []byte) error {
tmpMap := map[string]interface{}{}
json.Unmarshal(data, &tmpMap)
if value, ok := tmpMap["id"]; ok {
a.ID = fmt.Sprintf("%v", value)
}
return nil
}

// this is a custom unmarshal function that will check deployerToken and sshKey which come from Lagoon as `1|0` booleans because javascript
// this converts them from floats to bools
func (a *LagoonAdvancedTaskInfo) UnmarshalJSON(data []byte) error {
Expand Down
21 changes: 9 additions & 12 deletions apis/lagoon/v1beta2/lagoonbuild_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/uselagoon/remote-controller/internal/helpers"
"github.com/uselagoon/remote-controller/internal/metrics"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -44,7 +45,7 @@ var (
)

// BuildContainsStatus .
func BuildContainsStatus(slice []LagoonBuildConditions, s LagoonBuildConditions) bool {
func BuildContainsStatus(slice []metav1.Condition, s metav1.Condition) bool {
for _, item := range slice {
if item == s {
return true
Expand Down Expand Up @@ -363,20 +364,16 @@ func updateLagoonBuild(namespace string, jobSpec LagoonTaskSpec, lagoonBuild *La
conditions := lagoonBuild.Status.Conditions
// sort the build conditions by time so the first and last can be extracted
sort.Slice(conditions, func(i, j int) bool {
iTime, _ := time.Parse("2006-01-02T15:04:05Z", conditions[i].LastTransitionTime)
jTime, _ := time.Parse("2006-01-02T15:04:05Z", conditions[j].LastTransitionTime)
return iTime.Before(jTime)
iTime := conditions[i].LastTransitionTime
jTime := conditions[j].LastTransitionTime
return iTime.Before(&jTime)
})
// get the starting time, or fallback to default
sTime, err := time.Parse("2006-01-02T15:04:05Z", conditions[0].LastTransitionTime)
if err == nil {
msg.Meta.StartTime = sTime.Format("2006-01-02 15:04:05")
}
sTime := conditions[0].LastTransitionTime
msg.Meta.StartTime = sTime.Format("2006-01-02 15:04:05")
// get the ending time, or fallback to default
eTime, err := time.Parse("2006-01-02T15:04:05Z", conditions[len(conditions)-1].LastTransitionTime)
if err == nil {
msg.Meta.EndTime = eTime.Format("2006-01-02 15:04:05")
}
eTime := conditions[len(conditions)-1].LastTransitionTime
msg.Meta.EndTime = eTime.Format("2006-01-02 15:04:05")
}
msgBytes, err := json.Marshal(msg)
if err != nil {
Expand Down
23 changes: 9 additions & 14 deletions apis/lagoon/v1beta2/lagoonbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package v1beta2
import (
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:object:root=true
//+kubebuilder:storageversion
// +kubebuilder:storageversion
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=`.status.phase`,description="Status of the LagoonBuild"
// +kubebuilder:printcolumn:name="BuildStep",type="string",JSONPath=`.status.conditions[?(@.type == "BuildStep")].reason`,description="The build step of the LagoonBuild"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

// LagoonBuild is the Schema for the lagoonbuilds API
type LagoonBuild struct {
Expand Down Expand Up @@ -88,18 +90,11 @@ type LagoonBuildSpec struct {

// LagoonBuildStatus defines the observed state of LagoonBuild
type LagoonBuildStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Conditions []LagoonBuildConditions `json:"conditions,omitempty"`
Log []byte `json:"log,omitempty"`
}

// LagoonBuildConditions defines the observed conditions of build pods.
type LagoonBuildConditions struct {
LastTransitionTime string `json:"lastTransitionTime"`
Status corev1.ConditionStatus `json:"status"`
Type BuildStatusType `json:"type"`
// Condition string `json:"condition"`
// Conditions provide a standard mechanism for higher-level status reporting from a controller.
// They are an extension mechanism which allows tools and other controllers to collect summary information about
// resources without needing to understand resource-specific status details.
Conditions []metav1.Condition `json:"conditions,omitempty"`
Phase string `json:"phase,omitempty"`
}

func init() {
Expand Down
3 changes: 2 additions & 1 deletion apis/lagoon/v1beta2/lagoontask_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/uselagoon/machinery/api/schema"
"github.com/uselagoon/remote-controller/internal/helpers"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -41,7 +42,7 @@ var (
)

// TaskContainsStatus .
func TaskContainsStatus(slice []LagoonTaskConditions, s LagoonTaskConditions) bool {
func TaskContainsStatus(slice []metav1.Condition, s metav1.Condition) bool {
for _, item := range slice {
if item == s {
return true
Expand Down
21 changes: 7 additions & 14 deletions apis/lagoon/v1beta2/lagoontask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import (
"strings"

"github.com/uselagoon/machinery/api/schema"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// +kubebuilder:object:root=true
//+kubebuilder:storageversion
// +kubebuilder:storageversion
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=`.status.phase`,description="Status of the LagoonTask"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

// LagoonTask is the Schema for the lagoontasks API
type LagoonTask struct {
Expand Down Expand Up @@ -147,18 +148,10 @@ type LagoonTaskEnvironment struct {

// LagoonTaskStatus defines the observed state of LagoonTask
type LagoonTaskStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Conditions []LagoonTaskConditions `json:"conditions,omitempty"`
Log []byte `json:"log,omitempty"`
}

// LagoonTaskConditions defines the observed conditions of task pods.
type LagoonTaskConditions struct {
LastTransitionTime string `json:"lastTransitionTime"`
Status corev1.ConditionStatus `json:"status"`
Type TaskStatusType `json:"type"`
// Condition string `json:"condition"`
// They are an extension mechanism which allows tools and other controllers to collect summary information about
// resources without needing to understand resource-specific status details.
Conditions []metav1.Condition `json:"conditions,omitempty"`
Phase string `json:"phase,omitempty"`
}

func init() {
Expand Down
53 changes: 9 additions & 44 deletions apis/lagoon/v1beta2/zz_generated.deepcopy.go

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

84 changes: 75 additions & 9 deletions config/crd/bases/crd.lagoon.sh_lagoonbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,19 @@ spec:
type: object
served: true
storage: false
- name: v1beta2
- additionalPrinterColumns:
- description: Status of the LagoonBuild
jsonPath: .status.phase
name: Status
type: string
- description: The build step of the LagoonBuild
jsonPath: .status.conditions[?(@.type == "BuildStep")].reason
name: BuildStep
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1beta2
schema:
openAPIV3Schema:
description: LagoonBuild is the Schema for the lagoonbuilds API
Expand Down Expand Up @@ -842,33 +854,87 @@ spec:
description: LagoonBuildStatus defines the observed state of LagoonBuild
properties:
conditions:
description: 'INSERT ADDITIONAL STATUS FIELD - define observed state
of cluster Important: Run "make" to regenerate code after modifying
this file'
description: Conditions provide a standard mechanism for higher-level
status reporting from a controller. They are an extension mechanism
which allows tools and other controllers to collect summary information
about resources without needing to understand resource-specific
status details.
items:
description: LagoonBuildConditions defines the observed conditions
of build pods.
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions. For example,
\n \ttype FooStatus struct{ \t // Represents the observations
of a foo's current state. \t // Known .status.conditions.type
are: \"Available\", \"Progressing\", and \"Degraded\" \t //
+patchMergeKey=type \t // +patchStrategy=merge \t // +listType=map
\t // +listMapKey=type \t Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n \t // other fields
\t}"
properties:
lastTransitionTime:
description: lastTransitionTime is the 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: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if .metadata.generation
is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the current
state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: BuildStatusType const for the status 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. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
log:
format: byte
phase:
type: string
type: object
type: object
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: ""
Expand Down
Loading

0 comments on commit c10a43f

Please sign in to comment.