Skip to content
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

report available status for shipwright build object #14

Merged
merged 1 commit into from
Oct 20, 2021
Merged
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
17 changes: 15 additions & 2 deletions api/v1alpha1/shipwrightbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ type ShipwrightBuildSpec struct {
TargetNamespace string `json:"targetNamespace,omitempty"`
}

// ShipwrightBuildStatus defines the observed state of Shipwright-Build
type ShipwrightBuildStatus struct{}
// ShipwrightBuildStatus defines the observed state of ShipwrightBuild
type ShipwrightBuildStatus struct {
// Conditions holds the latest available observations of a resource's current state.
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:scope=Cluster
Expand Down Expand Up @@ -44,3 +47,13 @@ type ShipwrightBuildList struct {
func init() {
SchemeBuilder.Register(&ShipwrightBuild{}, &ShipwrightBuildList{})
}

// IsReady returns true the Ready condition status is True
func (status ShipwrightBuildStatus) IsReady() bool {
jkhelil marked this conversation as resolved.
Show resolved Hide resolved
for _, condition := range status.Conditions {
if condition.Type == "Ready" && condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
62 changes: 62 additions & 0 deletions api/v1alpha1/shipwrightbuild_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package v1alpha1

import (
"testing"

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

const (
ConditionReady = "Ready"
ConditionNotReady = "NotReady"
)

// TestIsReady tests IsReady condition status function
func TestIsReady(t *testing.T) {
testCases := map[string]struct {
status ShipwrightBuildStatus
expectedOutput bool
}{
"ready": {
status: ShipwrightBuildStatus{
Conditions: []metav1.Condition{
metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionTrue,
Reason: "Good",
},
metav1.Condition{
Type: ConditionNotReady,
Status: metav1.ConditionFalse,
Reason: "Good",
},
},
},
expectedOutput: true,
},
"notReady": {
status: ShipwrightBuildStatus{
Conditions: []metav1.Condition{
metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: "NotGood",
},
metav1.Condition{
Type: ConditionNotReady,
Status: metav1.ConditionFalse,
Reason: "Good",
},
},
},
expectedOutput: false,
},
}

for tcName, tc := range testCases {
if output := tc.status.IsReady(); output != tc.expectedOutput {
t.Errorf("%s Got %t while expecting %t", tcName, output, tc.expectedOutput)
}
}

}
10 changes: 9 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

74 changes: 73 additions & 1 deletion bundle/manifests/operator.shipwright.io_shipwrightbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,79 @@ spec:
type: string
type: object
status:
description: ShipwrightBuildStatus defines the observed state of Shipwright-Build
description: ShipwrightBuildStatus defines the observed state of ShipwrightBuild
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah pretty sure I heard this come up in the scrum portion of our retrospective meeting today ... need to work around the word wrapping pain with controller-gen by running make generate / make bundle whatever in an ubuntu container

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that said the verify passed, and the bundle-build was skipped, in the latest github action PR verification ... so maybe we don't obsess with this like we have in shipwright-io/builds

@adambkaplan @coreydaley @otaviof please chime in ... WDYT? .... did I in fact miss the discussion here during our last meeting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah pretty sure I heard this come up in the scrum portion of our retrospective meeting today ... need to work around the word wrapping pain with controller-gen by running make generate / make bundle whatever in an ubuntu container

Feels like something we should fix, or invest time on it, sooner rather than later. Can be frustrating having different outcomes locally than on CI, and plus, we may reuse this effort in the Build Controller project itself later on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - @otaviof please write this up in a GitHub issue here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! It's captured on #23, please consider.

properties:
conditions:
description: Conditions holds the latest available observations of
a resource's current state.
items:
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,
type FooStatus struct{ // Represents the observations of a
foo's current state. // Known .status.conditions.type are:
\"Available\", \"Progressing\", and \"Degraded\" // +patchMergeKey=type
\ // +patchStrategy=merge // +listType=map // +listMapKey=type
\ Conditions []metav1.Condition `json:\"conditions,omitempty\"
patchStrategy:\"merge\" patchMergeKey:\"type\" protobuf:\"bytes,1,rep,name=conditions\"`
\n // other fields }"
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: 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
type: object
type: object
served: true
Expand Down
74 changes: 73 additions & 1 deletion config/crd/bases/operator.shipwright.io_shipwrightbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,79 @@ spec:
type: string
type: object
status:
description: ShipwrightBuildStatus defines the observed state of Shipwright-Build
description: ShipwrightBuildStatus defines the observed state of ShipwrightBuild
properties:
conditions:
description: Conditions holds the latest available observations of
a resource's current state.
items:
description: "Condition contains details for one aspect of the current
jkhelil marked this conversation as resolved.
Show resolved Hide resolved
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions. For example,
type FooStatus struct{ // Represents the observations of a
foo's current state. // Known .status.conditions.type are:
\"Available\", \"Progressing\", and \"Degraded\" // +patchMergeKey=type
\ // +patchStrategy=merge // +listType=map // +listMapKey=type
\ Conditions []metav1.Condition `json:\"conditions,omitempty\"
patchStrategy:\"merge\" patchMergeKey:\"type\" protobuf:\"bytes,1,rep,name=conditions\"`
\n // other fields }"
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: 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
type: object
type: object
served: true
Expand Down
46 changes: 40 additions & 6 deletions controllers/shipwrightbuild_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
corev1 "k8s.io/api/core/v1"
crdclientv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -35,6 +36,9 @@ const (
FinalizerAnnotation = "finalizer.operator.shipwright.io"
// defaultTargetNamespace fallback namespace when `.spec.namepace` is not informed.
defaultTargetNamespace = "shipwright-build"

// Ready object is providing service.
ConditionReady = "Ready"
)

// ShipwrightBuildReconciler reconciles a ShipwrightBuild object
Expand Down Expand Up @@ -150,9 +154,22 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
logger.Info("Resource is not found!")
return NoRequeue()
}
logger.Error(err, "Retrieving ShipwrightBuild object from cache")
logger.Error(err, "retrieving ShipwrightBuild object from cache")
return RequeueOnError(err)
}
init := b.Status.Conditions == nil
jkhelil marked this conversation as resolved.
Show resolved Hide resolved
if init {
b.Status.Conditions = make([]metav1.Condition, 0)
apimeta.SetStatusCondition(&b.Status.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionUnknown, // we just started trying to reconcile
jkhelil marked this conversation as resolved.
Show resolved Hide resolved
Reason: "Init",
Message: "Initializing Shipwright Operator",
})
if err := r.Client.Status().Update(ctx, b); err != nil {
return RequeueWithError(err)
}
jkhelil marked this conversation as resolved.
Show resolved Hide resolved
}

// selecting the target namespace based on the CRD information, when not informed using the
// default namespace instead
Expand Down Expand Up @@ -189,7 +206,7 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
Filter(manifestival.Not(manifestival.ByKind("Namespace"))).
Transform(manifestival.InjectNamespace(targetNamespace))
if err != nil {
logger.Error(err, "Transforming manifests, injecting namespace")
logger.Error(err, "transforming manifests, injecting namespace")
return RequeueWithError(err)
}

Expand All @@ -205,12 +222,12 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ

logger.Info("Deleting manifests...")
if err := manifest.Delete(); err != nil {
logger.Error(err, "Deleting manifest's resources")
logger.Error(err, "deleting manifest's resources")
return RequeueWithError(err)
}
logger.Info("Removing finalizers...")
if err := r.unsetFinalizer(ctx, b); err != nil {
logger.Error(err, "Removing the finalizer")
logger.Error(err, "removing the finalizer")
return RequeueWithError(err)
}
logger.Info("All removed!")
Expand All @@ -221,14 +238,31 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// instance with required dependencies
logger.Info("Applying manifest's resources...")
if err := manifest.Apply(); err != nil {
logger.Error(err, "Rolling out manifest's resources")
logger.Error(err, "rolling out manifest's resources")
apimeta.SetStatusCondition(&b.Status.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: "Failed",
Message: fmt.Sprintf("Reconciling ShipwrightBuild failed: %v", err),
})
r.Client.Status().Update(ctx, b)
return RequeueWithError(err)
}
if err := r.setFinalizer(ctx, b); err != nil {
logger.Info(fmt.Sprintf("%#v", b))
logger.Error(err, "Setting the finalizer")
logger.Error(err, "setting the finalizer")
return RequeueWithError(err)
}
apimeta.SetStatusCondition(&b.Status.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionTrue,
Reason: "Success",
Message: "Reconciled ShipwrightBuild successfully",
})
if err := r.Client.Status().Update(ctx, b); err != nil {
logger.Error(err, "updating ShipwrightBuild status")
RequeueWithError(err)
jkhelil marked this conversation as resolved.
Show resolved Hide resolved
}
logger.Info("All done!")
return NoRequeue()
}
Expand Down
Loading