From c18568e12ff69f24c6f660e21d9d8d93e47f1911 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Tue, 28 Nov 2023 17:14:39 +0530 Subject: [PATCH 1/4] PLT-851: Removed deplicate cluster deleted check --- client/cluster.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cluster.go b/client/cluster.go index 9b19173e..579bb10c 100644 --- a/client/cluster.go +++ b/client/cluster.go @@ -213,7 +213,7 @@ func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1Spec if err != nil { return nil, err } - if cluster.Status.State != "Deleted" { + if cluster != nil { return cluster, nil } } From 774144c5794fa13cac5616b8313efadc3f145b52 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Wed, 29 Nov 2023 10:05:31 +0530 Subject: [PATCH 2/4] removed duplicate check --- client/cluster.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/client/cluster.go b/client/cluster.go index 579bb10c..15d8e984 100644 --- a/client/cluster.go +++ b/client/cluster.go @@ -210,12 +210,7 @@ func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1Spec for _, clusterMetadata := range clustersMetadataList { if clusterMetadata.Name == name { cluster, err := h.GetCluster(clusterContext, clusterMetadata.UID) - if err != nil { - return nil, err - } - if cluster != nil { - return cluster, nil - } + return cluster, err } } From 34a187f166ba6204d7b19daa54cce183ce90e792 Mon Sep 17 00:00:00 2001 From: nikolay-spectro Date: Wed, 29 Nov 2023 09:52:55 -0800 Subject: [PATCH 3/4] PLT-851: Fixed get cluster by name API call. --- client/cluster.go | 83 ----------------------------------------------- client/search.go | 78 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 83 deletions(-) create mode 100644 client/search.go diff --git a/client/cluster.go b/client/cluster.go index 15d8e984..a350393e 100644 --- a/client/cluster.go +++ b/client/cluster.go @@ -104,73 +104,6 @@ func (h *V1Client) SearchClusterSummaries(clusterContext string, filter *models. return resp.Payload.Items, nil } -func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroCluster, error) { - client, err := h.GetClusterClient() - if err != nil { - return nil, err - } - - var params *clusterC.V1SpectroClustersListParams - switch clusterContext { - case "project": - params = clusterC.NewV1SpectroClustersListParamsWithContext(h.Ctx) - case "tenant": - params = clusterC.NewV1SpectroClustersListParams() - } - var limit int64 = 0 - params.Limit = &limit - resp, err := client.V1SpectroClustersList(params) - - var e *transport.TransportError - if errors.As(err, &e) && e.HttpCode == 404 { - return nil, nil - } else if err != nil { - return nil, err - } - - return resp.Payload.Items, nil -} - -func (h *V1Client) listClustersMetadata(clusterContext string) ([]*models.V1ObjectMeta, error) { - client, err := h.GetHashboardClient() - if err != nil { - return nil, err - } - - var params *hashboardC.V1SpectroClustersMetadataParams - switch clusterContext { - case "project": - params = hashboardC.NewV1SpectroClustersMetadataParams().WithContext(h.Ctx) - case "tenant": - params = hashboardC.NewV1SpectroClustersMetadataParams() - } - resp, err := client.V1SpectroClustersMetadata(params) - - var e *transport.TransportError - if errors.As(err, &e) && e.HttpCode == 404 { - return nil, nil - } else if err != nil { - return nil, err - } - - return resp.Payload.Items, nil -} - -// ListClusters This API is Deprecated in 3.1 in hubble, so basically it will return only 50 cluster by default. -func (h *V1Client) ListClusters(clusterContext string) ([]*models.V1SpectroCluster, error) { - allClusters, err := h.listClusters(clusterContext) - if err != nil { - return nil, err - } - clusters := make([]*models.V1SpectroCluster, 0) - for _, c := range allClusters { - if c.Status.State != "Deleted" { - clusters = append(clusters, c) - } - } - return clusters, nil -} - func (h *V1Client) GetClusterWithoutStatus(scope, uid string) (*models.V1SpectroCluster, error) { if h.GetClusterWithoutStatusFn != nil { return h.GetClusterWithoutStatusFn(uid) @@ -201,22 +134,6 @@ func (h *V1Client) GetClusterWithoutStatus(scope, uid string) (*models.V1Spectro return cluster, nil } -func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1SpectroCluster, error) { - clustersMetadataList, err := h.listClustersMetadata(clusterContext) - if err != nil { - return nil, err - } - - for _, clusterMetadata := range clustersMetadataList { - if clusterMetadata.Name == name { - cluster, err := h.GetCluster(clusterContext, clusterMetadata.UID) - return cluster, err - } - } - - return nil, nil -} - func (h *V1Client) GetClusterKubeConfig(uid, ClusterContext string) (string, error) { if h.GetClusterKubeConfigFn != nil { return h.GetClusterKubeConfigFn(uid) diff --git a/client/search.go b/client/search.go new file mode 100644 index 00000000..e82c6b52 --- /dev/null +++ b/client/search.go @@ -0,0 +1,78 @@ +package client + +import ( + "fmt" + + "github.com/spectrocloud/hapi/models" +) + +func and() *models.V1SearchFilterConjunctionOperator { + and := models.V1SearchFilterConjunctionOperatorAnd + return &and +} + +func or() *models.V1SearchFilterConjunctionOperator { + or := models.V1SearchFilterConjunctionOperatorOr + return &or +} + +func getClusterFilter(extraFilters []*models.V1SearchFilterItem) *models.V1SearchFilterSpec { + filter := &models.V1SearchFilterSpec{ + Conjunction: and(), + FilterGroups: []*models.V1SearchFilterGroup{ + { + Conjunction: and(), + Filters: []*models.V1SearchFilterItem{ + { + Condition: &models.V1SearchFilterCondition{ + Bool: &models.V1SearchFilterBoolCondition{ + Value: false, + }, + }, + Property: "isDeleted", + Type: models.V1SearchFilterPropertyTypeBool, + }, + }, + }, + }, + } + + filter.FilterGroups[0].Filters = append(filter.FilterGroups[0].Filters, extraFilters...) + + return filter +} + +func nameFilter(name string) *models.V1SearchFilterItem { + return &models.V1SearchFilterItem{ + Condition: &models.V1SearchFilterCondition{ + String: &models.V1SearchFilterStringCondition{ + Match: &models.V1SearchFilterStringConditionMatch{ + Conjunction: or(), + Values: []string{name}, + }, + Operator: models.V1SearchFilterStringOperatorEq, + }, + }, + Property: "name", + Type: models.V1SearchFilterPropertyTypeString, + } +} + +func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1SpectroCluster, error) { + filters := []*models.V1SearchFilterItem{nameFilter(name)} + clusterSummaries, err := h.SearchClusterSummaries( + clusterContext, getClusterFilter(filters), nil, + ) + if err != nil { + return nil, err + } + if len(clusterSummaries) != 1 { + return nil, fmt.Errorf("expected 1 cluster: %v in %v, got %d", name, clusterContext, len(clusterSummaries)) + } + + cluster, err := h.GetCluster(clusterContext, clusterSummaries[0].Metadata.UID) + if err != nil { + return nil, err + } + return cluster, nil +} From 357cb9a18c4eeff13be98c5df580190911ffbcb6 Mon Sep 17 00:00:00 2001 From: nikolay-spectro Date: Wed, 29 Nov 2023 15:57:37 -0800 Subject: [PATCH 4/4] PLT-851: Fixed get cluster by name API context. --- client/search.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/client/search.go b/client/search.go index e82c6b52..569d5de8 100644 --- a/client/search.go +++ b/client/search.go @@ -23,6 +23,20 @@ func getClusterFilter(extraFilters []*models.V1SearchFilterItem) *models.V1Searc { Conjunction: and(), Filters: []*models.V1SearchFilterItem{ + { + Condition: &models.V1SearchFilterCondition{ + String: &models.V1SearchFilterStringCondition{ + Match: &models.V1SearchFilterStringConditionMatch{ + Conjunction: or(), + Values: []string{"nested"}, + }, + Negation: true, + Operator: models.V1SearchFilterStringOperatorEq, + }, + }, + Property: "environment", + Type: models.V1SearchFilterPropertyTypeString, + }, { Condition: &models.V1SearchFilterCondition{ Bool: &models.V1SearchFilterBoolCondition{ @@ -37,12 +51,12 @@ func getClusterFilter(extraFilters []*models.V1SearchFilterItem) *models.V1Searc }, } - filter.FilterGroups[0].Filters = append(filter.FilterGroups[0].Filters, extraFilters...) + filter.FilterGroups = append(filter.FilterGroups, &models.V1SearchFilterGroup{Conjunction: and(), Filters: extraFilters}) return filter } -func nameFilter(name string) *models.V1SearchFilterItem { +func clusterNameEqFilter(name string) *models.V1SearchFilterItem { return &models.V1SearchFilterItem{ Condition: &models.V1SearchFilterCondition{ String: &models.V1SearchFilterStringCondition{ @@ -50,19 +64,23 @@ func nameFilter(name string) *models.V1SearchFilterItem { Conjunction: or(), Values: []string{name}, }, - Operator: models.V1SearchFilterStringOperatorEq, + Operator: models.V1SearchFilterStringOperatorEq, + Negation: false, + IgnoreCase: false, }, }, - Property: "name", + Property: "clusterName", Type: models.V1SearchFilterPropertyTypeString, } } +func (h *V1Client) SearchClusterSummariesNonVirtual(filter *models.V1SearchFilterSpec, sort []*models.V1SearchFilterSortSpec) ([]*models.V1SpectroClusterSummary, error) { + return h.SearchClusterSummaries("tenant", filter, sort) // regular clusters search is working only in tenant context. +} + func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1SpectroCluster, error) { - filters := []*models.V1SearchFilterItem{nameFilter(name)} - clusterSummaries, err := h.SearchClusterSummaries( - clusterContext, getClusterFilter(filters), nil, - ) + filters := []*models.V1SearchFilterItem{clusterNameEqFilter(name)} + clusterSummaries, err := h.SearchClusterSummariesNonVirtual(getClusterFilter(filters), nil) if err != nil { return nil, err } @@ -70,7 +88,7 @@ func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1Spec return nil, fmt.Errorf("expected 1 cluster: %v in %v, got %d", name, clusterContext, len(clusterSummaries)) } - cluster, err := h.GetCluster(clusterContext, clusterSummaries[0].Metadata.UID) + cluster, err := h.GetCluster(clusterSummaries[0].Metadata.Annotations["scope"], clusterSummaries[0].Metadata.UID) if err != nil { return nil, err }