forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 3
/
repository.go
213 lines (193 loc) · 5.96 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package repository
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
"github.com/argoproj/argo-cd/common"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"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/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes"
)
// Service implements ManifestService interface
type Service struct {
ns string
kubeClient kubernetes.Interface
gitClient git.Client
repoLock *util.KeyLock
}
// NewService returns a new instance of the Manifest service
func NewService(namespace string, kubeClient kubernetes.Interface, gitClient git.Client) *Service {
return &Service{
ns: namespace,
kubeClient: kubeClient,
gitClient: gitClient,
repoLock: util.NewKeyLock(),
}
}
func (s *Service) GetKsonnetApp(ctx context.Context, in *KsonnetAppRequest) (*KsonnetAppResponse, error) {
appRepoPath := path.Join(os.TempDir(), strings.Replace(in.Repo.Repo, "/", "_", -1))
s.repoLock.Lock(appRepoPath)
defer func() {
err := s.gitClient.Reset(appRepoPath)
if err != nil {
log.Warn(err)
}
s.repoLock.Unlock(appRepoPath)
}()
ksApp, err := s.getAppSpec(*in.Repo, appRepoPath, in.Revision, in.Path)
if err != nil {
return nil, err
}
return ksAppToResponse(ksApp)
}
// ksAppToResponse converts a Ksonnet app instance to a API response object
func ksAppToResponse(ksApp ksutil.KsonnetApp) (*KsonnetAppResponse, error) {
var appRes KsonnetAppResponse
appRes.Environments = make(map[string]*KsonnetEnvironment)
for envName, env := range ksApp.Spec().Environments {
if env.Destination == nil {
return nil, fmt.Errorf("Environment '%s' has no destination defined", envName)
}
envRes := KsonnetEnvironment{
Name: envName,
K8SVersion: env.KubernetesVersion,
Path: env.Path,
Destination: &KsonnetEnvironmentDestination{
Server: env.Destination.Server,
Namespace: env.Destination.Namespace,
},
}
appRes.Environments[envName] = &envRes
}
return &appRes, 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)
defer func() {
err := s.gitClient.Reset(appRepoPath)
if err != nil {
log.Warn(err)
}
s.repoLock.Unlock(appRepoPath)
}()
err := s.gitClient.CloneOrFetch(q.Repo.Repo, q.Repo.Username, q.Repo.Password, q.Repo.SSHPrivateKey, appRepoPath)
if err != nil {
return nil, err
}
revision, err := s.gitClient.Checkout(appRepoPath, q.Revision)
if err != nil {
return nil, err
}
appPath := path.Join(appRepoPath, q.Path)
ksApp, err := ksutil.NewKsonnetApp(appPath)
if err != nil {
return nil, fmt.Errorf("unable to load application from %s: %v", appPath, err)
}
if q.ComponentParameterOverrides != nil {
for _, override := range q.ComponentParameterOverrides {
err = ksApp.SetComponentParams(q.Environment, override.Component, override.Name, override.Value)
if err != nil {
return nil, err
}
}
}
appSpec := ksApp.App()
env, err := appSpec.Environment(q.Environment)
if err != nil {
return nil, fmt.Errorf("environment '%s' does not exist in ksonnet app", q.Environment)
}
targetObjs, err := ksApp.Show(q.Environment)
if err != nil {
return nil, err
}
manifests := make([]string, len(targetObjs))
for i, target := range targetObjs {
if q.AppLabel != "" {
err = s.setAppLabels(target, q.AppLabel)
if err != nil {
return nil, err
}
}
manifestStr, err := json.Marshal(target.Object)
if err != nil {
return nil, err
}
manifests[i] = string(manifestStr)
}
return &ManifestResponse{
Revision: revision,
Manifests: manifests,
Namespace: env.Destination.Namespace,
Server: env.Destination.Server,
}, nil
}
func (s *Service) getAppSpec(repo v1alpha1.Repository, appRepoPath, revision, subPath string) (ksutil.KsonnetApp, error) {
err := s.gitClient.CloneOrFetch(repo.Repo, repo.Username, repo.Password, repo.SSHPrivateKey, appRepoPath)
if err != nil {
return nil, err
}
_, err = s.gitClient.Checkout(appRepoPath, revision)
if err != nil {
return nil, err
}
appPath := path.Join(appRepoPath, subPath)
ksApp, err := ksutil.NewKsonnetApp(appPath)
if err != nil {
return nil, err
}
return ksApp, nil
}
func (s *Service) setAppLabels(target *unstructured.Unstructured, appName string) error {
labels := target.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[common.LabelApplicationName] = appName
target.SetLabels(labels)
// special case for deployment: make sure that derived replicaset and pod has application label
if target.GetKind() == "Deployment" {
labels, ok := unstructured.NestedMap(target.UnstructuredContent(), "spec", "template", "metadata", "labels")
if ok {
if labels == nil {
labels = make(map[string]interface{})
}
labels[common.LabelApplicationName] = appName
}
unstructured.SetNestedMap(target.UnstructuredContent(), labels, "spec", "template", "metadata", "labels")
}
return nil
}
// GetEnvParams retrieves Ksonnet environment params in specified repo name and revision
func (s *Service) GetEnvParams(c context.Context, q *EnvParamsRequest) (*EnvParamsResponse, error) {
appRepoPath := path.Join(os.TempDir(), strings.Replace(q.Repo.Repo, "/", "_", -1))
s.repoLock.Lock(appRepoPath)
defer s.repoLock.Unlock(appRepoPath)
err := s.gitClient.CloneOrFetch(q.Repo.Repo, q.Repo.Username, q.Repo.Password, q.Repo.SSHPrivateKey, appRepoPath)
if err != nil {
return nil, err
}
_, err = s.gitClient.Checkout(appRepoPath, q.Revision)
if err != nil {
return nil, err
}
appPath := path.Join(appRepoPath, q.Path)
ksApp, err := ksutil.NewKsonnetApp(appPath)
if err != nil {
return nil, err
}
target, err := ksApp.ListEnvParams(q.Environment)
if err != nil {
return nil, err
}
return &EnvParamsResponse{
Params: target,
}, nil
}