From 4052552ab13b658cb8a31fb88d0703fccfb8db93 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Mon, 20 Jun 2022 17:30:53 -0400 Subject: [PATCH] Modify write-file to read multiple HEGEL_URLS Signed-off-by: Abhinav Pandey --- actions/writefile/v1/main.go | 58 +++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/actions/writefile/v1/main.go b/actions/writefile/v1/main.go index 23ca768..cfd9974 100644 --- a/actions/writefile/v1/main.go +++ b/actions/writefile/v1/main.go @@ -48,7 +48,7 @@ func main() { contents := os.Getenv("CONTENTS") bootconfig := os.Getenv("BOOTCONFIG_CONTENTS") - hegelUrl := os.Getenv("HEGEL_URL") + hegelUrls := os.Getenv("HEGEL_URLS") uid := os.Getenv("UID") gid := os.Getenv("GID") mode := os.Getenv("MODE") @@ -87,15 +87,15 @@ func main() { log.Fatalf("Could not parse mode: %v", err) } - // Only set one of contents, bootconfig or hegelUrl + // Only set one of contents, bootconfig or hegelUrls validationCount := 0 - for _, envVar := range []string{contents, bootconfig, hegelUrl} { + for _, envVar := range []string{contents, bootconfig, hegelUrls} { if envVar != "" { validationCount++ } } if validationCount != 1 { - log.Fatal("Only one environment vars of CONTENTS, BOOTCONFIG_CONTENTS, HEGEL_URL can be set") + log.Fatal("Only one environment vars of CONTENTS, BOOTCONFIG_CONTENTS, HEGEL_URLS can be set") } fileMode := os.FileMode(modePrime) @@ -138,26 +138,42 @@ func main() { log.Fatalf("Failed to ensure directory exists: %v", err) } - // If hegelUrl is set, get the user-data from hegel and set it to contents - if hegelUrl != "" { - userDataServiceUrl, err := url.Parse(hegelUrl) - if err != nil { - log.Fatalf("Error parsing hegel url: %v", err) - } - userDataServiceUrl.Path = path.Join(userDataServiceUrl.Path, hegelUserDataVersion, "user-data") - resp, err := http.Get(userDataServiceUrl.String()) - if err != nil { - log.Fatalf("Error with HTTP GET call: %v", err) - } - defer resp.Body.Close() + if hegelUrls != "" { + success := false + // go through all urls in hegelUrls, and attempt to retrieve userdata from them + // upon a successful userdata retrieval, this loop will exit and populate the userdata contents in contents + for _, hegelUrl := range strings.Split(hegelUrls, ",") { + userDataServiceUrl, err := url.ParseRequestURI(hegelUrl) + if err != nil { + log.Warnf("Error parsing hegel url: %v", err) + continue + } + userDataServiceUrl.Path = path.Join(userDataServiceUrl.Path, hegelUserDataVersion, "user-data") + client := http.Client{ + Timeout: time.Second * 10, + } + resp, err := client.Get(userDataServiceUrl.String()) + if err != nil { + log.Warnf("Error with HTTP GET call: %v", err) + continue + } + defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatalf("Error reading HTTP GET response body: %v", err) + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Warnf("Error reading HTTP GET response body: %v", err) + continue + } + + // Set contents to be the user-data + contents = string(respBody) + success = true + break } - // Set contents to be the user-data - contents = string(respBody) + if !success { + log.Fatalf("Failed to read user-data, exhausted all the urls defined in HEGEL_URLS: {%v}", hegelUrls) + } } // If bootconfig is set, contents will be empty and will serve as output initrd file provided -- 2.30.1 (Apple Git-130)