Skip to content

Commit

Permalink
Added retry budget and some refactoring.
Browse files Browse the repository at this point in the history
Signed-off-by: dmitry.koba <[email protected]>
  • Loading branch information
dmitry.koba committed Apr 9, 2024
1 parent 32dddd8 commit 14350a8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
43 changes: 15 additions & 28 deletions pkg/webhook/conversion/crd_client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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,
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion pkg/webhook/conversion/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package conversion

import (
"context"
"os"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
Expand Down

0 comments on commit 14350a8

Please sign in to comment.