diff --git a/pkg/config/config.go b/pkg/config/config.go index c8a75e47..aec9b944 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -19,6 +19,8 @@ import ( "fmt" "io" "io/ioutil" + "net/http" + "strings" packr "github.com/gobuffalo/packr/v2" corev1 "k8s.io/api/core/v1" @@ -112,12 +114,20 @@ type SecurityCapabilityLists struct { // ParseFile parses config from a file. func ParseFile(path string) (Configuration, error) { - configBox := packr.New("Config", "../../examples") var rawBytes []byte var err error + configBox := packr.New("Config", "../../examples") if path == "" { rawBytes, err = configBox.Find("config.yaml") + } else if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") { + //path is a url + response, err2 := http.Get(path) + if err2 != nil { + return Configuration{}, err2 + } + rawBytes, err = ioutil.ReadAll(response.Body) } else { + //path is local rawBytes, err = ioutil.ReadFile(path) } if err != nil { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 8a6f2c37..c2ba75b4 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -15,6 +15,11 @@ package config import ( + "context" + "io" + "log" + "net/http" + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -120,6 +125,36 @@ func TestParseJson(t *testing.T) { testParsedConfig(t, &parsedConf) } +func TestConfigFromURL(t *testing.T) { + var err error + var parsedConf Configuration + srv := &http.Server{Addr: ":8081"} + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, resourceConfYAML1) + }) + + go func() { + if err := srv.ListenAndServe(); err != http.ErrServerClosed { + log.Fatalf("ListenAndServe(): %s", err) + } + }() + + parsedConf, err = ParseFile("http://localhost:8081/exampleURL") + assert.NoError(t, err, "Expected no error when parsing YAML from URL") + if err := srv.Shutdown(context.TODO()); err != nil { + panic(err) + } + testParsedConfig(t, &parsedConf) + +} + +func TestConfigNoServerError(t *testing.T) { + var err error + _, err = ParseFile("http://localhost:8081/exampleURL") + assert.Error(t, err) + assert.Regexp(t, regexp.MustCompile("connection refused"), err.Error()) +} + func testParsedConfig(t *testing.T, config *Configuration) { cpuRequests := config.Resources.CPURequestRanges assert.Equal(t, int64(100), cpuRequests.Error.Below.ScaledValue(resource.Milli))