Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
feat: GetAppDetails function in templates/triggers (#155)
Browse files Browse the repository at this point in the history
feat: GetAppDetails function in templates/triggers (#155)
  • Loading branch information
paydaycay committed Jan 24, 2021
1 parent 24ca9c3 commit e3fb73b
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 4 deletions.
13 changes: 13 additions & 0 deletions cmd/tools/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"sync"

"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
"github.com/ghodss/yaml"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -71,6 +73,17 @@ func (svc *lazyArgocdServiceInitializer) GetCommitMetadata(ctx context.Context,
return svc.argocdService.GetCommitMetadata(ctx, repoURL, commitSHA)
}

func (svc *lazyArgocdServiceInitializer) GetAppDetails(ctx context.Context, appSource *v1alpha1.ApplicationSource) (*shared.AppDetail, error) {
var err error
svc.init.Do(func() {
err = svc.initArgoCDService()
})
if err != nil {
return nil, err
}
return svc.argocdService.GetAppDetails(ctx, appSource)
}

func getK8SClients(clientConfig clientcmd.ClientConfig) (kubernetes.Interface, dynamic.Interface, string, error) {
ns, _, err := clientConfig.Namespace()
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ Returns commit metadata. The commit must belong to the application source reposi
* `Author string` - commit author
* `Date time.Time` - commit creation date
* `Tags []string` - Associated tags

<hr>
**`repo.GetAppDetails() AppDetail`**

Returns application details. `AppDetail` fields:

* `Type string` - AppDetail type
* `Helm HelmAppSpec` - Helm details
* Fields :
* `Name string`
* `ValueFiles []string`
* `Parameters []*v1alpha1.HelmParameter`
* `Values string`
* `FileParameters []*v1alpha1.HelmFileParameter`
* Methods :
* `GetParameterValueByName(Name string)` Retrieve value by name in Parameters field
* `GetFileParameterPathByName(Name string)` Retrieve path by name in FileParameters field
* `Ksonnet *apiclient.KsonnetAppSpec` - Ksonnet details
* `Kustomize *apiclient.KustomizeAppSpec` - Kustomize details
* `Directory *apiclient.DirectoryAppSpec` - Directory details
39 changes: 37 additions & 2 deletions expr/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,49 @@ package repo

import (
"context"
"encoding/json"
"errors"
"regexp"
"strings"

giturls "github.com/whilp/git-urls"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"

"github.com/argoproj-labs/argocd-notifications/expr/shared"
"github.com/argoproj-labs/argocd-notifications/pkg/util/text"
"github.com/argoproj-labs/argocd-notifications/shared/argocd"
giturls "github.com/whilp/git-urls"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

var (
gitSuffix = regexp.MustCompile(`\.git$`)
)

func getApplicationSource(obj *unstructured.Unstructured) (*v1alpha1.ApplicationSource, error) {
data, err := json.Marshal(obj)
if err != nil {
return nil, err
}
application := &v1alpha1.Application{}
err = json.Unmarshal(data, application)
if err != nil {
return nil, err
}
return &application.Spec.Source, nil
}

func getAppDetails(app *unstructured.Unstructured, argocdService argocd.Service) (*shared.AppDetail, error) {
appSource, err := getApplicationSource(app)
if err != nil {
return nil, err
}
appDetail, err := argocdService.GetAppDetails(context.Background(), appSource)
if err != nil {
return nil, err
}
return appDetail, nil
}

func getCommitMetadata(commitSHA string, app *unstructured.Unstructured, argocdService argocd.Service) (*shared.CommitMetadata, error) {
repoURL, ok, err := unstructured.NestedString(app.Object, "spec", "source", "repoURL")
if err != nil {
Expand Down Expand Up @@ -69,5 +96,13 @@ func NewExprs(argocdService argocd.Service, app *unstructured.Unstructured) map[

return *meta
},
"GetAppDetails": func() interface{} {
appDetails, err := getAppDetails(app, argocdService)
if err != nil {
panic(err)
}

return *appDetails
},
}
}
16 changes: 16 additions & 0 deletions expr/shared/appdetail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package shared

import "github.com/argoproj/argo-cd/reposerver/apiclient"

type AppDetail struct {
// AppDetail Type
Type string
// Ksonnet details
Ksonnet *apiclient.KsonnetAppSpec
// Helm details
Helm *HelmAppSpec
// Kustomize details
Kustomize *apiclient.KustomizeAppSpec
// Directory details
Directory *apiclient.DirectoryAppSpec
}
35 changes: 35 additions & 0 deletions expr/shared/helmappspec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package shared

import (
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
)

type HelmAppSpec struct {
Name string
ValueFiles []string
Parameters []*v1alpha1.HelmParameter
Values string
FileParameters []*v1alpha1.HelmFileParameter
}

func (has HelmAppSpec) GetParameterValueByName(Name string) string {
var value string
for i := range has.Parameters {
if has.Parameters[i].Name == Name {
value = has.Parameters[i].Value
break
}
}
return value
}

func (has HelmAppSpec) GetFileParameterPathByName(Name string) string {
var path string
for i := range has.FileParameters {
if has.FileParameters[i].Name == Name {
path = has.FileParameters[i].Path
break
}
}
return path
}
8 changes: 8 additions & 0 deletions expr/shared/helmfileparameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package shared

type HelmFileParameter struct {
// Name is the name of the helm parameter
Name string
// Path is the path value for the helm parameter
Path string
}
10 changes: 10 additions & 0 deletions expr/shared/helmparameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package shared

type HelmParameter struct {
// Name is the name of the helm parameter
Name string
// Value is the value for the helm parameter
Value string
// ForceString determines whether to tell Helm to interpret booleans and numbers as strings
ForceString bool
}
16 changes: 16 additions & 0 deletions shared/argocd/mocks/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 50 additions & 2 deletions shared/argocd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package argocd
import (
"context"

"github.com/argoproj-labs/argocd-notifications/expr/shared"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/reposerver/apiclient"
"github.com/argoproj/argo-cd/util/db"
"github.com/argoproj/argo-cd/util/settings"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"

"github.com/argoproj-labs/argocd-notifications/expr/shared"
)

//go:generate mockgen -destination=./mocks/service.go -package=mocks github.com/argoproj-labs/argocd-notifications/shared/argocd Service

type Service interface {
GetCommitMetadata(ctx context.Context, repoURL string, commitSHA string) (*shared.CommitMetadata, error)
GetAppDetails(ctx context.Context, appSource *v1alpha1.ApplicationSource) (*shared.AppDetail, error)
}

func NewArgoCDService(clientset kubernetes.Interface, namespace string, repoServerAddress string) (*argoCDService, error) {
Expand Down Expand Up @@ -66,6 +67,53 @@ func (svc *argoCDService) GetCommitMetadata(ctx context.Context, repoURL string,
}, nil
}

func (svc *argoCDService) getKustomizeOptions(source *v1alpha1.ApplicationSource) (*v1alpha1.KustomizeOptions, error) {
kustomizeSettings, err := svc.settingsMgr.GetKustomizeSettings()
if err != nil {
return nil, err
}
return kustomizeSettings.GetOptions(*source)
}

func (svc *argoCDService) GetAppDetails(ctx context.Context, appSource *v1alpha1.ApplicationSource) (*shared.AppDetail, error) {
argocdDB := db.NewDB(svc.namespace, svc.settingsMgr, svc.clientset)
repo, err := argocdDB.GetRepository(ctx, appSource.RepoURL)
if err != nil {
return nil, err
}
helmRepos, err := argocdDB.ListHelmRepositories(ctx)
if err != nil {
return nil, err
}
kustomizeOptions, err := svc.getKustomizeOptions(appSource)
if err != nil {
return nil, err
}
appDetail, err := svc.repoServerClient.GetAppDetails(ctx, &apiclient.RepoServerAppDetailsQuery{
Repo: repo,
Source: appSource,
Repos: helmRepos,
KustomizeOptions: kustomizeOptions,
})
if err != nil {
return nil, err
}
has := &shared.HelmAppSpec{
Name: appDetail.Helm.Name,
ValueFiles: appDetail.Helm.ValueFiles,
Parameters: appDetail.Helm.Parameters,
Values: appDetail.Helm.Values,
FileParameters: appDetail.Helm.FileParameters,
}
return &shared.AppDetail{
Type: appDetail.Type,
Helm: has,
Ksonnet: appDetail.Ksonnet,
Kustomize: appDetail.Kustomize,
Directory: appDetail.Directory,
}, nil
}

func (svc *argoCDService) Close() {
svc.dispose()
}

0 comments on commit e3fb73b

Please sign in to comment.