Skip to content

Commit

Permalink
add shipwright build status
Browse files Browse the repository at this point in the history
  • Loading branch information
jkhelil committed Aug 3, 2021
1 parent 2b56144 commit 71f5fa4
Show file tree
Hide file tree
Showing 7 changed files with 848 additions and 27 deletions.
16 changes: 16 additions & 0 deletions api/v1alpha1/shipwrightbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package v1alpha1

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

buildcorev1alpha1 "github.com/shipwright-io/build/pkg/apis/core/v1alpha1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand All @@ -24,6 +26,9 @@ type ShipwrightBuildSpec struct {
type ShipwrightBuildStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Conditions holds the latest available observations of a resource's current state.
Conditions buildcorev1alpha1.Conditions `json:"conditions,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down Expand Up @@ -51,3 +56,14 @@ type ShipwrightBuildList struct {
func init() {
SchemeBuilder.Register(&ShipwrightBuild{}, &ShipwrightBuildList{})
}

// SetCondition updates a list of conditions with the provided condition
func (sbs *ShipwrightBuildStatus) SetCondition(condition *buildcorev1alpha1.Condition) {
for i, c := range sbs.Conditions {
if c.Type == condition.Type {
sbs.Conditions[i] = *condition
return
}
}
sbs.Conditions = append(sbs.Conditions, *condition)
}
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.

4 changes: 2 additions & 2 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/shipwright-io/build/pkg/ctxlog"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -21,6 +20,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/shipwright-io/build/pkg/ctxlog"
operatorv1alpha1 "github.com/shipwright-io/operator/api/v1alpha1"
"github.com/shipwright-io/operator/controllers"
// +kubebuilder:scaffold:imports
Expand Down Expand Up @@ -99,7 +99,7 @@ func main() {
os.Exit(1)
}

ctxlog.Info(ctx, "starting manager")
ctxlog.Info(ctx, "Starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
ctxlog.Error(ctx, err, "problem running manager")
os.Exit(1)
Expand Down
41 changes: 41 additions & 0 deletions config/crd/bases/operator.shipwright.io_shipwrightbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,47 @@ spec:
type: object
status:
description: ShipwrightBuildStatus defines the observed state of ShipwrightBuild
properties:
conditions:
description: Conditions
items:
description: 'Condition defines a readiness condition for a Knative
resource. See: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties'
properties:
lastTransitionTime:
description: LastTransitionTime is the last time the condition
transitioned from one status to another. We use VolatileTime
in place of metav1.Time to exclude this from creating equality.Semantic
differences (all other things held constant).
properties:
inner:
format: date-time
type: string
required:
- inner
type: object
message:
description: A human readable message indicating details about
the transition.
type: string
reason:
description: The reason for the condition's last transition.
type: string
severity:
description: Severity with which to treat failures of this type
of condition. When this is not specified, it defaults to Error.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: Type of condition.
type: string
required:
- status
- type
type: object
type: array
type: object
type: object
served: true
Expand Down
47 changes: 36 additions & 11 deletions controllers/shipwrightbuild_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@ import (
"os"
"path/filepath"

mfc "github.com/manifestival/controller-runtime-client"
"github.com/manifestival/manifestival"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

buildcorev1alpha1 "github.com/shipwright-io/build/pkg/apis/core/v1alpha1"
"github.com/shipwright-io/build/pkg/ctxlog"
"github.com/shipwright-io/operator/api/v1alpha1"

mfc "github.com/manifestival/controller-runtime-client"
"github.com/manifestival/manifestival"
)

const (
namespace string = "namespace"
name string = "name"
)

// ShipwrightBuildReconciler reconciles a ShipwrightBuild object
type ShipwrightBuildReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Manifest manifestival.Manifest
}
Expand Down Expand Up @@ -62,14 +68,12 @@ type ShipwrightBuildReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("shipwrightbuild", req.NamespacedName)

build := &v1alpha1.ShipwrightBuild{}
// Remove Namespaces from the manifest - cluster admins must provision the shipwright-build namespace
manifest := r.Manifest.Filter(manifestival.Not(manifestival.ByKind("Namespace")))
err := r.Client.Get(ctx, req.NamespacedName, build)
if errors.IsNotFound(err) {
log.Info("object not found, deleting Shipwright Build from the cluster")
ctxlog.Info(ctx, "Object not found, deleting Shipwright Build from the cluster", namespace, req.Namespace, name, req.NamespacedName)
err = manifest.Delete()
if err != nil {
return ctrl.Result{}, err
Expand All @@ -79,9 +83,31 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
if err != nil {
return ctrl.Result{}, err
}
log.Info("reconciling ShipwrightBuild with manifest")

ctxlog.Info(ctx, "Reconciling ShipwrightBuild with manifest", namespace, req.Namespace, name, req.NamespacedName)
err = manifest.Apply()
if err != nil {
build.Status.SetCondition(&buildcorev1alpha1.Condition{
Type: buildcorev1alpha1.ConditionReady,
Status: corev1.ConditionFalse,
Reason: "Failed",
Message: "Reconciling ShipwrightBuild failed",
})
ctxlog.Debug(ctx, "Updating shipwrightbuild status", namespace, req.Namespace, name, req.NamespacedName)
if err = r.Client.Status().Update(ctx, build); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, err
}

build.Status.SetCondition(&buildcorev1alpha1.Condition{
Type: buildcorev1alpha1.ConditionReady,
Status: corev1.ConditionTrue,
Reason: "Running",
Message: "Reconciling ShipwrightBuild succeeded",
})
ctxlog.Debug(ctx, "Updating shipwrightbuild status", namespace, req.Namespace, name, req.NamespacedName)
if err = r.Client.Status().Update(ctx, build); err != nil {
return ctrl.Result{}, err
}

Expand All @@ -91,14 +117,13 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// SetupWithManager sets up the controller with the Manager.
func (r *ShipwrightBuildReconciler) SetupWithManager(mgr ctrl.Manager) error {
mfclient := mfc.NewClient(mgr.GetClient())
mflogger := mgr.GetLogger().WithName("manifestival")
dataPath, exists := os.LookupEnv("KO_DATA_PATH")
if !exists {
return fmt.Errorf("KO_DATA_PATH is not set - cannot set up reconciler")
}
buildManifest := filepath.Join(dataPath, "release.yaml")

mf, err := manifestival.NewManifest(buildManifest, manifestival.UseClient(mfclient), manifestival.UseLogger(mflogger))
mf, err := manifestival.NewManifest(buildManifest, manifestival.UseClient(mfclient), manifestival.UseLogger(ctxlog.NewLogger("manifestival")))
if err != nil {
return err
}
Expand Down
20 changes: 7 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ module github.com/shipwright-io/operator
go 1.15

require (
github.com/go-logr/logr v0.4.0
github.com/go-logr/logr v0.4.0 // indirect
github.com/manifestival/controller-runtime-client v0.4.0
github.com/manifestival/manifestival v0.7.0
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
k8s.io/api v0.19.7
k8s.io/apiextensions-apiserver v0.19.2
k8s.io/apimachinery v0.19.7
k8s.io/client-go v0.19.7
github.com/onsi/gomega v1.10.3
github.com/shipwright-io/build v0.5.1
k8s.io/api v0.20.2
k8s.io/apiextensions-apiserver v0.19.7
k8s.io/apimachinery v0.20.2
k8s.io/client-go v0.20.2
sigs.k8s.io/controller-runtime v0.7.2
)

// github.com/manifestival/controller-runtime-client v0.4.0 depends on
// github.com/manifestival/manifestival v0.6.0, which in turn depends on
// k8s.io/client-go v0.15.7. This is not compatible with v0.19.7 and leads to transitive
// dependency hell. Pinning k8s.io/client-go to the version shared by controller-runtime and
// the latest version of manifestival
replace k8s.io/client-go => k8s.io/client-go v0.19.7
Loading

0 comments on commit 71f5fa4

Please sign in to comment.