Skip to content

Commit

Permalink
--task=862679929 【GitOps】Application 支持 Label 选择 (merge request !1403)
Browse files Browse the repository at this point in the history
  • Loading branch information
blazehu authored and jimwu committed Sep 6, 2023
1 parent 39ea8a0 commit 5aff391
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"encoding/json"
"fmt"
appclient "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -141,7 +142,7 @@ func (plugin *AppPlugin) listApplicationsHandler(ctx context.Context, r *http.Re
errors.Wrapf(err, "check project '%s' permission failed", projectName))
}
}
appList, err := plugin.middleware.ListApplications(ctx, projects)
appList, err := plugin.middleware.ListApplications(ctx, &appclient.ApplicationQuery{Projects: projects})
if err != nil {
return mw.ReturnErrorResponse(http.StatusInternalServerError,
errors.Wrapf(err, "list applications by project '%v' from storage failed", projects))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func (plugin *ApplicationSetPlugin) List(ctx context.Context, r *http.Request) *
errors.Wrapf(err, "check project '%s' permission failed", projectName))
}
}
appList, err := plugin.middleware.ListApplications(ctx, projects)
appsetList, err := plugin.middleware.ListApplicationSets(ctx, projects)
if err != nil {
return mw.ReturnErrorResponse(http.StatusInternalServerError,
errors.Wrapf(err, "list applications by project '%v' from storage failed", projects))
}
return mw.ReturnJSONResponse(appList)
return mw.ReturnJSONResponse(appsetList)
}

