From acc40b5bff59f17ff75107ee4ae80e66177900b0 Mon Sep 17 00:00:00 2001 From: Michal Gubricky Date: Tue, 30 Jul 2024 06:48:54 +0200 Subject: [PATCH] :sparkles: Generate for URL for SWIFT (#29) * Generate for URL for SWIFT Signed-off-by: michal.gubricky * Update docs Signed-off-by: michal.gubricky * Fix golint error Signed-off-by: michal.gubricky * Update docs on how patterns for urls is generated Signed-off-by: michal.gubricky * Update docs/how_to_use_csctl_plugin_openstack.md Co-authored-by: Roman Hros Signed-off-by: Michal Gubricky --------- Signed-off-by: michal.gubricky Signed-off-by: Michal Gubricky Co-authored-by: Roman Hros --- docs/how_to_use_csctl_plugin_openstack.md | 30 +++++++++++++++---- .../ferrol/node-images/registry.yaml | 1 + main.go | 20 ++++++++++--- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/docs/how_to_use_csctl_plugin_openstack.md b/docs/how_to_use_csctl_plugin_openstack.md index a3f7bf0..2761c2a 100644 --- a/docs/how_to_use_csctl_plugin_openstack.md +++ b/docs/how_to_use_csctl_plugin_openstack.md @@ -19,16 +19,36 @@ This method can be used when the creator of the cluster-stacks has already built ### Build method -The use case for this method is the opposite of the `Get` method. It means that the cluster-stack creator intends to use an image that has not yet been built. The plugin then builds image(s) based on Packer scripts in the `node-images` folder and pushes these image(s) to an S3 bucket. In this mode, you need to provide the path to your S3 storage credentials using the `--node-image-registry` flag, see [registry.yaml](../example/cluster-stacks/openstack/ferrol/node-images/registry.yaml). The URL does not need to be set in `config.yaml`, plugin can creates for you based on this pattern: +The use case for this method is the opposite of the `Get` method. It means that the cluster-stack creator intends to use an image that has not yet been built. The plugin then builds image(s) based on Packer scripts in the `node-images` folder and pushes these image(s) to an S3 bucket. In this mode, you need to provide the path to your S3 storage credentials using the `--node-image-registry` flag, see [registry.yaml](../example/cluster-stacks/openstack/ferrol/node-images/registry.yaml). The URL does not need to be set in `config.yaml`. The plugin can create it for you based on the following patterns: -```bash -https://// -``` +- for an `S3` type registry: + + ```bash + // + ``` + +- for a `Swift` type registry: + + ```bash + /swift/v1/AUTH_// + ``` Be aware of that in this method you need to specify `imageDir` in `config.yaml` file. > [!NOTE] -> URL creation does not work for OpenStack Swift. +> If you want to use URL creation for OpenStack Swift registry, please change the `registry.yaml` file accordingly: + +```yaml +type: Swift +config: + endpoint: + bucket: + accessKey: + secretKey: + projectID: + # verify: false # Only if you want to disable SSL certificate verification and use `http` url in endpoint + # cacert: # Use this field only if the S3 storage endpoint certificate is signed by a custom(non-public) authority +``` ## Installing csctl plugin for OpenStack diff --git a/example/cluster-stacks/openstack/ferrol/node-images/registry.yaml b/example/cluster-stacks/openstack/ferrol/node-images/registry.yaml index 1f999fa..5f96cd5 100644 --- a/example/cluster-stacks/openstack/ferrol/node-images/registry.yaml +++ b/example/cluster-stacks/openstack/ferrol/node-images/registry.yaml @@ -4,5 +4,6 @@ config: bucket: accessKey: secretKey: + # projectID: # Needs to be specified when type is equal to Swift and URL is not set in config.yaml file # verify: false # Only if you want to disable SSL certificate verification and use `http` url in endpoint # cacert: # Use this field only if the S3 storage endpoint certificate is signed by a custom(non-public) authority diff --git a/main.go b/main.go index 3fcbe82..2813dcb 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,7 @@ type RegistryConfig struct { SecretKey string `yaml:"secretKey"` Verify *bool `yaml:"verify,omitempty"` Cacert string `yaml:"cacert,omitempty"` + ProjectID string `yaml:"projectID,omitempty"` //nolint:tagliatelle // using 'projectID' instead of 'projectId' } `yaml:"config"` } @@ -211,8 +212,8 @@ func pushToS3(filePath, fileName, registryConfigPath string) error { return fmt.Errorf("error decoding registry config file: %w", err) } - if registryConfig.Type != "S3" { - return fmt.Errorf("error, only S3 compatible registry is supported") + if registryConfig.Type != "S3" && registryConfig.Type != "Swift" { + return fmt.Errorf("error, only S3 or Swift compatible registry is supported") } // Remove "http://" or "https://" from the endpoint if present cause Endpoint cannot have fully qualified paths in minioClient. @@ -313,8 +314,19 @@ func updateURLNodeImages(configFilePath, registryConfigPath, imageName string, i if err := decoder.Decode(®istryConfig); err != nil { return fmt.Errorf("error decoding registry config file: %w", err) } - // Generate URL - newURL := fmt.Sprintf("%s%s/%s/%s", "https://", registryConfig.Config.Endpoint, registryConfig.Config.Bucket, imageName) + + // Check registry type and projectID for Swift + if registryConfig.Type == "Swift" && registryConfig.Config.ProjectID == "" { + return fmt.Errorf("error, projectID must be specified for Swift registry") + } + + // Generate URL based on registry type + var newURL string + if registryConfig.Type == "S3" { + newURL = fmt.Sprintf("%s/%s/%s", registryConfig.Config.Endpoint, registryConfig.Config.Bucket, imageName) + } else if registryConfig.Type == "Swift" { + newURL = fmt.Sprintf("%s/swift/v1/AUTH_%s/%s/%s", registryConfig.Config.Endpoint, registryConfig.Config.ProjectID, registryConfig.Config.Bucket, imageName) + } // Assign the generated URL to the correct node-image nodeImages.OpenStackNodeImages[imageOrder].URL = newURL