Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generates Containerd and crio CRI configs #59

Closed
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b0304ff
adding state artifact fetcher to fetch the state from harbor
Mehul-Kumar-27 Sep 23, 2024
8a5ba11
completing the url fetcher
Mehul-Kumar-27 Sep 25, 2024
c291e6a
adding schedulers and process to satellite
Mehul-Kumar-27 Sep 29, 2024
fe299f8
adding simple notifier to fetch state process
Mehul-Kumar-27 Sep 29, 2024
b6b5dea
added description to the scheduler
Mehul-Kumar-27 Sep 29, 2024
91222d4
coderabbit fixes and changes to fetcher and schedulers
Mehul-Kumar-27 Sep 29, 2024
09419ce
adding new format of the state file
Mehul-Kumar-27 Oct 2, 2024
c60f7a4
adding config to process new state artifact file
Mehul-Kumar-27 Oct 8, 2024
18b5633
Merge branch 'main' into satellite-state-fetcher
Mehul-Kumar-27 Oct 8, 2024
dec1ba0
coderabbit review
Mehul-Kumar-27 Oct 8, 2024
ef0d82a
added ./zot to gitignore
Mehul-Kumar-27 Oct 8, 2024
74fc4b9
fixing the replication process
Mehul-Kumar-27 Oct 10, 2024
3d0e209
fixing the replication and deletion process
Mehul-Kumar-27 Oct 12, 2024
6e54a14
fixing paning while removing the null tags
Mehul-Kumar-27 Oct 13, 2024
8904e99
using repository name instead of the image name while uploading the i…
Mehul-Kumar-27 Oct 16, 2024
d14af7e
adding container runtime config
Mehul-Kumar-27 Oct 20, 2024
7254c1b
containerd function and changing the harbor satellite to a cobra cli …
Mehul-Kumar-27 Oct 21, 2024
d111454
generating config file for containerd
Mehul-Kumar-27 Oct 21, 2024
222c666
adding better logging
Mehul-Kumar-27 Oct 21, 2024
d2180bf
fix
Mehul-Kumar-27 Oct 21, 2024
97f8b01
adding config generation for containerd
Mehul-Kumar-27 Oct 28, 2024
949c60c
fixing host gen file
Mehul-Kumar-27 Oct 28, 2024
1b6b45c
generating the config for the containerd fixes
Mehul-Kumar-27 Oct 29, 2024
8624668
fixes
Mehul-Kumar-27 Oct 29, 2024
771a6e3
coderabbit fixes
Mehul-Kumar-27 Oct 29, 2024
a08405d
fixes
Mehul-Kumar-27 Oct 29, 2024
0dc1160
adding config command for crio
Mehul-Kumar-27 Nov 4, 2024
f754085
moving from toml config to json config
Mehul-Kumar-27 Nov 4, 2024
abb1a3e
making config.json work with the replicator
Mehul-Kumar-27 Nov 4, 2024
a641df3
avoid printing confedential information in log
Mehul-Kumar-27 Nov 4, 2024
2f11137
coderabbit fixes
Mehul-Kumar-27 Nov 4, 2024
17f8ffa
changing satellite config from toml to json
Mehul-Kumar-27 Nov 12, 2024
c194bb0
fixing startup
Mehul-Kumar-27 Nov 12, 2024
86430aa
fixing panic error in generating container runtime config
Mehul-Kumar-27 Nov 12, 2024
a656219
minor fixes
Mehul-Kumar-27 Nov 19, 2024
1e8dc43
handelling config error
Mehul-Kumar-27 Nov 19, 2024
5ca08c7
dagger version
Mehul-Kumar-27 Nov 19, 2024
e578bd4
Merge branch 'satellite-state-fetcher' into container
Mehul-Kumar-27 Nov 19, 2024
7062d10
Merge branch 'main' into container
Mehul-Kumar-27 Nov 21, 2024
9c6d293
Merge branch 'main' into satellite-state-fetcher
Mehul-Kumar-27 Nov 21, 2024
9782e7e
replication fix
Mehul-Kumar-27 Nov 25, 2024
fc24959
Merge branch 'satellite-state-fetcher' into container
Mehul-Kumar-27 Nov 25, 2024
2fdb90b
Merge branch 'main' into container
Mehul-Kumar-27 Nov 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ ZOT_URL="127.0.0.1:8585"
TOKEN=""
ENV=dev
USE_UNSECURE=true
GROUP_NAME=satellite-test-group-state
STATE_ARTIFACT_NAME=state
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ dist/
zot/cache.db
secrets.txt
__debug_bin1949266242

