Skip to content

Commit

Permalink
Merge pull request #562 from MUzairS15/MUzairS15/add-missing-commit
Browse files Browse the repository at this point in the history
add health check
  • Loading branch information
Mohd Uzair authored Jul 22, 2024
2 parents 5a3ddc8 + 7eb9a90 commit 9c14d19
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion controllers/broker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"context"

"github.com/go-logr/logr"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down
8 changes: 7 additions & 1 deletion controllers/meshsync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"context"

"github.com/go-logr/logr"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand All @@ -31,6 +31,7 @@ import (

mesheryv1alpha1 "github.com/layer5io/meshery-operator/api/v1alpha1"
brokerpackage "github.com/layer5io/meshery-operator/pkg/broker"
"github.com/layer5io/meshery-operator/pkg/meshsync"
meshsyncpackage "github.com/layer5io/meshery-operator/pkg/meshsync"
"github.com/layer5io/meshery-operator/pkg/utils"
kubeerror "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -81,6 +82,11 @@ func (r *MeshSyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, ErrReconcileMeshsync(err)
}

err = meshsync.CheckHealth(ctx, baseResource, r.Clientset)
if err != nil {
return ctrl.Result{Requeue: true}, ErrCheckHealth(err)
}

// Patch the meshsync resource
patch, err := utils.Marshal(baseResource)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ func main() {
LeaderElectionNamespace: namespace,
})
if err != nil {
fmt.Println("RERER")
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}

clientset, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
fmt.Println("RERER-222")
setupLog.Error(err, "unable to initialize clientset")
os.Exit(1)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/broker/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ const (
)

func ErrGettingResource(err error) error {
return errors.New(ErrGettingResourceCode + ":" + "Unable to get resource")
return errors.New(ErrGettingResourceCode + ":" + "Unable to get resource" + err.Error())
}

func ErrGettingEndpoint(err error) error {
return errors.New(ErrGettingEndpointCode + ":" + "Unable to get endpoint")
return errors.New(ErrGettingEndpointCode + ":" + "Unable to get endpoint" + err.Error())
}

func ErrReplicasNotReady(reason string) error {
return errors.New(ErrReplicasNotReadyCode + ":" + "The replicas are not ready")
return errors.New(ErrReplicasNotReadyCode + ":" + "The replicas are not ready" + reason)
}

func ErrConditionFalse(reason string) error {
return errors.New(ErrConditionFalseCode + ":" + "The condition is false")
return errors.New(ErrConditionFalseCode + ":" + "The condition is false" + reason)
}
44 changes: 44 additions & 0 deletions pkg/meshsync/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2023 Layer5, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package meshsync

import (
"errors"
)

const (
ErrGettingResourceCode = "1013"
ErrReplicasNotReadyCode = "1014"
ErrConditionFalseCode = "1015"
ErrGettingEndpointCode = "1016"
)

func ErrGettingResource(err error) error {
return errors.New(ErrGettingResourceCode + ":" + "Unable to get resource" + err.Error())
}

func ErrGettingEndpoint(err error) error {
return errors.New(ErrGettingEndpointCode + ":" + "Unable to get endpoint" + err.Error())
}

func ErrReplicasNotReady(reason string) error {
return errors.New(ErrReplicasNotReadyCode + ":" + "The replicas are not ready" + reason)
}

func ErrConditionFalse(reason string) error {
return errors.New(ErrConditionFalseCode + ":" + "The condition is false" + reason)
}
8 changes: 4 additions & 4 deletions pkg/meshsync/meshsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ func getServerObject(namespace, name string, replicas int32, url string) Object
func CheckHealth(ctx context.Context, m *mesheryv1alpha1.MeshSync, client *kubernetes.Clientset) error {
obj, err := client.AppsV1().Deployments(m.ObjectMeta.Namespace).Get(ctx, m.ObjectMeta.Name, metav1.GetOptions{})
if err != nil {
return err
return ErrGettingResource(err)
}

if obj.Status.Replicas != obj.Status.ReadyReplicas {
if len(obj.Status.Conditions) > 0 {
return err
return ErrReplicasNotReady(obj.Status.Conditions[0].Reason)
}
return err
return ErrReplicasNotReady("Condition Unknown")
}

if len(obj.Status.Conditions) > 0 && (obj.Status.Conditions[0].Status == corev1.ConditionFalse || obj.Status.Conditions[0].Status == corev1.ConditionUnknown) {
return err
return ErrConditionFalse(obj.Status.Conditions[0].Reason)
}

return nil
Expand Down

0 comments on commit 9c14d19

Please sign in to comment.