Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLT-851: Removed duplicate cluster deleted check #74

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 0 additions & 88 deletions client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -201,27 +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)
if err != nil {
return nil, err
}
if cluster.Status.State != "Deleted" {
return cluster, nil
}
}
}

return nil, nil
}

func (h *V1Client) GetClusterKubeConfig(uid, ClusterContext string) (string, error) {
if h.GetClusterKubeConfigFn != nil {
return h.GetClusterKubeConfigFn(uid)
Expand Down
96 changes: 96 additions & 0 deletions client/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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{
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{
Value: false,
},
},
Property: "isDeleted",
Type: models.V1SearchFilterPropertyTypeBool,
},
},
},
},
}

filter.FilterGroups = append(filter.FilterGroups, &models.V1SearchFilterGroup{Conjunction: and(), Filters: extraFilters})

return filter
}

func clusterNameEqFilter(name string) *models.V1SearchFilterItem {
return &models.V1SearchFilterItem{
Condition: &models.V1SearchFilterCondition{
String: &models.V1SearchFilterStringCondition{
Match: &models.V1SearchFilterStringConditionMatch{
Conjunction: or(),
Values: []string{name},
},
Operator: models.V1SearchFilterStringOperatorEq,
Negation: false,
IgnoreCase: false,
},
},
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{clusterNameEqFilter(name)}
clusterSummaries, err := h.SearchClusterSummariesNonVirtual(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(clusterSummaries[0].Metadata.Annotations["scope"], clusterSummaries[0].Metadata.UID)
if err != nil {
return nil, err
}
return cluster, nil
}