Skip to content

Commit

Permalink
Merge pull request #187 from meshery/nithish/refactor/controllers
Browse files Browse the repository at this point in the history
[Refactor] Controller Interface implementation of status logic
  • Loading branch information
humblenginr authored Jun 30, 2022
2 parents a90eeab + 10bd173 commit b21e073
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 48 deletions.
44 changes: 36 additions & 8 deletions models/controllers/meshery_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
kubeerror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubectl/pkg/polymorphichelpers"
)

type mesheryBroker struct {
Expand All @@ -29,17 +31,43 @@ func (mb *mesheryBroker) GetName() string {
}

func (mb *mesheryBroker) GetStatus() MesheryControllerStatus {
broker, err := mb.kclient.KubeClient.AppsV1().Deployments("meshery").Get(context.TODO(), "meshery-broker", metav1.GetOptions{})
// if the deployment is not found, then it is NotDeployed
if err != nil && !kubeerror.IsNotFound(err) {
operatorClient, err := opClient.New(&mb.kclient.RestConfig)
// TODO: Confirm if the presence of operator is needed to use the operator client sdk
broker, err := operatorClient.CoreV1Alpha1().Brokers("meshery").Get(context.TODO(), "meshery-broker", metav1.GetOptions{})
if err == nil {
if broker.Status.Endpoint.External != "" {
mb.status = Deployed
return mb.status
}
mb.status = NotDeployed
return mb.status
} else {
if kubeerror.IsNotFound(err) {
mb.status = NotDeployed
return mb.status
}
// when operatorClient is not able to get meshesry-broker, we try again with kubernetes client as a fallback
broker, err := mb.kclient.DynamicKubeClient.Resource(schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}).Namespace("meshery").Get(context.TODO(), "meshery-broker", metav1.GetOptions{})
if err != nil {
// if the resource is not found, then it is NotDeployed
if kubeerror.IsNotFound(err) {
mb.status = NotDeployed
return mb.status
}
return Unknown
}
mb.status = Deploying
sv, err := polymorphichelpers.StatusViewerFor(broker.GroupVersionKind().GroupKind())
_, done, err := sv.Status(broker, 0)
if err != nil {
mb.status = Unknown
return mb.status
}
if done {
mb.status = Deployed
}
return mb.status
}
mb.status = Deploying
if mesherykube.IsDeploymentDone(*broker) {
mb.status = Deployed
}
return mb.status
}

func (mb *mesheryBroker) Deploy() error {
Expand Down
14 changes: 11 additions & 3 deletions models/controllers/meshery_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
kubeerror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubectl/pkg/polymorphichelpers"
)

type mesheryOperator struct {
Expand Down Expand Up @@ -37,17 +39,23 @@ func (mo *mesheryOperator) GetName() string {

func (mo *mesheryOperator) GetStatus() MesheryControllerStatus {
// check if the deployment exists
deployment, err := mo.client.KubeClient.AppsV1().Deployments("meshery").Get(context.TODO(), "meshery-operator", metav1.GetOptions{})
deployment, err := mo.client.DynamicKubeClient.Resource(schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}).Namespace("meshery").Get(context.TODO(), "meshery-operator", metav1.GetOptions{})
if err != nil {
if kubeerror.IsNotFound(err) {
mo.status = NotDeployed
return mo.status
}
return Unknown
}

mo.status = Deploying
if mesherykube.IsDeploymentDone(*deployment) {

sv, err := polymorphichelpers.StatusViewerFor(deployment.GroupVersionKind().GroupKind())
_, done, err := sv.Status(deployment, 0)
if err != nil {
mo.status = Unknown
return mo.status
}
if done {
mo.status = Deployed
}
return mo.status
Expand Down
47 changes: 38 additions & 9 deletions models/controllers/meshsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"context"

// opClient "github.com/layer5io/meshery-operator/pkg/client"
opClient "github.com/layer5io/meshery-operator/pkg/client"
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
kubeerror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubectl/pkg/polymorphichelpers"
)

type meshsync struct {
Expand All @@ -17,7 +20,7 @@ type meshsync struct {

func NewMeshsyncHandler(kubernetesClient *mesherykube.Client) IMesheryController {
return &meshsync{
name: "Meshsync",
name: "MeshSync",
status: Unknown,
kclient: kubernetesClient,
}
Expand All @@ -28,17 +31,43 @@ func (ms *meshsync) GetName() string {
}

func (ms *meshsync) GetStatus() MesheryControllerStatus {
meshsync, err := ms.kclient.KubeClient.AppsV1().Deployments("meshery").Get(context.TODO(), "meshery-meshsync", metav1.GetOptions{})
// if the deployment is not found, then it is NotDeployed
if err != nil && !kubeerror.IsNotFound(err) {
operatorClient, err := opClient.New(&ms.kclient.RestConfig)
// TODO: Confirm if the presence of operator is needed to use the operator client sdk
meshSync, err := operatorClient.CoreV1Alpha1().MeshSyncs("meshery").Get(context.TODO(), "meshery-meshsync", metav1.GetOptions{})
if err == nil {
if meshSync.Status.PublishingTo != "" {
ms.status = Deployed
return ms.status
}
ms.status = NotDeployed
return ms.status
} else {
if kubeerror.IsNotFound(err) {
ms.status = NotDeployed
return ms.status
}
// when we are not able to get meshSync resource from OperatorClient, we try to get it using kubernetes client
meshSync, err := ms.kclient.DynamicKubeClient.Resource(schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}).Namespace("meshery").Get(context.TODO(), "meshery-meshsync", metav1.GetOptions{})
if err != nil {
// if the resource is not found, then it is NotDeployed
if kubeerror.IsNotFound(err) {
ms.status = NotDeployed
return ms.status
}
return Unknown
}
ms.status = Deploying
sv, err := polymorphichelpers.StatusViewerFor(meshSync.GroupVersionKind().GroupKind())
_, done, err := sv.Status(meshSync, 0)
if err != nil {
ms.status = Unknown
return ms.status
}
if done {
ms.status = Deployed
}
return ms.status
}
ms.status = Deploying
if mesherykube.IsDeploymentDone(*meshsync) {
ms.status = Deployed
}
return ms.status
}

func (ms *meshsync) Deploy() error {
Expand Down
28 changes: 0 additions & 28 deletions utils/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package kubernetes

import (
appsv1 "k8s.io/api/apps/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
deploymentutil "k8s.io/kubectl/pkg/util/deployment"
)

type Client struct {
Expand Down Expand Up @@ -40,29 +38,3 @@ func New(kubeconfig []byte) (*Client, error) {
KubeClient: kclient,
}, nil
}

// checks if deployment is done.
func IsDeploymentDone(deployment appsv1.Deployment) bool {
status := false
if deployment.Generation <= deployment.Status.ObservedGeneration {
cond := deploymentutil.GetDeploymentCondition(deployment.Status, appsv1.DeploymentProgressing)
if cond != nil && cond.Reason == deploymentutil.TimedOutReason {
// deployment exceeded its progress deadline
return status
}
if deployment.Spec.Replicas != nil && deployment.Status.UpdatedReplicas < *deployment.Spec.Replicas {
// Waiting for deployment rollout to finish...
return status
}
if deployment.Status.Replicas > deployment.Status.UpdatedReplicas {
// Waiting for deployment rollout to finish...
return status
}
if deployment.Status.AvailableReplicas < deployment.Status.UpdatedReplicas {
// Waiting for deployment rollout to finish...
return status
}
status = true
}
return status
}

0 comments on commit b21e073

Please sign in to comment.