/zot
runtime/
2 changes: 0 additions & 2 deletions ci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ func (m *HarborSatellite) Service(
AsService()
}


// builds given component from source

func (m *HarborSatellite) build(source *dagger.Directory, component string) *dagger.Directory {
fmt.Printf("Building %s\n", component)

Expand Down
165 changes: 165 additions & 0 deletions cmd/container_runtime/containerd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package runtime

import (
"context"
"fmt"
"os"
"path/filepath"

"container-registry.com/harbor-satellite/internal/config"
"container-registry.com/harbor-satellite/internal/utils"
"container-registry.com/harbor-satellite/logger"
"container-registry.com/harbor-satellite/registry"
containerd "github.com/containerd/containerd/pkg/cri/config"
toml "github.com/pelletier/go-toml"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)

const (
ContainerDCertPath = "/etc/containerd/certs.d"
DefaultGeneratedTomlName = "config.toml"
ContainerdRuntime = "containerd"
DefaultContainerdConfigPath = "/etc/containerd/config.toml"
)

type ContainerdController interface {
Load(ctx context.Context, log *zerolog.Logger) (*registry.DefaultZotConfig, error)
Generate(ctx context.Context, configPath string, log *zerolog.Logger) error
}

var DefaultGenPath string

func init() {
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error getting current working directory: %v\n", err)
DefaultGenPath = "/runtime/containerd" // Fallback in case of error
} else {
DefaultGenPath = filepath.Join(cwd, "runtime/containerd")
}
}
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved

func NewContainerdCommand() *cobra.Command {
var generateConfig bool
var defaultZotConfig *registry.DefaultZotConfig
var containerdConfigPath string
var containerDCertPath string

containerdCmd := &cobra.Command{
Use: "containerd",
Short: "Creates the config file for the containerd runtime to fetch the images from the local repository",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
utils.SetupContextForCommand(cmd)
log := logger.FromContext(cmd.Context())
if config.GetOwnRegistry() {
log.Info().Msg("Using own registry for config generation")
address, err := utils.ValidateRegistryAddress(config.GetOwnRegistryAdr(), config.GetOwnRegistryPort())
if err != nil {
log.Err(err).Msg("Error validating registry address")
return err
}
log.Info().Msgf("Registry address validated: %s", address)
defaultZotConfig.HTTP.Address = config.GetOwnRegistryAdr()
defaultZotConfig.HTTP.Port = config.GetOwnRegistryPort()
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
} else {
log.Info().Msg("Using default registry for config generation")
defaultZotConfig, err = registry.ReadConfig(config.GetZotConfigPath())
if err != nil {
return fmt.Errorf("could not read config: %w", err)
}
log.Info().Msgf("Default config read successfully: %v", defaultZotConfig.HTTP.Address+":"+defaultZotConfig.HTTP.Port)
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
}
return utils.CreateRuntimeDirectory(DefaultGenPath)
},
RunE: func(cmd *cobra.Command, args []string) error {
log := logger.FromContext(cmd.Context())
sourceRegistry := config.GetRemoteRegistryURL()
satelliteHostConfig := NewSatelliteHostConfig(defaultZotConfig.GetLocalRegistryURL(), sourceRegistry)
if generateConfig {
log.Info().Msg("Generating containerd config file for containerd ...")
log.Info().Msgf("Fetching containerd config from path path: %s", containerdConfigPath)
return GenerateContainerdHostConfig(containerDCertPath, DefaultGenPath, log, *satelliteHostConfig)
}
return nil
},
}

containerdCmd.Flags().BoolVarP(&generateConfig, "gen", "g", false, "Generate the containerd config file")
containerdCmd.PersistentFlags().StringVarP(&containerdConfigPath, "path", "p", DefaultContainerdConfigPath, "Path to the containerd config file of the container runtime")
containerdCmd.PersistentFlags().StringVarP(&containerDCertPath, "cert-path", "c", ContainerDCertPath, "Path to the containerd cert directory")
containerdCmd.AddCommand(NewReadConfigCommand(ContainerdRuntime))
return containerdCmd
}

