/
daemonconfig.go
141 lines (125 loc) · 4.43 KB
/
daemonconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2020. Ant Group. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package config
import (
"encoding/json"
"io/ioutil"
"github.com/pkg/errors"
"github.com/dragonflyoss/image-service/contrib/nydus-snapshotter/pkg/auth"
"github.com/dragonflyoss/image-service/contrib/nydus-snapshotter/pkg/utils/registry"
)
const (
backendTypeLocalfs = "localfs"
backendTypeOss = "oss"
backendTypeRegistry = "registry"
)
type DaemonConfig struct {
Device DeviceConfig `json:"device"`
Mode string `json:"mode"`
DigestValidate bool `json:"digest_validate"`
IOStatsFiles bool `json:"iostats_files,omitempty"`
EnableXattr bool `json:"enable_xattr,omitempty"`
FSPrefetch struct {
Enable bool `json:"enable"`
PrefetchAll bool `json:"prefetch_all"`
ThreadsCount int `json:"threads_count"`
MergingSize int `json:"merging_size"`
} `json:"fs_prefetch,omitempty"`
}
type DeviceConfig struct {
Backend struct {
BackendType string `json:"type"`
Config struct {
// Localfs backend configs
BlobFile string `json:"blob_file,omitempty"`
Dir string `json:"dir,omitempty"`
ReadAhead bool `json:"readahead"`
ReadAheadSec int `json:"readahead_sec,omitempty"`
// Registry backend configs
Host string `json:"host,omitempty"`
Repo string `json:"repo,omitempty"`
Auth string `json:"auth,omitempty"`
RegistryToken string `json:"registry_token,omitempty"`
BlobURLScheme string `json:"blob_url_scheme,omitempty"`
BlobRedirectedHost string `json:"blob_redirected_host,omitempty"`
// OSS backend configs
EndPoint string `json:"endpoint,omitempty"`
AccessKeyID string `json:"access_key_id,omitempty"`
AccessKeySecret string `json:"access_key_secret,omitempty"`
BucketName string `json:"bucket_name,omitempty"`
ObjectPrefix string `json:"object_prefix,omitempty"`
// Shared by registry and oss backend
Scheme string `json:"scheme,omitempty"`
// Below configs are common configs shared by all backends
Proxy struct {
URL string `json:"url,omitempty"`
Fallback bool `json:"fallback"`
PingURL string `json:"ping_url,omitempty"`
CheckInterval int `json:"check_interval,omitempty"`
} `json:"proxy,omitempty"`
Timeout int `json:"timeout,omitempty"`
ConnectTimeout int `json:"connect_timeout,omitempty"`
RetryLimit int `json:"retry_limit,omitempty"`
} `json:"config"`
} `json:"backend"`
Cache struct {
CacheType string `json:"type"`
Compressed bool `json:"compressed,omitempty"`
Config struct {
WorkDir string `json:"work_dir"`
DisableIndexedMap bool `json:"disable_indexed_map"`
} `json:"config"`
} `json:"cache"`
}
func LoadConfig(configFile string, cfg *DaemonConfig) error {
b, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
if err := json.Unmarshal(b, cfg); err != nil {
return err
}
return nil
}
func SaveConfig(c DaemonConfig, configFile string) error {
b, err := json.Marshal(c)
if err != nil {
return nil
}
return ioutil.WriteFile(configFile, b, 0755)
}
func NewDaemonConfig(cfg DaemonConfig, imageID string, vpcRegistry bool, labels map[string]string) (DaemonConfig, error) {
image, err := registry.ParseImage(imageID)
if err != nil {
return DaemonConfig{}, errors.Wrapf(err, "failed to parse image %s", imageID)
}
switch backend := cfg.Device.Backend.BackendType; backend {
case backendTypeRegistry:
registryHost := image.Host
if vpcRegistry {
registryHost = registry.ConvertToVPCHost(registryHost)
}
keyChain := auth.GetRegistryKeyChain(registryHost, labels)
// If no auth is provided, don't touch auth from provided nydusd configuration file.
// We don't validate the original nydusd auth from configuration file since it can be empty
// when repository is public.
if keyChain != nil {
if keyChain.TokenBase() {
cfg.Device.Backend.Config.RegistryToken = keyChain.Password
} else {
cfg.Device.Backend.Config.Auth = keyChain.ToBase64()
}
}
cfg.Device.Backend.Config.Host = registryHost
cfg.Device.Backend.Config.Repo = image.Repo
// Localfs and OSS backends don't need any update, just use the provided config in template
case backendTypeLocalfs:
case backendTypeOss:
default:
return DaemonConfig{}, errors.Errorf("unknown backend type %s", backend)
}
return cfg, nil
}