Skip to content

Commit

Permalink
Merge pull request #560 from MUzairS15/MUzairS15/operator/logs
Browse files Browse the repository at this point in the history
Operator Enhancement
  • Loading branch information
Mohd Uzair authored Jul 22, 2024
2 parents 68bae1e + 8566eac commit 5a3ddc8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 19 deletions.
18 changes: 14 additions & 4 deletions controllers/broker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -67,7 +68,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
Expand All @@ -79,18 +82,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
Expand All @@ -99,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)
}

Expand Down
26 changes: 13 additions & 13 deletions controllers/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
13 changes: 11 additions & 2 deletions controllers/meshsync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -68,24 +69,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
Expand All @@ -94,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)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/meshsync/meshsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}

0 comments on commit 5a3ddc8

Please sign in to comment.