diff --git a/controllers/broker_controller.go b/controllers/broker_controller.go index edf465e..a160aa6 100644 --- a/controllers/broker_controller.go +++ b/controllers/broker_controller.go @@ -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" diff --git a/controllers/meshsync_controller.go b/controllers/meshsync_controller.go index aadbc1e..03c7a1e 100644 --- a/controllers/meshsync_controller.go +++ b/controllers/meshsync_controller.go @@ -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" @@ -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" @@ -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 { diff --git a/main.go b/main.go index 06357f7..5189627 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/pkg/broker/error.go b/pkg/broker/error.go index f61b861..0c54a8c 100644 --- a/pkg/broker/error.go +++ b/pkg/broker/error.go @@ -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) } diff --git a/pkg/meshsync/error.go b/pkg/meshsync/error.go new file mode 100644 index 0000000..9f2747b --- /dev/null +++ b/pkg/meshsync/error.go @@ -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) +} diff --git a/pkg/meshsync/meshsync.go b/pkg/meshsync/meshsync.go index 67336d4..00589f2 100644 --- a/pkg/meshsync/meshsync.go +++ b/pkg/meshsync/meshsync.go @@ -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