eso_flux_helm.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "context"
  16. "time"
  17. fluxhelm "github.com/fluxcd/helm-controller/api/v2"
  18. "github.com/fluxcd/pkg/apis/meta"
  19. fluxsrc "github.com/fluxcd/source-controller/api/v1"
  20. v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  21. apierrors "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/apimachinery/pkg/util/wait"
  25. . "github.com/onsi/ginkgo/v2"
  26. . "github.com/onsi/gomega"
  27. )
  28. const fluxNamespace = "flux-system"
  29. // HelmChart installs the specified Chart into the cluster.
  30. type FluxHelmRelease struct {
  31. Name string
  32. Namespace string
  33. TargetNamespace string
  34. HelmChart string
  35. HelmRepo string
  36. HelmRevision string
  37. HelmValues string
  38. config *Config
  39. }
  40. // Setup stores the config in an internal field
  41. // to get access to the k8s api in orderto fetch logs.
  42. func (c *FluxHelmRelease) Setup(cfg *Config) error {
  43. c.config = cfg
  44. return nil
  45. }
  46. // Install adds the chart repo and installs the helm chart.
  47. func (c *FluxHelmRelease) Install() error {
  48. app := &fluxsrc.HelmRepository{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Name: c.Name,
  51. Namespace: fluxNamespace,
  52. },
  53. Spec: fluxsrc.HelmRepositorySpec{
  54. URL: c.HelmRepo,
  55. },
  56. }
  57. err := c.config.CRClient.Create(GinkgoT().Context(), app)
  58. if err != nil {
  59. return err
  60. }
  61. hr := &fluxhelm.HelmRelease{
  62. ObjectMeta: metav1.ObjectMeta{
  63. Name: c.Name,
  64. Namespace: c.Namespace,
  65. },
  66. Spec: fluxhelm.HelmReleaseSpec{
  67. ReleaseName: c.Name,
  68. TargetNamespace: c.TargetNamespace,
  69. Values: &v1.JSON{
  70. Raw: []byte(c.HelmValues),
  71. },
  72. Install: &fluxhelm.Install{
  73. CreateNamespace: true,
  74. Remediation: &fluxhelm.InstallRemediation{
  75. Retries: -1,
  76. },
  77. },
  78. Chart: &fluxhelm.HelmChartTemplate{
  79. Spec: fluxhelm.HelmChartTemplateSpec{
  80. Version: c.HelmRevision,
  81. Chart: c.HelmChart,
  82. SourceRef: fluxhelm.CrossNamespaceObjectReference{
  83. Kind: "HelmRepository",
  84. Name: c.Name,
  85. Namespace: fluxNamespace,
  86. },
  87. },
  88. },
  89. },
  90. }
  91. err = c.config.CRClient.Create(GinkgoT().Context(), hr)
  92. if err != nil {
  93. return err
  94. }
  95. // wait for app to become ready
  96. err = wait.PollUntilContextTimeout(GinkgoT().Context(), time.Second*5, time.Minute*3, true, func(ctx context.Context) (bool, error) {
  97. var hr fluxhelm.HelmRelease
  98. err := c.config.CRClient.Get(GinkgoT().Context(), types.NamespacedName{
  99. Name: c.Name,
  100. Namespace: c.Namespace,
  101. }, &hr)
  102. if err != nil {
  103. return false, nil
  104. }
  105. for _, cond := range hr.GetConditions() {
  106. GinkgoWriter.Printf("check condition: %s=%s: %s\n", cond.Type, cond.Status, cond.Message)
  107. if cond.Type == meta.ReadyCondition && cond.Status == metav1.ConditionTrue {
  108. return true, nil
  109. }
  110. }
  111. return false, nil
  112. })
  113. if err != nil {
  114. return err
  115. }
  116. return waitForExternalSecretWebhookReady(webhookServiceName(c.Name), c.TargetNamespace)
  117. }
  118. // Uninstall removes the chart aswell as the repo.
  119. func (c *FluxHelmRelease) Uninstall() error {
  120. err := uninstallCRDs(c.config)
  121. if err != nil {
  122. return err
  123. }
  124. err = c.config.CRClient.Delete(GinkgoT().Context(), &fluxhelm.HelmRelease{
  125. ObjectMeta: metav1.ObjectMeta{
  126. Name: c.Name,
  127. Namespace: c.Namespace,
  128. },
  129. })
  130. if err != nil && !apierrors.IsNotFound(err) {
  131. return err
  132. }
  133. Eventually(func() bool {
  134. var hr fluxhelm.HelmRelease
  135. err = c.config.CRClient.Get(GinkgoT().Context(), types.NamespacedName{
  136. Name: c.Name,
  137. Namespace: c.Namespace,
  138. }, &hr)
  139. if apierrors.IsNotFound(err) {
  140. return true
  141. }
  142. return false
  143. }).WithPolling(time.Second).WithTimeout(time.Second * 30).Should(BeTrue())
  144. if err := c.config.CRClient.Delete(GinkgoT().Context(), &fluxsrc.HelmRepository{
  145. ObjectMeta: metav1.ObjectMeta{
  146. Name: c.Name,
  147. Namespace: fluxNamespace,
  148. },
  149. }); err != nil && !apierrors.IsNotFound(err) {
  150. return err
  151. }
  152. return nil
  153. }
  154. func (c *FluxHelmRelease) Logs() error {
  155. return nil
  156. }