// GenerateConfig generates the containerd config file for the containerd runtime
// It takes the zot config a logger and the containerd config path
// It reads the containerd config file and adds the local registry to the config file
func GenerateConfig(defaultZotConfig *registry.DefaultZotConfig, log *zerolog.Logger, containerdConfigPath, containerdCertPath string) error {
// First Read the present config file at the configPath
data, err := utils.ReadFile(containerdConfigPath, false)
if err != nil {
log.Err(err).Msg("Error reading config file")
return fmt.Errorf("could not read config file: %w", err)
}
// Now we marshal the data into the containerd config
containerdConfig := &containerd.Config{}
err = toml.Unmarshal(data, containerdConfig)
if err != nil {
log.Err(err).Msg("Error unmarshalling config")
return fmt.Errorf("could not unmarshal config: %w", err)
}
// Steps to configure the containerd config:
// 1. Set the default registry config cert path
// -- This is the path where the certs of the registry are stored
// -- If the user has already has a cert path then we do not set it rather we would now use the
// user path as the default path
if containerdConfig.PluginConfig.Registry.ConfigPath == "" {
containerdConfig.PluginConfig.Registry.ConfigPath = containerdCertPath
}
log.Info().Msgf("Setting the registry cert path to: %s", containerdConfig.PluginConfig.Registry.ConfigPath)
// Now we add the local registry to the containerd config mirrors
registryMirror := map[string]containerd.Mirror{
defaultZotConfig.HTTP.Address: {
Endpoints: []string{defaultZotConfig.HTTP.Address + ":" + defaultZotConfig.HTTP.Port},
},
}
if containerdConfig.PluginConfig.Registry.Mirrors == nil {
containerdConfig.PluginConfig.Registry.Mirrors = registryMirror
} else {
for key, value := range registryMirror {
containerdConfig.PluginConfig.Registry.Mirrors[key] = value
}
}
registryConfig := map[string]containerd.RegistryConfig{
defaultZotConfig.HTTP.Address: {
TLS: &containerd.TLSConfig{
InsecureSkipVerify: config.UseUnsecure(),
},
},
}
// Now we add the local registry to the containerd config registry
if containerdConfig.PluginConfig.Registry.Configs == nil {
containerdConfig.PluginConfig.Registry.Configs = registryConfig
} else {
for key, value := range registryConfig {
containerdConfig.PluginConfig.Registry.Configs[key] = value
}
}
// ToDo: Find a way to remove the unwanted configuration added to the config file while marshalling
pathToWrite := filepath.Join(DefaultGenPath, DefaultGeneratedTomlName)
log.Info().Msgf("Writing the containerd config to path: %s", pathToWrite)
// Now we write the config to the file
data, err = toml.Marshal(containerdConfig)
if err != nil {
log.Err(err).Msg("Error marshalling config")
return fmt.Errorf("could not marshal config: %w", err)
}
err = utils.WriteFile(pathToWrite, data)
if err != nil {
log.Err(err).Msg("Error writing config to file")
return fmt.Errorf("could not write config to file: %w", err)
}
return nil
}
110 changes: 110 additions & 0 deletions cmd/container_runtime/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package runtime
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"os"
"strings"

"container-registry.com/harbor-satellite/internal/config"
"container-registry.com/harbor-satellite/internal/utils"
"github.com/pelletier/go-toml/v2"
"github.com/rs/zerolog"
)

const (
DockerIoConfigPath = "docker"
HostToml = "host_gen.toml"
DefaultTomlConfigPath = "_default"
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
DockerURL = "https://registry-1.docker.io"
)

type ContainerdHostConfig struct {
Server string `toml:"server,omitempty"`
Host map[string]HostConfig `toml:"host,omitempty"`
}

type HostConfig struct {
Capabilities []string `toml:"capabilities,omitempty"`
CA interface{} `toml:"ca,omitempty"`
Client interface{} `toml:"client,omitempty"`
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
SkipVerify bool `toml:"skip_verify,omitempty"`
Header map[string][]string `toml:"header,omitempty"`
OverridePath bool `toml:"override_path,omitempty"`
}

type SatelliteHostConfig struct {
LocalRegistry string
SourceRegistry string
}

