-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.go
More file actions
128 lines (117 loc) · 3.64 KB
/
Copy pathfetch.go
File metadata and controls
128 lines (117 loc) · 3.64 KB
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
package main
import (
"bytes"
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/devtron-labs/source-controller/bean"
"github.com/devtron-labs/source-controller/oci"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/v1/remote"
"k8s.io/apimachinery/pkg/util/json"
"net/http"
"strconv"
)
var (
RegistryURL string = "asia.gcr.io/google-containers"
Insecure bool = true
RepoName string = "addon-resizer-amd64"
ExternalCiId int = 3
ImageShowCount int = 20
)
func fetch() {
var auth authn.Authenticator
keychain := oci.Anonymous{}
transport := remote.DefaultTransport.(*http.Transport).Clone()
opts := makeRemoteOptions(context.Background(), transport, keychain, auth, true)
url, err := parseRepositoryURLInValidFormat(RegistryURL, RepoName)
if err != nil {
fmt.Println("error" + err.Error())
}
tags, err := getAllTags(url, opts.craneOpts)
if err != nil {
fmt.Println("error" + err.Error())
}
digests := make([]string, 0, len(tags))
digestTagMap := make(map[string]string)
for i := 0; i < len(tags) && i < ImageShowCount; i++ {
tag := tags[i]
// Determine which artifact revision to pull
tagUrl, err := getArtifactURLForTag(url, tag)
if err != nil {
fmt.Println("error" + err.Error())
}
digest, err := crane.Digest(tagUrl, opts.craneOpts...)
if err != nil {
fmt.Errorf("error")
}
digestTagMap[digest] = tag
digests = append(digests, digest)
}
//err = impl.FilterAlreadyPresentArtifacts(digests, digestTagMap)
//if err != nil {
// fmt.Println("error"+err.Error())
//}
for digest, tag := range digestTagMap {
CallExternalCIWebHook(digest, tag)
}
fmt.Println("success")
}
// CallingExternalCiWebhook
func CallExternalCIWebHook(digest, tag string) error {
host := RegistryURL
repoName := RepoName
image := fmt.Sprintf("%s/%s:%s", host, repoName, tag)
url := "http://172.190.239.166:30797/orchestrator/webhook/ext-ci/" + strconv.Itoa(ExternalCiId)
payload := bean.GetPayloadForExternalCi(image, digest)
b, err := json.Marshal(payload)
if err != nil {
return err
}
bearer := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTM3MzE4ODIsImp0aSI6Ijk4OThiNTNlLTU1ZTgtNDAxMS1iZjBkLWRlZjlhNGJjNWU5YSIsImlhdCI6MTY5MzY0NTQ4MiwiaXNzIjoiYXJnb2NkIiwibmJmIjoxNjkzNjQ1NDgyLCJzdWIiOiJhZG1pbiJ9.5ME76IAdLJbXhHN2u0oDcPHNmboFEFPYAUrf4rY_Ts0"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
req.Header.Set("api-token", bearer)
req.Header.Add("Content-Type", "application/json")
//req.Header.Add("token", bearer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// 445808685819.dkr.ecr.us-east-2.amazonaws.com/kushagratest
var (
repositoryName = ""
AccessKey = ""
Passkey = ""
EndpointUrl = ""
IsInSecure = ""
Region = ""
registryId = ""
)
func hello() {
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(AccessKey, Passkey, "")))
if err != nil {
fmt.Println("err", err.Error())
return
}
cfg.Region = Region
// Create ECR client from Config
client := ecr.NewFromConfig(cfg)
listImageInput := &ecr.ListImagesInput{
RepositoryName: &repositoryName,
RegistryId: ®istryId,
}
listImagesOutput, err := client.ListImages(context.Background(), listImageInput)
if err != nil {
fmt.Println("err", err.Error())
return
}
imageIdsIdentifier := listImagesOutput.ImageIds
fmt.Println(imageIdsIdentifier)
}