Skip to content

Commit

Permalink
panic fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehul-Kumar-27 committed Dec 3, 2024
1 parent a3a4233 commit af01828
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
9 changes: 4 additions & 5 deletions cmd/container_runtime/crio.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,22 @@ func init() {
}

func NewCrioCommand() *cobra.Command {
var defaultZotConfig *registry.DefaultZotConfig
var defaultZotConfig registry.DefaultZotConfig
var generateConfig bool
var crioConfigPath string

crioCmd := &cobra.Command{
Use: "crio",
Short: "Creates the config file for the crio runtime to fetch the images from the local repository",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return SetupContainerRuntimeCommand(cmd, defaultZotConfig, DefaultCrioGenPath)
return SetupContainerRuntimeCommand(cmd, &defaultZotConfig, DefaultCrioGenPath)
},
RunE: func(cmd *cobra.Command, args []string) error {
log := logger.FromContext(cmd.Context())
if generateConfig {
log.Info().Msg("Generating the config file for crio ...")
log.Info().Msgf("Fetching crio registry config file form path: %s", crioConfigPath)
// Generate the config file
err := GenerateCrioRegistryConfig(defaultZotConfig, crioConfigPath, log)
err := GenerateCrioRegistryConfig(&defaultZotConfig, crioConfigPath, log)
if err != nil {
log.Err(err).Msg("Error generating crio registry config")
return err
Expand Down Expand Up @@ -163,7 +162,7 @@ func SetupContainerRuntimeCommand(cmd *cobra.Command, defaultZotConfig *registry
defaultZotConfig.RemoteURL = config.GetRemoteRegistryURL()
} else {
log.Info().Msg("Using default registry for config generation")
defaultZotConfig, err = registry.ReadConfig(config.GetZotConfigPath())
err = registry.ReadConfig(config.GetZotConfigPath(), defaultZotConfig)
if err != nil || defaultZotConfig == nil {
return fmt.Errorf("could not read config: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func HandleOwnRegistry() error {

// LaunchDefaultZotRegistry launches the default Zot registry using the Zot config path
func LaunchDefaultZotRegistry() error {
defaultZotConfig, err := registry.ReadConfig(config.GetZotConfigPath())
var defaultZotConfig registry.DefaultZotConfig
err := registry.ReadConfig(config.GetZotConfigPath(), &defaultZotConfig)
if err != nil {
return fmt.Errorf("error reading config: %w", err)
}
Expand Down
13 changes: 6 additions & 7 deletions registry/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,25 @@ func (c *DefaultZotConfig) GetLocalRegistryURL() string {
}

// ReadConfig reads a JSON file from the specified path and unmarshals it into a Config struct.
func ReadConfig(filePath string) (*DefaultZotConfig, error) {
func ReadConfig(filePath string, zotConfig *DefaultZotConfig) (error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("could not open file: %w", err)
return fmt.Errorf("could not open file: %w", err)
}
defer file.Close()

// Read the file contents
bytes, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("could not read file: %w", err)
return fmt.Errorf("could not read file: %w", err)
}

// Unmarshal the JSON into a Config struct
var config DefaultZotConfig
err = json.Unmarshal(bytes, &config)
err = json.Unmarshal(bytes, &zotConfig)
if err != nil {
return nil, fmt.Errorf("could not unmarshal JSON: %w", err)
return fmt.Errorf("could not unmarshal JSON: %w", err)
}
return &config, nil
return nil
}

func (c *DefaultZotConfig) SetZotRemoteURL(url string) {
Expand Down

0 comments on commit af01828

Please sign in to comment.