Skip to content

Commit

Permalink
Maintain list of recent deployments in app CRD (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmt authored Mar 29, 2018
1 parent 6d79361 commit 59dbe8d
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 191 deletions.
344 changes: 220 additions & 124 deletions pkg/apis/application/v1alpha1/generated.pb.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions pkg/apis/application/v1alpha1/generated.proto

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

10 changes: 6 additions & 4 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (

// DeploymentInfo contains information relevant to an application deployment
type DeploymentInfo struct {
Params []ComponentParameter `json:"params" protobuf:"bytes,1,name=params"`
AppSource ApplicationSource `json:"appSource" protobuf:"bytes,2,opt,name=appSource"`
Params []ComponentParameter `json:"params" protobuf:"bytes,1,name=params"`
Revision string `json:"revision" protobuf:"bytes,2,opt,name=revision"`
ComponentParameterOverrides []ComponentParameter `json:"componentParameterOverrides,omitempty" protobuf:"bytes,3,opt,name=componentParameterOverrides"`
DeployedAt metav1.Time `json:"deployedAt" protobuf:"bytes,4,opt,name=deployedAt"`
}

// Application is a definition of Application resource.
Expand Down Expand Up @@ -100,8 +102,8 @@ const (

// ApplicationStatus contains information about application status in target environment.
type ApplicationStatus struct {
ComparisonResult ComparisonResult `json:"comparisonResult" protobuf:"bytes,1,opt,name=comparisonResult"`
RecentDeployment DeploymentInfo `json:"recentDeployment" protobuf:"bytes,2,opt,name=recentDeployment"`
ComparisonResult ComparisonResult `json:"comparisonResult" protobuf:"bytes,1,opt,name=comparisonResult"`
RecentDeployments []DeploymentInfo `json:"recentDeployments" protobuf:"bytes,2,opt,name=recentDeployment"`
}

// ComparisonResult is a comparison result of application spec and deployed application.
Expand Down
15 changes: 13 additions & 2 deletions pkg/apis/application/v1alpha1/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ func (in *ApplicationSpec) DeepCopy() *ApplicationSpec {
func (in *ApplicationStatus) DeepCopyInto(out *ApplicationStatus) {
*out = *in
in.ComparisonResult.DeepCopyInto(&out.ComparisonResult)
in.RecentDeployment.DeepCopyInto(&out.RecentDeployment)
if in.RecentDeployments != nil {
in, out := &in.RecentDeployments, &out.RecentDeployments
*out = make([]DeploymentInfo, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}

Expand Down Expand Up @@ -272,7 +278,12 @@ func (in *DeploymentInfo) DeepCopyInto(out *DeploymentInfo) {
*out = make([]ComponentParameter, len(*in))
copy(*out, *in)
}
in.AppSource.DeepCopyInto(&out.AppSource)
if in.ComponentParameterOverrides != nil {
in, out := &in.ComponentParameterOverrides, &out.ComponentParameterOverrides
*out = make([]ComponentParameter, len(*in))
copy(*out, *in)
}
in.DeployedAt.DeepCopyInto(&out.DeployedAt)
return
}

Expand Down
5 changes: 3 additions & 2 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *Service) GenerateManifest(c context.Context, q *ManifestRequest) (*Mani
return nil, err
}

err = s.gitClient.Checkout(appRepoPath, q.Revision)
revision, err := s.gitClient.Checkout(appRepoPath, q.Revision)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -96,6 +96,7 @@ func (s *Service) GenerateManifest(c context.Context, q *ManifestRequest) (*Mani
manifests[i] = string(manifestStr)
}
return &ManifestResponse{
Revision: revision,
Manifests: manifests,
Namespace: env.Destination.Namespace,
Server: env.Destination.Server,
Expand All @@ -113,7 +114,7 @@ func (s *Service) GetEnvParams(c context.Context, q *EnvParamsRequest) (*EnvPara
return nil, err
}

err = s.gitClient.Checkout(appRepoPath, q.Revision)
_, err = s.gitClient.Checkout(appRepoPath, q.Revision)
if err != nil {
return nil, err
}
Expand Down
68 changes: 38 additions & 30 deletions reposerver/repository/repository.pb.go

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

1 change: 1 addition & 0 deletions reposerver/repository/repository.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ message ManifestResponse {
repeated string manifests = 1;
string namespace = 2;
string server = 3;
string revision = 4;
}

message EnvParamsRequest {
Expand Down
47 changes: 29 additions & 18 deletions server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package application
import (
"fmt"

"time"

"github.com/argoproj/argo-cd/common"
appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
appclientset "github.com/argoproj/argo-cd/pkg/client/clientset/versioned"
Expand All @@ -23,6 +25,10 @@ import (
"k8s.io/client-go/kubernetes"
)

const (
maxRecentDeploymentsCnt = 5
)

// Server provides a Application service
type Server struct {
ns string
Expand Down Expand Up @@ -149,7 +155,7 @@ func (s *Server) Sync(ctx context.Context, syncReq *ApplicationSyncRequest) (*Ap
defer util.Close(conn)
// set fields in v1alpha/types.go
log.Infof("Retrieving deployment params for application %s", syncReq.Name)
deploymentInfo, err := repoClient.GetEnvParams(ctx, &repository.EnvParamsRequest{
envParams, err := repoClient.GetEnvParams(ctx, &repository.EnvParamsRequest{
Repo: repo,
Environment: app.Spec.Source.Environment,
Path: app.Spec.Source.Path,
Expand All @@ -159,23 +165,28 @@ func (s *Server) Sync(ctx context.Context, syncReq *ApplicationSyncRequest) (*Ap
if err != nil {
return nil, err
}
log.Infof("Received deployment params: %s", deploymentInfo.Params)
log.Infof("Received deployment params: %s", envParams.Params)

res, err := s.deploy(ctx, app.Spec.Source, app.Spec.Destination, app.Name, syncReq.DryRun)
res, manifest, err := s.deploy(ctx, app.Spec.Source, app.Spec.Destination, app.Name, syncReq.DryRun)
if err == nil {
// Persist app deployment info
params := make([]appv1.ComponentParameter, len(deploymentInfo.Params))
for i := range deploymentInfo.Params {
param := *deploymentInfo.Params[i]
params := make([]appv1.ComponentParameter, len(envParams.Params))
for i := range envParams.Params {
param := *envParams.Params[i]
params[i] = param
}
app, err = s.Get(ctx, &ApplicationQuery{Name: syncReq.Name})
if err != nil {
return nil, err
}
app.Status.RecentDeployment = appv1.DeploymentInfo{
AppSource: app.Spec.Source,
Params: params,
app.Status.RecentDeployments = append(app.Status.RecentDeployments, appv1.DeploymentInfo{
ComponentParameterOverrides: app.Spec.Source.ComponentParameterOverrides,
Revision: manifest.Revision,
Params: params,
DeployedAt: metav1.NewTime(time.Now()),
})
if len(app.Status.RecentDeployments) > maxRecentDeploymentsCnt {
app.Status.RecentDeployments = app.Status.RecentDeployments[:maxRecentDeploymentsCnt]
}
_, err = s.Update(ctx, app)
if err != nil {
Expand All @@ -199,12 +210,12 @@ func (s *Server) deploy(
source appv1.ApplicationSource,
destination *appv1.ApplicationDestination,
appLabel string,
dryRun bool) (*ApplicationSyncResult, error) {
dryRun bool) (*ApplicationSyncResult, *repository.ManifestResponse, error) {

repo := s.getRepo(ctx, source.RepoURL)
conn, repoClient, err := s.repoClientset.NewRepositoryClient()
if err != nil {
return nil, err
return nil, nil, err
}
defer util.Close(conn)
overrides := make([]*appv1.ComponentParameter, len(source.ComponentParameterOverrides))
Expand All @@ -224,32 +235,32 @@ func (s *Server) deploy(
AppLabel: appLabel,
})
if err != nil {
return nil, err
return nil, nil, err
}
server, namespace := argoutil.ResolveServerNamespace(destination, manifestInfo)

clst, err := s.clusterService.Get(ctx, &cluster.ClusterQuery{Server: server})
if err != nil {
return nil, err
return nil, nil, err
}
config := clst.RESTConfig()

targetObjs := make([]*unstructured.Unstructured, len(manifestInfo.Manifests))
for i, manifest := range manifestInfo.Manifests {
obj, err := appv1.UnmarshalToUnstructured(manifest)
if err != nil {
return nil, err
return nil, nil, err
}
targetObjs[i] = obj
}

liveObjs, err := kube.GetLiveResources(config, targetObjs, namespace)
if err != nil {
return nil, err
return nil, nil, err
}
diffResList, err := diff.DiffArray(targetObjs, liveObjs)
if err != nil {
return nil, err
return nil, nil, err
}
var syncRes ApplicationSyncResult
syncRes.Resources = make([]*ResourceDetails, 0)
Expand All @@ -271,7 +282,7 @@ func (s *Server) deploy(
} else {
_, err := kube.ApplyResource(config, targetObjs[i], namespace)
if err != nil {
return nil, err
return nil, nil, err
}
if needsCreate {
resDetails.Message = fmt.Sprintf("created")
Expand All @@ -282,5 +293,5 @@ func (s *Server) deploy(
syncRes.Resources = append(syncRes.Resources, &resDetails)
}
syncRes.Message = "successfully synced"
return &syncRes, nil
return &syncRes, manifestInfo, nil
}
4 changes: 2 additions & 2 deletions test/e2e/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ func (c *FakeGitClient) CloneOrFetch(repo string, username string, password stri
return err
}

func (c *FakeGitClient) Checkout(repoPath string, sha string) error {
func (c *FakeGitClient) Checkout(repoPath string, sha string) (string, error) {
// do nothing
return nil
return "latest", nil
}

func (c *FakeGitClient) Reset(repoPath string) error {
Expand Down
20 changes: 13 additions & 7 deletions util/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Client is a generic git client interface
type Client interface {
CloneOrFetch(url string, username string, password string, sshPrivateKey string, repoPath string) error
Checkout(repoPath string, sha string) error
Checkout(repoPath string, sha string) (string, error)
Reset(repoPath string) error
}

Expand Down Expand Up @@ -86,18 +86,24 @@ func (m *NativeGitClient) Reset(repoPath string) error {
}

// Checkout checkout specified git sha
func (m *NativeGitClient) Checkout(repoPath string, sha string) error {
func (m *NativeGitClient) Checkout(repoPath string, sha string) (string, error) {
if sha == "" {
sha = "origin/HEAD"
}
cmd := exec.Command("git", "checkout", sha)
cmd.Dir = repoPath
_, err := cmd.Output()
checkoutCmd := exec.Command("git", "checkout", sha)
checkoutCmd.Dir = repoPath
_, err := checkoutCmd.Output()
if err != nil {
return fmt.Errorf("unable to checkout revision %s: %v", sha, err)
return "", fmt.Errorf("unable to checkout revision %s: %v", sha, err)
}
revisionCmd := exec.Command("git", "rev-parse", "HEAD")
revisionCmd.Dir = repoPath
output, err := revisionCmd.Output()
if err != nil {
return "", err
}

return nil
return string(output), nil

}

Expand Down

0 comments on commit 59dbe8d

Please sign in to comment.