/
gitops.go
90 lines (77 loc) · 2.64 KB
/
gitops.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
package service
import (
"github.com/codefresh-io/argocd-listener/agent/pkg/api/argo"
"github.com/codefresh-io/argocd-listener/agent/pkg/infra/git/provider"
"github.com/codefresh-io/argocd-listener/agent/pkg/infra/logger"
"github.com/codefresh-io/argocd-listener/agent/pkg/util"
argoSdk "github.com/codefresh-io/argocd-sdk/pkg/api"
codefreshSdk "github.com/codefresh-io/go-sdk/pkg/codefresh"
)
type Gitops interface {
MarkEnvAsRemoved(obj interface{}) (error, *codefreshSdk.Environment)
HandleNewApplications(applications []string) []*EnvironmentWrapper
ExtractNewApplication(application string) (*EnvironmentWrapper, error)
}
type EnvironmentWrapper struct {
Environment codefreshSdk.Environment
Commit provider.ResourceCommit
}
type gitops struct {
argoApi argo.ArgoAPI
argoResourceService ArgoResourceService
envTransformer ETransformer
}
func NewGitopsService() Gitops {
return &gitops{
argoApi: argo.GetInstance(),
envTransformer: GetEnvTransformerInstance(argo.GetInstance()),
argoResourceService: NewArgoResourceService(),
}
}
func (gitops *gitops) MarkEnvAsRemoved(obj interface{}) (error, *codefreshSdk.Environment) {
//disable for now
//envTransformer := env.GetEnvTransformerInstance(argo.GetInstance())
//err, env := envTransformer.PrepareEnvironment(obj.(*unstructured.Unstructured).Object)
//if err != nil {
// return err, env
//}
//
//env.HealthStatus = "Deleted"
//
//err = events.GetRolloutEventHandlerInstance().Handle(env)
//
//if err != nil {
// return err, nil
//}
return nil, nil
}
func (gitops *gitops) ExtractNewApplication(application string) (*EnvironmentWrapper, error) {
applicationObj, err := gitops.argoApi.GetApplication(application)
if err != nil {
return nil, err
}
var app argoSdk.ArgoApplication
util.Convert(applicationObj, &app)
err, historyId := gitops.argoResourceService.ResolveHistoryId(app.Status.History, app.Status.OperationState.SyncResult.Revision, app.Metadata.Name)
if err != nil {
return nil, err
}
err, envWrapper := gitops.envTransformer.PrepareEnvironment(app, historyId)
if err != nil {
return nil, err
}
return envWrapper, nil
}
func (gitops *gitops) HandleNewApplications(applications []string) []*EnvironmentWrapper {
var apps []*EnvironmentWrapper
for _, application := range applications {
newApp, err := gitops.ExtractNewApplication(application)
if err != nil {
logger.GetLogger().Errorf("Failed to handle new gitops application %v, reason: %v", application, err)
continue
}
logger.GetLogger().Infof("Detect new gitops application %s, initiate initialization", application)
apps = append(apps, newApp)
}
return apps
}