helmserver.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. )
  24. type HelmServer struct {
  25. ChartDir string
  26. ChartRevision string
  27. config *Config
  28. srv *http.Server
  29. serveDir string
  30. }
  31. func (s *HelmServer) Setup(config *Config) error {
  32. s.config = config
  33. var err error
  34. s.serveDir, err = os.MkdirTemp("", "e2e-helm")
  35. if err != nil {
  36. return err
  37. }
  38. // nolint:gosec
  39. cmd := exec.Command("helm", "package", s.ChartDir, "--version", s.ChartRevision)
  40. cmd.Dir = s.serveDir
  41. out, err := cmd.CombinedOutput()
  42. if err != nil {
  43. return fmt.Errorf("unable to package helm chart: %w %s", err, string(out))
  44. }
  45. cmd = exec.Command("helm", "repo", "index", ".")
  46. cmd.Dir = s.serveDir
  47. out, err = cmd.CombinedOutput()
  48. if err != nil {
  49. return fmt.Errorf("unable to create helm index: %w %s", err, string(out))
  50. }
  51. _, err = s.config.KubeClientSet.CoreV1().Services("default").Create(GinkgoT().Context(), &v1.Service{
  52. ObjectMeta: metav1.ObjectMeta{
  53. Name: "e2e-helmserver",
  54. },
  55. Spec: v1.ServiceSpec{
  56. Selector: map[string]string{
  57. // set via e2e/run.sh
  58. "app": "eso-e2e",
  59. },
  60. Ports: []v1.ServicePort{
  61. {
  62. Name: "http",
  63. Port: 80,
  64. TargetPort: intstr.FromInt(3000),
  65. },
  66. }},
  67. }, metav1.CreateOptions{})
  68. return err
  69. }
  70. func (s *HelmServer) Install() error {
  71. fs := http.FileServer(http.Dir(s.serveDir))
  72. http.Handle("/", fs)
  73. s.srv = &http.Server{Addr: ":3000"}
  74. go func() {
  75. _ = s.srv.ListenAndServe()
  76. }()
  77. return nil
  78. }
  79. func (s *HelmServer) Logs() error {
  80. return nil
  81. }
  82. func (s *HelmServer) Uninstall() error {
  83. err := s.config.KubeClientSet.CoreV1().Services("default").Delete(GinkgoT().Context(), "e2e-helmserver", metav1.DeleteOptions{})
  84. if err != nil {
  85. return err
  86. }
  87. return s.srv.Close()
  88. }