Skip to content

Commit

Permalink
Update resource metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jun 7, 2024
1 parent 5bef270 commit 3aec32c
Show file tree
Hide file tree
Showing 144 changed files with 21,983 additions and 11,175 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ require (
k8s.io/kube-state-metrics/v2 v2.7.0
kmodules.xyz/apiversion v0.2.0
kmodules.xyz/authorizer v0.29.1
kmodules.xyz/client-go v0.30.0
kmodules.xyz/client-go v0.30.2
kmodules.xyz/custom-resources v0.29.1
kmodules.xyz/go-containerregistry v0.0.12
kmodules.xyz/monitoring-agent-api v0.29.0
kmodules.xyz/resource-metadata v0.18.7-0.20240606075826-3678251ac1f3
kmodules.xyz/resource-metadata v0.18.7-0.20240607010810-bb70dc0e8852
kmodules.xyz/resource-metrics v0.29.5
kmodules.xyz/sets v0.29.0
kubeops.dev/falco-ui-server v0.0.3
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,8 @@ kmodules.xyz/apiversion v0.2.0 h1:vAQYqZFm4xu4pbB1cAdHbFEPES6EQkcR4wc06xdTOWk=
kmodules.xyz/apiversion v0.2.0/go.mod h1:oPX8g8LvlPdPX3Yc5YvCzJHQnw3YF/X4/jdW0b1am80=
kmodules.xyz/authorizer v0.29.1 h1:uByGGoryKbZcfiEAhjcK/Y345I9mygNQP7DVpkMbNQQ=
kmodules.xyz/authorizer v0.29.1/go.mod h1:kZRhclL8twzyt2bQuJQJbpYww2sc+qFr8I5PPoq/sWY=
kmodules.xyz/client-go v0.30.0 h1:sEGX5DRXQwJiMxcN2DkDtXz9WsSA6fs9ye86RgbAxeo=
kmodules.xyz/client-go v0.30.0/go.mod h1:ekDSUC0UFLI0Jq3A62myW7VG8TYLBqCwMjqWJM1SrqU=
kmodules.xyz/client-go v0.30.2 h1:6h5MN/ERYdkzsPqKuRZ7RFUE6+ZRSKmY3ffZAnz2eUQ=
kmodules.xyz/client-go v0.30.2/go.mod h1:ekDSUC0UFLI0Jq3A62myW7VG8TYLBqCwMjqWJM1SrqU=
kmodules.xyz/crd-schema-fuzz v0.29.1 h1:zJTlWYOrT5dsVVHW8HGcnR/vaWfxQfNh11QwTtkYpcs=
kmodules.xyz/crd-schema-fuzz v0.29.1/go.mod h1:n708z9YQqLMP2KNLQVgBcRJw1QpSWLvpNCEi+KJDOYE=
kmodules.xyz/custom-resources v0.29.1 h1:xiNylhs3ILRbcUhxxy306AOy9GMA4Mq7xFIptZKgal4=
Expand All @@ -921,8 +921,8 @@ kmodules.xyz/monitoring-agent-api v0.29.0 h1:gpFl6OZrlMLb/ySMHdREI9EwGtnJ91oZBn9
kmodules.xyz/monitoring-agent-api v0.29.0/go.mod h1:iNbvaMTgVFOI5q2LJtGK91j4Dmjv4ZRiRdasGmWLKQI=
kmodules.xyz/offshoot-api v0.29.0 h1:GHLhxxT9jU1N8+FvOCCeJNyU5g0duYS46UGrs6AHNLY=
kmodules.xyz/offshoot-api v0.29.0/go.mod h1:5NxhBblXoDHWStx9HCDJR2KFTwYjEZ7i1Id3jelIunw=
kmodules.xyz/resource-metadata v0.18.7-0.20240606075826-3678251ac1f3 h1:WpoyoaUBV3v9khO21GE05/xmVfgknwGHoARIvQUz0Lw=
kmodules.xyz/resource-metadata v0.18.7-0.20240606075826-3678251ac1f3/go.mod h1:WKwJXM00EFKeDXDF+fjEMwxyZnKs/1SXMj9nt0VbDHs=
kmodules.xyz/resource-metadata v0.18.7-0.20240607010810-bb70dc0e8852 h1:VI9fBByPJ370xtyb1pvNkN67aRAgHdmTk8V2sMEr4qU=
kmodules.xyz/resource-metadata v0.18.7-0.20240607010810-bb70dc0e8852/go.mod h1:WKwJXM00EFKeDXDF+fjEMwxyZnKs/1SXMj9nt0VbDHs=
kmodules.xyz/resource-metrics v0.29.5 h1:ciuvRXuXsloLNW/JwkubqbYySsvHvHr6/nhkTL5Sf1o=
kmodules.xyz/resource-metrics v0.29.5/go.mod h1:OuG/QobZ7o8GFHl/u3lqaUR0fDZDegxtV8Vdh+MNBD4=
kmodules.xyz/sets v0.29.0 h1:ZX/qOECzUob95JhhRtngJElHSlJ1UNNdwK4hTEy+nl0=
Expand Down
52 changes: 46 additions & 6 deletions vendor/kmodules.xyz/client-go/apiextensions/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import (
)

func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) error {
return RegisterWithOpts(client, crds, false)
}

func RegisterWithOpts(client crd_cs.Interface, crds []*CustomResourceDefinition, preserveConversion bool) error {
for _, crd := range crds {
// Use crd v1 for k8s >= 1.16, if available
// ref: https://github.com/kubernetes/kubernetes/issues/91395
Expand All @@ -49,13 +53,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, preserveConversion),
metav1.UpdateOptions{},
)
if err != nil && !kerr.IsAlreadyExists(err) {
return err
}
}
return WaitForCRDReady(client, crds)
}

func UpdateWithOpts(client crd_cs.Interface, crds []*CustomResourceDefinition, preserveConversion bool) 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, preserveConversion),
metav1.UpdateOptions{},
)
if err != nil && !kerr.IsAlreadyExists(err) {
Expand All @@ -65,6 +90,21 @@ func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) err
return WaitForCRDReady(client, crds)
}

func transform(crd *CustomResourceDefinition, preserveConversion bool) 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 preserveConversion && crd.V1.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 vendor/kmodules.xyz/client-go/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
Loading

0 comments on commit 3aec32c

Please sign in to comment.