1 parent
98b65945f6
commit
0b6dcbd547
13 files changed
+417
-68
No files matched your search
@@ -42,6 +42,16 @@ type Client struct {
|
||||
// InCluster returns nil outside Kubernetes. The projected service account
|
||||
// token must be available; RBAC limits it to the fancy-dns-zones ConfigMap.
|
||||
func InCluster() (*Client, error) {
|
||||
name := os.Getenv("FANCYDNS_ZONE_CONFIGMAP")
|
||||
if name == "" {
|
||||
name = defaultName
|
||||
}
|
||||
return InClusterConfigMap(name)
|
||||
}
|
||||
|
||||
// InClusterConfigMap returns a client for a named ConfigMap. RBAC on the
|
||||
// service account still limits which ConfigMaps it can read or patch.
|
||||
func InClusterConfigMap(name string) (*Client, error) {
|
||||
host := os.Getenv("KUBERNETES_SERVICE_HOST")
|
||||
if host == "" {
|
||||
return nil, nil
|
||||
@@ -69,9 +79,8 @@ func InCluster() (*Client, error) {
|
||||
if !pool.AppendCertsFromPEM(ca) {
|
||||
return nil, errors.New("kube zones: invalid service account CA")
|
||||
}
|
||||
name := os.Getenv("FANCYDNS_ZONE_CONFIGMAP")
|
||||
if name == "" {
|
||||
name = defaultName
|
||||
if strings.TrimSpace(name) == "" {
|
||||
return nil, errors.New("kube zones: ConfigMap name is required")
|
||||
}
|
||||
return &Client{
|
||||
baseURL: "https://" + net.JoinHostPort(host, port),
|
||||
@@ -119,14 +128,47 @@ func (c *Client) Delete(ctx context.Context, name string) error {
|
||||
return c.change(ctx, filename(name), nil)
|
||||
}
|
||||
|
||||
// Data reads one arbitrary ConfigMap data key. It is used for console-owned
|
||||
// shared files that are not zone master files.
|
||||
func (c *Client) Data(ctx context.Context, key string) (string, bool, error) {
|
||||
if c == nil {
|
||||
return "", false, errors.New("kube zones: Kubernetes ConfigMap is unavailable")
|
||||
}
|
||||
cm, _, err := c.get(ctx)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
value, ok := cm.Data[key]
|
||||
return value, ok, nil
|
||||
}
|
||||
|
||||
// SaveData creates or replaces one arbitrary ConfigMap data key, retrying
|
||||
// resource-version conflicts so independent edits to different keys merge.
|
||||
func (c *Client) SaveData(ctx context.Context, key, content string) error {
|
||||
if c == nil {
|
||||
return errors.New("kube zones: Kubernetes ConfigMap is unavailable")
|
||||
}
|
||||
if key == "" || len(key) > 253 || strings.ContainsAny(key, `/\\`) {
|
||||
return errors.New("kube zones: invalid ConfigMap data key")
|
||||
}
|
||||
if len(content) > 512*1024 {
|
||||
return errors.New("kube zones: ConfigMap data exceeds 512 KiB")
|
||||
}
|
||||
return c.changeData(ctx, key, &content)
|
||||
}
|
||||
|
||||
func filename(name string) string { return "zone--" + strings.TrimSuffix(name, ".") + ".zone" }
|
||||
|
||||
func (c *Client) change(ctx context.Context, name string, content *string) error {
|
||||
if !validKey(name) {
|
||||
return errors.New("zone filename must be a DNS name ending in .zone")
|
||||
}
|
||||
return c.changeData(ctx, name, content)
|
||||
}
|
||||
|
||||
func (c *Client) changeData(ctx context.Context, name string, content *string) error {
|
||||
if content != nil && len(*content) > 512*1024 {
|
||||
return errors.New("zone file exceeds 512 KiB")
|
||||
return errors.New("ConfigMap data exceeds 512 KiB")
|
||||
}
|
||||
for attempt := 0; attempt < 3; attempt++ {
|
||||
_, rv, err := c.get(ctx)
|
||||
|
||||
Reference in new issue
Block a user