eso_flux_helm.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright 2020 The cert-manager 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. 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 addon
  14. import (
  15. "bytes"
  16. "context"
  17. "crypto/tls"
  18. "net/http"
  19. "time"
  20. fluxhelm "github.com/fluxcd/helm-controller/api/v2beta1"
  21. "github.com/fluxcd/pkg/apis/meta"
  22. fluxsrc "github.com/fluxcd/source-controller/api/v1beta2"
  23. "github.com/onsi/ginkgo/v2"
  24. v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "k8s.io/apimachinery/pkg/types"
  27. "k8s.io/apimachinery/pkg/util/wait"
  28. )
  29. const fluxNamespace = "flux-system"
  30. // HelmChart installs the specified Chart into the cluster.
  31. type FluxHelmRelease struct {
  32. Name string
  33. Namespace string
  34. TargetNamespace string
  35. HelmChart string
  36. HelmRepo string
  37. HelmRevision string
  38. HelmValues string
  39. config *Config
  40. }
  41. // Setup stores the config in an internal field
  42. // to get access to the k8s api in orderto fetch logs.
  43. func (c *FluxHelmRelease) Setup(cfg *Config) error {
  44. c.config = cfg
  45. return nil
  46. }
  47. // Install adds the chart repo and installs the helm chart.
  48. func (c *FluxHelmRelease) Install() error {
  49. app := &fluxsrc.HelmRepository{
  50. ObjectMeta: metav1.ObjectMeta{
  51. Name: c.Name,
  52. Namespace: fluxNamespace,
  53. },
  54. Spec: fluxsrc.HelmRepositorySpec{
  55. URL: c.HelmRepo,
  56. },
  57. }
  58. err := c.config.CRClient.Create(context.Background(), app)
  59. if err != nil {
  60. return err
  61. }
  62. hr := &fluxhelm.HelmRelease{
  63. ObjectMeta: metav1.ObjectMeta{
  64. Name: c.Name,
  65. Namespace: c.Namespace,
  66. },
  67. Spec: fluxhelm.HelmReleaseSpec{
  68. ReleaseName: c.Name,
  69. TargetNamespace: c.TargetNamespace,
  70. Values: &v1.JSON{
  71. Raw: []byte(c.HelmValues),
  72. },
  73. Install: &fluxhelm.Install{
  74. CreateNamespace: true,
  75. Remediation: &fluxhelm.InstallRemediation{
  76. Retries: -1,
  77. },
  78. },
  79. Chart: fluxhelm.HelmChartTemplate{
  80. Spec: fluxhelm.HelmChartTemplateSpec{
  81. Version: c.HelmRevision,
  82. Chart: c.HelmChart,
  83. SourceRef: fluxhelm.CrossNamespaceObjectReference{
  84. Kind: "HelmRepository",
  85. Name: c.Name,
  86. Namespace: fluxNamespace,
  87. },
  88. },
  89. },
  90. },
  91. }
  92. err = c.config.CRClient.Create(context.Background(), hr)
  93. if err != nil {
  94. return err
  95. }
  96. // wait for app to become ready
  97. err = wait.PollUntilContextTimeout(context.Background(), time.Second*5, time.Minute*3, true, func(ctx context.Context) (bool, error) {
  98. var hr fluxhelm.HelmRelease
  99. err := c.config.CRClient.Get(context.Background(), types.NamespacedName{
  100. Name: c.Name,
  101. Namespace: c.Namespace,
  102. }, &hr)
  103. if err != nil {
  104. return false, nil
  105. }
  106. for _, cond := range hr.GetConditions() {
  107. ginkgo.GinkgoWriter.Printf("check condition: %s=%s: %s\n", cond.Type, cond.Status, cond.Message)
  108. if cond.Type == meta.ReadyCondition && cond.Status == metav1.ConditionTrue {
  109. return true, nil
  110. }
  111. }
  112. return false, nil
  113. })
  114. if err != nil {
  115. return err
  116. }
  117. // we have to wait for the webhook to become ready
  118. tr := &http.Transport{
  119. // nolint:gosec
  120. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  121. }
  122. client := &http.Client{Transport: tr}
  123. return wait.PollUntilContextTimeout(context.Background(), time.Second, time.Minute*5, true, func(ctx context.Context) (bool, error) {
  124. const payload = `{"apiVersion": "admission.k8s.io/v1","kind": "AdmissionReview","request": {"uid": "test","kind": {"group": "external-secrets.io","version": "v1","kind": "ExternalSecret"}, "resource": "external-secrets.io/v1.externalsecrets","dryRun": true, "operation": "CREATE", "userInfo":{"username":"test","uid":"test","groups":[],"extra":{}}}}`
  125. res, err := client.Post("https://external-secrets-webhook.external-secrets.svc.cluster.local/validate-external-secrets-io-v1-externalsecret", "application/json", bytes.NewBufferString(payload))
  126. if err != nil {
  127. return false, nil
  128. }
  129. defer func() {
  130. _ = res.Body.Close()
  131. }()
  132. ginkgo.GinkgoWriter.Printf("webhook res: %d", res.StatusCode)
  133. return res.StatusCode == http.StatusOK, nil
  134. })
  135. }
  136. // Uninstall removes the chart aswell as the repo.
  137. func (c *FluxHelmRelease) Uninstall() error {
  138. err := uninstallCRDs(c.config)
  139. if err != nil {
  140. return err
  141. }
  142. err = c.config.CRClient.Delete(context.Background(), &fluxhelm.HelmRelease{
  143. ObjectMeta: metav1.ObjectMeta{
  144. Name: c.Name,
  145. Namespace: c.Namespace,
  146. },
  147. })
  148. if err != nil {
  149. return err
  150. }
  151. return c.config.CRClient.Delete(context.Background(), &fluxsrc.HelmRepository{
  152. ObjectMeta: metav1.ObjectMeta{
  153. Name: c.Name,
  154. Namespace: fluxNamespace,
  155. },
  156. })
  157. }
  158. func (c *FluxHelmRelease) Logs() error {
  159. return nil
  160. }