-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (136 loc) · 3.92 KB
/
main.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
package main
import (
"context"
"os"
durationpb "google.golang.org/protobuf/types/known/durationpb"
"time"
"github.com/gleich/lumber/v2"
"github.com/yandex-cloud/go-genproto/yandex/cloud/containerregistry/v1"
ycsdk "github.com/yandex-cloud/go-sdk"
)
func main() {
checkEnv("YANDEX_OAUTH_TOKEN")
checkEnv("YANDEX_FOLDER_ID")
token := os.Getenv("YANDEX_OAUTH_TOKEN")
ctx := context.Background()
sdk, err := ycsdk.Build(ctx, ycsdk.Config{
Credentials: ycsdk.OAuthToken(token),
})
if err != nil {
lumber.Fatal(err)
}
getListDocker(ctx, sdk)
}
func checkEnv(key string) {
_, ok := os.LookupEnv(key)
if !ok {
lumber.FatalMsg("%s not set\n", key)
}
}
func getListDocker(ctx context.Context, sdk *ycsdk.SDK) (string, error) {
listDocker, err := sdk.ContainerRegistry().Repository().List(ctx, &containerregistry.ListRepositoriesRequest{
FolderId: os.Getenv("YANDEX_FOLDER_ID"),
})
for _, v := range listDocker.GetRepositories() {
lumber.Info("Repo", v)
// Получаем список политик для регистри
listPolicies, err := sdk.ContainerRegistry().LifecyclePolicy().List(ctx, &containerregistry.ListLifecyclePoliciesRequest{
Id: &containerregistry.ListLifecyclePoliciesRequest_RepositoryId{
RepositoryId: v.Id,
},
})
policies := listPolicies.GetLifecyclePolicies()
// Если политик нет то создаём нужную
if len(policies) == 0 {
_, err = createPolicy(ctx, sdk, v.Id)
if err != nil {
lumber.ErrorMsg(err)
} else {
lumber.Success("Lifecycle policy successfully created!")
}
} else {
// Если есть ...
for _, z := range listPolicies.GetLifecyclePolicies() {
// То обновляем ту которая ACTIVE
if z.Status == 1 {
_, err = updatePolicy(ctx, sdk, z.Id)
if err != nil {
lumber.ErrorMsg(err)
} else {
lumber.Success("Lifecycle policy successfully updated!")
}
}
}
}
}
return listDocker.String(), err
}
func createPolicy(ctx context.Context, sdk *ycsdk.SDK, RepositoryId_In string) (string, error) {
createPolicy, err := sdk.ContainerRegistry().LifecyclePolicy().Create(ctx, &containerregistry.CreateLifecyclePolicyRequest{
RepositoryId: RepositoryId_In,
Name: "delete",
Description: "delete",
Status: 1,
Rules: PolicyRules,
})
return createPolicy.String(), err
}
func updatePolicy(ctx context.Context, sdk *ycsdk.SDK, LifecyclePolicyId string) (string, error) {
updatePolicy, err := sdk.ContainerRegistry().LifecyclePolicy().Update(ctx, &containerregistry.UpdateLifecyclePolicyRequest{
LifecyclePolicyId: LifecyclePolicyId,
Name: "delete",
Description: "delete",
Status: 1,
Rules: PolicyRules,
})
return updatePolicy.String(), err
}
var PolicyRules = []*containerregistry.LifecycleRule{
{
Description: "clear untagged images",
Untagged: true,
ExpirePeriod: durationpb.New(168 * time.Hour),
},
{
Description: "remove all with tags",
TagRegexp: ".*",
Untagged: false,
ExpirePeriod: durationpb.New(168 * time.Hour),
RetainedTop: 5,
},
{
Description: "clear feature-* images",
TagRegexp: "feature-.*",
Untagged: false,
ExpirePeriod: durationpb.New(168 * time.Hour),
RetainedTop: 10,
},
{
Description: "clear develop images",
TagRegexp: "develop",
Untagged: false,
ExpirePeriod: durationpb.New(168 * time.Hour),
RetainedTop: 10,
},
{
Description: "clear hotfix-* images",
TagRegexp: "hotfix-.*",
Untagged: false,
ExpirePeriod: durationpb.New(720 * time.Hour),
RetainedTop: 10,
},
{
Description: "clear release-* images",
TagRegexp: "release-.*",
Untagged: false,
ExpirePeriod: durationpb.New(720 * time.Hour),
RetainedTop: 10,
},
{
Description: "clear master images",
TagRegexp: "master",
Untagged: false,
ExpirePeriod: durationpb.New(720 * time.Hour),
RetainedTop: 10,
},
}