// Get one applicationset
Expand Down
19 changes: 2 additions & 17 deletions bcs-scenarios/bcs-gitops-manager/pkg/proxy/argocd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,27 +500,12 @@ func (plugin *GrpcPlugin) handleAppList(ctx context.Context, req *http.Request)
names = append(names, item.Name)
}
}
appList, err := plugin.middleware.ListApplications(ctx, names)
query.Projects = names
appList, err := plugin.middleware.ListApplications(ctx, query)
if err != nil {
return mw.ReturnGRPCErrorResponse(http.StatusInternalServerError,
errors.Wrapf(err, "list applications by project '%s' from storage failed", names))
}
result := make([]v1alpha1.Application, 0, len(appList.Items))
for i := range appList.Items {
item := appList.Items[i]
if query.Name != nil && (*query.Name != "" && *query.Name != item.Name) {
continue
}
if query.Repo != nil && (*query.Repo != "" && *query.Repo != item.Spec.Source.RepoURL) {
continue
}
if query.AppNamespace != nil && (*query.AppNamespace != "" && *query.AppNamespace !=
item.Spec.Destination.Namespace) {
continue
}
result = append(result, item)
}
appList.Items = result
return mw.ReturnGRPCResponse(appList)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package middleware
import (
"context"
"fmt"
appclient "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"net/http"
"reflect"
"runtime"
Expand All @@ -34,7 +35,6 @@ import (
"github.com/Tencent/bk-bcs/bcs-scenarios/bcs-gitops-manager/pkg/common"
"github.com/Tencent/bk-bcs/bcs-scenarios/bcs-gitops-manager/pkg/proxy"
"github.com/Tencent/bk-bcs/bcs-scenarios/bcs-gitops-manager/pkg/proxy/argocd/session"
"github.com/Tencent/bk-bcs/bcs-scenarios/bcs-gitops-manager/pkg/store"
"github.com/Tencent/bk-bcs/bcs-services/pkg/bcs-auth/cluster"
"github.com/Tencent/bk-bcs/bcs-services/pkg/bcs-auth/project"
)
Expand Down Expand Up @@ -65,7 +65,7 @@ type MiddlewareInterface interface {
ListClusters(ctx context.Context, projectNames []string) (*v1alpha1.ClusterList, int, error)
ListRepositories(ctx context.Context, projectNames []string,
needCheckPermission bool) (*v1alpha1.RepositoryList, int, error)
ListApplications(ctx context.Context, projectNames []string) (*v1alpha1.ApplicationList, error)
ListApplications(ctx context.Context, query *appclient.ApplicationQuery) (*v1alpha1.ApplicationList, error)

CheckCreateApplicationSet(ctx context.Context, appset *v1alpha1.ApplicationSet) (int, error)
CheckDeleteApplicationSet(ctx context.Context, appsetName string) (int, error)
Expand Down Expand Up @@ -424,10 +424,11 @@ func (h *handler) ListRepositories(ctx context.Context, projectNames []string,
}

// ListApplications 根据项目名称获取所有应用
func (h *handler) ListApplications(ctx context.Context, projectNames []string) (*v1alpha1.ApplicationList, error) {
apps, err := h.option.Storage.ListApplications(ctx, &store.ListAppOptions{Projects: projectNames})
func (h *handler) ListApplications(ctx context.Context, query *appclient.ApplicationQuery) (
*v1alpha1.ApplicationList, error) {
apps, err := h.option.Storage.ListApplications(ctx, query)
if err != nil {
return nil, errors.Wrapf(err, "list application swith project '%v' failed", projectNames)
return nil, errors.Wrapf(err, "list application swith project '%v' failed", query.Projects)
}
return apps, nil
}
35 changes: 30 additions & 5 deletions bcs-scenarios/bcs-gitops-manager/pkg/store/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/watch"

applicationpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
Expand Down Expand Up @@ -267,24 +268,48 @@ func (cd *argo) GetApplication(ctx context.Context, name string) (*v1alpha1.Appl
}

// ListApplications interface
func (cd *argo) ListApplications(ctx context.Context, option *ListAppOptions) (*v1alpha1.ApplicationList, error) {
func (cd *argo) ListApplications(ctx context.Context, query *appclient.ApplicationQuery) (
*v1alpha1.ApplicationList, error) {
if !cd.cacheSynced.Load() {
apps, err := cd.appClient.List(ctx, &appclient.ApplicationQuery{Projects: option.Projects})
apps, err := cd.appClient.List(ctx, query)
if err != nil {
return nil, errors.Wrapf(err, "argocd list application for project '%v' failed", option.Projects)
return nil, errors.Wrapf(err, "argocd list application for project '%v' failed", query.Projects)
}
return apps, nil
}
var (
selector labels.Selector
err error
)
if query.Selector != nil && *query.Selector != "" {
selector, err = labels.Parse(*query.Selector)
if err != nil {
return nil, errors.Wrapf(err, "argocd list application for project '%v' failed", query.Projects)
}
}
result := &v1alpha1.ApplicationList{
Items: make([]v1alpha1.Application, 0),
}
for i := range option.Projects {
projName := option.Projects[i]
for i := range query.Projects {
projName := query.Projects[i]
projApps, ok := cd.cacheApplication.Load(projName)
if !ok {
continue
}
for _, v := range projApps.(map[string]*v1alpha1.Application) {
if query.Name != nil && (*query.Name != "" && *query.Name != v.Name) {
continue
}
if query.Repo != nil && (*query.Repo != "" && *query.Repo != v.Spec.Source.RepoURL) {
continue
}
if query.AppNamespace != nil && (*query.AppNamespace != "" && *query.AppNamespace !=
v.Spec.Destination.Namespace) {
continue
}
if query.Selector != nil && (*query.Selector != "" && !selector.Matches(labels.Set(v.Labels))) {
continue
}
result.Items = append(result.Items, *v)
}
}
Expand Down
8 changes: 2 additions & 6 deletions bcs-scenarios/bcs-gitops-manager/pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package store

import (
"context"
appclient "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"sync"

"github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
Expand All @@ -28,11 +29,6 @@ type Options struct {
Cache bool // init cache for performance
}

// ListAppOptions for data filter
type ListAppOptions struct {
Projects []string
}

// Store define data interface for argocd structure.
type Store interface {
// store control interface
Expand All @@ -59,7 +55,7 @@ type Store interface {
ListRepository(ctx context.Context) (*v1alpha1.RepositoryList, error)

GetApplication(ctx context.Context, name string) (*v1alpha1.Application, error)
ListApplications(ctx context.Context, option *ListAppOptions) (*v1alpha1.ApplicationList, error)
ListApplications(ctx context.Context, query *appclient.ApplicationQuery) (*v1alpha1.ApplicationList, error)
DeleteApplicationResource(ctx context.Context, application *v1alpha1.Application) error

GetApplicationSet(ctx context.Context, name string) (*v1alpha1.ApplicationSet, error)
Expand Down

0 comments on commit 5aff391

Please sign in to comment.