Skip to content

Commit 359b345

Browse files
Merge pull request #379 from chiragkyal/trust-manager-reconcilers
CM-867: Implement trust-manager resource reconcilers
2 parents 8eb5d9e + 219524a commit 359b345

32 files changed

Lines changed: 5069 additions & 170 deletions

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,11 @@ local-run: build ## Run the operator locally against the cluster configured in ~
305305
RELATED_IMAGE_CERT_MANAGER_CONTROLLER=quay.io/jetstack/cert-manager-controller:$(CERT_MANAGER_VERSION) \
306306
RELATED_IMAGE_CERT_MANAGER_ACMESOLVER=quay.io/jetstack/cert-manager-acmesolver:$(CERT_MANAGER_VERSION) \
307307
RELATED_IMAGE_CERT_MANAGER_ISTIOCSR=quay.io/jetstack/cert-manager-istio-csr:$(ISTIO_CSR_VERSION) \
308+
RELATED_IMAGE_CERT_MANAGER_TRUST_MANAGER=quay.io/jetstack/trust-manager:$(TRUST_MANAGER_VERSION) \
308309
OPERATOR_NAME=cert-manager-operator \
309310
OPERAND_IMAGE_VERSION=$(BUNDLE_VERSION) \
311+
ISTIOCSR_OPERAND_IMAGE_VERSION=$(ISTIO_CSR_VERSION) \
312+
TRUSTMANAGER_OPERAND_IMAGE_VERSION=$(TRUST_MANAGER_VERSION) \
310313
OPERATOR_IMAGE_VERSION=$(BUNDLE_VERSION) \
311314
./cert-manager-operator start \
312315
--config=./hack/local-run-config.yaml \

bundle/manifests/cert-manager-operator.clusterserviceversion.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,26 @@ spec:
681681
- patch
682682
- update
683683
- watch
684+
- apiGroups:
685+
- trust.cert-manager.io
686+
resources:
687+
- bundles
688+
verbs:
689+
- get
690+
- list
691+
- watch
692+
- apiGroups:
693+
- trust.cert-manager.io
694+
resources:
695+
- bundles/finalizers
696+
verbs:
697+
- update
698+
- apiGroups:
699+
- trust.cert-manager.io
700+
resources:
701+
- bundles/status
702+
verbs:
703+
- patch
684704
- apiGroups:
685705
- authentication.k8s.io
686706
resources:
@@ -762,10 +782,14 @@ spec:
762782
value: quay.io/jetstack/cert-manager-acmesolver:v1.19.2
763783
- name: RELATED_IMAGE_CERT_MANAGER_ISTIOCSR
764784
value: quay.io/jetstack/cert-manager-istio-csr:v0.15.0
785+
- name: RELATED_IMAGE_CERT_MANAGER_TRUST_MANAGER
786+
value: quay.io/jetstack/trust-manager:v0.20.3
765787
- name: OPERAND_IMAGE_VERSION
766788
value: 1.19.2
767789
- name: ISTIOCSR_OPERAND_IMAGE_VERSION
768790
value: 0.15.0
791+
- name: TRUSTMANAGER_OPERAND_IMAGE_VERSION
792+
value: 0.20.3
769793
- name: OPERATOR_IMAGE_VERSION
770794
value: 1.19.0
771795
- name: OPERATOR_LOG_LEVEL
@@ -880,5 +904,7 @@ spec:
880904
name: cert-manager-acmesolver
881905
- image: quay.io/jetstack/cert-manager-istio-csr:v0.15.0
882906
name: cert-manager-istiocsr
907+
- image: quay.io/jetstack/trust-manager:v0.20.3
908+
name: cert-manager-trust-manager
883909
replaces: cert-manager-operator.v1.18.0
884910
version: 1.19.0

config/manager/manager.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ spec:
8484
value: quay.io/jetstack/cert-manager-acmesolver:v1.19.2
8585
- name: RELATED_IMAGE_CERT_MANAGER_ISTIOCSR
8686
value: quay.io/jetstack/cert-manager-istio-csr:v0.15.0
87+
- name: RELATED_IMAGE_CERT_MANAGER_TRUST_MANAGER
88+
value: quay.io/jetstack/trust-manager:v0.20.3
8789
- name: OPERAND_IMAGE_VERSION
8890
value: 1.19.2
8991
- name: ISTIOCSR_OPERAND_IMAGE_VERSION
9092
value: 0.15.0
93+
- name: TRUSTMANAGER_OPERAND_IMAGE_VERSION
94+
value: 0.20.3
9195
- name: OPERATOR_IMAGE_VERSION
9296
value: 1.19.0
9397
- name: OPERATOR_LOG_LEVEL

