Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Loosen equality on crds. #192

Merged
merged 4 commits into from
Oct 21, 2021
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
41 changes: 40 additions & 1 deletion internal/crd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

werror "github.com/palantir/witchcraft-go-error"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -51,8 +52,41 @@ func emptyIfNil(x map[string]string) map[string]string {
return x
}

func annotationsAreEqual(existingAnnotations, desiredAnnotations map[string]string) bool {
return reflect.DeepEqual(emptyIfNil(existingAnnotations), emptyIfNil(desiredAnnotations))
}

func verifyCRD(existing, desired *v1.CustomResourceDefinition) bool {
return reflect.DeepEqual(existing.Spec.Versions, desired.Spec.Versions) && reflect.DeepEqual(emptyIfNil(existing.Annotations), emptyIfNil(desired.Annotations))
return versionsAreEqual(existing.Spec.Versions, desired.Spec.Versions) && annotationsAreEqual(existing.Annotations, desired.Annotations)
}

// getVersionWithName returns the CustomResourceDefinitionVersion with the specified name if it is found
func getVersionWithName(name string, versions []v1.CustomResourceDefinitionVersion) (v1.CustomResourceDefinitionVersion, bool) {
for _, version := range versions {
if version.Name == name {
return version, true
}
}
return v1.CustomResourceDefinitionVersion{}, false
}

// TODO(cbattarbee): Convert to map comparison
func versionsAreEqual(existingVersions []v1.CustomResourceDefinitionVersion, desiredVersions []v1.CustomResourceDefinitionVersion) bool {
if len(existingVersions) != len(desiredVersions) {
return false
}
for _, existingVersion := range existingVersions {
Chrisbattarbee marked this conversation as resolved.
Show resolved Hide resolved
desiredVersion, found := getVersionWithName(existingVersion.Name, desiredVersions)
if !found {
return false
}

if existingVersion.Storage != desiredVersion.Storage ||
existingVersion.Served != desiredVersion.Served {
return false
}
}
return true
}

// EnsureResourceReservationsCRD is responsible for creating and ensuring the ResourceReservation CRD
Expand All @@ -74,28 +108,33 @@ func EnsureResourceReservationsCRD(ctx context.Context, clientset apiextensionsc
_, err = clientset.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), crd, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
svc1log.FromContext(ctx).Info("CRD already exists")
existing, getErr := clientset.ApiextensionsV1().CustomResourceDefinitions().Get(context.Background(), crd.Name, metav1.GetOptions{})
if getErr != nil {
return werror.Wrap(getErr, "Failed to get existing CRD")
}
copyCrd := crd.DeepCopy()
copyCrd.ResourceVersion = existing.ResourceVersion
svc1log.FromContext(ctx).Info("Upgrading CRD")
_, updateErr := clientset.ApiextensionsV1().CustomResourceDefinitions().Update(context.Background(), copyCrd, metav1.UpdateOptions{})
if updateErr != nil {
return werror.Wrap(updateErr, "Failed to update CRD")
}
svc1log.FromContext(ctx).Info("Upgraded CRD")
} else {
return werror.Wrap(err, "Failed to create CRD")
}
}

svc1log.FromContext(ctx).Info("Verifying upgraded CRD has applied.")
err = wait.Poll(500*time.Millisecond, 60*time.Second, func() (bool, error) {
existing, ready, err := CheckCRDExists(ctx, crd.Name, clientset)
if err != nil {
return false, err
}
return ready && verifyCRD(existing, crd), nil
})
svc1log.FromContext(ctx).Info("Verified upgraded CRD has applied.")

if err != nil {
deleteErr := clientset.ApiextensionsV1().CustomResourceDefinitions().Delete(context.Background(), crd.Name, metav1.DeleteOptions{})
Expand Down