Skip to content

Commit

Permalink
Add logic to add/update status
Browse files Browse the repository at this point in the history
  • Loading branch information
christinaexyou committed Jan 13, 2025
1 parent 9109135 commit 1dbc770
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 63 deletions.
54 changes: 41 additions & 13 deletions api/gorch/v1alpha1/guardrailsorchestrator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"time"

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

Expand Down Expand Up @@ -70,23 +72,49 @@ type GuardrailsOrchestratorSpec struct {
TLS string `json:"tls,omitempty"`
}

// const (
// OrchestratorConditionReconciled ConditionType = "Reconciled"
// // OrchestratorConditionReady represents the fact that the orchestrator's components are ready
// OrchestratorConditionReady ConditionType = "Ready"
// OrchestratorDeploymentNotReady ConditionReason = "DeploymentNotReady"
// OrchestratorInferenceServiceNotReady ConditionReason = "InferenceServiceNotReady"
// OrchestratorRouteNotAdmitted ConditionReason = "RouteNotAdmitted"
// OrchestratorHealthy ConditionReason = "Healthy"
// OrchestratorReadinessCheckFailed ConditionReason = "ReadinessCheckFailed"
// )

type GuardrailsOrchestratorStatus struct {
Conditions []metav1.Condition `json:"conditions"`
Conditions []GuardrailsOrchestratorCondition `json:"conditions,omitempty"`
}

var testTime *time.Time

func (in *GuardrailsOrchestratorStatus) SetConditions(condition GuardrailsOrchestratorCondition) {
var now time.Time
if testTime == nil {
now = time.Now()
} else {
now = *testTime
}

lastTransitionTime := metav1.NewTime(now.Truncate(time.Second))

for i, prevCondition := range in.Conditions {
if prevCondition.Type == condition.Type {
if prevCondition.Status != condition.Status {
condition.LastTransitionTime = lastTransitionTime
} else {
condition.LastTransitionTime = prevCondition.LastTransitionTime
}
in.Conditions[i] = condition
return
}
}
condition.LastTransitionTime = lastTransitionTime
in.Conditions = append(in.Conditions, condition)
}

