|
17 | 17 | package server |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "encoding/json" |
20 | 21 | "fmt" |
21 | 22 | "io" |
22 | 23 | "net/http" |
| 24 | + "os" |
23 | 25 | "path/filepath" |
24 | 26 | "time" |
25 | 27 |
|
26 | 28 | "github.com/containerd/containerd" |
| 29 | + "github.com/containerd/containerd/oci" |
27 | 30 | "github.com/containerd/containerd/plugin" |
28 | | - "github.com/containerd/cri/pkg/store/label" |
29 | 31 | cni "github.com/containerd/go-cni" |
30 | 32 | "github.com/pkg/errors" |
31 | 33 | "github.com/sirupsen/logrus" |
32 | 34 | "google.golang.org/grpc" |
33 | 35 | runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" |
34 | 36 | "k8s.io/kubernetes/pkg/kubelet/server/streaming" |
35 | 37 |
|
| 38 | + "github.com/containerd/cri/pkg/store/label" |
| 39 | + |
36 | 40 | "github.com/containerd/cri/pkg/atomic" |
37 | 41 | criconfig "github.com/containerd/cri/pkg/config" |
38 | 42 | ctrdutil "github.com/containerd/cri/pkg/containerd/util" |
@@ -95,6 +99,8 @@ type criService struct { |
95 | 99 | // cniNetConfMonitor is used to reload cni network conf if there is |
96 | 100 | // any valid fs change events from cni network conf dir. |
97 | 101 | cniNetConfMonitor *cniNetConfSyncer |
| 102 | + // baseOCISpecs contains cached OCI specs loaded via `Runtime.BaseRuntimeSpec` |
| 103 | + baseOCISpecs map[string]*oci.Spec |
98 | 104 | } |
99 | 105 |
|
100 | 106 | // NewCRIService returns a new instance of CRIService |
@@ -138,6 +144,12 @@ func NewCRIService(config criconfig.Config, client *containerd.Client) (CRIServi |
138 | 144 | return nil, errors.Wrap(err, "failed to create cni conf monitor") |
139 | 145 | } |
140 | 146 |
|
| 147 | + // Preload base OCI specs |
| 148 | + c.baseOCISpecs, err = loadBaseOCISpecs(&config) |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
141 | 153 | return c, nil |
142 | 154 | } |
143 | 155 |
|
@@ -273,3 +285,41 @@ func (c *criService) register(s *grpc.Server) error { |
273 | 285 | func imageFSPath(rootDir, snapshotter string) string { |
274 | 286 | return filepath.Join(rootDir, fmt.Sprintf("%s.%s", plugin.SnapshotPlugin, snapshotter)) |
275 | 287 | } |
| 288 | + |
| 289 | +func loadOCISpec(filename string) (*oci.Spec, error) { |
| 290 | + file, err := os.Open(filename) |
| 291 | + if err != nil { |
| 292 | + return nil, errors.Wrapf(err, "failed to open base OCI spec: %s", filename) |
| 293 | + } |
| 294 | + defer file.Close() |
| 295 | + |
| 296 | + spec := oci.Spec{} |
| 297 | + if err := json.NewDecoder(file).Decode(&spec); err != nil { |
| 298 | + return nil, errors.Wrap(err, "failed to parse base OCI spec file") |
| 299 | + } |
| 300 | + |
| 301 | + return &spec, nil |
| 302 | +} |
| 303 | + |
| 304 | +func loadBaseOCISpecs(config *criconfig.Config) (map[string]*oci.Spec, error) { |
| 305 | + specs := map[string]*oci.Spec{} |
| 306 | + for _, cfg := range config.Runtimes { |
| 307 | + if cfg.BaseRuntimeSpec == "" { |
| 308 | + continue |
| 309 | + } |
| 310 | + |
| 311 | + // Don't load same file twice |
| 312 | + if _, ok := specs[cfg.BaseRuntimeSpec]; ok { |
| 313 | + continue |
| 314 | + } |
| 315 | + |
| 316 | + spec, err := loadOCISpec(cfg.BaseRuntimeSpec) |
| 317 | + if err != nil { |
| 318 | + return nil, errors.Wrapf(err, "failed to load base OCI spec from file: %s", cfg.BaseRuntimeSpec) |
| 319 | + } |
| 320 | + |
| 321 | + specs[cfg.BaseRuntimeSpec] = spec |
| 322 | + } |
| 323 | + |
| 324 | + return specs, nil |
| 325 | +} |
0 commit comments