Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix only openyurt crd conversion should be handled for upgrading cert #2013

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions pkg/yurtmanager/webhook/util/controller/webhook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"fmt"
"strings"
"sync"
"time"

Expand All @@ -30,6 +31,8 @@ import (
apiextensionsinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
apiextensionslister "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -42,6 +45,7 @@ import (
"k8s.io/klog/v2"

"github.com/openyurtio/openyurt/cmd/yurt-manager/app/config"
"github.com/openyurtio/openyurt/pkg/apis"
webhookutil "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util"
"github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util/configuration"
"github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util/generator"
Expand All @@ -55,8 +59,14 @@ const (
var (
uninit = make(chan struct{})
onceInit = sync.Once{}

yurtScheme = runtime.NewScheme()
)

func init() {
utilruntime.Must(apis.AddToScheme(yurtScheme))
}

func Inited() chan struct{} {
return uninit
}
Expand Down Expand Up @@ -100,14 +110,14 @@ func New(handlers map[string]struct{}, cc *config.CompletedConfig, restCfg *rest
crdInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
crd := obj.(*apiextensionsv1.CustomResourceDefinition)
if crdHasWebhookConversion(crd) {
if yurtCRDHasWebhookConversion(crd) {
klog.Infof("CRD %s with conversion added", crd.Name)
c.queue.Add(crd.Name)
}
},
UpdateFunc: func(old, new interface{}) {
crd := new.(*apiextensionsv1.CustomResourceDefinition)
if crdHasWebhookConversion(crd) {
if yurtCRDHasWebhookConversion(crd) {
klog.Infof("CRD %s with conversion updated", crd.Name)
c.queue.Add(crd.Name)
}
Expand Down Expand Up @@ -262,7 +272,12 @@ func (c *Controller) sync(key string) error {
return nil
}

func crdHasWebhookConversion(crd *apiextensionsv1.CustomResourceDefinition) bool {
func yurtCRDHasWebhookConversion(crd *apiextensionsv1.CustomResourceDefinition) bool {
// it is an openyurt crd
if !strings.Contains(crd.Spec.Group, "openyurt.io") {
return false
}

conversion := crd.Spec.Conversion
if conversion == nil {
return false
Expand All @@ -276,12 +291,22 @@ func crdHasWebhookConversion(crd *apiextensionsv1.CustomResourceDefinition) bool
}

func ensureCRDConversionCA(client apiextensionsclientset.Interface, crd *apiextensionsv1.CustomResourceDefinition, newCABundle []byte) error {
if crd.Spec.Conversion == nil ||
if len(crd.Spec.Versions) == 0 ||
crd.Spec.Conversion == nil ||
crd.Spec.Conversion.Strategy != apiextensionsv1.WebhookConverter ||
crd.Spec.Conversion.Webhook == nil ||
crd.Spec.Conversion.Webhook.ClientConfig == nil {
return nil
}

if !yurtScheme.Recognizes(schema.GroupVersionKind{
Group: crd.Spec.Group,
Version: crd.Spec.Versions[0].Name,
Kind: crd.Spec.Names.Kind,
}) {
return nil
}

if bytes.Equal(crd.Spec.Conversion.Webhook.ClientConfig.CABundle, newCABundle) {
return nil
}
Expand Down
Loading