Skip to content

Commit ed7b1e0

Browse files
authored
Merge /v1/clusters and /v1/tfcontroller endpoints (#1830)
* Merge /v1/clusters and /v1/tfcontroller endpoints - Preview now includes the common-bases if its been added - /v1/tfcontroller now includes the namespace in the path it writes out. - old: ./clusters/management/clusters/rds.yaml - new: ./clusters/management/clusters/default/rds.yaml
1 parent 71428ed commit ed7b1e0

File tree

12 files changed

+899
-923
lines changed

12 files changed

+899
-923
lines changed

cmd/clusters-service/api/cluster_services.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ message CreatePullRequestRequest {
383383
string cluster_namespace = 12;
384384
repeated Kustomization kustomizations = 13;
385385
string template_namespace = 14;
386+
string template_kind = 15;
386387
}
387388

388389
message CreatePullRequestResponse {

cmd/clusters-service/api/cluster_services.swagger.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,9 @@
12371237
},
12381238
"templateNamespace": {
12391239
"type": "string"
1240+
},
1241+
"templateKind": {
1242+
"type": "string"
12401243
}
12411244
}
12421245
},

cmd/clusters-service/pkg/protos/cluster_services.pb.go

Lines changed: 789 additions & 779 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/clusters-service/pkg/server/clusters.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/client"
3434
"sigs.k8s.io/yaml"
3535

36+
capiv1 "github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/api/capi/v1alpha1"
3637
templatesv1 "github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/api/templates"
3738
"github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/pkg/charts"
3839
"github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/pkg/git"
@@ -130,6 +131,10 @@ func (s *server) ListGitopsClusters(ctx context.Context, msg *capiv1_proto.ListG
130131
}
131132

