helmserver.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright © The ESO Authors
  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. https://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 addon
  14. import (
  15. "fmt"
  16. "net/http"
  17. "os"
  18. "os/exec"
  19. . "github.com/onsi/ginkgo/v2"
  20. v1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/util/intstr"
  23. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  24. )
  25. type HelmServer struct {
  26. ChartDir string
  27. ChartRevision string
  28. config *Config
  29. srv *http.Server
  30. serveDir string
  31. }
  32. const serviceName = "e2e-helmserver"
  33. func (s *HelmServer) Setup(config *Config) error {
  34. s.config = config
  35. var err error
  36. s.serveDir, err = os.MkdirTemp("", "e2e-helm")
  37. if err != nil {
  38. return err
  39. }
  40. // The chart's charts/ subchart tarballs are gitignored, so a fresh
  41. // checkout packages an empty charts/ and `helm package` fails with
  42. // "missing in charts/ directory". Build deps from the committed
  43. // Chart.lock first so this addon is self-contained: a suite that uses
  44. // only HelmServer (e.g. flux) has nothing else to populate charts/.
  45. cmd := exec.Command("helm", "dependency", "build", s.ChartDir)
  46. out, err := cmd.CombinedOutput()
  47. if err != nil {
  48. return fmt.Errorf("unable to build helm chart dependencies: %w %s", err, string(out))
  49. }
  50. // nolint:gosec
  51. cmd = exec.Command("helm", "package", s.ChartDir, "--version", s.ChartRevision)
  52. cmd.Dir = s.serveDir
  53. out, err = cmd.CombinedOutput()
  54. if err != nil {
  55. return fmt.Errorf("unable to package helm chart: %w %s", err, string(out))
  56. }
  57. cmd = exec.Command("helm", "repo", "index", ".")
  58. cmd.Dir = s.serveDir
  59. out, err = cmd.CombinedOutput()
  60. if err != nil {
  61. return fmt.Errorf("unable to create helm index: %w %s", err, string(out))
  62. }
  63. svc := &v1.Service{
  64. ObjectMeta: metav1.ObjectMeta{
  65. Name: serviceName,
  66. Namespace: "default",
  67. },
  68. }
  69. _, err = controllerutil.CreateOrUpdate(GinkgoT().Context(), s.config.CRClient, svc, func() error {
  70. svc.Spec = v1.ServiceSpec{
  71. Selector: map[string]string{
  72. // set via e2e/run.sh
  73. "app": "eso-e2e",
  74. },
  75. Ports: []v1.ServicePort{
  76. {
  77. Name: "http",
  78. Port: 80,
  79. TargetPort: intstr.FromInt(3000),
  80. },
  81. }}
  82. return nil
  83. })
  84. return err
  85. }
  86. func (s *HelmServer) Install() error {
  87. fs := http.FileServer(http.Dir(s.serveDir))
  88. http.Handle("/", fs)
  89. s.srv = &http.Server{Addr: ":3000"}
  90. go func() {
  91. _ = s.srv.ListenAndServe()
  92. }()
  93. return nil
  94. }
  95. func (s *HelmServer) Logs() error {
  96. return nil
  97. }
  98. func (s *HelmServer) Uninstall() error {
  99. err := s.config.KubeClientSet.CoreV1().Services("default").Delete(GinkgoT().Context(), serviceName, metav1.DeleteOptions{})
  100. if err != nil {
  101. return err
  102. }
  103. return s.srv.Close()
  104. }