diff --git a/pkg/webhook/conversion/crd_client_config.go b/pkg/webhook/conversion/crd_client_config.go index c1facc9d..a397543a 100644 --- a/pkg/webhook/conversion/crd_client_config.go +++ b/pkg/webhook/conversion/crd_client_config.go @@ -4,7 +4,6 @@ import ( "context" "time" - log "github.com/sirupsen/logrus" extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -23,43 +22,34 @@ type CrdClientConfig struct { var SupportedConversionReviewVersions = []string{"v1", "v1beta1"} -func (c *CrdClientConfig) Update() error { +func (c *CrdClientConfig) Update(ctx context.Context) error { var ( retryTimeout = 15 * time.Second + retryBudget = 12 // 12 times * 15 sec = 3 min client = c.KubeClient ) tryToGetCRD: - listOpts := metav1.ListOptions{ - FieldSelector: "metadata.name=" + c.CrdName, - } - - crdList, err := client.ApiExt().CustomResourceDefinitions().List(context.TODO(), listOpts) + crd, err := client.ApiExt().CustomResourceDefinitions().Get(ctx, c.CrdName, metav1.GetOptions{}) if err != nil { - return err - } + if retryBudget > 0 { + retryBudget-- + time.Sleep(retryTimeout) + goto tryToGetCRD + } - if len(crdList.Items) == 0 { - log.Warnf("crd/%s not found. Will try to find it later", c.CrdName) - time.Sleep(retryTimeout) - goto tryToGetCRD + return err } - crd := crdList.Items[0] - if crd.Spec.Conversion == nil { crd.Spec.Conversion = new(extv1.CustomResourceConversion) } - conv := crd.Spec.Conversion + crd.Spec.Conversion.Strategy = extv1.WebhookConverter - conv.Strategy = extv1.WebhookConverter - if conv.Webhook == nil { - conv.Webhook = new(extv1.WebhookConversion) + if crd.Spec.Conversion.Webhook == nil { + crd.Spec.Conversion.Webhook = new(extv1.WebhookConversion) } - - webhook := conv.Webhook - - webhook.ClientConfig = &extv1.WebhookClientConfig{ + crd.Spec.Conversion.Webhook.ClientConfig = &extv1.WebhookClientConfig{ URL: nil, Service: &extv1.ServiceReference{ Namespace: c.Namespace, @@ -68,15 +58,12 @@ tryToGetCRD: }, CABundle: c.CABundle, } + crd.Spec.Conversion.Webhook.ConversionReviewVersions = SupportedConversionReviewVersions - webhook.ConversionReviewVersions = SupportedConversionReviewVersions - - _, err = client.ApiExt().CustomResourceDefinitions().Update(context.TODO(), &crd, metav1.UpdateOptions{}) + _, err = client.ApiExt().CustomResourceDefinitions().Update(ctx, crd, metav1.UpdateOptions{}) if err != nil { return err } - log.Infof("crd/%s spec.conversion is updated to a webhook behind %s/%s", c.CrdName, c.ServiceName, c.Path) - return nil } diff --git a/pkg/webhook/conversion/manager.go b/pkg/webhook/conversion/manager.go index 2bcc6f4b..8fd3a606 100644 --- a/pkg/webhook/conversion/manager.go +++ b/pkg/webhook/conversion/manager.go @@ -1,6 +1,7 @@ package conversion import ( + "context" "os" log "github.com/sirupsen/logrus" @@ -66,6 +67,7 @@ func (m *WebhookManager) Init() error { // Start webhook server and update spec.conversion in CRDs. func (m *WebhookManager) Start() error { + ctx := context.Background() log.Info("Start conversion webhooks manager. Load certificates.") err := m.Server.Start() @@ -74,7 +76,7 @@ func (m *WebhookManager) Start() error { } for _, clientCfg := range m.ClientConfigs { - err = clientCfg.Update() + err = clientCfg.Update(ctx) if err != nil { return err }