|
| 1 | +/* |
| 2 | +Copyright 2023 Red Hat |
| 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 certmanager |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" |
| 25 | + "github.com/openstack-k8s-operators/lib-common/modules/common/helper" |
| 26 | + "github.com/openstack-k8s-operators/lib-common/modules/common/util" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 29 | + |
| 30 | + k8s_errors "k8s.io/apimachinery/pkg/api/errors" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | +) |
| 33 | + |
| 34 | +// Certificate - |
| 35 | +type Certificate struct { |
| 36 | + certificate *certmgrv1.Certificate |
| 37 | + timeout time.Duration |
| 38 | +} |
| 39 | + |
| 40 | +// NewCertificate returns an initialized Certificate. |
| 41 | +func NewCertificate( |
| 42 | + certificate *certmgrv1.Certificate, |
| 43 | + timeout time.Duration, |
| 44 | +) *Certificate { |
| 45 | + return &Certificate{ |
| 46 | + certificate: certificate, |
| 47 | + timeout: timeout, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Cert returns an initialized certificate request obj. |
| 52 | +// minimal spec should be: |
| 53 | +// Spec: |
| 54 | +// |
| 55 | +// commonName: keystone-public-openstack.apps-crc.testing |
| 56 | +// dnsNames: |
| 57 | +// - keystone-public-openstack |
| 58 | +// - keystone-public-openstack.apps-crc.testing |
| 59 | +// issuerRef: |
| 60 | +// kind: Issuer |
| 61 | +// name: osp-rootca-issuer |
| 62 | +// secretName: keystone-public-cert |
| 63 | +func Cert( |
| 64 | + name string, |
| 65 | + namespace string, |
| 66 | + labels map[string]string, |
| 67 | + spec certmgrv1.CertificateSpec, |
| 68 | + |
| 69 | +) *certmgrv1.Certificate { |
| 70 | + return &certmgrv1.Certificate{ |
| 71 | + ObjectMeta: metav1.ObjectMeta{ |
| 72 | + Name: name, |
| 73 | + Namespace: namespace, |
| 74 | + Labels: labels, |
| 75 | + }, |
| 76 | + Spec: spec, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// CreateOrPatch - creates or patches a certificate, reconciles after Xs if object won't exist. |
| 81 | +func (c *Certificate) CreateOrPatch( |
| 82 | + ctx context.Context, |
| 83 | + h *helper.Helper, |
| 84 | +) (ctrl.Result, error) { |
| 85 | + cert := &certmgrv1.Certificate{ |
| 86 | + ObjectMeta: metav1.ObjectMeta{ |
| 87 | + Name: c.certificate.Name, |
| 88 | + Namespace: c.certificate.Namespace, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + op, err := controllerutil.CreateOrPatch(ctx, h.GetClient(), cert, func() error { |
| 93 | + cert.Labels = util.MergeStringMaps(cert.Labels, c.certificate.Labels) |
| 94 | + cert.Annotations = c.certificate.Annotations |
| 95 | + cert.Spec = c.certificate.Spec |
| 96 | + |
| 97 | + err := controllerutil.SetControllerReference(h.GetBeforeObject(), cert, h.GetScheme()) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + if k8s_errors.IsNotFound(err) { |
| 106 | + h.GetLogger().Info(fmt.Sprintf("Certificate %s not found, reconcile in %s", cert.Name, c.timeout)) |
| 107 | + return ctrl.Result{RequeueAfter: c.timeout}, nil |
| 108 | + } |
| 109 | + return ctrl.Result{}, err |
| 110 | + } |
| 111 | + if op != controllerutil.OperationResultNone { |
| 112 | + h.GetLogger().Info(fmt.Sprintf("Route %s - %s", cert.Name, op)) |
| 113 | + } |
| 114 | + |
| 115 | + return ctrl.Result{}, nil |
| 116 | +} |
| 117 | + |
| 118 | +// Delete - delete a certificate. |
| 119 | +func (c *Certificate) Delete( |
| 120 | + ctx context.Context, |
| 121 | + h *helper.Helper, |
| 122 | +) error { |
| 123 | + |
| 124 | + err := h.GetClient().Delete(ctx, c.certificate) |
| 125 | + if err != nil && !k8s_errors.IsNotFound(err) { |
| 126 | + return fmt.Errorf("Error deleting certificate %s: %w", c.certificate.Name, err) |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
0 commit comments