eso_argocd_application.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. argoapp "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
  21. "github.com/onsi/ginkgo/v2"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/apimachinery/pkg/util/wait"
  25. )
  26. // HelmChart installs the specified Chart into the cluster.
  27. type ArgoCDApplication struct {
  28. Name string
  29. Namespace string
  30. DestinationNamespace string
  31. HelmChart string
  32. HelmRepo string
  33. HelmRevision string
  34. HelmValues string
  35. config *Config
  36. }
  37. // Setup stores the config in an internal field
  38. // to get access to the k8s api in orderto fetch logs.
  39. func (c *ArgoCDApplication) Setup(cfg *Config) error {
  40. c.config = cfg
  41. return nil
  42. }
  43. // Install adds the chart repo and installs the helm chart.
  44. func (c *ArgoCDApplication) Install() error {
  45. app := &argoapp.Application{
  46. ObjectMeta: metav1.ObjectMeta{
  47. Name: c.Name,
  48. Namespace: c.Namespace,
  49. Annotations: map[string]string{
  50. "argocd.argoproj.io/refresh": "hard",
  51. },
  52. },
  53. Spec: argoapp.ApplicationSpec{
  54. Project: "default",
  55. SyncPolicy: &argoapp.SyncPolicy{
  56. Automated: &argoapp.SyncPolicyAutomated{
  57. Prune: true,
  58. SelfHeal: true,
  59. },
  60. SyncOptions: argoapp.SyncOptions{
  61. "CreateNamespace=true",
  62. },
  63. },
  64. Source: argoapp.ApplicationSource{
  65. Chart: c.HelmChart,
  66. RepoURL: c.HelmRepo,
  67. TargetRevision: c.HelmRevision,
  68. Helm: &argoapp.ApplicationSourceHelm{
  69. ReleaseName: c.Name,
  70. Values: c.HelmValues,
  71. },
  72. },
  73. Destination: argoapp.ApplicationDestination{
  74. Server: "https://kubernetes.default.svc",
  75. Namespace: c.DestinationNamespace,
  76. },
  77. },
  78. }
  79. err := c.config.CRClient.Create(context.Background(), app)
  80. if err != nil {
  81. return err
  82. }
  83. // wait for app to become ready
  84. err = wait.PollImmediate(time.Second*5, time.Minute*10, func() (bool, error) {
  85. var app argoapp.Application
  86. err := c.config.CRClient.Get(context.Background(), types.NamespacedName{
  87. Name: c.Name,
  88. Namespace: c.Namespace,
  89. }, &app)
  90. if err != nil {
  91. return false, nil
  92. }
  93. return app.Status.Sync.Status == argoapp.SyncStatusCodeSynced, nil
  94. })
  95. if err != nil {
  96. return err
  97. }
  98. // we have to wait for the webhook to become ready
  99. tr := &http.Transport{
  100. // nolint:gosec
  101. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  102. }
  103. client := &http.Client{Transport: tr}
  104. return wait.PollImmediate(time.Second, time.Minute*5, func() (bool, error) {
  105. const payload = `{"apiVersion": "apiextensions.k8s.io/v1","kind": "ConversionReview","request": {}}`
  106. res, err := client.Post("https://external-secrets-webhook.external-secrets.svc.cluster.local/convert", "application/json", bytes.NewBufferString(payload))
  107. if err != nil {
  108. return false, nil
  109. }
  110. defer res.Body.Close()
  111. ginkgo.GinkgoWriter.Printf("conversion res: %d", res.StatusCode)
  112. return res.StatusCode == http.StatusOK, nil
  113. })
  114. }
  115. // Uninstall removes the chart aswell as the repo.
  116. func (c *ArgoCDApplication) Uninstall() error {
  117. err := c.config.CRClient.Delete(context.Background(), &argoapp.Application{
  118. ObjectMeta: metav1.ObjectMeta{
  119. Name: c.Name,
  120. Namespace: c.Namespace,
  121. },
  122. })
  123. if err != nil {
  124. return err
  125. }
  126. err = uninstallCRDs(c.config)
  127. if err != nil {
  128. return err
  129. }
  130. return nil
  131. }
  132. func (c *ArgoCDApplication) Logs() error {
  133. return nil
  134. }