Skip to content

Commit 4f052d7

Browse files
smi-miMariia Smirnova
andauthored
Metadata removing sdk dependency (#11)
* removed sdk dependency from metadata package * fixed Dockerfile config arg * add net/url usage Co-authored-by: Mariia Smirnova <mariia-smi@yandex-team.ru>
1 parent 4fe94e0 commit 4f052d7

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ARG golang_version=1.17.8
55
FROM golang:${golang_version} as builder
66
ARG plugin_version
77
ARG fluent_bit_version
8-
ARG config=github.com/yandex-cloud/fluent-bit-plugin-yandex/config
8+
ARG config=github.com/yandex-cloud/fluent-bit-plugin-yandex/v2/config
99
WORKDIR /build
1010
COPY . .
1111
RUN CGO_ENABLED=1 go build \

metadata/metadata.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"net/http"
8+
"net/url"
89
"os"
910
"strings"
1011
"sync"
@@ -13,33 +14,41 @@ import (
1314
"google.golang.org/protobuf/types/known/structpb"
1415

1516
"github.com/startdusk/strnaming"
16-
ycsdk "github.com/yandex-cloud/go-sdk"
1717
)
1818

19-
func getMetadataUrl() string {
19+
func getMetadataUrl(instanceMetadataAddr string) (*url.URL, error) {
2020
const (
2121
keyMetadataUrlEnv = "YC_METADATA_URL"
22-
urlSuffix = "/computeMetadata/v1/"
22+
urlPath = "/computeMetadata/v1/"
2323
)
2424
metadataEndpoint := os.Getenv(keyMetadataUrlEnv)
2525
if len(metadataEndpoint) == 0 {
26-
metadataEndpoint = "http://" + ycsdk.InstanceMetadataAddr
26+
metadataEndpoint = "http://" + instanceMetadataAddr
2727
}
28-
return metadataEndpoint + urlSuffix
28+
urlMetadata, err := url.Parse(metadataEndpoint)
29+
if err != nil {
30+
return nil, err
31+
}
32+
urlMetadata.Path = urlPath
33+
return urlMetadata, nil
2934
}
3035

3136
type Provider interface {
3237
GetValue(key string) (string, error)
3338
}
3439

3540
type cachingProvider struct {
36-
mu sync.RWMutex
37-
lastUpdate time.Time
38-
cache *structpb.Struct
41+
mu sync.RWMutex
42+
instanceMetadataAddr string
43+
lastUpdate time.Time
44+
cache *structpb.Struct
3945
}
4046

4147
func (mp *cachingProvider) GetValue(key string) (string, error) {
4248
cache, err := mp.getAllMetadata()
49+
if err != nil {
50+
return "", fmt.Errorf("failed to get metadata value by key%q: %s", key, err.Error())
51+
}
4352

4453
toCamel := strnaming.NewCamel()
4554
toCamel.WithDelimiter('-')
@@ -48,30 +57,38 @@ func (mp *cachingProvider) GetValue(key string) (string, error) {
4857

4958
value, err := getValue(cache, path)
5059
if err != nil {
51-
return "", fmt.Errorf("failed to get metadata value by key %q because of error: %s", key, err.Error())
60+
return "", fmt.Errorf("failed to get metadata value by key %q: %s", key, err.Error())
5261
}
5362
return value, nil
5463
}
5564

5665
func (mp *cachingProvider) getAllMetadata() (*structpb.Struct, error) {
57-
const updateBackoff = time.Second
66+
const (
67+
updateBackoff = time.Second
68+
queryParamKey = "recursive"
69+
queryParamValue = "true"
70+
requestTimeout = 5 * time.Second
71+
)
72+
5873
mp.mu.RLock()
74+
5975
passed := time.Since(mp.lastUpdate)
6076
if mp.cache != nil && passed < updateBackoff {
6177
defer mp.mu.RUnlock()
6278
return mp.cache, nil
6379
}
64-
mp.mu.RUnlock()
65-
66-
const (
67-
queryParam = "?recursive=true"
68-
requestTimeout = 5 * time.Second
69-
)
80+
urlMetadata, err := getMetadataUrl(mp.instanceMetadataAddr)
81+
if err != nil {
82+
return nil, fmt.Errorf("incorrect metadata URL: %s", err.Error())
83+
}
84+
q := urlMetadata.Query()
85+
q.Set(queryParamKey, queryParamValue)
86+
urlMetadata.RawQuery = q.Encode()
7087

71-
urlMetadata := getMetadataUrl() + queryParam
88+
mp.mu.RUnlock()
7289

7390
client := http.Client{}
74-
req, err := http.NewRequest(http.MethodGet, urlMetadata, nil)
91+
req, err := http.NewRequest(http.MethodGet, urlMetadata.String(), nil)
7592
if err != nil {
7693
return nil, fmt.Errorf("could not make request to get all metadata: %s", err.Error())
7794
}
@@ -105,6 +122,8 @@ func (mp *cachingProvider) getAllMetadata() (*structpb.Struct, error) {
105122
return metadataStruct, nil
106123
}
107124

108-
func NewCachingProvider() Provider {
109-
return &cachingProvider{}
125+
func NewCachingProvider(instanceMetadataAddr string) Provider {
126+
return &cachingProvider{
127+
instanceMetadataAddr: instanceMetadataAddr,
128+
}
110129
}

yclogging.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"unsafe"
66

7+
ycsdk "github.com/yandex-cloud/go-sdk"
8+
79
"github.com/yandex-cloud/fluent-bit-plugin-yandex/v2/yclient"
810

911
"github.com/yandex-cloud/fluent-bit-plugin-yandex/v2/config"
@@ -33,7 +35,7 @@ func FLBPluginInit(plugin unsafe.Pointer) int {
3335
getConfigValue := func(key string) string {
3436
return config.GetKey(plugin, key)
3537
}
36-
metadataProvider := metadata.NewCachingProvider()
38+
metadataProvider := metadata.NewCachingProvider(ycsdk.InstanceMetadataAddr)
3739

3840
destination, err := config.GetDestination(getConfigValue, metadataProvider)
3941
if err != nil {

0 commit comments

Comments
 (0)