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

added simple status-conditions to the existing reconcile #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ limitations under the License.
*/

// Package v1alpha1 contains API Schema definitions for the resource-management v1alpha1 API group
//+kubebuilder:object:generate=true
//+groupName=resource-management.tikalk.com
// +kubebuilder:object:generate=true
// +groupName=resource-management.tikalk.com
package v1alpha1

import (
Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha1/resourcemanager_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type ExpiryCondition struct {
type ResourceManagerStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
// Conditions is the list of status condition updates
Conditions []metav1.Condition `json:"conditions"`
}

//+kubebuilder:object:root=true
Expand Down
9 changes: 8 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.

40 changes: 33 additions & 7 deletions controllers/resourcemanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ package controllers
import (
"context"
"fmt"
"time"

resourcemanagmentv1alpha1 "github.com/tikalk/resource-manager/api/v1alpha1"
appsv1 "k8s.io/api/apps/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"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

resourcemanagmentv1alpha1 "github.com/tikalk/resource-manager/api/v1alpha1"
)

// ResourceManagerReconciler reconciles a ResourceManager object
Expand All @@ -54,10 +57,25 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
name := req.NamespacedName.String()

// your logic here
// Get the resourcemanager Operator object.
resourceManagerObj := &resourcemanagmentv1alpha1.ResourceManager{}
err := r.Get(ctx, req.NamespacedName, resourceManagerObj)
if err != nil {
l.Error(err, fmt.Sprintf("Failed reconcile obj %s", name))
// If one is not found, log a message
if err != nil && errors.IsNotFound(err) {
l.Error(err, fmt.Sprintf("Failed reconcile obj %s , Operator resource not found", name))

// If the Operator is unable to access its custom resource (for any reason besides a simple IsNotFound error)
// set the OperatorDegraded condition to True with the reason OperatorResourceNotAvailable.
} else if err != nil {
l.Error(err, "Error getting operator resource object")
meta.SetStatusCondition(&resourceManagerObj.Status.Conditions, metav1.Condition{
Type: "OperatorDegraded",
Status: metav1.ConditionTrue,
Reason: "OperatorResourceNotAvailable",
LastTransitionTime: metav1.NewTime(time.Now()),
Message: fmt.Sprintf("unable to get operator custom resource: %s", err.Error()),
})
return ctrl.Result{}, utilerrors.NewAggregate([]error{err, r.Status().Update(ctx, resourceManagerObj)})
}

// pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
Expand All @@ -72,9 +90,17 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
err = r.Client.List(ctx, deploy, &client.ListOptions{})
fmt.Printf("There are %d deployments in the cluster\n", len(deploy.Items))

// Finally, if the Reconcile() function has completed with no critical errors
// set the OperatorDegraded condition to False with the reason operatorv1alpha1.ReasonSucceeded,
l.Info(fmt.Sprintf("Done reconcile 12-- obj %s", name))

return ctrl.Result{}, nil
meta.SetStatusCondition(&resourceManagerObj.Status.Conditions, metav1.Condition{
Type: "OperatorDegraded",
Status: metav1.ConditionFalse,
Reason: "list deployments succeeded",
LastTransitionTime: metav1.NewTime(time.Now()),
Message: "operator successfully reconciling",
})
return ctrl.Result{}, utilerrors.NewAggregate([]error{err, r.Status().Update(ctx, resourceManagerObj)})
}

// SetupWithManager sets up the controller with the Manager.
Expand Down