Skip to content

Commit 13724f4

Browse files
authored
Merge pull request #312 from stuggi/certmanager_module
[tlse] Add certmanager module
2 parents 8305912 + deb57ed commit 13724f4

File tree

20 files changed

+3146
-38
lines changed

20 files changed

+3146
-38
lines changed

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ $(LOCALBIN):
1515
## Tool Binaries
1616
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
1717
ENVTEST ?= $(LOCALBIN)/setup-envtest
18+
GINKGO ?= $(LOCALBIN)/ginkgo
1819

1920
## Tool Versions
2021
CONTROLLER_TOOLS_VERSION ?= v0.10.0
@@ -43,10 +44,14 @@ vet: gowork ## Run go vet against code.
4344
done
4445

4546
.PHONY: test
46-
test: gowork generate fmt vet envtest ## Run tests.
47+
test: gowork generate fmt vet envtest ginkgo ## Run tests.
4748
for mod in $(shell find modules/ -maxdepth 1 -mindepth 1 -type d); do \
4849
pushd ./$$mod ; \
49-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out || exit 1; \
50+
if [ -f test/functional/suite_test.go ]; then \
51+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) -v debug --bin-dir $(LOCALBIN) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) --trace --cover --coverprofile cover.out --covermode=atomic ${PROC_CMD} $(GINKGO_ARGS) ./test/... || exit 1; \
52+
else \
53+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out || exit 1; \
54+
fi; \
5055
popd ; \
5156
done
5257
##@ Build
@@ -79,6 +84,11 @@ envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
7984
$(ENVTEST): $(LOCALBIN)
8085
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
8186

87+
.PHONY: ginkgo
88+
ginkgo: $(GINKGO) ## Download ginkgo locally if necessary.
89+
$(GINKGO): $(LOCALBIN)
90+
test -s $(LOCALBIN)/ginkgo || GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo
91+
8292
.PHONY: generate
8393
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
8494
for mod in $(shell find modules/ -maxdepth 1 -mindepth 1 -type d); do \

modules/certmanager/certificate.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

modules/certmanager/go.mod

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module github.com/openstack-k8s-operators/lib-common/modules/certmanager
2+
3+
go 1.19
4+
5+
require (
6+
github.com/cert-manager/cert-manager v1.11.4
7+
github.com/go-logr/logr v1.2.4
8+
github.com/google/uuid v1.3.0
9+
github.com/onsi/ginkgo/v2 v2.11.0
10+
github.com/onsi/gomega v1.27.8
11+
github.com/openstack-k8s-operators/lib-common/modules/common v0.1.0
12+
github.com/openstack-k8s-operators/lib-common/modules/test v0.0.0-20230612101529-af40f24b2b62
13+
go.uber.org/zap v1.24.0
14+
k8s.io/api v0.26.6
15+
k8s.io/apimachinery v0.26.6
16+
k8s.io/client-go v0.26.6
17+
sigs.k8s.io/controller-runtime v0.14.6
18+
)
19+
20+
require (
21+
github.com/beorn7/perks v1.0.1 // indirect
22+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
23+
github.com/davecgh/go-spew v1.1.1 // indirect
24+
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
25+
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
26+
github.com/fsnotify/fsnotify v1.6.0 // indirect
27+
github.com/go-logr/zapr v1.2.3 // indirect
28+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
29+
github.com/go-openapi/jsonreference v0.20.1 // indirect
30+
github.com/go-openapi/swag v0.22.3 // indirect
31+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
32+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
33+
github.com/golang/protobuf v1.5.3 // indirect
34+
github.com/google/gnostic v0.6.9 // indirect
35+
github.com/google/go-cmp v0.5.9 // indirect
36+
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
37+
github.com/gophercloud/gophercloud v1.5.0 // indirect
38+
github.com/imdario/mergo v0.3.16 // indirect
39+
github.com/josharian/intern v1.0.0 // indirect
40+
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 // indirect
41+
github.com/mailru/easyjson v0.7.7 // indirect
42+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
43+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
44+
github.com/openshift/api v3.9.0+incompatible // indirect
45+
github.com/openstack-k8s-operators/infra-operator/apis v0.0.0-20230720153501-076b82bb4427 // indirect
46+
github.com/openstack-k8s-operators/keystone-operator/api v0.0.0-20230612072624-8ebcfc19377a // indirect
47+
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.0.0-20230606033311-3b01713e4d45 // indirect
48+
github.com/openstack-k8s-operators/mariadb-operator/api v0.0.0-20230717141726-1bd909777952 // indirect
49+
github.com/pkg/errors v0.9.1 // indirect
50+
github.com/prometheus/client_golang v1.14.0 // indirect
51+
github.com/prometheus/client_model v0.3.0 // indirect
52+
github.com/prometheus/common v0.37.0 // indirect
53+
github.com/prometheus/procfs v0.8.0 // indirect
54+
github.com/spf13/pflag v1.0.5 // indirect
55+
go.uber.org/atomic v1.9.0 // indirect
56+
go.uber.org/multierr v1.8.0 // indirect
57+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
58+
golang.org/x/oauth2 v0.4.0 // indirect
59+
golang.org/x/sys v0.9.0 // indirect
60+
golang.org/x/term v0.8.0 // indirect
61+
golang.org/x/time v0.3.0 // indirect
62+
golang.org/x/tools v0.9.3 // indirect
63+
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
64+
google.golang.org/appengine v1.6.7 // indirect
65+
google.golang.org/protobuf v1.28.1 // indirect
66+
gopkg.in/yaml.v3 v3.0.1 // indirect
67+
k8s.io/apiextensions-apiserver v0.26.6 // indirect
68+
k8s.io/component-base v0.26.6 // indirect
69+
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
70+
sigs.k8s.io/gateway-api v0.6.0 // indirect
71+
sigs.k8s.io/yaml v1.3.0 // indirect
72+
)
73+
74+
require (
75+
github.com/gogo/protobuf v1.3.2 // indirect
76+
github.com/google/gofuzz v1.2.0 // indirect
77+
github.com/json-iterator/go v1.1.12 // indirect
78+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
79+
github.com/modern-go/reflect2 v1.0.2 // indirect
80+
golang.org/x/net v0.10.0 // indirect
81+
golang.org/x/text v0.9.0 // indirect
82+
gopkg.in/inf.v0 v0.9.1 // indirect
83+
gopkg.in/yaml.v2 v2.4.0 // indirect
84+
k8s.io/klog/v2 v2.100.1 // indirect
85+
k8s.io/utils v0.0.0-20230711102312-30195339c3c7 // indirect; indirect // indirect
86+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect; indirect // indirect
87+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
88+
)
89+
90+
replace github.com/openstack-k8s-operators/lib-common/modules/common => ../common
91+
92+
replace github.com/openstack-k8s-operators/lib-common/modules/test => ../test
93+
94+
// mschuppert: map to latest commit from release-4.13 tag
95+
// must consistent within modules and service operators
96+
replace github.com/openshift/api => github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7

0 commit comments

Comments
 (0)