@@ -19,12 +19,14 @@ package cluster
19
19
import (
20
20
"context"
21
21
_ "embed"
22
+ "slices"
22
23
"time"
23
24
24
25
"github.com/blang/semver/v4"
25
26
"github.com/pkg/errors"
26
27
corev1 "k8s.io/api/core/v1"
27
28
apierrors "k8s.io/apimachinery/pkg/api/errors"
29
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
30
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29
31
"sigs.k8s.io/controller-runtime/pkg/client"
30
32
@@ -53,6 +55,10 @@ const (
53
55
var (
54
56
//go:embed assets/cert-manager-test-resources.yaml
55
57
certManagerTestManifest []byte
58
+ // namespaces for all relevant objects in a cert-manager installation.
59
+ // It also includes relevant resources in the kube-system namespace, which is used by cert-manager
60
+ // for leader election (https://github.com/cert-manager/cert-manager/issues/6716).
61
+ certManagerNamespaces = []string {certManagerNamespace , metav1 .NamespaceSystem }
56
62
)
57
63
58
64
// CertManagerUpgradePlan defines the upgrade plan if cert-manager needs to be
@@ -202,9 +208,9 @@ func (cm *certManagerClient) install(ctx context.Context, version string, objs [
202
208
func (cm * certManagerClient ) PlanUpgrade (ctx context.Context ) (CertManagerUpgradePlan , error ) {
203
209
log := logf .Log
204
210
205
- objs , err := cm .proxy .ListResources (ctx , map [string ]string {clusterctlv1 .ClusterctlCoreLabel : clusterctlv1 .ClusterctlCoreLabelCertManagerValue }, certManagerNamespace )
211
+ objs , err := cm .proxy .ListResources (ctx , map [string ]string {clusterctlv1 .ClusterctlCoreLabel : clusterctlv1 .ClusterctlCoreLabelCertManagerValue }, certManagerNamespaces ... )
206
212
if err != nil {
207
- return CertManagerUpgradePlan {}, errors .Wrap (err , "failed get cert manager components" )
213
+ return CertManagerUpgradePlan {}, errors .Wrap (err , "failed to get cert- manager components" )
208
214
}
209
215
210
216
// If there are no cert manager components with the clusterctl labels, it means that cert-manager is externally managed.
@@ -240,12 +246,10 @@ func (cm *certManagerClient) PlanUpgrade(ctx context.Context) (CertManagerUpgrad
240
246
// older than the version currently suggested by clusterctl, upgrades it.
241
247
func (cm * certManagerClient ) EnsureLatestVersion (ctx context.Context ) error {
242
248
log := logf .Log
243
-
244
- objs , err := cm .proxy .ListResources (ctx , map [string ]string {clusterctlv1 .ClusterctlCoreLabel : clusterctlv1 .ClusterctlCoreLabelCertManagerValue }, certManagerNamespace )
249
+ objs , err := cm .proxy .ListResources (ctx , map [string ]string {clusterctlv1 .ClusterctlCoreLabel : clusterctlv1 .ClusterctlCoreLabelCertManagerValue }, certManagerNamespaces ... )
245
250
if err != nil {
246
- return errors .Wrap (err , "failed get cert manager components" )
251
+ return errors .Wrap (err , "failed to get cert- manager components" )
247
252
}
248
-
249
253
// If there are no cert manager components with the clusterctl labels, it means that cert-manager is externally managed.
250
254
if len (objs ) == 0 {
251
255
log .V (5 ).Info ("Skipping cert-manager upgrade because externally managed" )
@@ -338,14 +342,16 @@ func (cm *certManagerClient) shouldUpgrade(desiredVersion string, objs, installO
338
342
339
343
needUpgrade := false
340
344
currentVersion := ""
345
+
346
+ // removes resources that are generated by the kubernetes API
347
+ // this is relevant if the versions are the same, because we compare
348
+ // the number of objects when version of objects are equal
349
+ objs = slices .DeleteFunc (objs , func (obj unstructured.Unstructured ) bool {
350
+ return obj .GetKind () == "Endpoints" || obj .GetKind () == "EndpointSlice"
351
+ })
341
352
for i := range objs {
342
353
obj := objs [i ]
343
354
344
- // Endpoints and EndpointSlices are generated by Kubernetes without the version annotation, so we are skipping them
345
- if obj .GetKind () == "Endpoints" || obj .GetKind () == "EndpointSlice" {
346
- continue
347
- }
348
-
349
355
// if there is no version annotation, this means the obj is cert-manager v0.11.0 (installed with older version of clusterctl)
350
356
objVersion , ok := obj .GetAnnotations ()[clusterctlv1 .CertManagerVersionAnnotation ]
351
357
if ! ok {
0 commit comments