-
Notifications
You must be signed in to change notification settings - Fork 21
/
manifests.go
97 lines (82 loc) · 3.24 KB
/
manifests.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
package argo_client
import (
"context"
"fmt"
"time"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/settings"
argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
repoapiclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient"
"github.com/argoproj/argo-cd/v2/reposerver/repository"
"github.com/argoproj/argo-cd/v2/util/git"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/api/resource"
"github.com/zapier/kubechecks/telemetry"
)
func GetManifestsLocal(ctx context.Context, argoClient *ArgoClient, name, tempRepoDir, changedAppFilePath string, app argoappv1.Application) ([]string, error) {
var err error
ctx, span := tracer.Start(ctx, "GetManifestsLocal")
defer span.End()
log.Debug().Str("name", name).Msg("GetManifestsLocal")
start := time.Now()
defer func() {
duration := time.Since(start)
getManifestsDuration.WithLabelValues(name).Observe(duration.Seconds())
}()
clusterCloser, clusterClient := argoClient.GetClusterClient()
defer clusterCloser.Close()
settingsCloser, settingsClient := argoClient.GetSettingsClient()
defer settingsCloser.Close()
log.Debug().
Str("clusterName", app.Spec.Destination.Name).
Str("clusterServer", app.Spec.Destination.Server).
Msg("getting cluster")
cluster, err := clusterClient.Get(ctx, &cluster.ClusterQuery{Name: app.Spec.Destination.Name, Server: app.Spec.Destination.Server})
if err != nil {
telemetry.SetError(span, err, "Argo Get Cluster")
getManifestsFailed.WithLabelValues(name).Inc()
return nil, errors.Wrap(err, "failed to get cluster")
}
argoSettings, err := settingsClient.Get(ctx, &settings.SettingsQuery{})
if err != nil {
telemetry.SetError(span, err, "Argo Get Settings")
getManifestsFailed.WithLabelValues(name).Inc()
return nil, errors.Wrap(err, "failed to get settings")
}
source := app.Spec.GetSource()
log.Debug().Str("name", name).Msg("generating diff for application...")
res, err := repository.GenerateManifests(ctx, fmt.Sprintf("%s/%s", tempRepoDir, changedAppFilePath), tempRepoDir, source.TargetRevision, &repoapiclient.ManifestRequest{
Repo: &argoappv1.Repository{Repo: source.RepoURL},
AppLabelKey: argoSettings.AppLabelKey,
AppName: app.Name,
Namespace: app.Spec.Destination.Namespace,
ApplicationSource: &source,
KustomizeOptions: argoSettings.KustomizeOptions,
KubeVersion: cluster.Info.ServerVersion,
ApiVersions: cluster.Info.APIVersions,
TrackingMethod: argoSettings.TrackingMethod,
}, true, &git.NoopCredsStore{}, resource.MustParse("0"), nil)
if err != nil {
telemetry.SetError(span, err, "Generate Manifests")
return nil, errors.Wrap(err, "failed to generate manifests")
}
if res.Manifests == nil {
return nil, nil
}
getManifestsSuccess.WithLabelValues(name).Inc()
return res.Manifests, nil
}
func ConvertJsonToYamlManifests(jsonManifests []string) []string {
var manifests []string
for _, manifest := range jsonManifests {
ret, err := yaml.JSONToYAML([]byte(manifest))
if err != nil {
log.Warn().Err(err).Msg("Failed to format manifest")
continue
}
manifests = append(manifests, fmt.Sprintf("---\n%s", string(ret)))
}
return manifests
}