type GuardrailsOrchestratorCondition struct {
Type string `json:"type"`
Status metav1.ConditionStatus `json:"status"`
// +optional
Reason string `json:"reason,omitmempty"`
// +optional
Message string `json:"message,omitempty"`
// +optional
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// GuardrailsOrchestrator is the Schema for the guardrailsorchestrators API.
type GuardrailsOrchestrator struct {
Expand Down
19 changes: 17 additions & 2 deletions api/gorch/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ spec:
type: string
port:
type: integer
protocol:
type: string
tls:
properties:
ca_cert_path:
Expand All @@ -73,7 +71,6 @@ spec:
required:
- hostname
- port
- protocol
type: object
required:
- provider
Expand All @@ -94,8 +91,6 @@ spec:
type: string
port:
type: integer
protocol:
type: string
tls:
properties:
ca_cert_path:
Expand All @@ -115,7 +110,6 @@ spec:
required:
- hostname
- port
- protocol
type: object
type:
type: string
Expand All @@ -137,8 +131,6 @@ spec:
type: string
port:
type: integer
protocol:
type: string
tls:
properties:
ca_cert_path:
Expand All @@ -158,7 +150,6 @@ spec:
required:
- hostname
- port
- protocol
type: object
required:
- provider
Expand All @@ -177,21 +168,27 @@ spec:
- replicas
type: object
status:
description: GuardrailsOrchestratorStatus defines the observed state of
GuardrailsOrchestrator.
properties:
condition:
description: |-
INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Important: Run "make" to regenerate code after modifying this file
type: string
ready:
type: boolean
required:
- ready
conditions:
items:
properties:
lastTransitionTime:
format: date-time
type: string
message:
type: string
reason:
type: string
status:
type: string
type:
type: string
required:
- status
- type
type: object
type: array
type: object
type: object
served: true
storage: true
subresources:
status: {}
41 changes: 16 additions & 25 deletions controllers/gorch/guardrailsorchestrator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"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 @@ -38,11 +37,6 @@ import (
gorchv1alpha1 "github.com/trustyai-explainability/trustyai-service-operator/api/gorch/v1alpha1"
)

const (
typeDegradedOrchestrator = "Degraded"
typeAvailableOrchestrator = "Available"
)

// GuardrailsOrchestratorReconciler reconciles a GuardrailsOrchestrator object
type GuardrailsOrchestratorReconciler struct {
client.Client
Expand Down Expand Up @@ -110,9 +104,9 @@ func (r *GuardrailsOrchestratorReconciler) Reconcile(ctx context.Context, req ct
if controllerutil.ContainsFinalizer(orchestrator, finalizerName) {
log.Info("Performing Finalizer Operations for GuardrailsOrchestrator before delete CR")

meta.SetStatusCondition(&orchestrator.Status.Conditions, metav1.Condition{Type: typeDegradedOrchestrator,
Status: metav1.ConditionUnknown, Reason: "Finalizing",
Message: fmt.Sprintf("Performing finalizer operations for the custom resource: %s", orchestrator.Name)})
condition := gorchv1alpha1.GuardrailsOrchestratorCondition{Type: typeDegradedOrchestrator, Status: metav1.ConditionUnknown, Reason: "Finalizing",
Message: fmt.Sprintf("Performing finalizer operations for the custom resource: %s", orchestrator.Name)}
orchestrator.Status.SetConditions(condition)

if err := r.Status().Update(ctx, orchestrator); err != nil {
log.Error(err, "Failed to update GuardrailsOrchestrator status")
Expand All @@ -128,10 +122,11 @@ func (r *GuardrailsOrchestratorReconciler) Reconcile(ctx context.Context, req ct
log.Error(err, "Failed to re-fetch GuardrailsOrchestrator")
return ctrl.Result{}, err
}
condition.Status = metav1.ConditionTrue
condition.Reason = "Finalizing"
condition.Message = fmt.Sprintf("Finalizer operations for custom resource %s were sucessfully accomplished", orchestrator.Name)

meta.SetStatusCondition(&orchestrator.Status.Conditions, metav1.Condition{Type: typeDegradedOrchestrator,
Status: metav1.ConditionTrue, Reason: "Finalizing",
Message: fmt.Sprintf("Finalizer operations for custom resource %s were sucessfully accomplished", orchestratorName)})
orchestrator.Status.SetConditions(condition)

if err := r.Status().Update(ctx, orchestrator); err != nil {
log.Error(err, "Failed to update GuardrailsOrchestrator status")
Expand Down Expand Up @@ -170,16 +165,17 @@ func (r *GuardrailsOrchestratorReconciler) Reconcile(ctx context.Context, req ct
existingDeployment := &appsv1.Deployment{}
err = r.Get(ctx, types.NamespacedName{Name: orchestratorName, Namespace: orchestrator.Namespace}, existingDeployment)
if err != nil && errors.IsNotFound(err) {
// Create a new deployment
deployment := r.createDeployment(ctx, orchestrator)
log.Info("Creating a new Deployment", "Deployment.Namespace", deployment.Namespace, "Deployment.Name", deployment.Name)
err = r.Create(ctx, deployment)
if err != nil {
log.Error(err, "Failed to create new Deployment", "Deployment.Namespace", deployment.Namespace, "Deployment.Name", deployment.Name)
return ctrl.Result{}, err
return ctrl.Result{}, r.statusUpdateError(ctx, orchestrator, r.setStatusFailed("FailedToCreateDeployment"), fmt.Errorf("failed to get %v Deployment resource, err %w", orchestratorName, err))
}
} else if err != nil {
log.Error(err, "Failed to get Deployment")
return ctrl.Result{}, err
return ctrl.Result{}, r.statusUpdateError(ctx, orchestrator, r.setStatusFailed("FailedToGetDeployment"), fmt.Errorf("failed to get %v Deployment resource, err %w", orchestratorName, err))
}

existingService := &corev1.Service{}
Expand All @@ -191,11 +187,11 @@ func (r *GuardrailsOrchestratorReconciler) Reconcile(ctx context.Context, req ct
err = r.Create(ctx, service)
if err != nil {
log.Error(err, "Failed to create new Service", "Service.Namespace", service.Namespace, "Service.Name", service.Name)
return ctrl.Result{}, err
return ctrl.Result{}, r.statusUpdateError(ctx, orchestrator, r.setStatusFailed("FailedToCreateService"), fmt.Errorf("failed to get %v Service resource, err %w", orchestratorName, err))
}
} else if err != nil {
log.Error(err, "Failed to get Service")
return ctrl.Result{}, err
return ctrl.Result{}, r.statusUpdateError(ctx, orchestrator, r.setStatusFailed("FailedToGetService"), fmt.Errorf("failed to get %v Service resource, err %w", orchestratorName, err))
}

existingRoute := &routev1.Route{}
Expand All @@ -207,21 +203,16 @@ func (r *GuardrailsOrchestratorReconciler) Reconcile(ctx context.Context, req ct
err = r.Create(ctx, route)
if err != nil {
log.Error(err, "Failed to create new Route", "Route.Namespace", route.Namespace, "Route.Name", route.Name)
return ctrl.Result{}, err
return ctrl.Result{}, r.statusUpdateError(ctx, orchestrator, r.setStatusFailed("FailedToCreateRoute"), fmt.Errorf("failed to get %v Route resource, err %w", orchestratorName, err))
}
} else if err != nil {
log.Error(err, "Failed to get Route")
return ctrl.Result{}, err
return ctrl.Result{}, r.statusUpdateError(ctx, orchestrator, r.setStatusFailed("FailedToGetRoute"), fmt.Errorf("failed to get %v Route resource, err %w", orchestratorName, err))
}

meta.SetStatusCondition(&orchestrator.Status.Conditions, metav1.Condition{Type: typeAvailableOrchestrator, Status: metav1.ConditionTrue,
Reason: "Available", Message: "GuardrailsOrchestrator is available"})
if err := r.Status().Update(ctx, orchestrator); err != nil {
log.Error(err, "Failed to update GuardrailsOrchestrator status")
return ctrl.Result{}, err
}
updateStatusConditions(orchestrator, r.Client, statusReady())

return ctrl.Result{}, nil
return ctrl.Result{}, r.Get(ctx, req.NamespacedName, orchestrator)
}

func (r *GuardrailsOrchestratorReconciler) deleteRoute(ctx context.Context, orchestrator gorchv1alpha1.GuardrailsOrchestrator) (err error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
routev1 "github.com/openshift/api/route/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

// "golang.org/x/sys/unix"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

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

Expand Down
Loading

0 comments on commit 1dbc770

Please sign in to comment.