settings.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2021 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package installer
  14. import (
  15. "fmt"
  16. "io"
  17. "net/http"
  18. "os"
  19. "path"
  20. "regexp"
  21. "time"
  22. "github.com/pkg/errors"
  23. "github.com/rook/rook/tests/framework/utils"
  24. )
  25. var imageMatch = regexp.MustCompile(`image: rook\/ceph:[a-z0-9.-]+`)
  26. func readManifest(filename string) string {
  27. rootDir, err := utils.FindRookRoot()
  28. if err != nil {
  29. panic(err)
  30. }
  31. manifest := path.Join(rootDir, "deploy/examples/", filename)
  32. logger.Infof("Reading manifest: %s", manifest)
  33. contents, err := os.ReadFile(manifest)
  34. if err != nil {
  35. panic(errors.Wrapf(err, "failed to read manifest at %s", manifest))
  36. }
  37. return imageMatch.ReplaceAllString(string(contents), "image: rook/ceph:"+LocalBuildTag)
  38. }
  39. func buildURL(rookVersion, filename string) string {
  40. re := regexp.MustCompile(`(?m)^v1.[6-7].[0-9]{1,2}$`)
  41. for range re.FindAllString(rookVersion, -1) {
  42. return fmt.Sprintf("%s/cluster/examples/kubernetes/ceph/%s", rookVersion, filename)
  43. }
  44. return fmt.Sprintf("%s/deploy/examples/%s", rookVersion, filename)
  45. }
  46. func readManifestFromGitHub(rookVersion, filename string) string {
  47. url := fmt.Sprintf("https://raw.githubusercontent.com/rook/rook/%s", buildURL(rookVersion, filename))
  48. return readManifestFromURL(url)
  49. }
  50. func readManifestFromURL(url string) string {
  51. logger.Infof("Retrieving manifest: %s", url)
  52. var response *http.Response
  53. var err error
  54. for i := 1; i <= 3; i++ {
  55. //nolint:gosec // // This is only test code and is expected to read from a url
  56. response, err = http.Get(url)
  57. if err != nil {
  58. if i == 3 {
  59. panic(errors.Wrapf(err, "failed to read manifest from %s", url))
  60. }
  61. logger.Warningf("failed to read manifest from %s. retrying in 1sec. %v", url, err)
  62. time.Sleep(time.Second)
  63. continue
  64. }
  65. break
  66. }
  67. defer response.Body.Close()
  68. content, err := io.ReadAll(response.Body)
  69. if err != nil {
  70. panic(errors.Wrapf(err, "failed to read content from %s", url))
  71. }
  72. return string(content)
  73. }