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

new(cmd/driver): support user provided headers option for driver download #428

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion cmd/driver/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
type driverDownloadOptions struct {
InsecureDownload bool
HTTPTimeout time.Duration
HTTPHeaders string
}

type driverInstallOptions struct {
Expand Down Expand Up @@ -92,6 +93,10 @@ func NewDriverInstallCmd(ctx context.Context, opt *options.Common, driver *optio
"(e.g. '#1 SMP PREEMPT_DYNAMIC Debian 6.1.38-2 (2023-07-27)')")
cmd.Flags().BoolVar(&o.InsecureDownload, "http-insecure", false, "Whether you want to allow insecure downloads or not")
cmd.Flags().DurationVar(&o.HTTPTimeout, "http-timeout", 60*time.Second, "Timeout for each http try")
cmd.Flags().StringVar(&o.HTTPHeaders, "http-headers",
"",
"Optional comma-separated list of headers for the http GET request "+
"(e.g. --http-headers='x-emc-namespace: default,Proxy-Authenticate: Basic'). Not necessary if default repo is used")
return cmd
}

Expand Down Expand Up @@ -173,7 +178,8 @@ func (o *driverInstallOptions) RunDriverInstall(ctx context.Context) (string, er
if !o.Printer.DisableStyling {
o.Printer.Spinner, _ = o.Printer.Spinner.Start("Trying to download the driver")
}
dest, err = driverdistro.Download(ctx, d, o.Printer.WithWriter(&buf), kr, o.Driver.Name, o.Driver.Type, o.Driver.Version, o.Driver.Repos)
dest, err = driverdistro.Download(ctx, d, o.Printer.WithWriter(&buf), kr, o.Driver.Name,
o.Driver.Type, o.Driver.Version, o.Driver.Repos, o.HTTPHeaders)
if o.Printer.Spinner != nil {
_ = o.Printer.Spinner.Stop()
}
Expand Down
1 change: 1 addition & 0 deletions cmd/driver/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Flags:
--compile Whether to enable local compilation of drivers (default true)
--download Whether to enable download of prebuilt drivers (default true)
-h, --help help for install
--http-headers string Optional comma-separated list of headers for the http GET request (e.g. --http-headers='x-emc-namespace: default,Proxy-Authenticate: Basic'). Not necessary if default repo is used
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, --http-headers should be before --http-insecure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!! Thank you very much! :)

--http-insecure Whether you want to allow insecure downloads or not
--http-timeout duration Timeout for each http try (default 1m0s)
--kernelrelease string Specify the kernel release for which to download/build the driver in the same format used by 'uname -r' (e.g. '6.1.0-10-cloud-amd64')
Expand Down
12 changes: 12 additions & 0 deletions pkg/driver/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func Download(ctx context.Context,
driverName string,
driverType drivertype.DriverType,
driverVer string, repos []string,
httpHeaders string,
) (string, error) {
driverFileName := toFilename(d, &kr, driverName, driverType)
// Skip if existent
Expand All @@ -222,6 +223,17 @@ func Download(ctx context.Context,
printer.Logger.Warn("Error creating http request.", printer.Logger.Args("err", err))
continue
}
if httpHeaders != "" {
FedeDP marked this conversation as resolved.
Show resolved Hide resolved
header := http.Header{}
for _, h := range strings.Split(httpHeaders, ",") {
key, value := func() (string, string) {
x := strings.Split(h, ":")
return x[0], x[1]
}()
header.Add(key, value)
}
req.Header = header
}
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != 200 {
if err == nil {
Expand Down
Loading