132133
func (s *server) CreatePullRequest(ctx context.Context, msg *capiv1_proto.CreatePullRequestRequest) (*capiv1_proto.CreatePullRequestResponse, error) {
134+
if msg.TemplateKind == "" {
135+
msg.TemplateKind = capiv1.Kind
136+
}
137+
133138
gp, err := getGitProvider(ctx)
134139
if err != nil {
135140
return nil, grpcStatus.Errorf(codes.Unauthenticated, "error creating pull request: %s", err.Error())
@@ -141,7 +146,7 @@ func (s *server) CreatePullRequest(ctx context.Context, msg *capiv1_proto.Create
141146
s.log.Error(err, "Failed to create pull request, message payload was invalid")
142147
return nil, err
143148
}
144-
tmpl, err := s.getTemplate(ctx, msg.TemplateName, msg.TemplateNamespace, "CAPITemplate")
149+
tmpl, err := s.getTemplate(ctx, msg.TemplateName, msg.TemplateNamespace, msg.TemplateKind)
145150
if err != nil {
146151
return nil, fmt.Errorf("error looking up template %v: %v", msg.TemplateName, err)
147152
}
@@ -166,14 +171,6 @@ func (s *server) CreatePullRequest(ctx context.Context, msg *capiv1_proto.Create
166171
},
167172
}
168173

169-
if viper.GetString("add-bases-kustomization") == "enabled" {
170-
commonKustomization, err := getCommonKustomization(git_files.Cluster)
171-
if err != nil {
172-
return nil, fmt.Errorf("failed to get common kustomization for %s: %s", msg.ParameterValues["CLUSTER_NAME"], err)
173-
}
174-
files = append(files, *commonKustomization)
175-
}
176-
177174
files = append(files, git_files.ProfileFiles...)
178175
files = append(files, git_files.KustomizationFiles...)
179176

cmd/clusters-service/pkg/server/clusters_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ metadata:
520520
kustomize.toolkit.fluxcd.io/prune: disabled
521521
templates.weave.works/create-request: '{"repository_url":"https://github.com/org/repo.git","head_branch":"feature-01","base_branch":"main","title":"New
522522
Cluster","description":"Creates a cluster through a CAPI template","template_name":"cluster-template-1","parameter_values":{"CLUSTER_NAME":"dev","NAMESPACE":"default"},"commit_message":"Add
523-
cluster manifest","values":[{"name":"demo-profile","version":"0.0.1"}],"template_namespace":"default"}'
523+
cluster manifest","values":[{"name":"demo-profile","version":"0.0.1"}],"template_namespace":"default","template_kind":"CAPITemplate"}'
524524
labels:
525525
templates.weave.works/template-name: cluster-template-1
526526
templates.weave.works/template-namespace: default
@@ -627,7 +627,7 @@ metadata:
627627
kustomize.toolkit.fluxcd.io/prune: disabled
628628
templates.weave.works/create-request: '{"repository_url":"https://github.com/org/repo.git","head_branch":"feature-01","base_branch":"main","title":"New
629629
Cluster","description":"Creates a cluster through a CAPI template","template_name":"cluster-template-1","parameter_values":{"CLUSTER_NAME":"dev","NAMESPACE":"clusters-namespace"},"commit_message":"Add
630-
cluster manifest","values":[{"name":"demo-profile","version":"0.0.1","namespace":"test-system"}],"template_namespace":"default"}'
630+
cluster manifest","values":[{"name":"demo-profile","version":"0.0.1","namespace":"test-system"}],"template_namespace":"default","template_kind":"CAPITemplate"}'
631631
labels:
632632
templates.weave.works/template-name: cluster-template-1
633633
templates.weave.works/template-namespace: default
@@ -745,7 +745,7 @@ metadata:
745745
kustomize.toolkit.fluxcd.io/prune: disabled
746746
templates.weave.works/create-request: '{"repository_url":"https://github.com/org/repo.git","head_branch":"feature-01","base_branch":"main","title":"New
747747
Cluster","description":"Creates a cluster through a CAPI template","template_name":"cluster-template-1","parameter_values":{"CLUSTER_NAME":"dev","NAMESPACE":"clusters-namespace"},"commit_message":"Add
748-
cluster manifest","kustomizations":[{"metadata":{"name":"apps-capi","namespace":"flux-system"},"spec":{"path":"./apps/capi","source_ref":{"name":"flux-system","namespace":"flux-system"},"target_namespace":"foo-ns"}},{"metadata":{"name":"apps-billing","namespace":"flux-system"},"spec":{"path":"./apps/billing","source_ref":{"name":"flux-system","namespace":"flux-system"}}}],"template_namespace":"default"}'
748+
cluster manifest","kustomizations":[{"metadata":{"name":"apps-capi","namespace":"flux-system"},"spec":{"path":"./apps/capi","source_ref":{"name":"flux-system","namespace":"flux-system"},"target_namespace":"foo-ns"}},{"metadata":{"name":"apps-billing","namespace":"flux-system"},"spec":{"path":"./apps/billing","source_ref":{"name":"flux-system","namespace":"flux-system"}}}],"template_namespace":"default","template_kind":"CAPITemplate"}'
749749
labels:
750750
templates.weave.works/template-name: cluster-template-1
751751
templates.weave.works/template-namespace: default
@@ -924,6 +924,12 @@ status: {}
924924
}
925925
}
926926

927+
func sortCommitFiles(files []*capiv1_protos.CommitFile) {
928+
sort.Slice(files, func(i, j int) bool {
929+
return files[i].Path < files[j].Path
930+
})
931+
}
932+
927933
func prepCommitedFiles(t *testing.T, serverUrl string, files []*capiv1_protos.CommitFile) []*capiv1_protos.CommitFile {
928934
parsedURL, err := url.Parse(serverUrl)
929935
if err != nil {
@@ -936,6 +942,7 @@ func prepCommitedFiles(t *testing.T, serverUrl string, files []*capiv1_protos.Co
936942
Content: simpleTemplate(t, f.Content, struct{ Port string }{Port: parsedURL.Port()}),
937943
})
938944
}
945+
sortCommitFiles(newFiles)
939946
return newFiles
940947
}
941948

