Skip to content

Commit

Permalink
Support deploying/destroying ephemeral environments (argoproj#40)
Browse files Browse the repository at this point in the history
* Support deploying ephemeral environments

* Support deleting application resources

* Fix merge conflict

* Add missing break statement
  • Loading branch information
alexmt authored Mar 16, 2018
1 parent 98754c7 commit 5c062bd
Show file tree
Hide file tree
Showing 14 changed files with 530 additions and 132 deletions.
9 changes: 8 additions & 1 deletion Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ required = [
[[constraint]]
name = "github.com/ksonnet/ksonnet"
branch = "master"

[[constraint]]
name = "github.com/pborman/uuid"
version = "1.1.0"
2 changes: 1 addition & 1 deletion cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func NewApplicationRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.
conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie()
defer util.Close(conn)
for _, appName := range args {
_, err := appIf.Delete(context.Background(), &application.ApplicationQuery{Name: appName})
_, err := appIf.Delete(context.Background(), &application.DeleteApplicationRequest{Name: appName})
errors.CheckError(err)
}
},
Expand Down
2 changes: 2 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (

// LabelKeyApplicationControllerInstanceID is the label which allows to separate application among multiple running application controllers.
LabelKeyApplicationControllerInstanceID = application.ApplicationFullName + "/controller-instanceid"
// LabelApplicationName is the label which indicates that resource belongs to application with the specified name
LabelApplicationName = application.ApplicationFullName + "/app-name"
)

// ArgoCDManagerServiceAccount is the name of the service account for managing a cluster
Expand Down
3 changes: 2 additions & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (ctrl *ApplicationController) tryRefreshAppStatus(app *appv1.Application) (
Revision: revision,
Path: app.Spec.Source.Path,
Environment: app.Spec.Source.Environment,
AppLabel: app.Name,
})
if err != nil {
log.Errorf("Failed to load application manifest %v", err)
Expand All @@ -166,7 +167,7 @@ func (ctrl *ApplicationController) tryRefreshAppStatus(app *appv1.Application) (
targetObjs[i] = &obj
}

server, namespace := argoutil.ResolveServerNamespace(app, manifestInfo)
server, namespace := argoutil.ResolveServerNamespace(app.Spec.Destination, manifestInfo)
comparisonResult, err := ctrl.appComparator.CompareAppState(server, namespace, targetObjs, app)
if err != nil {
return nil, err
Expand Down
50 changes: 45 additions & 5 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package repository

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"

"fmt"

"encoding/json"

"github.com/argoproj/argo-cd/common"
"github.com/argoproj/argo-cd/util"
"github.com/argoproj/argo-cd/util/git"
ksutil "github.com/argoproj/argo-cd/util/ksonnet"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -34,7 +36,24 @@ func NewService(namespace string, kubeClient kubernetes.Interface, gitClient git
}
}

// GenerateManifest generate a manifest for application in specified repo name and revision
func (s *Service) createTempRepo(appRepoPath string, appPath string, sourceEnv string, files map[string]string) (string, error) {
tmpRepoPath, err := ioutil.TempDir("", "")
if err != nil {
return "", err
}
_, err = exec.Command("cp", "-r", appRepoPath+"/", tmpRepoPath).Output()
if err != nil {
return "", err
}
for file, content := range files {
err = ioutil.WriteFile(path.Join(tmpRepoPath, appPath, "environments", sourceEnv, file), []byte(content), 0644)
if err != nil {
return "", err
}
}
return tmpRepoPath, nil
}

func (s *Service) GenerateManifest(c context.Context, q *ManifestRequest) (*ManifestResponse, error) {
appRepoPath := path.Join(os.TempDir(), strings.Replace(q.Repo.Repo, "/", "_", -1))
s.repoLock.Lock(appRepoPath)
Expand All @@ -50,6 +69,19 @@ func (s *Service) GenerateManifest(c context.Context, q *ManifestRequest) (*Mani
return nil, err
}
appPath := path.Join(appRepoPath, q.Path)
if q.InputFiles != nil && len(q.InputFiles) > 0 {
tempRepoPath, err := s.createTempRepo(appRepoPath, q.Path, q.Environment, q.InputFiles)
if err != nil {
return nil, err
}
appPath = path.Join(tempRepoPath, q.Path)
defer func() {
err := os.RemoveAll(tempRepoPath)
if err != nil {
log.Warningf("Unable to cleanup temp directory: %v", err)
}
}()
}
ksApp, err := ksutil.NewKsonnetApp(appPath)
if err != nil {
return nil, err
Expand All @@ -66,6 +98,14 @@ func (s *Service) GenerateManifest(c context.Context, q *ManifestRequest) (*Mani
}
manifests := make([]string, len(targetObjs))
for i, target := range targetObjs {
if q.AppLabel != "" {
labels := target.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[common.LabelApplicationName] = q.AppLabel
target.SetLabels(labels)
}
manifestStr, err := json.Marshal(target.Object)
if err != nil {
return nil, err
Expand Down
81 changes: 50 additions & 31 deletions reposerver/repository/repository.pb.go

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

3 changes: 2 additions & 1 deletion reposerver/repository/repository.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import "google/api/annotations.proto";
import "k8s.io/api/core/v1/generated.proto";
import "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1/generated.proto";


// ManifestRequest is a query for manifest generation.
message ManifestRequest {
github.com.argoproj.argo_cd.pkg.apis.application.v1alpha1.Repository repo = 1;
string revision = 2;
string path = 3;
string environment = 4;
string appLabel = 5;
map<string, string> inputFiles = 6;
}

message ManifestResponse {
Expand Down
Loading

0 comments on commit 5c062bd

Please sign in to comment.