helmserver.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // nolint:gosec
  41. cmd := exec.Command("helm", "package", s.ChartDir, "--version", s.ChartRevision)
  42. cmd.Dir = s.serveDir
  43. out, err := cmd.CombinedOutput()
  44. if err != nil {
  45. return fmt.Errorf("unable to package helm chart: %w %s", err, string(out))
  46. }
  47. cmd = exec.Command("helm", "repo", "index", ".")
  48. cmd.Dir = s.serveDir
  49. out, err = cmd.CombinedOutput()
  50. if err != nil {
  51. return fmt.Errorf("unable to create helm index: %w %s", err, string(out))
  52. }
  53. svc := &v1.Service{
  54. ObjectMeta: metav1.ObjectMeta{
  55. Name: serviceName,
  56. Namespace: "default",
  57. },
  58. }
  59. _, err = controllerutil.CreateOrUpdate(GinkgoT().Context(), s.config.CRClient, svc, func() error {
  60. svc.Spec = v1.ServiceSpec{
  61. Selector: map[string]string{
  62. // set via e2e/run.sh
  63. "app": "eso-e2e",
  64. },
  65. Ports: []v1.ServicePort{
  66. {
  67. Name: "http",
  68. Port: 80,
  69. TargetPort: intstr.FromInt(3000),
  70. },
  71. }}
  72. return nil
  73. })
  74. return err
  75. }
  76. func (s *HelmServer) Install() error {
  77. fs := http.FileServer(http.Dir(s.serveDir))
  78. http.Handle("/", fs)
  79. s.srv = &http.Server{Addr: ":3000"}
  80. go func() {
  81. _ = s.srv.ListenAndServe()
  82. }()
  83. return nil
  84. }
  85. func (s *HelmServer) Logs() error {
  86. return nil
  87. }
  88. func (s *HelmServer) Uninstall() error {
  89. err := s.config.KubeClientSet.CoreV1().Services("default").Delete(GinkgoT().Context(), serviceName, metav1.DeleteOptions{})
  90. if err != nil {
  91. return err
  92. }
  93. return s.srv.Close()
  94. }