config/rbac/role.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,23 @@ rules:
278278
- patch
279279
- update
280280
- watch
281+
- apiGroups:
282+
- trust.cert-manager.io
283+
resources:
284+
- bundles
285+
verbs:
286+
- get
287+
- list
288+
- watch
289+
- apiGroups:
290+
- trust.cert-manager.io
291+
resources:
292+
- bundles/finalizers
293+
verbs:
294+
- update
295+
- apiGroups:
296+
- trust.cert-manager.io
297+
resources:
298+
- bundles/status
299+
verbs:
300+
- patch

pkg/controller/istiocsr/core_validation_helpers_duplication.go renamed to pkg/controller/common/core_validation_helpers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package istiocsr
1+
package common
22

33
import (
44
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -13,6 +13,7 @@ import (
1313
* Methods here are the replicates of the methods defined in k8s.io/kubernetes/pkg/apis/core/validation package, which
1414
* is done just because of the lack of better alternative to use private methods.
1515
* TODO: Remove this source file when validateAffinity method is made public.
16+
* Upstream tracker: https://github.com/kubernetes/kubernetes/issues/136422
1617
*/
1718

1819
// validateAffinity checks if given affinities are valid.

pkg/controller/common/errors.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewMultipleInstanceError(err error) *ReconcileError {
4949
}
5050
return &ReconcileError{
5151
Reason: MultipleInstanceError,
52-
Message: fmt.Sprint(err.Error()),
52+
Message: err.Error(),
5353
Err: err,
5454
}
5555
}
@@ -121,3 +121,8 @@ func IsMultipleInstanceError(err error) bool {
121121
func (e *ReconcileError) Error() string {
122122
return fmt.Sprintf("%s: %s", e.Message, e.Err)
123123
}
124+
125+
// Unwrap returns the underlying error for error chain traversal.
126+
func (e *ReconcileError) Unwrap() error {
127+
return e.Err
128+
}

pkg/controller/common/utils.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package common
22

33
import (
4+
"fmt"
45
"reflect"
56

7+
"k8s.io/apimachinery/pkg/runtime"
8+
"k8s.io/apimachinery/pkg/runtime/schema"
9+
"k8s.io/apimachinery/pkg/runtime/serializer"
610
"sigs.k8s.io/controller-runtime/pkg/client"
711
)
812

13+
// UpdateName sets the name on the given object.
14+
func UpdateName(obj client.Object, name string) {
15+
obj.SetName(name)
16+
}
17+
918
// UpdateNamespace sets the namespace on the given object.
1019
func UpdateNamespace(obj client.Object, newNamespace string) {
1120
obj.SetNamespace(newNamespace)
@@ -16,15 +25,6 @@ func UpdateResourceLabels(obj client.Object, labels map[string]string) {
1625
obj.SetLabels(labels)
1726
}
1827

19-
// HasObjectChanged compares two objects of the same type and returns true if they differ.
20-
// Returns false if the objects are not of the same type.
21-
func HasObjectChanged(desired, fetched client.Object) bool {
22-
if reflect.TypeOf(desired) != reflect.TypeOf(fetched) {
23-
return false
24-
}
25-
return ObjectMetadataModified(desired, fetched)
26-
}
27-
2828
// ObjectMetadataModified compares the labels of two objects and returns true if they differ.
2929
func ObjectMetadataModified(desired, fetched client.Object) bool {
3030
return !reflect.DeepEqual(desired.GetLabels(), fetched.GetLabels())
@@ -50,3 +50,17 @@ func AddAnnotation(obj client.Object, annotation, value string) bool {
5050
}
5151
return false
5252
}
53+
54+
// DecodeObjBytes decodes raw YAML/JSON bytes into a typed Kubernetes object.
55+
// Panics on decode failure or type mismatch.
56+
func DecodeObjBytes[T runtime.Object](codecs serializer.CodecFactory, gv schema.GroupVersion, objBytes []byte) T {
57+
obj, err := runtime.Decode(codecs.UniversalDecoder(gv), objBytes)
58+
if err != nil {
59+
panic(fmt.Sprintf("failed to decode object bytes for %T: %v", *new(T), err))
60+
}
61+
typed, ok := obj.(T)
62+
if !ok {
63+
panic(fmt.Sprintf("failed to convert decoded object to %T", *new(T)))
64+
}
65+
return typed
66+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package common
2+
3+
import (
4+
"unsafe"
5+
6+
corev1 "k8s.io/api/core/v1"
7+
apivalidation "k8s.io/apimachinery/pkg/api/validation"
8+
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
9+
"k8s.io/apimachinery/pkg/util/validation/field"
10+
"k8s.io/kubernetes/pkg/apis/core"
11+
corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
12+
)
13+
14+
// ValidateNodeSelectorConfig validates the NodeSelector configuration using
15+
// the Kubernetes label validation rules.
16+
func ValidateNodeSelectorConfig(nodeSelector map[string]string, fldPath *field.Path) error {
17+
return metav1validation.ValidateLabels(nodeSelector, fldPath.Child("nodeSelector")).ToAggregate()
18+
}
19+
20+
// ValidateTolerationsConfig validates the Tolerations configuration using
21+
// the Kubernetes core toleration validation rules.
22+
func ValidateTolerationsConfig(tolerations []corev1.Toleration, fldPath *field.Path) error {
23+
// convert corev1.Tolerations to core.Tolerations, required for validation.
24+
convTolerations := *(*[]core.Toleration)(unsafe.Pointer(&tolerations))
25+
return corevalidation.ValidateTolerations(convTolerations, fldPath.Child("tolerations")).ToAggregate()
26+
}
27+
28+
// ValidateResourceRequirements validates the ResourceRequirements configuration
29+
// using the Kubernetes core resource requirements validation rules.
30+
func ValidateResourceRequirements(requirements corev1.ResourceRequirements, fldPath *field.Path) error {
31+
// convert corev1.ResourceRequirements to core.ResourceRequirements, required for validation.
32+
convRequirements := *(*core.ResourceRequirements)(unsafe.Pointer(&requirements))
33+
return corevalidation.ValidateContainerResourceRequirements(&convRequirements, nil, fldPath.Child("resources"), corevalidation.PodValidationOptions{}).ToAggregate()
34+
}
35+
36+
// ValidateAffinityRules validates the Affinity configuration using
37+
// the Kubernetes core affinity validation rules.
38+
func ValidateAffinityRules(affinity *corev1.Affinity, fldPath *field.Path) error {
39+
// convert corev1.Affinity to core.Affinity, required for validation.
40+
convAffinity := (*core.Affinity)(unsafe.Pointer(affinity))
41+
return validateAffinity(convAffinity, corevalidation.PodValidationOptions{}, fldPath.Child("affinity")).ToAggregate()
42+
}
43+
44+
// ValidateLabelsConfig validates label keys and values using the Kubernetes
45+
// metadata validation rules.
46+
func ValidateLabelsConfig(labels map[string]string, fldPath *field.Path) error {
47+
return metav1validation.ValidateLabels(labels, fldPath.Child("labels")).ToAggregate()
48+
}
49+
50+
// ValidateAnnotationsConfig validates annotation keys and sizes using the
51+
// Kubernetes metadata validation rules.
52+
func ValidateAnnotationsConfig(annotations map[string]string, fldPath *field.Path) error {
53+
return apivalidation.ValidateAnnotations(annotations, fldPath.Child("annotations")).ToAggregate()
54+
}

pkg/controller/istiocsr/controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
152152
Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(mapFunc), controllerConfigMapWatchPredicates).
153153
WatchesMetadata(&corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(mapFunc), controllerWatchResourcePredicates).
154154
Watches(&networkingv1.NetworkPolicy{}, handler.EnqueueRequestsFromMapFunc(mapFunc), controllerManagedResourcePredicates).
155+
Watches(&certmanagerv1.Issuer{}, handler.EnqueueRequestsFromMapFunc(mapFunc), controllerWatchResourcePredicates).
156+
Watches(&certmanagerv1.ClusterIssuer{}, handler.EnqueueRequestsFromMapFunc(mapFunc), controllerWatchResourcePredicates).
155157
Complete(r)
156158
}
157159

pkg/controller/istiocsr/deployments.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ import (
77
"os"
88
"reflect"
99
"strings"
10-
"unsafe"
1110

1211
appsv1 "k8s.io/api/apps/v1"
1312
corev1 "k8s.io/api/core/v1"
1413
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15-
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
1614
"k8s.io/apimachinery/pkg/types"
1715
"k8s.io/apimachinery/pkg/util/validation/field"
18-
"k8s.io/kubernetes/pkg/apis/core"
19-
corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
2016
"sigs.k8s.io/controller-runtime/pkg/client"
2117

2218
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
@@ -188,7 +184,7 @@ func updateResourceRequirement(deployment *appsv1.Deployment, istiocsr *v1alpha1
188184
if reflect.ValueOf(istiocsr.Spec.IstioCSRConfig.Resources).IsZero() {
189185
return nil
190186
}
191-
if err := validateResourceRequirements(istiocsr.Spec.IstioCSRConfig.Resources,
187+
if err := common.ValidateResourceRequirements(istiocsr.Spec.IstioCSRConfig.Resources,
192188
field.NewPath("spec", "istioCSRConfig")); err != nil {
193189
return err
194190
}
@@ -202,7 +198,7 @@ func updateAffinityRules(deployment *appsv1.Deployment, istiocsr *v1alpha1.Istio
202198
if istiocsr.Spec.IstioCSRConfig.Affinity == nil {
203199
return nil
204200
}
205-
if err := validateAffinityRules(istiocsr.Spec.IstioCSRConfig.Affinity,
201+
if err := common.ValidateAffinityRules(istiocsr.Spec.IstioCSRConfig.Affinity,
206202
field.NewPath("spec", "istioCSRConfig")); err != nil {
207203
return err
208204
}
@@ -214,7 +210,7 @@ func updatePodTolerations(deployment *appsv1.Deployment, istiocsr *v1alpha1.Isti
214210
if istiocsr.Spec.IstioCSRConfig.Tolerations == nil {
215211
return nil
216212
}
217-
if err := validateTolerationsConfig(istiocsr.Spec.IstioCSRConfig.Tolerations,
213+
if err := common.ValidateTolerationsConfig(istiocsr.Spec.IstioCSRConfig.Tolerations,
218214
field.NewPath("spec", "istioCSRConfig")); err != nil {
219215
return err
220216
}
@@ -226,7 +222,7 @@ func updateNodeSelector(deployment *appsv1.Deployment, istiocsr *v1alpha1.IstioC
226222
if istiocsr.Spec.IstioCSRConfig.NodeSelector == nil {
227223
return nil
228224
}
229-
if err := validateNodeSelectorConfig(istiocsr.Spec.IstioCSRConfig.NodeSelector,
225+
if err := common.ValidateNodeSelectorConfig(istiocsr.Spec.IstioCSRConfig.NodeSelector,
230226
field.NewPath("spec", "istioCSRConfig")); err != nil {
231227
return err
232228
}
@@ -269,6 +265,10 @@ func (r *Reconciler) assertIssuerRefExists(istiocsr *v1alpha1.IstioCSR) error {
269265
return common.NewIrrecoverableError(errInvalidIssuerRefConfig, "spec.istioCSRConfig.certManager.issuerRef uses unsupported ACME issuer")
270266
}
271267

268+
if err := r.updateWatchLabel(obj, istiocsr); err != nil {
269+
return common.FromClientError(err, "failed to update watch label on cert-manager issuer %s", istiocsr.Spec.IstioCSRConfig.CertManager.IssuerRef.Name)
270+
}
271+
272272
return nil
273273
}
274274

@@ -641,26 +641,3 @@ func (r *Reconciler) updateWatchLabel(obj client.Object, istiocsr *v1alpha1.Isti
641641
}
642642
return nil
643643
}
644-
645-
// validateNodeSelectorConfig validates the NodeSelector configuration.
646-
func validateNodeSelectorConfig(nodeSelector map[string]string, fldPath *field.Path) error {
647-
return metav1validation.ValidateLabels(nodeSelector, fldPath.Child("nodeSelector")).ToAggregate()
648-
}
649-
650-
func validateTolerationsConfig(tolerations []corev1.Toleration, fldPath *field.Path) error {
651-
// convert corev1.Tolerations to core.Tolerations, required for validation.
652-
convTolerations := *(*[]core.Toleration)(unsafe.Pointer(&tolerations))
653-
return corevalidation.ValidateTolerations(convTolerations, fldPath.Child("tolerations")).ToAggregate()
654-
}
655-
656-
func validateResourceRequirements(requirements corev1.ResourceRequirements, fldPath *field.Path) error {
657-
// convert corev1.ResourceRequirements to core.ResourceRequirements, required for validation.
658-
convRequirements := *(*core.ResourceRequirements)(unsafe.Pointer(&requirements))
659-
return corevalidation.ValidateContainerResourceRequirements(&convRequirements, nil, fldPath.Child("resources"), corevalidation.PodValidationOptions{}).ToAggregate()
660-
}
661-
662-
func validateAffinityRules(affinity *corev1.Affinity, fldPath *field.Path) error {
663-
// convert corev1.Affinity to core.Affinity, required for validation.
664-
convAffinity := (*core.Affinity)(unsafe.Pointer(affinity))
665-
return validateAffinity(convAffinity, corevalidation.PodValidationOptions{}, fldPath.Child("affinity")).ToAggregate()
666-
}

0 commit comments

Comments
 (0)