Skip to content

Commit

Permalink
Preserve crd conversion config and update crds if present
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jun 2, 2024
1 parent d0e01cd commit 89e4aae
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
48 changes: 42 additions & 6 deletions apiextensions/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,34 @@ func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) err
context.TODO(),
client,
crd.V1.Name,
func(in *crdv1.CustomResourceDefinition) *crdv1.CustomResourceDefinition {
in.Labels = meta_util.OverwriteKeys(in.Labels, crd.V1.Labels)
in.Annotations = meta_util.OverwriteKeys(in.Annotations, crd.V1.Annotations)
transform(crd),
metav1.UpdateOptions{},
)
if err != nil && !kerr.IsAlreadyExists(err) {
return err
}
}
return WaitForCRDReady(client, crds)
}

func UpdateCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) error {
for _, crd := range crds {
// Use crd v1 for k8s >= 1.16, if available
// ref: https://github.com/kubernetes/kubernetes/issues/91395
if crd.V1 == nil {
gvr := schema.GroupVersionResource{
Group: crd.V1beta1.Spec.Group,
Version: crd.V1beta1.Spec.Versions[0].Name,
Resource: crd.V1beta1.Spec.Names.Plural,
}
return fmt.Errorf("missing V1 definition for %s", gvr)
}

in.Spec = crd.V1.Spec
return in
},
_, _, err := v1.UpdateCustomResourceDefinitionIfPresent(
context.TODO(),
client,
crd.V1.Name,
transform(crd),
metav1.UpdateOptions{},
)
if err != nil && !kerr.IsAlreadyExists(err) {
Expand All @@ -65,6 +86,21 @@ func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) err
return WaitForCRDReady(client, crds)
}

func transform(crd *CustomResourceDefinition) func(in *crdv1.CustomResourceDefinition) *crdv1.CustomResourceDefinition {
return func(in *crdv1.CustomResourceDefinition) *crdv1.CustomResourceDefinition {
in.Labels = meta_util.OverwriteKeys(in.Labels, crd.V1.Labels)
in.Annotations = meta_util.OverwriteKeys(in.Annotations, crd.V1.Annotations)

conversion := in.Spec.Conversion
in.Spec = crd.V1.Spec
// preserve conversion
if in.Spec.Conversion == nil && conversion != nil {
in.Spec.Conversion = conversion
}
return in
}
}

func WaitForCRDReady(client crd_cs.Interface, crds []*CustomResourceDefinition) error {
err := wait.PollUntilContextTimeout(context.Background(), 3*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) {
for _, crd := range crds {
Expand Down
28 changes: 28 additions & 0 deletions apiextensions/v1/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ func CreateOrUpdateCustomResourceDefinition(
return cur, kutil.VerbUpdated, nil
}

func UpdateCustomResourceDefinitionIfPresent(
ctx context.Context,
c cs.Interface,
name string,
transform func(in *api.CustomResourceDefinition) *api.CustomResourceDefinition,
opts metav1.UpdateOptions,
) (*api.CustomResourceDefinition, kutil.VerbType, error) {
_, err := c.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
if kerr.IsNotFound(err) {
return transform(&api.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: api.SchemeGroupVersion.String(),
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}), kutil.VerbUnchanged, nil
} else if err != nil {
return nil, kutil.VerbUnchanged, err
}
cur, err := TryUpdateCustomResourceDefinition(ctx, c, name, transform, opts)
if err != nil {
return nil, kutil.VerbUnchanged, err
}
return cur, kutil.VerbUpdated, nil
}

func TryUpdateCustomResourceDefinition(
ctx context.Context,
c cs.Interface,
Expand Down

0 comments on commit 89e4aae

Please sign in to comment.