Skip to content

Commit ce0c42c

Browse files
github-actions[bot]rambohe-ch
andauthored
fix only openyurt crd conversion should be handled for upgrading cert (#2014)
(cherry picked from commit 10313f7) Co-authored-by: rambohe-ch <[email protected]>
1 parent 32a9758 commit ce0c42c

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

pkg/yurtmanager/webhook/util/controller/webhook_controller.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
"fmt"
23+
"strings"
2324
"sync"
2425
"time"
2526

@@ -30,6 +31,8 @@ import (
3031
apiextensionsinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
3132
apiextensionslister "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1"
3233
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/runtime"
35+
"k8s.io/apimachinery/pkg/runtime/schema"
3336
"k8s.io/apimachinery/pkg/types"
3437
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3538
"k8s.io/apimachinery/pkg/util/wait"
@@ -42,6 +45,7 @@ import (
4245
"k8s.io/klog/v2"
4346

4447
"github.com/openyurtio/openyurt/cmd/yurt-manager/app/config"
48+
"github.com/openyurtio/openyurt/pkg/apis"
4549
webhookutil "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util"
4650
"github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util/configuration"
4751
"github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util/generator"
@@ -55,8 +59,14 @@ const (
5559
var (
5660
uninit = make(chan struct{})
5761
onceInit = sync.Once{}
62+
63+
yurtScheme = runtime.NewScheme()
5864
)
5965

66+
func init() {
67+
utilruntime.Must(apis.AddToScheme(yurtScheme))
68+
}
69+
6070
func Inited() chan struct{} {
6171
return uninit
6272
}
@@ -100,14 +110,14 @@ func New(handlers map[string]struct{}, cc *config.CompletedConfig, restCfg *rest
100110
crdInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
101111
AddFunc: func(obj interface{}) {
102112
crd := obj.(*apiextensionsv1.CustomResourceDefinition)
103-
if crdHasWebhookConversion(crd) {
113+
if yurtCRDHasWebhookConversion(crd) {
104114
klog.Infof("CRD %s with conversion added", crd.Name)
105115
c.queue.Add(crd.Name)
106116
}
107117
},
108118
UpdateFunc: func(old, new interface{}) {
109119
crd := new.(*apiextensionsv1.CustomResourceDefinition)
110-
if crdHasWebhookConversion(crd) {
120+
if yurtCRDHasWebhookConversion(crd) {
111121
klog.Infof("CRD %s with conversion updated", crd.Name)
112122
c.queue.Add(crd.Name)
113123
}
@@ -262,7 +272,12 @@ func (c *Controller) sync(key string) error {
262272
return nil
263273
}
264274

265-
func crdHasWebhookConversion(crd *apiextensionsv1.CustomResourceDefinition) bool {
275+
func yurtCRDHasWebhookConversion(crd *apiextensionsv1.CustomResourceDefinition) bool {
276+
// it is an openyurt crd
277+
if !strings.Contains(crd.Spec.Group, "openyurt.io") {
278+
return false
279+
}
280+
266281
conversion := crd.Spec.Conversion
267282
if conversion == nil {
268283
return false
@@ -276,12 +291,22 @@ func crdHasWebhookConversion(crd *apiextensionsv1.CustomResourceDefinition) bool
276291
}
277292

278293
func ensureCRDConversionCA(client apiextensionsclientset.Interface, crd *apiextensionsv1.CustomResourceDefinition, newCABundle []byte) error {
279-
if crd.Spec.Conversion == nil ||
294+
if len(crd.Spec.Versions) == 0 ||
295+
crd.Spec.Conversion == nil ||
296+
crd.Spec.Conversion.Strategy != apiextensionsv1.WebhookConverter ||
280297
crd.Spec.Conversion.Webhook == nil ||
281298
crd.Spec.Conversion.Webhook.ClientConfig == nil {
282299
return nil
283300
}
284301

302+
if !yurtScheme.Recognizes(schema.GroupVersionKind{
303+
Group: crd.Spec.Group,
304+
Version: crd.Spec.Versions[0].Name,
305+
Kind: crd.Spec.Names.Kind,
306+
}) {
307+
return nil
308+
}
309+
285310
if bytes.Equal(crd.Spec.Conversion.Webhook.ClientConfig.CABundle, newCABundle) {
286311
return nil
287312
}

0 commit comments

Comments
 (0)