@@ -1360,6 +1367,7 @@ func (p *FakeGitProvider) GetCommittedFiles() []*capiv1_protos.CommitFile {
13601367
Content: *f.Content,
13611368
})
13621369
}
1370+
sortCommitFiles(committedFiles)
13631371
return committedFiles
13641372
}
13651373

cmd/clusters-service/pkg/server/helpers.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ func renderTemplateWithValues(t apitemplates.Template, name, namespace string, v
1919
"templates.weave.works/template-namespace": viper.GetString("capi-templates-namespace"),
2020
}),
2121
}
22-
if viper.GetString("inject-prune-annotation") != "disabled" && isCAPITemplate(t) {
22+
23+
if shouldInjectPruneAnnotation(t) {
2324
opts = append(opts, templates.InjectPruneAnnotation)
2425
}
2526

@@ -39,6 +40,16 @@ func renderTemplateWithValues(t apitemplates.Template, name, namespace string, v
3940
return templateBits, nil
4041
}
4142

43+
func shouldInjectPruneAnnotation(t apitemplates.Template) bool {
44+
anno := t.GetAnnotations()[templates.InjectPruneAnnotationAnnotation]
45+
if anno != "" {
46+
return anno == "true"
47+
}
48+
49+
// FIXME: want to phase configuration option out. You can enable per template by adding the annotation
50+
return viper.GetString("inject-prune-annotation") != "disabled" && isCAPITemplate(t)
51+
}
52+
4253
func getProvider(t apitemplates.Template, annotation string) string {
4354
meta, err := templates.ParseTemplateMeta(t, annotation)
4455

cmd/clusters-service/pkg/server/templates.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func (s *server) RenderTemplate(ctx context.Context, msg *capiv1_proto.RenderTem
250250

251251
func (s *server) getFiles(ctx context.Context, tmpl apiTemplates.Template, msg GetFilesRequest, createRequestMessage *capiv1_proto.CreatePullRequestRequest) (*GetFilesReturn, error) {
252252
clusterNamespace := getClusterNamespace(msg.ParameterValues["NAMESPACE"])
253+
253254
tmplWithValues, err := renderTemplateWithValues(tmpl, msg.TemplateName, getClusterNamespace(msg.ClusterNamespace), msg.ParameterValues)
254255
if err != nil {
255256
return nil, err
@@ -292,11 +293,18 @@ func (s *server) getFiles(ctx context.Context, tmpl apiTemplates.Template, msg G
292293
}
293294

294295
cluster := createNamespacedName(resourceName, clusterNamespace)
295-
content := string(tmplWithValuesAndCredentials[:])
296296

297297
var profileFiles []gitprovider.CommitFile
298298
var kustomizationFiles []gitprovider.CommitFile
299299

300+
if shouldAddCommonBases(tmpl) {
301+
commonKustomization, err := getCommonKustomization(cluster)
302+
if err != nil {
303+
return nil, fmt.Errorf("failed to get common kustomization for %s: %s", msg.ParameterValues, err)
304+
}
305+
kustomizationFiles = append(kustomizationFiles, *commonKustomization)
306+
}
307+
300308
if len(msg.Profiles) > 0 {
301309
profilesFile, err := generateProfileFiles(
302310
ctx,
@@ -339,9 +347,21 @@ func (s *server) getFiles(ctx context.Context, tmpl apiTemplates.Template, msg G
339347
}
340348
}
341349

350+
content := string(tmplWithValuesAndCredentials)
351+
342352
return &GetFilesReturn{RenderedTemplate: content, ProfileFiles: profileFiles, KustomizationFiles: kustomizationFiles, Cluster: cluster, CostEstimate: costEstimate}, err
343353
}
344354

355+
func shouldAddCommonBases(t apiTemplates.Template) bool {
356+
anno := t.GetAnnotations()[templates.AddCommonBasesAnnotation]
357+
if anno != "" {
358+
return anno == "true"
359+
}
360+
361+
// FIXME: want to phase configuration option out. You can enable per template by adding the annotation
362+
return viper.GetString("add-bases-kustomization") != "disabled" && isCAPITemplate(t)
363+
}
364+
345365
func getCostEstimate(ctx context.Context, estimator estimation.Estimator, tmplWithValues [][]byte) *capiv1_proto.CostEstimate {
346366
unstructureds, err := templates.ConvertToUnstructured(tmplWithValues)
347367
if err != nil {

cmd/clusters-service/pkg/server/templates_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
capiv1 "github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/api/capi/v1alpha1"
2121
gapiv1 "github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/api/gitopstemplate/v1alpha1"
22-
"github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/api/templates"
22+
templates "github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/api/templates"
2323
capiv1_protos "github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/pkg/protos"
2424
"github.com/weaveworks/weave-gitops-enterprise/pkg/estimation"
2525
"github.com/weaveworks/weave-gitops/pkg/server/auth"
@@ -760,6 +760,18 @@ func TestRenderTemplate(t *testing.T) {
760760
},
761761
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n kustomize.toolkit.fluxcd.io/prune: disabled\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: test-ns\n",
762762
},
763+
{
764+
name: "enable prune injections via anno",
765+
clusterNamespace: "test-ns",
766+
clusterState: []runtime.Object{
767+
makeCAPITemplate(t, func(ct *capiv1.CAPITemplate) {
768+
ct.ObjectMeta.Annotations = map[string]string{
769+
"templates.weave.works/inject-prune-annotation": "true",
770+
}
771+
}),
772+
},
773+
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n kustomize.toolkit.fluxcd.io/prune: disabled\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: test-ns\n",
774+
},
763775
{
764776
name: "enable prune injections with non-CAPI template",
765777
pruneEnvVar: "enabled",
@@ -1078,8 +1090,27 @@ status: {}
10781090
},
10791091
},
10801092
expected: &capiv1_protos.RenderTemplateResponse{
1081-
RenderedTemplate: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: \"\"\n name: dev\n namespace: test-ns\n",
1082-
KustomizationFiles: []*capiv1_protos.CommitFile{},
1093+
RenderedTemplate: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: \"\"\n name: dev\n namespace: test-ns\n",
1094+
KustomizationFiles: []*capiv1_protos.CommitFile{
1095+
{
1096+
Path: "clusters/clusters-namespace/dev/clusters-bases-kustomization.yaml",
1097+
Content: `apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
1098+
kind: Kustomization
1099+
metadata:
1100+
creationTimestamp: null
1101+
name: clusters-bases-kustomization
1102+
namespace: flux-system
1103+
spec:
1104+
interval: 10m0s
1105+
path: clusters/bases
1106+
prune: true
1107+
sourceRef:
1108+
kind: GitRepository
1109+
name: flux-system
1110+
status: {}
1111+
`,
1112+
},
1113+
},
10831114
ProfileFiles: []*capiv1_protos.CommitFile{
10841115
{
10851116
Path: "clusters/clusters-namespace/dev/profiles.yaml",
@@ -1153,6 +1184,7 @@ status: {}
11531184
t.Run(tt.name, func(t *testing.T) {
11541185
viper.Reset()
11551186
viper.SetDefault("runtime-namespace", "default")
1187+
viper.SetDefault("add-bases-kustomization", "disabled")
11561188
viper.SetDefault("inject-prune-annotation", tt.pruneEnvVar)
11571189
viper.SetDefault("capi-clusters-namespace", tt.clusterNamespace)
11581190
viper.SetDefault("capi-repository-clusters-path", "clusters")

0 commit comments

Comments
 (0)