@@ -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
@@ -201,12 +203,10 @@ func (cm *certManagerClient) install(ctx context.Context, version string, objs [
201
203
// a cert-manager upgrade if necessary.
202
204
func (cm * certManagerClient ) PlanUpgrade (ctx context.Context ) (CertManagerUpgradePlan , error ) {
203
205
log := logf .Log
204
-
205
- objs , err := cm .proxy .ListResources (ctx , map [string ]string {clusterctlv1 .ClusterctlCoreLabel : clusterctlv1 .ClusterctlCoreLabelCertManagerValue }, certManagerNamespace )
206
+ objs , err := cm .getFilteredCertManagerResources (ctx )
206
207
if err != nil {
207
- return CertManagerUpgradePlan {}, errors .Wrap (err , "failed get cert manager components" )
208
+ return CertManagerUpgradePlan {}, errors .Wrap (err , "failed to calculate cert- manager components for upgrade " )
208
209
}
209
-
210
210
// If there are no cert manager components with the clusterctl labels, it means that cert-manager is externally managed.
211
211
if len (objs ) == 0 {
212
212
log .V (5 ).Info ("Skipping cert-manager version check because externally managed" )
@@ -236,14 +236,30 @@ func (cm *certManagerClient) PlanUpgrade(ctx context.Context) (CertManagerUpgrad
236
236
}, nil
237
237
}
238
238
239
+ // getFilteredCertManagerResources gets all relevant objects for a cert-manager installation.
240
+ // It also includes relevant resources in the kube-system namespace, which is used by cert-manager
241
+ // for leader election (https://github.com/cert-manager/cert-manager/issues/6716).
242
+ // It excludes resources that are related to the cert-manager installation, but not relevant
243
+ // to evaluating if cert-manager needs an upgrade.
244
+ func (cm * certManagerClient ) getFilteredCertManagerResources (ctx context.Context ) ([]unstructured.Unstructured , error ) {
245
+ objs , err := cm .proxy .ListResources (ctx , map [string ]string {clusterctlv1 .ClusterctlCoreLabel : clusterctlv1 .ClusterctlCoreLabelCertManagerValue }, certManagerNamespace , metav1 .NamespaceSystem )
246
+ if err != nil {
247
+ return nil , errors .Wrap (err , "failed to list cert-manager components for upgrade" )
248
+ }
249
+ objs = slices .DeleteFunc (objs , func (obj unstructured.Unstructured ) bool {
250
+ return obj .GetKind () == "Endpoints" || obj .GetKind () == "EndpointSlice"
251
+ })
252
+ return objs , nil
253
+ }
254
+
239
255
// EnsureLatestVersion checks the cert-manager version currently installed, and if it is
240
256
// older than the version currently suggested by clusterctl, upgrades it.
241
257
func (cm * certManagerClient ) EnsureLatestVersion (ctx context.Context ) error {
242
258
log := logf .Log
243
259
244
- objs , err := cm .proxy . ListResources (ctx , map [ string ] string { clusterctlv1 . ClusterctlCoreLabel : clusterctlv1 . ClusterctlCoreLabelCertManagerValue }, certManagerNamespace )
260
+ objs , err := cm .getFilteredCertManagerResources (ctx )
245
261
if err != nil {
246
- return errors .Wrap (err , "failed get cert manager components" )
262
+ return errors .Wrap (err , "failed to calculate cert- manager components for upgrade " )
247
263
}
248
264
249
265
// If there are no cert manager components with the clusterctl labels, it means that cert-manager is externally managed.
0 commit comments