From f7540854e8aa1ae21aae23af41b84ab384fa31b4 Mon Sep 17 00:00:00 2001 From: Mehul-Kumar-27 <202151092@iiitvadodara.ac.in> Date: Tue, 5 Nov 2024 00:55:38 +0530 Subject: [PATCH] moving from toml config to json config --- .gitignore | 1 + config.json | 15 ++ internal/config/config.go | 374 +++++++++++++++++----------------- internal/config/new_config.go | 136 +++++++++++++ internal/server/server.go | 5 +- internal/state/fetcher.go | 29 +-- main.go | 15 +- 7 files changed, 357 insertions(+), 218 deletions(-) create mode 100644 config.json create mode 100644 internal/config/new_config.go diff --git a/.gitignore b/.gitignore index 32234d8..420e0d1 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ secrets.txt __debug_bin1949266242 /zot +/runtime \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..1e90a3e --- /dev/null +++ b/config.json @@ -0,0 +1,15 @@ +{ + "auth": { + "name": "admin", + "registry": "https://registry.bupd.xyz", + "secret": "Harbor12345" + }, + "bring_own_registry": false, + "ground_control_url": "http://localhost:8080", + "log_level": "info", + "own_registry_adr": "127.0.0.1", + "own_registry_port": "8585", + "states": ["https://registry.bupd.xyz/satellite/group10/state:latest"], + "url_or_file": "https://registry.bupd.xyz", + "zotconfigpath": "./registry/config.json" +} diff --git a/internal/config/config.go b/internal/config/config.go index d4a9946..11d0d59 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,193 +1,185 @@ package config -import ( - "fmt" - "os" - - "github.com/joho/godotenv" - "github.com/spf13/viper" -) - -var AppConfig *Config - -type Config struct { - log_level string - own_registry bool - own_registry_adr string - own_registry_port string - zot_config_path string - input string - zot_url string - registry string - repository string - user_input string - scheme string - api_version string - image string - harbor_password string - harbor_username string - env string - use_unsecure bool - remote_registry_url string - group_name string - state_artifact_name string - state_fetch_period string -} - -func GetLogLevel() string { - return AppConfig.log_level -} - -func GetOwnRegistry() bool { - return AppConfig.own_registry -} - -func GetOwnRegistryAdr() string { - return AppConfig.own_registry_adr -} - -func GetOwnRegistryPort() string { - return AppConfig.own_registry_port -} - -func GetZotConfigPath() string { - return AppConfig.zot_config_path -} - -func GetInput() string { - return AppConfig.input -} - -func SetZotURL(url string) { - AppConfig.zot_url = url -} - -func GetZotURL() string { - return AppConfig.zot_url -} - -func SetRegistry(registry string) { - AppConfig.registry = registry -} - -func GetRegistry() string { - return AppConfig.registry -} - -func SetRepository(repository string) { - AppConfig.repository = repository -} - -func GetRepository() string { - return AppConfig.repository -} - -func SetUserInput(user_input string) { - AppConfig.user_input = user_input -} - -func GetUserInput() string { - return AppConfig.user_input -} - -func SetScheme(scheme string) { - AppConfig.scheme = scheme -} - -func GetScheme() string { - return AppConfig.scheme -} - -func SetAPIVersion(api_version string) { - AppConfig.api_version = api_version -} - -func GetAPIVersion() string { - return AppConfig.api_version -} - -func SetImage(image string) { - AppConfig.image = image -} - -func GetImage() string { - return AppConfig.image -} - -func UseUnsecure() bool { - return AppConfig.use_unsecure -} - -func GetHarborPassword() string { - return AppConfig.harbor_password -} - -func GetHarborUsername() string { - return AppConfig.harbor_username -} - -func SetRemoteRegistryURL(url string) { - AppConfig.remote_registry_url = url -} - -func GetRemoteRegistryURL() string { - return AppConfig.remote_registry_url -} - -func GetGroupName() string { - return AppConfig.group_name -} - -func GetStateArtifactName() string { - return AppConfig.state_artifact_name -} - -func GetStateFetchPeriod() string { - return AppConfig.state_fetch_period -} - -func LoadConfig() (*Config, error) { - viper.SetConfigName("config") - viper.SetConfigType("toml") - viper.AddConfigPath(".") - if err := viper.ReadInConfig(); err != nil { - return nil, fmt.Errorf("error reading config file at path '%s': %w", viper.ConfigFileUsed(), err) - } - - // Load environment and start satellite - if err := godotenv.Load(); err != nil { - return &Config{}, fmt.Errorf("error loading .env file: %w", err) - } - var use_unsecure bool - if os.Getenv("USE_UNSECURE") == "true" { - use_unsecure = true - } else { - use_unsecure = false - } - - return &Config{ - log_level: viper.GetString("log_level"), - own_registry: viper.GetBool("bring_own_registry"), - own_registry_adr: viper.GetString("own_registry_adr"), - own_registry_port: viper.GetString("own_registry_port"), - zot_config_path: viper.GetString("zotConfigPath"), - input: viper.GetString("url_or_file"), - harbor_password: os.Getenv("HARBOR_PASSWORD"), - harbor_username: os.Getenv("HARBOR_USERNAME"), - env: os.Getenv("ENV"), - zot_url: os.Getenv("ZOT_URL"), - use_unsecure: use_unsecure, - group_name: os.Getenv("GROUP_NAME"), - state_artifact_name: os.Getenv("STATE_ARTIFACT_NAME"), - state_fetch_period: os.Getenv("STATE_FETCH_PERIOD"), - }, nil -} - -func InitConfig() error { - var err error - AppConfig, err = LoadConfig() - if err != nil { - return err - } - return nil -} +// import ( +// "fmt" +// "os" + +// "github.com/joho/godotenv" +// "github.com/spf13/viper" +// ) + +// var AppConfig *Config + +// type Config struct { +// log_level string +// own_registry bool +// own_registry_adr string +// own_registry_port string +// zot_config_path string +// input string +// zot_url string +// registry string +// repository string +// user_input string +// scheme string +// api_version string +// image string +// harbor_password string +// harbor_username string +// env string +// use_unsecure bool +// remote_registry_url string +// group_name string +// state_artifact_name string +// state_fetch_period string +// } + +// func GetLogLevel() string { +// return AppConfig.log_level +// } + +// func GetOwnRegistry() bool { +// return AppConfig.own_registry +// } + +// func GetOwnRegistryAdr() string { +// return AppConfig.own_registry_adr +// } + +// func GetOwnRegistryPort() string { +// return AppConfig.own_registry_port +// } + +// func GetZotConfigPath() string { +// return AppConfig.zot_config_path +// } + +// func GetInput() string { +// return AppConfig.input +// } + +// func SetZotURL(url string) { +// AppConfig.zot_url = url +// } + +// func GetZotURL() string { +// return AppConfig.zot_url +// } + +// func SetRegistry(registry string) { +// AppConfig.registry = registry +// } + +// func GetRegistry() string { +// return AppConfig.registry +// } + +// func SetRepository(repository string) { +// AppConfig.repository = repository +// } + +// func GetRepository() string { +// return AppConfig.repository +// } + +// func SetUserInput(user_input string) { +// AppConfig.user_input = user_input +// } + +// func GetUserInput() string { +// return AppConfig.user_input +// } + +// func SetScheme(scheme string) { +// AppConfig.scheme = scheme +// } + +// func GetScheme() string { +// return AppConfig.scheme +// } + +// func SetAPIVersion(api_version string) { +// AppConfig.api_version = api_version +// } + +// func GetAPIVersion() string { +// return AppConfig.api_version +// } + +// func SetImage(image string) { +// AppConfig.image = image +// } + +// func GetImage() string { +// return AppConfig.image +// } + +// func UseUnsecure() bool { +// return AppConfig.use_unsecure +// } + +// func GetHarborPassword() string { +// return AppConfig.harbor_password +// } + +// func GetHarborUsername() string { +// return AppConfig.harbor_username +// } + +// func SetRemoteRegistryURL(url string) { +// AppConfig.remote_registry_url = url +// } + +// func GetRemoteRegistryURL() string { +// return AppConfig.remote_registry_url +// } + +// func GetGroupName() string { +// return AppConfig.group_name +// } + +// func GetStateArtifactName() string { +// return AppConfig.state_artifact_name +// } + +// func GetStateFetchPeriod() string { +// return AppConfig.state_fetch_period +// } + +// func LoadConfig() (*Config, error) { +// viper.SetConfigName("config") +// viper.SetConfigType("toml") +// viper.AddConfigPath(".") +// if err := viper.ReadInConfig(); err != nil { +// return nil, fmt.Errorf("error reading config file at path '%s': %w", viper.ConfigFileUsed(), err) +// } + +// // Load environment and start satellite +// if err := godotenv.Load(); err != nil { +// return &Config{}, fmt.Errorf("error loading .env file: %w", err) +// } +// var use_unsecure bool +// if os.Getenv("USE_UNSECURE") == "true" { +// use_unsecure = true +// } else { +// use_unsecure = false +// } + +// return &Config{ +// log_level: viper.GetString("log_level"), +// own_registry: viper.GetBool("bring_own_registry"), +// own_registry_adr: viper.GetString("own_registry_adr"), +// own_registry_port: viper.GetString("own_registry_port"), +// zot_config_path: viper.GetString("zotConfigPath"), +// input: viper.GetString("url_or_file"), +// harbor_password: os.Getenv("HARBOR_PASSWORD"), +// harbor_username: os.Getenv("HARBOR_USERNAME"), +// env: os.Getenv("ENV"), +// zot_url: os.Getenv("ZOT_URL"), +// use_unsecure: use_unsecure, +// group_name: os.Getenv("GROUP_NAME"), +// state_artifact_name: os.Getenv("STATE_ARTIFACT_NAME"), +// state_fetch_period: os.Getenv("STATE_FETCH_PERIOD"), +// }, nil +// } + diff --git a/internal/config/new_config.go b/internal/config/new_config.go new file mode 100644 index 0000000..8dc3463 --- /dev/null +++ b/internal/config/new_config.go @@ -0,0 +1,136 @@ +package config + +import ( + "encoding/json" + "fmt" + "os" +) + +var appConfig *Config + +const DefaultConfigPath string = "config.json" + +type Auth struct { + Name string `json:"name"` + Registry string `json:"registry"` + Secret string `json:"secret"` +} + +type Config struct { + Auths Auth `json:"auth"` + BringOwnRegistry bool `json:"bring_own_registry"` + GroundControlURL string `json:"ground_control_url"` + LogLevel string `json:"log_level"` + OwnRegistryAddress string `json:"own_registry_adr"` + OwnRegistryPort string `json:"own_registry_port"` + States []string `json:"states"` + URLOrFile string `json:"url_or_file"` + ZotConfigPath string `json:"zotconfigpath"` + UseUnsecure bool `json:"use_unsecure"` + ZotUrl string `json:"zot_url"` + StateFetchPeriod string `json:"state_fetch_period"` +} + +func GetLogLevel() string { + return appConfig.LogLevel +} + +func GetOwnRegistry() bool { + return appConfig.BringOwnRegistry +} + +func GetOwnRegistryAdr() string { + return appConfig.OwnRegistryAddress +} + +func GetOwnRegistryPort() string { + return appConfig.OwnRegistryPort +} + +func GetZotConfigPath() string { + return appConfig.ZotConfigPath +} + +func GetInput() string { + return appConfig.URLOrFile +} + +func SetZotURL(url string) { + appConfig.ZotUrl = url +} + +func GetZotURL() string { + return appConfig.ZotUrl +} + +func UseUnsecure() bool { + return appConfig.UseUnsecure +} + +func GetHarborPassword() string { + return appConfig.Auths.Secret +} + +func GetHarborUsername() string { + return appConfig.Auths.Name +} + +func SetRemoteRegistryURL(url string) { + appConfig.Auths.Registry = url +} + +func GetRemoteRegistryURL() string { + return appConfig.Auths.Registry +} + +func GetStateFetchPeriod() string { + return appConfig.StateFetchPeriod +} + +func ParseConfigFromJson(jsonData string) (*Config, error) { + var config Config + err := json.Unmarshal([]byte(jsonData), &config) + if err != nil { + return nil, err + } + return &config, nil +} + +func ReadConfigData(configPath string) ([]byte, error) { + + fileInfo, err := os.Stat(configPath) + if err != nil { + return nil, err + } + if fileInfo.IsDir() { + return nil, os.ErrNotExist + } + data, err := os.ReadFile(configPath) + if err != nil { + return nil, err + } + return data, nil +} + +func LoadConfig() (*Config, error) { + configData, err := ReadConfigData(DefaultConfigPath) + if err != nil { + fmt.Printf("Error reading config file: %v\n", err) + os.Exit(1) + } + config, err := ParseConfigFromJson(string(configData)) + if err != nil { + fmt.Printf("Error parsing config file: %v\n", err) + os.Exit(1) + } + return config, nil +} + +func InitConfig() error { + var err error + appConfig, err = LoadConfig() + if err != nil { + return err + } + return nil +} diff --git a/internal/server/server.go b/internal/server/server.go index 019ed01..661e98c 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" - "container-registry.com/harbor-satellite/internal/config" "github.com/rs/zerolog" "golang.org/x/sync/errgroup" ) @@ -22,16 +21,14 @@ type App struct { server *http.Server ctx context.Context Logger *zerolog.Logger - config *config.Config } -func NewApp(router Router, ctx context.Context, logger *zerolog.Logger, config *config.Config, registrars ...RouteRegistrar) *App { +func NewApp(router Router, ctx context.Context, logger *zerolog.Logger, registrars ...RouteRegistrar) *App { return &App{ router: router, registrars: registrars, ctx: ctx, Logger: logger, - config: config, server: &http.Server{Addr: ":9090", Handler: router}, } } diff --git a/internal/state/fetcher.go b/internal/state/fetcher.go index d2f9710..b72cc59 100644 --- a/internal/state/fetcher.go +++ b/internal/state/fetcher.go @@ -19,9 +19,8 @@ type StateFetcher interface { } type baseStateFetcher struct { - group_name string - state_artifact_name string - state_artifact_reader StateReader + username string + password string } type URLStateFetcher struct { @@ -34,27 +33,24 @@ type FileStateArtifactFetcher struct { filePath string } -func NewURLStateFetcher() StateFetcher { - url := config.GetRemoteRegistryURL() - url = utils.FormatRegistryURL(url) +func NewURLStateFetcher(stateURL, userName, password string) StateFetcher { + url := utils.FormatRegistryURL(stateURL) return &URLStateFetcher{ baseStateFetcher: baseStateFetcher{ - group_name: config.GetGroupName(), - state_artifact_name: config.GetStateArtifactName(), - state_artifact_reader: NewState(), + username: userName, + password: password, }, url: url, } } -func NewFileStateFetcher() StateFetcher { +func NewFileStateFetcher(filePath, userName, password string) StateFetcher { return &FileStateArtifactFetcher{ baseStateFetcher: baseStateFetcher{ - group_name: config.GetGroupName(), - state_artifact_name: config.GetStateArtifactName(), - state_artifact_reader: NewState(), + username: userName, + password: password, }, - filePath: config.GetInput(), + filePath: filePath, } } @@ -81,10 +77,7 @@ func (f *URLStateFetcher) FetchStateArtifact(state interface{}) error { options = append(options, crane.Insecure) } - 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(f.url, options...) if err != nil { return fmt.Errorf("failed to pull the state artifact: %v", err) } diff --git a/main.go b/main.go index 37e46a9..11467f2 100644 --- a/main.go +++ b/main.go @@ -91,7 +91,6 @@ func setupServerApp(ctx context.Context, log *zerolog.Logger) *server.App { router, ctx, log, - config.AppConfig, &server.MetricsRegistrar{}, &server.DebugRegistrar{}, &satellite.SatelliteRegistrar{}, @@ -131,20 +130,26 @@ func processInput(ctx context.Context, log *zerolog.Logger) (state.StateFetcher, return nil, err } - return processFileInput(log) + return processFileInput(input, log) } func processURLInput(input string, log *zerolog.Logger) (state.StateFetcher, error) { log.Info().Msg("Input is a valid URL") config.SetRemoteRegistryURL(input) - stateArtifactFetcher := state.NewURLStateFetcher() + username := config.GetHarborUsername() + password := config.GetHarborPassword() + + stateArtifactFetcher := state.NewURLStateFetcher(input, username, password) return stateArtifactFetcher, nil } -func processFileInput(log *zerolog.Logger) (state.StateFetcher, error) { - stateArtifactFetcher := state.NewFileStateFetcher() +func processFileInput(input string, log *zerolog.Logger) (state.StateFetcher, error) { + log.Info().Msg("Input is a valid file path") + username := config.GetHarborUsername() + password := config.GetHarborPassword() + stateArtifactFetcher := state.NewFileStateFetcher(input, username, password) return stateArtifactFetcher, nil }