aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/deploy
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/deploy
parent9478806fde1f4d40bd5a45e7cbe77237dab613e9 (diff)
downloadydb-a361f5b98b98b44ea510d274f6769164640dd5e1.tar.gz
metrics have been added
Diffstat (limited to 'library/go/yandex/deploy')
-rw-r--r--library/go/yandex/deploy/podagent/client.go66
-rw-r--r--library/go/yandex/deploy/podagent/doc.go4
-rw-r--r--library/go/yandex/deploy/podagent/env.go33
-rw-r--r--library/go/yandex/deploy/podagent/options.go17
-rw-r--r--library/go/yandex/deploy/podagent/responses.go82
-rw-r--r--library/go/yandex/deploy/podagent/ya.make15
6 files changed, 217 insertions, 0 deletions
diff --git a/library/go/yandex/deploy/podagent/client.go b/library/go/yandex/deploy/podagent/client.go
new file mode 100644
index 0000000000..8f87d0e682
--- /dev/null
+++ b/library/go/yandex/deploy/podagent/client.go
@@ -0,0 +1,66 @@
+package podagent
+
+import (
+ "context"
+ "time"
+
+ "github.com/go-resty/resty/v2"
+ "github.com/ydb-platform/ydb/library/go/core/xerrors"
+ "github.com/ydb-platform/ydb/library/go/httputil/headers"
+)
+
+const (
+ EndpointURL = "http://127.0.0.1:1/"
+ HTTPTimeout = 500 * time.Millisecond
+)
+
+type Client struct {
+ httpc *resty.Client
+}
+
+func NewClient(opts ...Option) *Client {
+ c := &Client{
+ httpc: resty.New().
+ SetBaseURL(EndpointURL).
+ SetTimeout(HTTPTimeout),
+ }
+
+ for _, opt := range opts {
+ opt(c)
+ }
+ return c
+}
+
+// PodAttributes returns current pod attributes.
+//
+// Documentation: https://deploy.yandex-team.ru/docs/reference/api/pod-agent-public-api#localhost:1pod_attributes
+func (c *Client) PodAttributes(ctx context.Context) (rsp PodAttributesResponse, err error) {
+ err = c.call(ctx, "/pod_attributes", &rsp)
+ return
+}
+
+// PodStatus returns current pod status.
+//
+// Documentation: https://deploy.yandex-team.ru/docs/reference/api/pod-agent-public-api#localhost:1pod_status
+func (c *Client) PodStatus(ctx context.Context) (rsp PodStatusResponse, err error) {
+ err = c.call(ctx, "/pod_status", &rsp)
+ return
+}
+
+func (c *Client) call(ctx context.Context, handler string, result interface{}) error {
+ rsp, err := c.httpc.R().
+ SetContext(ctx).
+ ExpectContentType(headers.TypeApplicationJSON.String()).
+ SetResult(&result).
+ Get(handler)
+
+ if err != nil {
+ return xerrors.Errorf("failed to request pod agent API: %w", err)
+ }
+
+ if !rsp.IsSuccess() {
+ return xerrors.Errorf("unexpected status code: %d", rsp.StatusCode())
+ }
+
+ return nil
+}
diff --git a/library/go/yandex/deploy/podagent/doc.go b/library/go/yandex/deploy/podagent/doc.go
new file mode 100644
index 0000000000..326b84040f
--- /dev/null
+++ b/library/go/yandex/deploy/podagent/doc.go
@@ -0,0 +1,4 @@
+// Package podagent provides the client and types for making API requests to Y.Deploy PodAgent.
+//
+// Official documentation for PogAgent public API: https://deploy.yandex-team.ru/docs/reference/api/pod-agent-public-api
+package podagent
diff --git a/library/go/yandex/deploy/podagent/env.go b/library/go/yandex/deploy/podagent/env.go
new file mode 100644
index 0000000000..4dd4ae1790
--- /dev/null
+++ b/library/go/yandex/deploy/podagent/env.go
@@ -0,0 +1,33 @@
+package podagent
+
+import "os"
+
+// Box/Workload environment variable names, documentation references:
+// - https://deploy.yandex-team.ru/docs/concepts/pod/box#systemenv
+// - https://deploy.yandex-team.ru/docs/concepts/pod/workload/workload#system_env
+const (
+ EnvWorkloadIDKey = "DEPLOY_WORKLOAD_ID"
+ EnvContainerIDKey = "DEPLOY_CONTAINER_ID"
+ EnvBoxIDKey = "DEPLOY_BOX_ID"
+ EnvPodIDKey = "DEPLOY_POD_ID"
+ EnvProjectIDKey = "DEPLOY_PROJECT_ID"
+ EnvStageIDKey = "DEPLOY_STAGE_ID"
+ EnvUnitIDKey = "DEPLOY_UNIT_ID"
+
+ EnvLogsEndpointKey = "DEPLOY_LOGS_ENDPOINT"
+ EnvLogsNameKey = "DEPLOY_LOGS_DEFAULT_NAME"
+ EnvLogsSecretKey = "DEPLOY_LOGS_SECRET"
+
+ EnvNodeClusterKey = "DEPLOY_NODE_CLUSTER"
+ EnvNodeDCKey = "DEPLOY_NODE_DC"
+ EnvNodeFQDNKey = "DEPLOY_NODE_FQDN"
+
+ EnvPodPersistentFQDN = "DEPLOY_POD_PERSISTENT_FQDN"
+ EnvPodTransientFQDN = "DEPLOY_POD_TRANSIENT_FQDN"
+)
+
+// UnderPodAgent returns true if application managed by pod-agent.
+func UnderPodAgent() bool {
+ _, ok := os.LookupEnv(EnvPodIDKey)
+ return ok
+}
diff --git a/library/go/yandex/deploy/podagent/options.go b/library/go/yandex/deploy/podagent/options.go
new file mode 100644
index 0000000000..f0ab9ba4c3
--- /dev/null
+++ b/library/go/yandex/deploy/podagent/options.go
@@ -0,0 +1,17 @@
+package podagent
+
+import "github.com/ydb-platform/ydb/library/go/core/log"
+
+type Option func(client *Client)
+
+func WithEndpoint(endpointURL string) Option {
+ return func(c *Client) {
+ c.httpc.SetBaseURL(endpointURL)
+ }
+}
+
+func WithLogger(l log.Fmt) Option {
+ return func(c *Client) {
+ c.httpc.SetLogger(l)
+ }
+}
diff --git a/library/go/yandex/deploy/podagent/responses.go b/library/go/yandex/deploy/podagent/responses.go
new file mode 100644
index 0000000000..e97c70dc7c
--- /dev/null
+++ b/library/go/yandex/deploy/podagent/responses.go
@@ -0,0 +1,82 @@
+package podagent
+
+import (
+ "encoding/json"
+ "net"
+)
+
+type BoxStatus struct {
+ ID string `json:"id"`
+ Revision uint32 `json:"revision"`
+}
+
+type WorkloadStatus struct {
+ ID string `json:"id"`
+ Revision uint32 `json:"revision"`
+}
+
+type PodStatusResponse struct {
+ Boxes []BoxStatus `json:"boxes"`
+ Workloads []WorkloadStatus `json:"workloads"`
+}
+
+type MemoryResource struct {
+ Guarantee uint64 `json:"memory_guarantee_bytes"`
+ Limit uint64 `json:"memory_limit_bytes"`
+}
+
+type CPUResource struct {
+ Guarantee float64 `json:"cpu_guarantee_millicores"`
+ Limit float64 `json:"cpu_limit_millicores"`
+}
+
+type ResourceRequirements struct {
+ Memory MemoryResource `json:"memory"`
+ CPU CPUResource `json:"cpu"`
+}
+
+type NodeMeta struct {
+ DC string `json:"dc"`
+ Cluster string `json:"cluster"`
+ FQDN string `json:"fqdn"`
+}
+
+type PodMeta struct {
+ PodID string `json:"pod_id"`
+ PodSetID string `json:"pod_set_id"`
+ Annotations json.RawMessage `json:"annotations"`
+ Labels json.RawMessage `json:"labels"`
+}
+
+type Resources struct {
+ Boxes map[string]ResourceRequirements `json:"box_resource_requirements"`
+ Pod ResourceRequirements `json:"resource_requirements"`
+}
+
+type InternetAddress struct {
+ Address net.IP `json:"ip4_address"`
+ ID string `json:"id"`
+}
+
+type VirtualService struct {
+ IPv4Addrs []net.IP `json:"ip4_addresses"`
+ IPv6Addrs []net.IP `json:"ip6_addresses"`
+}
+
+type IPAllocation struct {
+ InternetAddress InternetAddress `json:"internet_address"`
+ TransientFQDN string `json:"transient_fqdn"`
+ PersistentFQDN string `json:"persistent_fqdn"`
+ Addr net.IP `json:"address"`
+ VlanID string `json:"vlan_id"`
+ VirtualServices []VirtualService `json:"virtual_services"`
+ Labels map[string]string `json:"labels"`
+}
+
+type PodAttributesResponse struct {
+ NodeMeta NodeMeta `json:"node_meta"`
+ PodMeta PodMeta `json:"metadata"`
+ BoxesRequirements map[string]ResourceRequirements `json:"box_resource_requirements"`
+ PodRequirements ResourceRequirements `json:"resource_requirements"`
+ IPAllocations []IPAllocation `json:"ip6_address_allocations"`
+}
diff --git a/library/go/yandex/deploy/podagent/ya.make b/library/go/yandex/deploy/podagent/ya.make
new file mode 100644
index 0000000000..4ae3d12925
--- /dev/null
+++ b/library/go/yandex/deploy/podagent/ya.make
@@ -0,0 +1,15 @@
+GO_LIBRARY()
+
+SRCS(
+ client.go
+ doc.go
+ env.go
+ options.go
+ responses.go
+)
+
+GO_XTEST_SRCS(client_test.go)
+
+END()
+
+RECURSE(gotest)