Skip to content

Commit 1b6b45c

Browse files
generating the config for the containerd fixes
1 parent 949c60c commit 1b6b45c

File tree

4 files changed

+207
-149
lines changed

4 files changed

+207
-149
lines changed

cmd/container_runtime/containerd.go

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"container-registry.com/harbor-satellite/internal/utils"
1111
"container-registry.com/harbor-satellite/logger"
1212
"container-registry.com/harbor-satellite/registry"
13-
containerd "github.com/containerd/containerd/pkg/cri/config"
1413
toml "github.com/pelletier/go-toml"
1514
"github.com/rs/zerolog"
1615
"github.com/spf13/cobra"
@@ -21,6 +20,7 @@ const (
2120
DefaultGeneratedTomlName = "config.toml"
2221
ContainerdRuntime = "containerd"
2322
DefaultContainerdConfigPath = "/etc/containerd/config.toml"
23+
DefaultConfigVersion = 2
2424
)
2525

2626
type ContainerdController interface {
@@ -80,7 +80,12 @@ func NewContainerdCommand() *cobra.Command {
8080
if generateConfig {
8181
log.Info().Msg("Generating containerd config file for containerd ...")
8282
log.Info().Msgf("Fetching containerd config from path path: %s", containerdConfigPath)
83-
return GenerateContainerdHostConfig(containerDCertPath, DefaultGenPath, log, *satelliteHostConfig)
83+
err := GenerateContainerdHostConfig(containerDCertPath, DefaultGenPath, log, *satelliteHostConfig)
84+
if err != nil {
85+
log.Err(err).Msg("Error generating containerd config")
86+
return fmt.Errorf("could not generate containerd config: %w", err)
87+
}
88+
return GenerateConfig(defaultZotConfig, log, containerdConfigPath, containerDCertPath)
8489
}
8590
return nil
8691
},
@@ -104,48 +109,32 @@ func GenerateConfig(defaultZotConfig *registry.DefaultZotConfig, log *zerolog.Lo
104109
return fmt.Errorf("could not read config file: %w", err)
105110
}
106111
// Now we marshal the data into the containerd config
107-
containerdConfig := &containerd.Config{}
112+
containerdConfig := &ContainerdConfigToml{}
108113
err = toml.Unmarshal(data, containerdConfig)
109114
if err != nil {
110115
log.Err(err).Msg("Error unmarshalling config")
111116
return fmt.Errorf("could not unmarshal config: %w", err)
112117
}
113-
// Steps to configure the containerd config:
114-
// 1. Set the default registry config cert path
115-
// -- This is the path where the certs of the registry are stored
116-
// -- If the user has already has a cert path then we do not set it rather we would now use the
117-
// user path as the default path
118-
if containerdConfig.PluginConfig.Registry.ConfigPath == "" {
119-
containerdConfig.PluginConfig.Registry.ConfigPath = containerdCertPath
118+
// Add the certs.d path to the config
119+
if containerdConfig.Plugins.Cri.Registry.ConfigPath == "" {
120+
containerdConfig.Plugins.Cri.Registry.ConfigPath = containerdCertPath
120121
}
121-
log.Info().Msgf("Setting the registry cert path to: %s", containerdConfig.PluginConfig.Registry.ConfigPath)
122-
// Now we add the local registry to the containerd config mirrors
123-
registryMirror := map[string]containerd.Mirror{
124-
defaultZotConfig.HTTP.Address: {
125-
Endpoints: []string{defaultZotConfig.HTTP.Address + ":" + defaultZotConfig.HTTP.Port},
126-
},
122+
// Set default version
123+
if containerdConfig.Version == 0 {
124+
containerdConfig.Version = DefaultConfigVersion
127125
}
128-
if containerdConfig.PluginConfig.Registry.Mirrors == nil {
129-
containerdConfig.PluginConfig.Registry.Mirrors = registryMirror
130-
} else {
131-
for key, value := range registryMirror {
132-
containerdConfig.PluginConfig.Registry.Mirrors[key] = value
126+
// if config disabled plugins container cri then remove it
127+
if len(containerdConfig.DisabledPlugins) > 0 {
128+
filteredPlugins := make([]string, len(containerdConfig.DisabledPlugins))
129+
for _, plugin := range containerdConfig.DisabledPlugins {
130+
if plugin != "cri" {
131+
filteredPlugins = append(filteredPlugins, plugin)
132+
}
133133
}
134-
}
135-
registryConfig := map[string]containerd.RegistryConfig{
136-
defaultZotConfig.HTTP.Address: {
137-
TLS: &containerd.TLSConfig{
138-
InsecureSkipVerify: config.UseUnsecure(),
139-
},
140-
},
141-
}
142-
// Now we add the local registry to the containerd config registry
143-
if containerdConfig.PluginConfig.Registry.Configs == nil {
144-
containerdConfig.PluginConfig.Registry.Configs = registryConfig
145-
} else {
146-
for key, value := range registryConfig {
147-
containerdConfig.PluginConfig.Registry.Configs[key] = value
134+
if len(filteredPlugins) == 0 {
135+
containerdConfig.DisabledPlugins = nil
148136
}
137+
containerdConfig.DisabledPlugins = filteredPlugins
149138
}
150139
// ToDo: Find a way to remove the unwanted configuration added to the config file while marshalling
151140
pathToWrite := filepath.Join(DefaultGenPath, DefaultGeneratedTomlName)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package runtime
2+
3+
// ContainerdConfigToml provides containerd configuration data for the server
4+
type ContainerdConfigToml struct {
5+
// Version of the config file
6+
Version int `toml:"version"`
7+
// Root is the path to a directory where containerd will store persistent data
8+
Root string `toml:"root"`
9+
// State is the path to a directory where containerd will store transient data
10+
State string `toml:"state"`
11+
// TempDir is the path to a directory where to place containerd temporary files
12+
TempDir string `toml:"temp,omitempty"`
13+
// PluginDir is the directory for dynamic plugins to be stored
14+
//
15+
// Deprecated: Please use proxy or binary external plugins.
16+
PluginDir string `toml:"plugin_dir,omitempty"`
17+
// GRPC configuration settings
18+
GRPC GRPCConfig `toml:"grpc,omitempty"`
19+
// TTRPC configuration settings
20+
TTRPC TTRPCConfig `toml:"ttrpc,omitempty"`
21+
// Debug and profiling settings
22+
Debug Debug `toml:"debug,omitempty"`
23+
// Metrics and monitoring settings
24+
Metrics MetricsConfig `toml:"metrics,omitempty"`
25+
// DisabledPlugins are IDs of plugins to disable. Disabled plugins won't be
26+
// initialized and started.
27+
// DisabledPlugins must use a fully qualified plugin URI.
28+
DisabledPlugins []string `toml:"disabled_plugins,omitempty"`
29+
// RequiredPlugins are IDs of required plugins. Containerd exits if any
30+
// required plugin doesn't exist or fails to be initialized or started.
31+
// RequiredPlugins must use a fully qualified plugin URI.
32+
RequiredPlugins []string `toml:"required_plugins,omitempty"`
33+
// Plugins provides plugin specific configuration for the initialization of a plugin
34+
Plugins PluginsConfig `toml:"plugins,omitempty"`
35+
// OOMScore adjust the containerd's oom score
36+
OOMScore int `toml:"oom_score,omitempty"`
37+
// Cgroup specifies cgroup information for the containerd daemon process
38+
Cgroup CgroupConfig `toml:"cgroup,omitempty"`
39+
// ProxyPlugins configures plugins which are communicated to over GRPC
40+
ProxyPlugins map[string]ProxyPlugin `toml:"proxy_plugins,omitempty"`
41+
// Timeouts specified as a duration
42+
Timeouts map[string]string `toml:"timeouts,omitempty"`
43+
// Imports are additional file path list to config files that can overwrite main config file fields
44+
Imports []string `toml:"imports,omitempty"`
45+
// StreamProcessors configuration
46+
StreamProcessors map[string]StreamProcessor `toml:"stream_processors,omitempty"`
47+
}
48+
49+
type StreamProcessor struct {
50+
// Accepts specific media-types
51+
Accepts []string `toml:"accepts,omitempty"`
52+
// Returns the media-type
53+
Returns string `toml:"returns,omitempty"`
54+
// Path or name of the binary
55+
Path string `toml:"path"`
56+
// Args to the binary
57+
Args []string `toml:"args,omitempty"`
58+
// Environment variables for the binary
59+
Env []string `toml:"env,omitempty"`
60+
}
61+
62+
type GRPCConfig struct {
63+
Address string `toml:"address"`
64+
TCPAddress string `toml:"tcp_address,omitempty"`
65+
TCPTLSCA string `toml:"tcp_tls_ca,omitempty"`
66+
TCPTLSCert string `toml:"tcp_tls_cert,omitempty"`
67+
TCPTLSKey string `toml:"tcp_tls_key,omitempty"`
68+
UID int `toml:"uid,omitempty"`
69+
GID int `toml:"gid,omitempty"`
70+
MaxRecvMsgSize int `toml:"max_recv_message_size,omitempty"`
71+
MaxSendMsgSize int `toml:"max_send_message_size,omitempty"`
72+
}
73+
74+
// TTRPCConfig provides TTRPC configuration for the socket
75+
type TTRPCConfig struct {
76+
Address string `toml:"address"`
77+
UID int `toml:"uid,omitempty"`
78+
GID int `toml:"gid,omitempty"`
79+
}
80+
81+
// Debug provides debug configuration
82+
type Debug struct {
83+
Address string `toml:"address,omitempty"`
84+
UID int `toml:"uid,omitempty"`
85+
GID int `toml:"gid,omitempty"`
86+
Level string `toml:"level,omitempty"`
87+
// Format represents the logging format. Supported values are 'text' and 'json'.
88+
Format string `toml:"format,omitempty"`
89+
}
90+
91+
// MetricsConfig provides metrics configuration
92+
type MetricsConfig struct {
93+
Address string `toml:"address,omitempty"`
94+
GRPCHistogram bool `toml:"grpc_histogram,omitempty"`
95+
}
96+
97+
// CgroupConfig provides cgroup configuration
98+
type CgroupConfig struct {
99+
Path string `toml:"path,omitempty"`
100+
}
101+
102+
// ProxyPlugin provides a proxy plugin configuration
103+
type ProxyPlugin struct {
104+
Type string `toml:"type"`
105+
Address string `toml:"address"`
106+
Platform string `toml:"platform,omitempty"`
107+
Exports map[string]string `toml:"exports,omitempty"`
108+
Capabilities []string `toml:"capabilities,omitempty"`
109+
}
110+
111+
type PluginsConfig struct {
112+
Cri CriConfig `toml:"io.containerd.grpc.v1.cri,omitempty"`
113+
Cgroups MonitorConfig `toml:"io.containerd.monitor.v1.cgroups,omitempty"`
114+
LinuxRuntime interface{} `toml:"io.containerd.runtime.v1.linux,omitempty"`
115+
Scheduler GCSchedulerConfig `toml:"io.containerd.gc.v1.scheduler,omitempty"`
116+
Bolt interface{} `toml:"io.containerd.metadata.v1.bolt,omitempty"`
117+
Task RuntimeV2TaskConfig `toml:"io.containerd.runtime.v2.task,omitempty"`
118+
Opt interface{} `toml:"io.containerd.internal.v1.opt,omitempty"`
119+
Restart interface{} `toml:"io.containerd.internal.v1.restart,omitempty"`
120+
Tracing interface{} `toml:"io.containerd.internal.v1.tracing,omitempty"`
121+
Otlp interface{} `toml:"io.containerd.tracing.processor.v1.otlp,omitempty"`
122+
Aufs interface{} `toml:"io.containerd.snapshotter.v1.aufs,omitempty"`
123+
Btrfs interface{} `toml:"io.containerd.snapshotter.v1.btrfs,omitempty"`
124+
Devmapper interface{} `toml:"io.containerd.snapshotter.v1.devmapper,omitempty"`
125+
Native interface{} `toml:"io.containerd.snapshotter.v1.native,omitempty"`
126+
Overlayfs interface{} `toml:"io.containerd.snapshotter.v1.overlayfs,omitempty"`
127+
Zfs interface{} `toml:"io.containerd.snapshotter.v1.zfs,omitempty"`
128+
}
129+
130+
type MonitorConfig struct {
131+
NoPrometheus bool `toml:"no_prometheus,omitempty"`
132+
}
133+
134+
type GCSchedulerConfig struct {
135+
PauseThreshold float64 `toml:"pause_threshold,omitempty"`
136+
DeletionThreshold int `toml:"deletion_threshold,omitempty"`
137+
MutationThreshold int `toml:"mutation_threshold,omitempty"`
138+
ScheduleDelay string `toml:"schedule_delay,omitempty"`
139+
StartupDelay string `toml:"startup_delay,omitempty"`
140+
}
141+
142+
type RuntimeV2TaskConfig struct {
143+
Platforms []string `toml:"platforms,omitempty"`
144+
SchedCore bool `toml:"sched_core,omitempty"`
145+
}
146+
147+
type CriConfig struct {
148+
Containerd CriContainerdConfig `toml:"containerd,omitempty"`
149+
Registry RegistryConfig `toml:"registry,omitempty"`
150+
}
151+
152+
type CriContainerdConfig struct {
153+
DefaultRuntimeName string `toml:"default_runtime_name,omitempty"`
154+
Runtimes map[string]RuntimeConfig `toml:"runtimes,omitempty"`
155+
}
156+
157+
type RuntimeConfig struct {
158+
PrivilegedWithoutHostDevices bool `toml:"privileged_without_host_devices,omitempty"`
159+
RuntimeType string `toml:"runtime_type"`
160+
Options RuntimeOptions `toml:"options,omitempty"`
161+
}
162+
163+
type RuntimeOptions struct {
164+
BinaryName string `toml:"BinaryName,omitempty"`
165+
}
166+
167+
type RegistryConfig struct {
168+
ConfigPath string `toml:"config_path,omitempty"`
169+
}

registry/default_config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"strings"
89
)
910

1011
type DefaultZotConfig struct {
@@ -22,7 +23,11 @@ type DefaultZotConfig struct {
2223
}
2324

2425
func (c *DefaultZotConfig) GetLocalRegistryURL() string {
25-
return fmt.Sprintf("%s:%s", c.HTTP.Address, c.HTTP.Port)
26+
address := c.HTTP.Address
27+
if !strings.HasPrefix(address, "http://") && !strings.HasPrefix(address, "https://") {
28+
address = "http://" + address
29+
}
30+
return fmt.Sprintf("%s:%s", address, c.HTTP.Port)
2631
}
2732

2833
// ReadConfig reads a JSON file from the specified path and unmarshals it into a Config struct.

0 commit comments

Comments
 (0)