aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/deploy/podagent
diff options
context:
space:
mode:
authorqrort <qrort@yandex-team.com>2022-11-30 23:47:12 +0300
committerqrort <qrort@yandex-team.com>2022-11-30 23:47:12 +0300
commit22f8ae0e3f5d68b92aecccdf96c1d841a0334311 (patch)
treebffa27765faf54126ad44bcafa89fadecb7a73d7 /library/go/yandex/deploy/podagent
parent332b99e2173f0425444abb759eebcb2fafaa9209 (diff)
downloadydb-22f8ae0e3f5d68b92aecccdf96c1d841a0334311.tar.gz
validate canons without yatest_common
Diffstat (limited to 'library/go/yandex/deploy/podagent')
-rw-r--r--library/go/yandex/deploy/podagent/client.go67
-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
5 files changed, 203 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..09dc10ada6
--- /dev/null
+++ b/library/go/yandex/deploy/podagent/client.go
@@ -0,0 +1,67 @@
+package podagent
+
+import (
+ "context"
+ "time"
+
+ "github.com/go-resty/resty/v2"
+
+ "a.yandex-team.ru/library/go/core/xerrors"
+ "a.yandex-team.ru/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..32c3ac71aa
--- /dev/null
+++ b/library/go/yandex/deploy/podagent/options.go
@@ -0,0 +1,17 @@
+package podagent
+
+import "a.yandex-team.ru/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"`
+}