From a50694e371bf0b812a03fe061ddf0928699cefad Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Thu, 18 Jul 2024 22:08:08 +0530 Subject: [PATCH 1/2] enabe logging Signed-off-by: MUzairS15 --- controllers/broker_controller.go | 16 ++++++++++++---- controllers/error.go | 26 +++++++++++++------------- controllers/meshsync_controller.go | 11 +++++++++-- go.mod | 1 + go.sum | 4 ++++ main.go | 4 ++++ pkg/meshsync/meshsync.go | 25 +++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 19 deletions(-) diff --git a/controllers/broker_controller.go b/controllers/broker_controller.go index 8532db3b..e1e9aea8 100644 --- a/controllers/broker_controller.go +++ b/controllers/broker_controller.go @@ -67,7 +67,9 @@ func (r *BrokerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr // Check if Broker controller deployed result, err := r.reconcileBroker(ctx, true, baseResource, req) if err != nil { - return ctrl.Result{}, ErrReconcileBroker(err) + err = ErrReconcileBroker(err) + r.Log.Error(err, "broker reconcilation failed") + return ctrl.Result{}, err } // Check if Broker controller started @@ -79,18 +81,24 @@ func (r *BrokerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr // Get broker endpoint err = brokerpackage.GetEndpoint(ctx, baseResource, r.Clientset, r.KubeConfig.Host) if err != nil { - return ctrl.Result{}, ErrGetEndpoint(err) + err = ErrGetEndpoint(err) + r.Log.Error(err, "unable to get the broker endpoint") + return ctrl.Result{}, err } // Patch the broker resource patch, err := utils.Marshal(baseResource) if err != nil { - return ctrl.Result{}, ErrUpdateResource(err) + err = ErrUpdateResource(err) + r.Log.Error(err, "unable to update broker resource") + return ctrl.Result{}, err } err = r.Status().Patch(ctx, baseResource, client.RawPatch(types.MergePatchType, []byte(patch))) if err != nil { - return ctrl.Result{}, ErrUpdateResource(err) + err = ErrUpdateResource(err) + r.Log.Error(err, "unable to update broker resource") + return ctrl.Result{}, err } return result, nil diff --git a/controllers/error.go b/controllers/error.go index 571c4ce2..0dac72db 100644 --- a/controllers/error.go +++ b/controllers/error.go @@ -39,53 +39,53 @@ const ( // Error definitions func ErrGetMeshsync(err error) error { - return errors.New(ErrGetMeshsyncCode + ":" + "Unable to get meshsync resource") + return errors.New(ErrGetMeshsyncCode + ":" + "Unable to get meshsync resource" + err.Error()) } func ErrCreateMeshsync(err error) error { - return errors.New(ErrCreateMeshsyncCode + ":" + "Unable to create meshsync controller") + return errors.New(ErrCreateMeshsyncCode + ":" + "Unable to create meshsync controller" + err.Error()) } func ErrDeleteMeshsync(err error) error { - return errors.New(ErrDeleteMeshsyncCode + ":" + "Unable to delete meshsync controller") + return errors.New(ErrDeleteMeshsyncCode + ":" + "Unable to delete meshsync controller" + err.Error()) } func ErrReconcileMeshsync(err error) error { - return errors.New(ErrReconcileMeshsyncCode + ":" + "Error during meshsync resource reconciliation") + return errors.New(ErrReconcileMeshsyncCode + ":" + "Error during meshsync resource reconciliation" + err.Error()) } func ErrGetBroker(err error) error { - return errors.New(ErrGetBrokerCode + ":" + "Broker resource not found") + return errors.New(ErrGetBrokerCode + ":" + "Broker resource not found" + err.Error()) } func ErrCreateBroker(err error) error { - return errors.New(ErrCreateBrokerCode + ":" + "Unable to create broker controller") + return errors.New(ErrCreateBrokerCode + ":" + "Unable to create broker controller" + err.Error()) } func ErrDeleteBroker(err error) error { - return errors.New(ErrDeleteBrokerCode + ":" + "Unable to delete broker controller") + return errors.New(ErrDeleteBrokerCode + ":" + "Unable to delete broker controller" + err.Error()) } func ErrReconcileBroker(err error) error { - return errors.New(ErrReconcileBrokerCode + ":" + "Error during broker resource reconciliation") + return errors.New(ErrReconcileBrokerCode + ":" + "Error during broker resource reconciliation" + err.Error()) } func ErrReconcileCR(err error) error { - return errors.New(ErrReconcileCRCode + ":" + "Error during custom resource reconciliation") + return errors.New(ErrReconcileCRCode + ":" + "Error during custom resource reconciliation" + err.Error()) } func ErrCheckHealth(err error) error { - return errors.New(ErrCheckHealthCode + ":" + "Error during health check") + return errors.New(ErrCheckHealthCode + ":" + "Error during health check" + err.Error()) } func ErrGetEndpoint(err error) error { - return errors.New(ErrGetEndpointCode + ":" + "Unable to get endpoint") + return errors.New(ErrGetEndpointCode + ":" + "Unable to get endpoint" + err.Error()) } func ErrUpdateResource(err error) error { - return errors.New(ErrUpdateResourceCode + ":" + "Unable to update resource") + return errors.New(ErrUpdateResourceCode + ":" + "Unable to update resource" + err.Error()) } func ErrMarshal(err error) error { - return errors.New(ErrMarshalCode + ":" + "Error during marshaling") + return errors.New(ErrMarshalCode + ":" + "Error during marshaling" + err.Error()) } diff --git a/controllers/meshsync_controller.go b/controllers/meshsync_controller.go index 00a7ccbc..9ecb625a 100644 --- a/controllers/meshsync_controller.go +++ b/controllers/meshsync_controller.go @@ -68,24 +68,31 @@ func (r *MeshSyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c // Get broker configuration err = r.reconcileBrokerConfig(ctx, baseResource) if err != nil { + r.Log.Error(err, "meshsync reconcilation failed") return ctrl.Result{}, ErrReconcileMeshsync(err) } // Check if Meshsync controller running result, err := r.reconcileMeshsync(ctx, true, baseResource, req) if err != nil { + err = ErrReconcileMeshsync(err) + r.Log.Error(err, "meshsync reconcilation failed") return ctrl.Result{}, ErrReconcileMeshsync(err) } // Patch the meshsync resource patch, err := utils.Marshal(baseResource) if err != nil { - return ctrl.Result{}, ErrUpdateResource(err) + err = ErrUpdateResource(err) + r.Log.Error(err, "unable to update meshsync resource") + return ctrl.Result{}, err } err = r.Status().Patch(ctx, baseResource, client.RawPatch(types.MergePatchType, []byte(patch))) if err != nil { - return ctrl.Result{}, ErrUpdateResource(err) + err = ErrUpdateResource(err) + r.Log.Error(err, "unable to update meshsync resource") + return ctrl.Result{}, err } return result, nil diff --git a/go.mod b/go.mod index aac54368..93c78f6e 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/go-logr/logr v1.3.0 github.com/onsi/ginkgo/v2 v2.13.0 github.com/onsi/gomega v1.27.10 + github.com/sirupsen/logrus v1.8.1 k8s.io/api v0.26.1 k8s.io/apiextensions-apiserver v0.26.1 k8s.io/apimachinery v0.26.1 diff --git a/go.sum b/go.sum index 28975c2c..f87b831d 100644 --- a/go.sum +++ b/go.sum @@ -135,12 +135,15 @@ github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJf github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= @@ -199,6 +202,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 39feae01..06357f7d 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/log/zap" mesheryv1alpha1 "github.com/layer5io/meshery-operator/api/v1alpha1" "github.com/layer5io/meshery-operator/controllers" @@ -55,6 +56,7 @@ func main() { "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") flag.Parse() + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zap.Options{}))) opID := uuid.NewUUID() mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ @@ -66,12 +68,14 @@ 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/meshsync/meshsync.go b/pkg/meshsync/meshsync.go index 56e3fb10..67336d4d 100644 --- a/pkg/meshsync/meshsync.go +++ b/pkg/meshsync/meshsync.go @@ -17,10 +17,15 @@ limitations under the License. package meshsync import ( + "context" + mesheryv1alpha1 "github.com/layer5io/meshery-operator/api/v1alpha1" v1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/kubernetes" ) const ( @@ -47,3 +52,23 @@ func getServerObject(namespace, name string, replicas int32, url string) Object obj.Spec.Template.Spec.Containers[0].Env[0].Value = url // Set broker endpoint return obj } + +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 + } + + if obj.Status.Replicas != obj.Status.ReadyReplicas { + if len(obj.Status.Conditions) > 0 { + return err + } + return err + } + + if len(obj.Status.Conditions) > 0 && (obj.Status.Conditions[0].Status == corev1.ConditionFalse || obj.Status.Conditions[0].Status == corev1.ConditionUnknown) { + return err + } + + return nil +} From 1289a237118d331a83acf747fb3743d8813b6240 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 19 Jul 2024 00:20:29 +0530 Subject: [PATCH 2/2] ensure controllers are redeployed by operator when deleted Signed-off-by: MUzairS15 --- controllers/broker_controller.go | 2 ++ controllers/meshsync_controller.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/controllers/broker_controller.go b/controllers/broker_controller.go index e1e9aea8..edf465e9 100644 --- a/controllers/broker_controller.go +++ b/controllers/broker_controller.go @@ -21,6 +21,7 @@ import ( "github.com/go-logr/logr" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -107,6 +108,7 @@ func (r *BrokerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr func (r *BrokerReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&mesheryv1alpha1.Broker{}). + Owns(&appsv1.StatefulSet{}). Complete(r) } diff --git a/controllers/meshsync_controller.go b/controllers/meshsync_controller.go index 9ecb625a..aadbc1ef 100644 --- a/controllers/meshsync_controller.go +++ b/controllers/meshsync_controller.go @@ -21,6 +21,7 @@ import ( "github.com/go-logr/logr" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -101,6 +102,7 @@ func (r *MeshSyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c func (r *MeshSyncReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&mesheryv1alpha1.MeshSync{}). + Owns(&appsv1.Deployment{}). Complete(r) }