This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
argocd.go
86 lines (73 loc) · 3.3 KB
/
argocd.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
/*
Copyright © 2021 Michael Gruener
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package argocd
import (
// "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"fmt"
"strings"
"github.com/bedag/kusible/pkg/playbook/config"
"sigs.k8s.io/yaml"
)
// ApplicationFromPlay renders a a set of ArgoCD Application resources (see https://argoproj.github.io/argo-cd/operator-manual/declarative-setup/)
// for a given play. Each chart of the play results in a separate Application resource containing
// the details of the helm release (release name, chart name, repo of the chart, chart version, values).
//
// The project parameter is the argocd project the application should belong to
// The namespace parameter is the namespace where ArgoCD is expection Application resources
// The server parameter is the server name(!) as configured in ArgoCD where ArgoCD should deploy the rendered resources
func ApplicationFromPlay(play *config.Play, project string, namespace string, server string) (string, error) {
// https://github.com/argoproj/argo-cd/blob/master/pkg/apis/application/v1alpha1/types.go
result := ""
for _, chart := range play.Charts {
app := Application{}
// global Application resource settings
app.APIVersion = "argoproj.io/v1alpha1"
app.Kind = "Application"
app.ObjectMeta.Namespace = namespace
// TODO: Implement a proper approach to avoid argocd application name collisions.
// All argocd application resources for all clusters exist in the same namespace
// on the cluster hosting argocd. Aside from a proper name generation this requires
// a collision detection because the config structure of kusible allows for a setup
// that leads to non-preventable name collisions.
app.ObjectMeta.Name = fmt.Sprintf("%s.%s.%s", chart.Name, project, server)
app.Spec.Project = project
// helm chart settings
for _, repo := range play.Repos {
if repo.Name == chart.Repo {
app.Spec.Source.RepoURL = repo.URL
}
}
if app.Spec.Source.RepoURL == "" {
return result, fmt.Errorf("no repo '%s' for chart '%s' configured in play", chart.Repo, chart.Name)
}
app.Spec.Source.Chart = chart.Chart
app.Spec.Source.TargetRevision = chart.Version
// design decision: only support helm 3
app.Spec.Source.Helm = &ApplicationSourceHelm{}
app.Spec.Source.Helm.Version = "v3"
values, err := yaml.Marshal(chart.Values)
if err != nil {
return result, fmt.Errorf("failed to convert values of chart '%s' to yaml: %s", chart.Name, err)
}
app.Spec.Source.Helm.Values = string(values)
// target cluster + namespace settings
app.Spec.Destination.Namespace = chart.Namespace
app.Spec.Destination.Name = server
manifest, err := yaml.Marshal(app)
if err != nil {
return result, err
}
result = fmt.Sprintf("---\n%s%s\n", result, strings.TrimSpace(string(manifest)))
}
return result, nil
}