aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/yplite
diff options
context:
space:
mode:
authorhcpp <hcpp@ydb.tech>2023-11-08 12:09:41 +0300
committerhcpp <hcpp@ydb.tech>2023-11-08 12:56:14 +0300
commita361f5b98b98b44ea510d274f6769164640dd5e1 (patch)
treec47c80962c6e2e7b06798238752fd3da0191a3f6 /library/go/yandex/yplite
parent9478806fde1f4d40bd5a45e7cbe77237dab613e9 (diff)
downloadydb-a361f5b98b98b44ea510d274f6769164640dd5e1.tar.gz
metrics have been added
Diffstat (limited to 'library/go/yandex/yplite')
-rw-r--r--library/go/yandex/yplite/spec.go46
-rw-r--r--library/go/yandex/yplite/ya.make8
-rw-r--r--library/go/yandex/yplite/yplite.go67
3 files changed, 121 insertions, 0 deletions
diff --git a/library/go/yandex/yplite/spec.go b/library/go/yandex/yplite/spec.go
new file mode 100644
index 0000000000..228f9627ef
--- /dev/null
+++ b/library/go/yandex/yplite/spec.go
@@ -0,0 +1,46 @@
+package yplite
+
+type PodSpec struct {
+ DNS PodDNS `json:"dns"`
+ ResourceRequests ResourceRequest `json:"resourceRequests"`
+ PortoProperties []PortoProperty `json:"portoProperties"`
+ IP6AddressAllocations []IP6AddressAllocation `json:"ip6AddressAllocations"`
+}
+
+type PodAttributes struct {
+ ResourceRequirements struct {
+ CPU struct {
+ Guarantee uint64 `json:"cpu_guarantee_millicores,string"`
+ Limit uint64 `json:"cpu_limit_millicores,string"`
+ } `json:"cpu"`
+ Memory struct {
+ Guarantee uint64 `json:"memory_guarantee_bytes,string"`
+ Limit uint64 `json:"memory_limit_bytes,string"`
+ } `json:"memory"`
+ } `json:"resource_requirements"`
+}
+
+type ResourceRequest struct {
+ CPUGuarantee uint64 `json:"vcpuGuarantee,string"`
+ CPULimit uint64 `json:"vcpuLimit,string"`
+ MemoryGuarantee uint64 `json:"memoryGuarantee,string"`
+ MemoryLimit uint64 `json:"memoryLimit,string"`
+ AnonymousMemoryLimit uint64 `json:"anonymousMemoryLimit,string"`
+}
+
+type IP6AddressAllocation struct {
+ Address string `json:"address"`
+ VlanID string `json:"vlanId"`
+ PersistentFQDN string `json:"persistentFqdn"`
+ TransientFQDN string `json:"transientFqdn"`
+}
+
+type PortoProperty struct {
+ Name string `json:"key"`
+ Value string `json:"value"`
+}
+
+type PodDNS struct {
+ PersistentFqdn string `json:"persistentFqdn"`
+ TransientFqdn string `json:"transientFqdn"`
+}
diff --git a/library/go/yandex/yplite/ya.make b/library/go/yandex/yplite/ya.make
new file mode 100644
index 0000000000..8583357d0a
--- /dev/null
+++ b/library/go/yandex/yplite/ya.make
@@ -0,0 +1,8 @@
+GO_LIBRARY()
+
+SRCS(
+ spec.go
+ yplite.go
+)
+
+END()
diff --git a/library/go/yandex/yplite/yplite.go b/library/go/yandex/yplite/yplite.go
new file mode 100644
index 0000000000..a39d889391
--- /dev/null
+++ b/library/go/yandex/yplite/yplite.go
@@ -0,0 +1,67 @@
+package yplite
+
+import (
+ "context"
+ "encoding/json"
+ "net"
+ "net/http"
+ "os"
+ "time"
+
+ "github.com/ydb-platform/ydb/library/go/core/xerrors"
+)
+
+const (
+ PodSocketPath = "/run/iss/pod.socket"
+ NodeAgentTimeout = 1 * time.Second
+)
+
+var (
+ httpClient = http.Client{
+ Transport: &http.Transport{
+ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
+ return net.DialTimeout("unix", PodSocketPath, NodeAgentTimeout)
+ },
+ },
+ Timeout: NodeAgentTimeout,
+ }
+)
+
+func IsAPIAvailable() bool {
+ if _, err := os.Stat(PodSocketPath); err == nil {
+ return true
+ }
+ return false
+}
+
+func FetchPodSpec() (*PodSpec, error) {
+ res, err := httpClient.Get("http://localhost/pod_spec")
+ if err != nil {
+ return nil, xerrors.Errorf("failed to request pod spec: %w", err)
+ }
+ defer func() { _ = res.Body.Close() }()
+
+ spec := new(PodSpec)
+ err = json.NewDecoder(res.Body).Decode(spec)
+ if err != nil {
+ return nil, xerrors.Errorf("failed to decode pod spec: %w", err)
+ }
+
+ return spec, nil
+}
+
+func FetchPodAttributes() (*PodAttributes, error) {
+ res, err := httpClient.Get("http://localhost/pod_attributes")
+ if err != nil {
+ return nil, xerrors.Errorf("failed to request pod attributes: %w", err)
+ }
+ defer func() { _ = res.Body.Close() }()
+
+ attrs := new(PodAttributes)
+ err = json.NewDecoder(res.Body).Decode(attrs)
+ if err != nil {
+ return nil, xerrors.Errorf("failed to decode pod attributes: %w", err)
+ }
+
+ return attrs, nil
+}