| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
golog "log" |
| 25 |
"math/rand" |
| 26 |
"os" |
| 27 |
"os/signal" |
| 28 |
"path/filepath" |
| 29 |
"syscall" |
| 30 |
"time" |
| 31 |
|
| 32 |
"github.com/containerd/containerd/log" |
| 33 |
dbmetadata "github.com/containerd/stargz-snapshotter/cmd/containerd-stargz-grpc/db" |
| 34 |
"github.com/containerd/stargz-snapshotter/fs/config" |
| 35 |
"github.com/containerd/stargz-snapshotter/metadata" |
| 36 |
memorymetadata "github.com/containerd/stargz-snapshotter/metadata/memory" |
| 37 |
"github.com/containerd/stargz-snapshotter/service/keychain/dockerconfig" |
| 38 |
"github.com/containerd/stargz-snapshotter/service/keychain/kubeconfig" |
| 39 |
"github.com/containerd/stargz-snapshotter/service/resolver" |
| 40 |
"github.com/containerd/stargz-snapshotter/store" |
| 41 |
sddaemon "github.com/coreos/go-systemd/v22/daemon" |
| 42 |
"github.com/pelletier/go-toml" |
| 43 |
"github.com/sirupsen/logrus" |
| 44 |
bolt "go.etcd.io/bbolt" |
| 45 |
) |
| 46 |
|
| 47 |
const ( |
| 48 |
defaultLogLevel = logrus.InfoLevel |
| 49 |
defaultConfigPath = "/etc/stargz-store/config.toml" |
| 50 |
defaultRootDir = "/var/lib/stargz-store" |
| 51 |
) |
| 52 |
|
| 53 |
var ( |
| 54 |
configPath = flag.String("config", defaultConfigPath, "path to the configuration file") |
| 55 |
logLevel = flag.String("log-level", defaultLogLevel.String(), "set the logging level [trace, debug, info, warn, error, fatal, panic]") |
| 56 |
rootDir = flag.String("root", defaultRootDir, "path to the root directory for this snapshotter") |
| 57 |
) |
| 58 |
|
| 59 |
type Config struct { |
| 60 |
config.Config |
| 61 |
|
| 62 |
// KubeconfigKeychainConfig is config for kubeconfig-based keychain. |
| 63 |
KubeconfigKeychainConfig `toml:"kubeconfig_keychain"` |
| 64 |
|
| 65 |
// ResolverConfig is config for resolving registries. |
| 66 |
ResolverConfig `toml:"resolver"` |
| 67 |
|
| 68 |
// MetadataStore is the type of the metadata store to use. |
| 69 |
MetadataStore string `toml:"metadata_store" default:"memory"` |
| 70 |
} |
| 71 |
|
| 72 |
type KubeconfigKeychainConfig struct { |
| 73 |
EnableKeychain bool `toml:"enable_keychain"` |
| 74 |
KubeconfigPath string `toml:"kubeconfig_path"` |
| 75 |
} |
| 76 |
|
| 77 |
type ResolverConfig resolver.Config |
| 78 |
|
| 79 |
func main() { |
| 80 |
rand.Seed(time.Now().UnixNano()) |
| 81 |
flag.Parse() |
| 82 |
mountPoint := flag.Arg(0) |
| 83 |
lvl, err := logrus.ParseLevel(*logLevel) |
| 84 |
if err != nil { |
| 85 |
log.L.WithError(err).Fatal("failed to prepare logger") |
| 86 |
} |
| 87 |
logrus.SetLevel(lvl) |
| 88 |
logrus.SetFormatter(&logrus.JSONFormatter{ |
| 89 |
TimestampFormat: log.RFC3339NanoFixed, |
| 90 |
}) |
| 91 |
var ( |
| 92 |
ctx = log.WithLogger(context.Background(), log.L) |
| 93 |
config Config |
| 94 |
) |
| 95 |
// Streams log of standard lib (go-fuse uses this) into debug log |
| 96 |
// Snapshotter should use "github.com/containerd/containerd/log" otherwise |
| 97 |
// logs are always printed as "debug" mode. |
| 98 |
golog.SetOutput(log.G(ctx).WriterLevel(logrus.DebugLevel)) |
| 99 |
|
| 100 |
if mountPoint == "" { |
| 101 |
log.G(ctx).Fatalf("mount point must be specified") |
| 102 |
} |
| 103 |
|
| 104 |
// Get configuration from specified file |
| 105 |
if *configPath != "" { |
| 106 |
tree, err := toml.LoadFile(*configPath) |
| 107 |
if err != nil && !(os.IsNotExist(err) && *configPath == defaultConfigPath) { |
| 108 |
log.G(ctx).WithError(err).Fatalf("failed to load config file %q", *configPath) |
| 109 |
} |
| 110 |
if err := tree.Unmarshal(&config); err != nil { |
| 111 |
log.G(ctx).WithError(err).Fatalf("failed to unmarshal config file %q", *configPath) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Prepare kubeconfig-based keychain if required |
| 116 |
credsFuncs := []resolver.Credential{dockerconfig.NewDockerconfigKeychain(ctx)} |
| 117 |
if config.KubeconfigKeychainConfig.EnableKeychain { |
| 118 |
var opts []kubeconfig.Option |
| 119 |
if kcp := config.KubeconfigKeychainConfig.KubeconfigPath; kcp != "" { |
| 120 |
opts = append(opts, kubeconfig.WithKubeconfigPath(kcp)) |
| 121 |
} |
| 122 |
credsFuncs = append(credsFuncs, kubeconfig.NewKubeconfigKeychain(ctx, opts...)) |
| 123 |
} |
| 124 |
|
| 125 |
// Use RegistryHosts based on ResolverConfig and keychain |
| 126 |
hosts := resolver.RegistryHostsFromConfig(resolver.Config(config.ResolverConfig), credsFuncs...) |
| 127 |
|
| 128 |
// Configure and mount filesystem |
| 129 |
if _, err := os.Stat(mountPoint); err != nil { |
| 130 |
if err2 := os.MkdirAll(mountPoint, 0755); err2 != nil && !os.IsExist(err2) { |
| 131 |
log.G(ctx).WithError(err).WithError(err2). |
| 132 |
Fatalf("failed to prepare mountpoint %q", mountPoint) |
| 133 |
} |
| 134 |
} |
| 135 |
if !config.Config.DisableVerification { |
| 136 |
log.G(ctx).Warnf("content verification is not supported; switching to non-verification mode") |
| 137 |
config.Config.DisableVerification = true |
| 138 |
} |
| 139 |
mt, err := getMetadataStore(*rootDir, config) |
| 140 |
if err != nil { |
| 141 |
log.G(ctx).WithError(err).Fatalf("failed to configure metadata store") |
| 142 |
} |
| 143 |
layerManager, err := store.NewLayerManager(ctx, *rootDir, hosts, mt, config.Config) |
| 144 |
if err != nil { |
| 145 |
log.G(ctx).WithError(err).Fatalf("failed to prepare pool") |
| 146 |
} |
| 147 |
if err := store.Mount(ctx, mountPoint, layerManager, config.Config.Debug); err != nil { |
| 148 |
log.G(ctx).WithError(err).Fatalf("failed to mount fs at %q", mountPoint) |
| 149 |
} |
| 150 |
defer func() { |
| 151 |
syscall.Unmount(mountPoint, 0) |
| 152 |
log.G(ctx).Info("Exiting") |
| 153 |
}() |
| 154 |
|
| 155 |
if os.Getenv("NOTIFY_SOCKET") != "" { |
| 156 |
notified, notifyErr := sddaemon.SdNotify(false, sddaemon.SdNotifyReady) |
| 157 |
log.G(ctx).Debugf("SdNotifyReady notified=%v, err=%v", notified, notifyErr) |
| 158 |
} |
| 159 |
defer func() { |
| 160 |
if os.Getenv("NOTIFY_SOCKET") != "" { |
| 161 |
notified, notifyErr := sddaemon.SdNotify(false, sddaemon.SdNotifyStopping) |
| 162 |
log.G(ctx).Debugf("SdNotifyStopping notified=%v, err=%v", notified, notifyErr) |
| 163 |
} |
| 164 |
}() |
| 165 |
|
| 166 |
waitForSIGINT() |
| 167 |
log.G(ctx).Info("Got SIGINT") |
| 168 |
} |
| 169 |
|
| 170 |
func waitForSIGINT() { |
| 171 |
c := make(chan os.Signal, 1) |
| 172 |
signal.Notify(c, os.Interrupt) |
| 173 |
<-c |
| 174 |
} |
| 175 |
|
| 176 |
const ( |
| 177 |
memoryMetadataType = "memory" |
| 178 |
dbMetadataType = "db" |
| 179 |
) |
| 180 |
|
| 181 |
func getMetadataStore(rootDir string, config Config) (metadata.Store, error) { |
| 182 |
switch config.MetadataStore { |
| 183 |
case "", memoryMetadataType: |
| 184 |
return memorymetadata.NewReader, nil |
| 185 |
case dbMetadataType: |
| 186 |
bOpts := bolt.Options{ |
| 187 |
NoFreelistSync: true, |
| 188 |
InitialMmapSize: 64 * 1024 * 1024, |
| 189 |
FreelistType: bolt.FreelistMapType, |
| 190 |
} |
| 191 |
db, err := bolt.Open(filepath.Join(rootDir, "metadata.db"), 0600, &bOpts) |
| 192 |
if err != nil { |
| 193 |
return nil, err |
| 194 |
} |
| 195 |
return func(sr *io.SectionReader, opts ...metadata.Option) (metadata.Reader, error) { |
| 196 |
return dbmetadata.NewReader(db, sr, opts...) |
| 197 |
}, nil |
| 198 |
default: |
| 199 |
return nil, fmt.Errorf("unknown metadata store type: %v; must be %v or %v", |
| 200 |
config.MetadataStore, memoryMetadataType, dbMetadataType) |
| 201 |
} |
| 202 |
} |