func NewSatelliteHostConfig(localRegistry, sourceRegistry string) *SatelliteHostConfig {
return &SatelliteHostConfig{
LocalRegistry: localRegistry,
SourceRegistry: sourceRegistry,
}
}

// GenerateContainerdHostConfig generates the host.toml file for containerd docker.io and also create a default config.toml file
func GenerateContainerdHostConfig(containerdCertPath, genPath string, log *zerolog.Logger, satelliteHostConfig SatelliteHostConfig) error {
mirrorGenPath := fmt.Sprintf("%s/%s", genPath, DockerIoConfigPath)
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
err := utils.CreateRuntimeDirectory(mirrorGenPath)
if err != nil {
log.Err(err).Msgf("Error creating the directory: %s", mirrorGenPath)
return fmt.Errorf("error creating the directory: %v", err)
}
dockerHubHostConfigPath := fmt.Sprintf("%s/%s/%s", containerdCertPath, DockerIoConfigPath, HostToml)
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
var dockerContainerdHostConfig ContainerdHostConfig

// Read the `docker.io/host.toml` file if present
data, err := utils.ReadFile(dockerHubHostConfigPath, false)
if err != nil {
if os.IsNotExist(err) {
log.Warn().Msgf("The docker.io/host.toml file does not exist at path: %s", dockerHubHostConfigPath)
} else {
return fmt.Errorf("error reading the docker.io/host.toml file: %v", err)
}
}
err = toml.Unmarshal(data, &dockerContainerdHostConfig)
if err != nil {
log.Err(err).Msgf("Error unmarshalling the docker.io/host.toml file at path: %s", dockerHubHostConfigPath)
return fmt.Errorf("error unmarshalling the docker.io/host.toml file: %v", err)
}
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
satelliteHostConfigToAdd := ContainerdHostConfig{
Host: map[string]HostConfig{
satelliteHostConfig.LocalRegistry: {
Capabilities: []string{"pull", "push", "resolve"},
SkipVerify: config.UseUnsecure(),
},
},
}

if dockerContainerdHostConfig.Server == "" {
dockerContainerdHostConfig.Server = DockerURL
}
if dockerContainerdHostConfig.Host == nil {
dockerContainerdHostConfig.Host = satelliteHostConfigToAdd.Host
} else {
for key, value := range dockerContainerdHostConfig.Host {
satelliteHostConfigToAdd.Host[key] = value
}
dockerContainerdHostConfig.Host = satelliteHostConfigToAdd.Host
}

pathTOWrite := fmt.Sprintf("%s/%s", mirrorGenPath, HostToml)
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
log.Info().Msgf("Writing the host.toml file at path: %s", pathTOWrite)
hostData, err := toml.Marshal(dockerContainerdHostConfig)
hostStr := string(hostData)
hostStr = strings.Replace(hostStr, "[host]\n", "", 1)
hostData = []byte(hostStr)
if err != nil {
Mehul-Kumar-27 marked this conversation as resolved.
Show resolved Hide resolved
log.Err(err).Msg("Error marshalling the host.toml file")
return fmt.Errorf("error marshalling the host.toml file: %v", err)
}
err = utils.WriteFile(pathTOWrite, hostData)
if err != nil {
log.Err(err).Msg("Error writing the host.toml file")
return fmt.Errorf("error writing the host.toml file: %v", err)
}
log.Info().Msg("Successfully wrote the host.toml file")
return nil
}
35 changes: 35 additions & 0 deletions cmd/container_runtime/read_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package runtime

import (
"fmt"


"container-registry.com/harbor-satellite/internal/utils"
"container-registry.com/harbor-satellite/logger"
"github.com/spf13/cobra"
)

func NewReadConfigCommand(runtime string) *cobra.Command {
readContainerdConfig := &cobra.Command{
Use: "read",
Short: fmt.Sprintf("Reads the config file for the %s runtime", runtime),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
utils.SetupContextForCommand(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
//Parse the flags
path, err := cmd.Flags().GetString("path")
if err != nil {
return fmt.Errorf("error reading the path flag: %v", err)
}
log := logger.FromContext(cmd.Context())
log.Info().Msgf("Reading the containerd config file from path: %s", path)
_, err = utils.ReadFile(path, true)
if err != nil {
return fmt.Errorf("error reading the containerd config file: %v", err)
}
return nil
},
}
return readContainerdConfig
}
Loading