From 03d171dfc5011f88ec77f4aa991f12cd2f6d9831 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Thu, 28 Jul 2022 14:12:59 +0530 Subject: [PATCH 1/9] Refactor controller status logic Signed-off-by: MUzairS15 --- models/controllers/controllers.go | 6 ++++++ models/controllers/meshery_broker.go | 23 ++++++++++++++++++++++- models/controllers/meshsync.go | 19 +++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/models/controllers/controllers.go b/models/controllers/controllers.go index 0b8923f5..76cc0230 100644 --- a/models/controllers/controllers.go +++ b/models/controllers/controllers.go @@ -6,6 +6,8 @@ const ( Deployed MesheryControllerStatus = iota Deploying NotDeployed + Enabled + Running // we don't know since we have not checked yet Unknown ) @@ -18,6 +20,10 @@ func (mcs MesheryControllerStatus) String() string { return "Deploying" case NotDeployed: return "Not Deployed" + case Enabled: + return "Enabled" + case Running: + return "Running" case Unknown: return "Unknown" } diff --git a/models/controllers/meshery_broker.go b/models/controllers/meshery_broker.go index 43b55fcb..6d035665 100644 --- a/models/controllers/meshery_broker.go +++ b/models/controllers/meshery_broker.go @@ -3,9 +3,11 @@ package controllers import ( "context" "fmt" + "strings" opClient "github.com/layer5io/meshery-operator/pkg/client" mesherykube "github.com/layer5io/meshkit/utils/kubernetes" + v1 "k8s.io/api/core/v1" kubeerror "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" @@ -16,6 +18,7 @@ type mesheryBroker struct { name string status MesheryControllerStatus kclient *mesherykube.Client + version string } func NewMesheryBrokerHandler(kubernetesClient *mesherykube.Client) IMesheryController { @@ -23,6 +26,7 @@ func NewMesheryBrokerHandler(kubernetesClient *mesherykube.Client) IMesheryContr name: "MesheryBroker", status: Unknown, kclient: kubernetesClient, + version: "", } } @@ -100,5 +104,22 @@ func (mb *mesheryBroker) GetPublicEndpoint() (string, error) { } func (mb *mesheryBroker) GetVersion() (string, error) { - return "", nil + if len(mb.version) == 0 { + statefulSet, err := mb.kclient.KubeClient.AppsV1().StatefulSets("meshery").Get(context.TODO(), "meshery-broker", metav1.GetOptions{}) + if kubeerror.IsNotFound(err) { + return "", err + } + return getImageVersionOfContainer(statefulSet.Spec.Template, "nats"), nil + } + return mb.version, nil +} + +func getImageVersionOfContainer(container v1.PodTemplateSpec, containerName string) string { + var version string + for _, container := range container.Spec.Containers { + if strings.Compare(container.Name, containerName) == 0 { + version = strings.Split(container.Image, ":")[1] + } + } + return version } diff --git a/models/controllers/meshsync.go b/models/controllers/meshsync.go index 7cda7726..d31be370 100644 --- a/models/controllers/meshsync.go +++ b/models/controllers/meshsync.go @@ -6,6 +6,7 @@ import ( // opClient "github.com/layer5io/meshery-operator/pkg/client" opClient "github.com/layer5io/meshery-operator/pkg/client" mesherykube "github.com/layer5io/meshkit/utils/kubernetes" + v1 "k8s.io/api/core/v1" kubeerror "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" @@ -33,14 +34,24 @@ func (ms *meshsync) GetName() string { func (ms *meshsync) GetStatus() MesheryControllerStatus { operatorClient, _ := 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{}) + _, err := operatorClient.CoreV1Alpha1().MeshSyncs("meshery").Get(context.TODO(), "meshery-meshsync", metav1.GetOptions{}) if err == nil { - if meshSync.Status.PublishingTo != "" { + ms.status = Enabled + meshSyncPod, err := ms.kclient.KubeClient.CoreV1().Pods("meshery").List(context.TODO(), metav1.ListOptions{ + LabelSelector: "component=meshsync", + }) + if len(meshSyncPod.Items) == 0 || kubeerror.IsNotFound(err) { + return ms.status + } + + switch meshSyncPod.Items[0].Status.Phase { + case v1.PodRunning: + ms.status = Running + return ms.status + default: ms.status = Deployed return ms.status } - ms.status = NotDeployed - return ms.status } else { if kubeerror.IsNotFound(err) { ms.status = NotDeployed From 8abcd85cd817c922341123e59e674446040cc34c Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 16 Sep 2022 09:21:35 +0530 Subject: [PATCH 2/9] Apply suggestion Signed-off-by: MUzairS15 --- models/controllers/meshery_broker.go | 11 +++++++---- models/controllers/meshery_operator.go | 2 +- models/controllers/meshsync.go | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/models/controllers/meshery_broker.go b/models/controllers/meshery_broker.go index e55244cd..dcd3ba88 100644 --- a/models/controllers/meshery_broker.go +++ b/models/controllers/meshery_broker.go @@ -46,12 +46,12 @@ func (mb *mesheryBroker) GetStatus() MesheryControllerStatus { mb.status = Deployed return mb.status } - mb.status = NotDeployed + mb.status = Undeployed return mb.status } else { if kubeerror.IsNotFound(err) { if mb.status != Undeployed { - mb.status = NotDeployed + mb.status = Undeployed } return mb.status } @@ -60,7 +60,7 @@ func (mb *mesheryBroker) GetStatus() MesheryControllerStatus { if err != nil { // if the resource is not found, then it is NotDeployed if kubeerror.IsNotFound(err) { - mb.status = NotDeployed + mb.status = Undeployed return mb.status } return Unknown @@ -124,7 +124,10 @@ func getImageVersionOfContainer(container v1.PodTemplateSpec, containerName stri var version string for _, container := range container.Spec.Containers { if strings.Compare(container.Name, containerName) == 0 { - version = strings.Split(container.Image, ":")[1] + versionTag := strings.Split(container.Image, ":") + if (len(versionTag) > 1) { + version = versionTag[1] + } } } return version diff --git a/models/controllers/meshery_operator.go b/models/controllers/meshery_operator.go index b6094e80..529c6cd2 100644 --- a/models/controllers/meshery_operator.go +++ b/models/controllers/meshery_operator.go @@ -47,7 +47,7 @@ func (mo *mesheryOperator) GetStatus() MesheryControllerStatus { 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.setStatus(NotDeployed) + mo.setStatus(Undeployed) return mo.status } return Unknown diff --git a/models/controllers/meshsync.go b/models/controllers/meshsync.go index 6e8f7eea..4442655d 100644 --- a/models/controllers/meshsync.go +++ b/models/controllers/meshsync.go @@ -55,7 +55,7 @@ func (ms *meshsync) GetStatus() MesheryControllerStatus { } else { if kubeerror.IsNotFound(err) { if ms.status != Undeployed { - ms.status = NotDeployed + ms.status = Undeployed } return ms.status } @@ -64,7 +64,7 @@ func (ms *meshsync) GetStatus() MesheryControllerStatus { if err != nil { // if the resource is not found, then it is NotDeployed if kubeerror.IsNotFound(err) { - ms.status = NotDeployed + ms.status = Undeployed return ms.status } return Unknown From 329be4d62387d7af60cdb709dd088c313b3b2aeb Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 16 Sep 2022 09:22:58 +0530 Subject: [PATCH 3/9] lint fixes Signed-off-by: MUzairS15 --- models/controllers/controllers.go | 2 +- models/controllers/meshery_broker.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/models/controllers/controllers.go b/models/controllers/controllers.go index 58731138..dcb90c0c 100644 --- a/models/controllers/controllers.go +++ b/models/controllers/controllers.go @@ -5,7 +5,7 @@ type MesheryControllerStatus int const ( Deployed MesheryControllerStatus = iota Deploying - Undeployed + Undeployed Enabled Running // we don't know since we have not checked yet diff --git a/models/controllers/meshery_broker.go b/models/controllers/meshery_broker.go index dcd3ba88..5f0012ba 100644 --- a/models/controllers/meshery_broker.go +++ b/models/controllers/meshery_broker.go @@ -125,9 +125,9 @@ func getImageVersionOfContainer(container v1.PodTemplateSpec, containerName stri for _, container := range container.Spec.Containers { if strings.Compare(container.Name, containerName) == 0 { versionTag := strings.Split(container.Image, ":") - if (len(versionTag) > 1) { + if len(versionTag) > 1 { version = versionTag[1] - } + } } } return version From bd60fdf859da789c064039f7c910fe553e93bf0b Mon Sep 17 00:00:00 2001 From: Nithish Date: Sun, 25 Sep 2022 17:47:47 +0530 Subject: [PATCH 4/9] feat: Add support for versioning Signed-off-by: Nithish --- utils/artifacthub/package.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/utils/artifacthub/package.go b/utils/artifacthub/package.go index 619feeba..48b43747 100644 --- a/utils/artifacthub/package.go +++ b/utils/artifacthub/package.go @@ -22,6 +22,7 @@ type AhPackage struct { Url string Official bool VerifiedPublisher bool + Version string } func (pkg AhPackage) GenerateComponents() ([]v1alpha1.Component, error) { @@ -37,6 +38,7 @@ func (pkg AhPackage) GenerateComponents() ([]v1alpha1.Component, error) { if err != nil { continue } + comp.Metadata["version"] = pkg.Version components = append(components, comp) } return components, nil @@ -65,8 +67,10 @@ func (pkg *AhPackage) UpdatePackageData() error { chartUrl := res[ArtifactHubChartUrlFieldName].(string) official := res["repository"].(map[string]interface{})["official"].(bool) verPub := res["repository"].(map[string]interface{})["verified_publisher"].(bool) + version := res["version"].(string) pkg.Url = chartUrl pkg.Official = official pkg.VerifiedPublisher = verPub + pkg.Version = version return nil } From 14d0c1cbc527d8b53d453bddd56769e9b37d0201 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Thu, 28 Jul 2022 14:12:59 +0530 Subject: [PATCH 5/9] Refactor controller status logic Signed-off-by: MUzairS15 --- models/controllers/controllers.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/models/controllers/controllers.go b/models/controllers/controllers.go index dcb90c0c..d1d6a3d7 100644 --- a/models/controllers/controllers.go +++ b/models/controllers/controllers.go @@ -3,12 +3,13 @@ package controllers type MesheryControllerStatus int const ( - Deployed MesheryControllerStatus = iota - Deploying - Undeployed + Deployed MesheryControllerStatus = iota //The controller is deployed(default behavior) + Deploying //The controller is being deployed + NotDeployed //The controller is not deployed yet + Undeployed //The controller has been intentionally undeployed. This state is useful to avoid automatic redeployment. + // we don't know since we have not checked yet Enabled Running - // we don't know since we have not checked yet Unknown ) @@ -18,12 +19,12 @@ func (mcs MesheryControllerStatus) String() string { return "Deployed" case Deploying: return "Deploying" + case Undeployed: + return "Undeployed" case Enabled: return "Enabled" case Running: return "Running" - case Undeployed: - return "Undeployed" case Unknown: return "Unknown" } From ba69188e0f64a6674aedccef8393949b3f110b3e Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Thu, 29 Sep 2022 16:17:44 +0530 Subject: [PATCH 6/9] Add test logic Signed-off-by: MUzairS15 --- models/controllers/controllers.go | 5 ++++ models/controllers/helpers.go | 43 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/models/controllers/controllers.go b/models/controllers/controllers.go index d1d6a3d7..229f92ac 100644 --- a/models/controllers/controllers.go +++ b/models/controllers/controllers.go @@ -10,6 +10,7 @@ const ( // we don't know since we have not checked yet Enabled Running + Connected Unknown ) @@ -19,12 +20,16 @@ func (mcs MesheryControllerStatus) String() string { return "Deployed" case Deploying: return "Deploying" + case NotDeployed: + return "Not Deployed" case Undeployed: return "Undeployed" case Enabled: return "Enabled" case Running: return "Running" + case Connected: + return "Connected" case Unknown: return "Unknown" } diff --git a/models/controllers/helpers.go b/models/controllers/helpers.go index 36f7fdfb..26f20c7d 100644 --- a/models/controllers/helpers.go +++ b/models/controllers/helpers.go @@ -1,10 +1,13 @@ package controllers import ( + "encoding/json" "fmt" + "io" "strconv" "strings" + "net/http" "net/url" "github.com/layer5io/meshery-operator/api/v1alpha1" @@ -13,6 +16,16 @@ import ( "github.com/spf13/viper" ) +const BrokerPingEndpoint = "8222/connz" + +type Connections struct { + Connections []connection `json:"connections"` +} + +type connection struct { + Name string `json:"name"` +} + func GetBrokerEndpoint(kclient *mesherykube.Client, broker *v1alpha1.Broker) string { endpoint := broker.Status.Endpoint.Internal if len(strings.Split(broker.Status.Endpoint.Internal, ":")) > 1 { @@ -78,3 +91,33 @@ func applyOperatorHelmChart(chartRepo string, client mesherykube.Client, meshery } return nil } + +func ConnectivityTest(clientName, externalIP string) bool { + endpoint, err := url.Parse("http://" + externalIP + ":" + BrokerPingEndpoint) + if err != nil { + return false + } + + resp, err := http.Get(endpoint.String()) + if err != nil { + return false + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return false + } + + var natsResponse Connections + err = json.Unmarshal(body, &natsResponse) + if err != nil { + return false + } + + for _, client := range natsResponse.Connections { + if client.Name == clientName { + return true + } + } + return false +} From a7f65556feb6b36c9a7f511e3aa3afc0f6d3df98 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 30 Sep 2022 09:54:05 +0530 Subject: [PATCH 7/9] Refactor Signed-off-by: MUzairS15 --- models/controllers/controllers.go | 6 ++++++ models/controllers/meshery_broker.go | 14 ++++++++------ models/controllers/meshsync.go | 11 +++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/models/controllers/controllers.go b/models/controllers/controllers.go index 229f92ac..535517bc 100644 --- a/models/controllers/controllers.go +++ b/models/controllers/controllers.go @@ -14,6 +14,12 @@ const ( Unknown ) +const ( + MeshSync = "meshsync" + MesheryBroker = "meshery-broker" + MesheryServer = "meshery-server" +) + func (mcs MesheryControllerStatus) String() string { switch mcs { case Deployed: diff --git a/models/controllers/meshery_broker.go b/models/controllers/meshery_broker.go index 5f0012ba..09ab1215 100644 --- a/models/controllers/meshery_broker.go +++ b/models/controllers/meshery_broker.go @@ -42,11 +42,13 @@ func (mb *mesheryBroker) GetStatus() MesheryControllerStatus { // 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 + brokerEndpoint := broker.Status.Endpoint.External + hostIP := strings.Split(brokerEndpoint, ":")[0] + if broker.Status.Endpoint.External != "" && ConnectivityTest(MesheryServer, hostIP) { + mb.status = Connected return mb.status } - mb.status = Undeployed + mb.status = Deployed return mb.status } else { if kubeerror.IsNotFound(err) { @@ -56,7 +58,7 @@ func (mb *mesheryBroker) GetStatus() MesheryControllerStatus { 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{}) + broker, err := mb.kclient.DynamicKubeClient.Resource(schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}).Namespace("meshery").Get(context.TODO(), MesheryBroker, metav1.GetOptions{}) if err != nil { // if the resource is not found, then it is NotDeployed if kubeerror.IsNotFound(err) { @@ -97,7 +99,7 @@ func (mb *mesheryBroker) GetPublicEndpoint() (string, error) { if err != nil { return "", ErrGetControllerPublicEndpoint(err) } - broker, err := operatorClient.CoreV1Alpha1().Brokers("meshery").Get(context.TODO(), "meshery-broker", metav1.GetOptions{}) + broker, err := operatorClient.CoreV1Alpha1().Brokers("meshery").Get(context.TODO(), MesheryBroker, metav1.GetOptions{}) if broker.Status.Endpoint.External == "" { if err == nil { err = fmt.Errorf("Could not get the External endpoint for meshery-broker") @@ -111,7 +113,7 @@ func (mb *mesheryBroker) GetPublicEndpoint() (string, error) { func (mb *mesheryBroker) GetVersion() (string, error) { if len(mb.version) == 0 { - statefulSet, err := mb.kclient.KubeClient.AppsV1().StatefulSets("meshery").Get(context.TODO(), "meshery-broker", metav1.GetOptions{}) + statefulSet, err := mb.kclient.KubeClient.AppsV1().StatefulSets("meshery").Get(context.TODO(), MesheryBroker, metav1.GetOptions{}) if kubeerror.IsNotFound(err) { return "", err } diff --git a/models/controllers/meshsync.go b/models/controllers/meshsync.go index 4442655d..fe0f140b 100644 --- a/models/controllers/meshsync.go +++ b/models/controllers/meshsync.go @@ -2,6 +2,7 @@ package controllers import ( "context" + "strings" // opClient "github.com/layer5io/meshery-operator/pkg/client" opClient "github.com/layer5io/meshery-operator/pkg/client" @@ -47,6 +48,16 @@ func (ms *meshsync) GetStatus() MesheryControllerStatus { switch meshSyncPod.Items[0].Status.Phase { case v1.PodRunning: ms.status = Running + broker := NewMesheryBrokerHandler(ms.kclient) + brokerEndpoint, err := broker.GetPublicEndpoint() + if err != nil { + return ms.status + } + hostIP := strings.Split(brokerEndpoint, ":")[0] + isConnected := ConnectivityTest(MeshSync, hostIP) + if isConnected { + ms.status = Connected + } return ms.status default: ms.status = Deployed From 5b3b495dcaae4e2dc55757448da28fa067d07f48 Mon Sep 17 00:00:00 2001 From: Mohd Uzair Date: Fri, 30 Sep 2022 13:15:55 +0530 Subject: [PATCH 8/9] Update meshery_operator.go Signed-off-by: Mohd Uzair --- models/controllers/meshery_operator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/controllers/meshery_operator.go b/models/controllers/meshery_operator.go index 529c6cd2..b6094e80 100644 --- a/models/controllers/meshery_operator.go +++ b/models/controllers/meshery_operator.go @@ -47,7 +47,7 @@ func (mo *mesheryOperator) GetStatus() MesheryControllerStatus { 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.setStatus(Undeployed) + mo.setStatus(NotDeployed) return mo.status } return Unknown From 7f668452430d23b64dd50af815448b4570babcee Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Sat, 1 Oct 2022 09:31:24 +0530 Subject: [PATCH 9/9] Fix lint issues Signed-off-by: MUzairS15 --- models/controllers/helpers.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/controllers/helpers.go b/models/controllers/helpers.go index 26f20c7d..d60e3137 100644 --- a/models/controllers/helpers.go +++ b/models/controllers/helpers.go @@ -102,18 +102,18 @@ func ConnectivityTest(clientName, externalIP string) bool { if err != nil { return false } - + body, err := io.ReadAll(resp.Body) if err != nil { return false } - + var natsResponse Connections err = json.Unmarshal(body, &natsResponse) if err != nil { return false } - + for _, client := range natsResponse.Connections { if client.Name == clientName { return true