|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package utils |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os/exec" |
| 22 | + "strings" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + // use LTS version of cert-manager |
| 27 | + certManagerVersion = "v1.12.13" |
| 28 | + certManagerURLTmpl = "https://github.com/jetstack/cert-manager/releases/download/%s/cert-manager.yaml" |
| 29 | +) |
| 30 | + |
| 31 | +// UninstallCertManager uninstalls the cert manager |
| 32 | +func UninstallCertManager() { |
| 33 | + url := fmt.Sprintf(certManagerURLTmpl, certManagerVersion) |
| 34 | + cmd := exec.Command("kubectl", "delete", "-f", url) |
| 35 | + if _, err := Run(cmd); err != nil { |
| 36 | + warnError(err) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// InstallCertManager installs the cert manager bundle. |
| 41 | +func InstallCertManager() error { |
| 42 | + // remove any existing cert-manager leases |
| 43 | + // NOTE: this is required to avoid issues where cert-manager is reinstalled quickly due to rerunning tests |
| 44 | + cmd := exec.Command("kubectl", "delete", |
| 45 | + "leases", |
| 46 | + "--ignore-not-found", |
| 47 | + "--namespace", "kube-system", |
| 48 | + "cert-manager-controller", |
| 49 | + "cert-manager-cainjector-leader-election", |
| 50 | + ) |
| 51 | + _, err := Run(cmd) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + // install cert-manager |
| 57 | + url := fmt.Sprintf(certManagerURLTmpl, certManagerVersion) |
| 58 | + cmd = exec.Command("kubectl", "apply", "-f", url) |
| 59 | + _, err = Run(cmd) |
| 60 | + return err |
| 61 | +} |
| 62 | + |
| 63 | +// WaitCertManagerRunning waits for cert manager to be running, and returns an error if not. |
| 64 | +func WaitCertManagerRunning() error { |
| 65 | + |
| 66 | + // Wait for the cert-manager Deployments to be Available |
| 67 | + cmd := exec.Command("kubectl", "wait", |
| 68 | + "deployment.apps", |
| 69 | + "--for", "condition=Available", |
| 70 | + "--selector", "app.kubernetes.io/instance=cert-manager", |
| 71 | + "--all-namespaces", |
| 72 | + "--timeout", "5m", |
| 73 | + ) |
| 74 | + _, err := Run(cmd) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + // Wait for the cert-manager Endpoints to be ready |
| 80 | + // NOTE: the webhooks will not function correctly until this is ready |
| 81 | + cmd = exec.Command("kubectl", "wait", |
| 82 | + "endpoints", |
| 83 | + "--for", "jsonpath=subsets[0].addresses[0].targetRef.kind=Pod", |
| 84 | + "--selector", "app.kubernetes.io/instance=cert-manager", |
| 85 | + "--all-namespaces", |
| 86 | + "--timeout", "2m", |
| 87 | + ) |
| 88 | + _, err = Run(cmd) |
| 89 | + return err |
| 90 | +} |
| 91 | + |
| 92 | +// IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed |
| 93 | +// by verifying the existence of key CRDs related to Cert Manager. |
| 94 | +func IsCertManagerCRDsInstalled() bool { |
| 95 | + // List of common Cert Manager CRDs |
| 96 | + certManagerCRDs := []string{ |
| 97 | + "certificates.cert-manager.io", |
| 98 | + "issuers.cert-manager.io", |
| 99 | + "clusterissuers.cert-manager.io", |
| 100 | + "certificaterequests.cert-manager.io", |
| 101 | + "orders.acme.cert-manager.io", |
| 102 | + "challenges.acme.cert-manager.io", |
| 103 | + } |
| 104 | + |
| 105 | + // Execute the kubectl command to get all CRDs |
| 106 | + cmd := exec.Command("kubectl", "get", "crds", "-o", "name") |
| 107 | + output, err := Run(cmd) |
| 108 | + if err != nil { |
| 109 | + return false |
| 110 | + } |
| 111 | + |
| 112 | + // Check if any of the Cert Manager CRDs are present |
| 113 | + crdList := GetNonEmptyLines(output) |
| 114 | + for _, crd := range certManagerCRDs { |
| 115 | + for _, line := range crdList { |
| 116 | + if strings.Contains(line, crd) { |
| 117 | + return true |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return false |
| 123 | +} |
0 commit comments