Skip to content

Commit

Permalink
coderabbit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehul-Kumar-27 committed Oct 29, 2024
1 parent 8624668 commit 771a6e3
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 36 deletions.
10 changes: 8 additions & 2 deletions cmd/container_runtime/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ 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
if _, err := os.Stat(DefaultGenPath); os.IsNotExist(err) {
err := os.MkdirAll(DefaultGenPath, os.ModePerm)
if err != nil {
fmt.Printf("Error creating default directory: %v\n", err)
}
}
} else {
DefaultGenPath = filepath.Join(cwd, "runtime/containerd")
}
Expand All @@ -53,6 +58,7 @@ func NewContainerdCommand() *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
utils.SetupContextForCommand(cmd)
config.InitConfig()
log := logger.FromContext(cmd.Context())
if config.GetOwnRegistry() {
log.Info().Msg("Using own registry for config generation")
Expand All @@ -67,7 +73,7 @@ func NewContainerdCommand() *cobra.Command {
} else {
log.Info().Msg("Using default registry for config generation")
defaultZotConfig, err = registry.ReadConfig(config.GetZotConfigPath())
if err != nil {
if err != nil || defaultZotConfig == nil {
return fmt.Errorf("could not read config: %w", err)
}
log.Info().Msgf("Default config read successfully: %v", defaultZotConfig.HTTP.Address+":"+defaultZotConfig.HTTP.Port)
Expand Down
41 changes: 21 additions & 20 deletions cmd/container_runtime/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runtime
import (
"fmt"
"os"
"path/filepath"
"strings"

"container-registry.com/harbor-satellite/internal/config"
Expand Down Expand Up @@ -52,22 +53,22 @@ func GenerateContainerdHostConfig(containerdCertPath, genPath string, log *zerol
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, SatelliteConfigPath, HostToml)
var dockerContainerdHostConfig ContainerdHostConfig
satelliteHubHostConfigPath := fmt.Sprintf("%s/%s/%s", containerdCertPath, SatelliteConfigPath, HostToml)
var satelliteContainerdHostConfig ContainerdHostConfig

// Read the `docker.io/host.toml` file if present
data, err := utils.ReadFile(dockerHubHostConfigPath, false)
// Read the `satellite/host.toml` file if present
data, err := utils.ReadFile(satelliteHubHostConfigPath, false)
if err != nil {
if os.IsNotExist(err) {
log.Warn().Msgf("The docker.io/host.toml file does not exist at path: %s", dockerHubHostConfigPath)
log.Warn().Msgf("The satellite/host.toml file does not exist at path: %s", satelliteHubHostConfigPath)
} else {
return fmt.Errorf("error reading the docker.io/host.toml file: %v", err)
return fmt.Errorf("error reading the satellite/host.toml file: %v", err)
}
}
err = toml.Unmarshal(data, &dockerContainerdHostConfig)
err = toml.Unmarshal(data, &satelliteContainerdHostConfig)
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)
log.Err(err).Msgf("Error unmarshalling the satellite/host.toml file at path: %s", satelliteHubHostConfigPath)
return fmt.Errorf("error unmarshalling the satellite/host.toml file: %v", err)
}
satelliteHostConfigToAdd := ContainerdHostConfig{
Host: map[string]HostConfig{
Expand All @@ -78,28 +79,28 @@ func GenerateContainerdHostConfig(containerdCertPath, genPath string, log *zerol
},
}

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

pathTOWrite := fmt.Sprintf("%s/%s", mirrorGenPath, HostToml)
pathTOWrite := filepath.Join(mirrorGenPath, HostToml)
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)
hostData, err := toml.Marshal(satelliteContainerdHostConfig)
if err != nil {
log.Err(err).Msg("Error marshalling the host.toml file")
return fmt.Errorf("error marshalling the host.toml file: %v", err)
}
hostStr := string(hostData)
hostStr = strings.Replace(hostStr, "[host]\n", "", 1)
hostData = []byte(hostStr)
err = utils.WriteFile(pathTOWrite, hostData)
if err != nil {
log.Err(err).Msg("Error writing the host.toml file")
Expand Down
3 changes: 2 additions & 1 deletion cmd/container_runtime/read_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package runtime
import (
"fmt"


"container-registry.com/harbor-satellite/internal/config"
"container-registry.com/harbor-satellite/internal/utils"
"container-registry.com/harbor-satellite/logger"
"github.com/spf13/cobra"
Expand All @@ -15,6 +15,7 @@ func NewReadConfigCommand(runtime string) *cobra.Command {
Short: fmt.Sprintf("Reads the config file for the %s runtime", runtime),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
utils.SetupContextForCommand(cmd)
config.InitConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
//Parse the flags
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func run(ctx context.Context, cancel context.CancelFunc) error {
if err != nil || stateArtifactFetcher == nil {
return fmt.Errorf("error processing input: %w", err)
}
if stateArtifactFetcher == nil {
return fmt.Errorf("state artifact fetcher is nil")
}

satelliteService := satellite.NewSatellite(ctx, stateArtifactFetcher, scheduler.GetSchedulerKey())

Expand Down
8 changes: 6 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (
"github.com/spf13/viper"
)

func init(){
InitConfig()
func init() {
err := InitConfig()
if err != nil {
fmt.Printf("Error initializing config: %v\n", err)
os.Exit(1)
}
}

var AppConfig *Config
Expand Down
7 changes: 6 additions & 1 deletion internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func (s *BasicScheduler) Schedule(process Process) error {
}
// Add the process to the scheduler
_, err := s.cron.AddFunc(process.GetCronExpr(), func() {
s.executeProcess(process)
err := s.executeProcess(process)
if err != nil {
log.Error().Err(err).Msgf("Error executing process %s", process.GetName())
}
})
if err != nil {
return fmt.Errorf("error adding process to scheduler: %w", err)
Expand All @@ -91,6 +94,8 @@ func (s *BasicScheduler) Start() error {
}

func (s *BasicScheduler) Stop() error {
s.mu.Lock()
defer s.mu.Unlock()
s.stopped = true
s.cron.Stop()
return nil
Expand Down
4 changes: 4 additions & 0 deletions internal/state/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func NewArtifact(deleted bool, repository string, tags []string, digest, artifac
}
}

func (a *Artifact) GetLabels() []string {
return a.Labels
}

func (a *Artifact) GetRepository() string {
return a.Repository
}
Expand Down
20 changes: 10 additions & 10 deletions internal/state/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type StateFetcher interface {
}

type baseStateFetcher struct {
group_name string
state_artifact_name string
state_artifact_reader StateReader
groupName string
stateArtifactName string
stateArtifactReader StateReader
}

type URLStateFetcher struct {
Expand All @@ -39,9 +39,9 @@ func NewURLStateFetcher() StateFetcher {
url = utils.FormatRegistryURL(url)
return &URLStateFetcher{
baseStateFetcher: baseStateFetcher{
group_name: config.GetGroupName(),
state_artifact_name: config.GetStateArtifactName(),
state_artifact_reader: NewState(),
groupName: config.GetGroupName(),
stateArtifactName: config.GetStateArtifactName(),
stateArtifactReader: NewState(),
},
url: url,
}
Expand All @@ -50,9 +50,9 @@ func NewURLStateFetcher() StateFetcher {
func NewFileStateFetcher() StateFetcher {
return &FileStateArtifactFetcher{
baseStateFetcher: baseStateFetcher{
group_name: config.GetGroupName(),
state_artifact_name: config.GetStateArtifactName(),
state_artifact_reader: NewState(),
groupName: config.GetGroupName(),
stateArtifactName: config.GetStateArtifactName(),
stateArtifactReader: NewState(),
},
filePath: config.GetInput(),
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (f *URLStateFetcher) FetchStateArtifact(state interface{}) error {
sourceRegistry := utils.FormatRegistryURL(config.GetRemoteRegistryURL())
tag := "latest"

img, err := crane.Pull(fmt.Sprintf("%s/%s/%s:%s", sourceRegistry, f.group_name, f.state_artifact_name, tag), options...)
img, err := crane.Pull(fmt.Sprintf("%s/%s/%s:%s", sourceRegistry, f.groupName, f.stateArtifactName, tag), options...)
if err != nil {
return fmt.Errorf("failed to pull the state artifact: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ func main() {
err := cmd.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}

0 comments on commit 771a6e3

Please sign in to comment.