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

ROX-16507: Download ACS Operator CRDs dynamically #1010

Merged
merged 8 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
27 changes: 27 additions & 0 deletions fleetshard/pkg/central/charts/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ package charts
import (
"embed"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path"
"strings"

Expand Down Expand Up @@ -65,3 +68,27 @@ func MustGetChart(name string) *chart.Chart {
}
return chrt
}

// DownloadTemplate downloads a file from the URL to the templates folder of specified chart
func DownloadTemplate(url string, chartName string) error {
resp, err := http.Get(url)
kurlov marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("failed Get for %s: %w", url, err)
}
defer resp.Body.Close()

// generate the filename from the URL
filename := url[strings.LastIndex(url, "/")+1:]
filepath := path.Join("data", chartName, "templates", filename)
out, err := os.Create(filepath)
if err != nil {
return fmt.Errorf("cannot create file %s: %w", filepath, err)
}
defer out.Close()

if _, err = io.Copy(out, resp.Body); err != nil {
return fmt.Errorf("cannot copy to file %s: %w", filepath, err)
}

return nil
}
13 changes: 13 additions & 0 deletions fleetshard/pkg/central/charts/charts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package charts

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -12,3 +13,15 @@ func TestTenantResourcesChart(t *testing.T) {
require.NoError(t, err)
assert.NotNil(t, c)
}

func TestDownloadTemplate(t *testing.T) {
crdURL := "https://raw.githubusercontent.com/stackrox/stackrox/master/operator/bundle/manifests/platform.stackrox.io_securedclusters.yaml"
err := DownloadTemplate(crdURL, "rhacs-operator")
require.NoError(t, err)
_, err = os.Stat("data/rhacs-operator/templates/platform.stackrox.io_securedclusters.yaml")
require.NoError(t, err)

c, err := GetChart("rhacs-operator")
require.NoError(t, err)
assert.NotNil(t, c)
}
Loading