Skip to content

Commit 9d1637b

Browse files
authored
Merge pull request #7371 from aniruddha2000/aniruddha/add-raw-template-url
✨ Add support for raw template URL in clusterctl generate yaml
2 parents 02240ca + 48a837d commit 9d1637b

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

cmd/clusterctl/client/cluster/template.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type templateClient struct {
5151
configClient config.Client
5252
gitHubClientFactory func(configVariablesClient config.VariablesClient) (*github.Client, error)
5353
processor yaml.Processor
54+
httpClient *http.Client
5455
}
5556

5657
// ensure templateClient implements TemplateClient.
@@ -70,6 +71,7 @@ func newTemplateClient(input TemplateClientInput) *templateClient {
7071
configClient: input.configClient,
7172
gitHubClientFactory: getGitHubClient,
7273
processor: input.processor,
74+
httpClient: http.DefaultClient,
7375
}
7476
}
7577

@@ -143,8 +145,11 @@ func (t *templateClient) getURLContent(templateURL string) ([]byte, error) {
143145
return nil, errors.Wrapf(err, "failed to parse %q", templateURL)
144146
}
145147

146-
if rURL.Scheme == "https" && rURL.Host == "github.com" {
147-
return t.getGitHubFileContent(rURL)
148+
if rURL.Scheme == "https" {
149+
if rURL.Host == "github.com" {
150+
return t.getGitHubFileContent(rURL)
151+
}
152+
return t.getRawURLFileContent(templateURL)
148153
}
149154

150155
if rURL.Scheme == "file" || rURL.Scheme == "" {
@@ -210,6 +215,30 @@ func (t *templateClient) getGitHubFileContent(rURL *url.URL) ([]byte, error) {
210215
return content, nil
211216
}
212217

218+
func (t *templateClient) getRawURLFileContent(rURL string) ([]byte, error) {
219+
request, err := http.NewRequestWithContext(ctx, http.MethodGet, rURL, http.NoBody)
220+
if err != nil {
221+
return nil, err
222+
}
223+
224+
response, err := t.httpClient.Do(request)
225+
if err != nil {
226+
return nil, err
227+
}
228+
defer response.Body.Close()
229+
230+
if response.StatusCode != http.StatusOK {
231+
return nil, errors.Errorf("failed to get file, got %d", response.StatusCode)
232+
}
233+
234+
content, err := io.ReadAll(response.Body)
235+
if err != nil {
236+
return nil, err
237+
}
238+
239+
return content, nil
240+
}
241+
213242
func getGitHubClient(configVariablesClient config.VariablesClient) (*github.Client, error) {
214243
var authenticatingHTTPClient *http.Client
215244
if token, err := configVariablesClient.Get(config.GitHubTokenVariable); err == nil {

cmd/clusterctl/client/cluster/template_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/base64"
2121
"fmt"
2222
"net/http"
23+
"net/http/httptest"
2324
"net/url"
2425
"os"
2526
"path/filepath"
@@ -225,6 +226,49 @@ func Test_templateClient_getGitHubFileContent(t *testing.T) {
225226
}
226227
}
227228

229+
func Test_templateClient_getRawUrlFileContent(t *testing.T) {
230+
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
231+
fmt.Fprint(w, template)
232+
}))
233+
234+
defer fakeServer.Close()
235+
236+
type args struct {
237+
rURL string
238+
}
239+
tests := []struct {
240+
name string
241+
args args
242+
want []byte
243+
wantErr bool
244+
}{
245+
{
246+
name: "Return custom template",
247+
args: args{
248+
rURL: fakeServer.URL,
249+
},
250+
want: []byte(template),
251+
wantErr: false,
252+
},
253+
}
254+
for _, tt := range tests {
255+
t.Run(tt.name, func(t *testing.T) {
256+
g := NewWithT(t)
257+
258+
c := newTemplateClient(TemplateClientInput{})
259+
got, err := c.getRawURLFileContent(tt.args.rURL)
260+
if tt.wantErr {
261+
g.Expect(err).To(HaveOccurred())
262+
return
263+
}
264+
265+
g.Expect(err).NotTo(HaveOccurred())
266+
267+
g.Expect(got).To(Equal(tt.want))
268+
})
269+
}
270+
}
271+
228272
func Test_templateClient_getLocalFileContent(t *testing.T) {
229273
g := NewWithT(t)
230274

0 commit comments

Comments
 (0)