Skip to content

Commit

Permalink
Merge pull request kuidio#34 from kuidio/genericbe
Browse files Browse the repository at this point in the history
updated geenric backend
  • Loading branch information
henderiw authored Nov 20, 2024
2 parents 9be1c00 + 643f005 commit 95c2742
Show file tree
Hide file tree
Showing 19 changed files with 210 additions and 291 deletions.
62 changes: 22 additions & 40 deletions apis/backend/as/asclaim_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,24 @@ func (r *ASClaim) GetIndex() string { return r.Spec.Index }
func (r *ASClaim) GetSelector() *metav1.LabelSelector { return r.Spec.Selector }

func (r *ASClaim) IsOwner(labels labels.Set) bool {
ownerLabels := r.getOnwerLabels()
for k, v := range ownerLabels {
for k, v := range r.getOwnerLabels() {
if val, ok := labels[k]; !ok || val != v {
return false
}
}
return true
}

func (r *ASClaim) getOnwerLabels() map[string]string {
claimName := r.Name
claimKind := r.Kind
claimUID := r.UID
for _, owner := range r.GetOwnerReferences() {
if owner.APIVersion == SchemeGroupVersion.Identifier() &&
owner.Kind == ASIndexKind {
claimName = owner.Name
claimKind = owner.Kind
claimUID = owner.UID
}
}

func (r *ASClaim) getOwnerLabels() map[string]string {
return map[string]string{
backend.KuidClaimNameKey: claimName,
backend.KuidClaimUIDKey: string(claimUID),
backend.KuidOwnerKindKey: claimKind,
backend.KuidClaimNameKey: r.Name,
backend.KuidClaimUIDKey: string(r.UID),
}
}

// GetOwnerSelector selects the route based on the name of the claim
func (r *ASClaim) GetOwnerSelector() (labels.Selector, error) {
l := r.getOnwerLabels()
l := r.getOwnerLabels()

fullselector := labels.NewSelector()
for k, v := range l {
Expand All @@ -213,24 +199,11 @@ func (r *ASClaim) GetLabelSelector() (labels.Selector, error) { return r.Spec.Ge
func (r *ASClaim) GetClaimLabels() labels.Set {
labels := r.Spec.GetUserDefinedLabels()

// for claims originated from the index we need to use the ownerreferences, since these claims
// are never stored in the apiserver, the ip entries need to reference the index instead
claimName := r.Name
claimKind := ASClaimKind
claimUID := r.UID
for _, owner := range r.GetOwnerReferences() {
if owner.APIVersion == SchemeGroupVersion.Identifier() &&
owner.Kind == ASIndexKind {
claimName = owner.Name
claimKind = owner.Kind
claimUID = owner.UID
}
}
// system defined labels
labels[backend.KuidClaimTypeKey] = string(r.GetClaimType())
labels[backend.KuidClaimNameKey] = claimName
labels[backend.KuidClaimUIDKey] = string(claimUID)
labels[backend.KuidOwnerKindKey] = claimKind
labels[backend.KuidClaimNameKey] = r.Name
labels[backend.KuidClaimUIDKey] = string(r.UID)
labels[backend.KuidOwnerKindKey] = r.Kind
return labels
}

Expand Down Expand Up @@ -287,6 +260,13 @@ func (r *ASClaim) GetClaimID(t string, id uint64) tree.ID {
return id32.NewID(uint32(id), id32.IDBitSize)
}

func (r *ASClaim) GetStatusClaimID() tree.ID {
if r.Status.ID == nil {
return nil
}
return id32.NewID(uint32(*r.Status.ID), id32.IDBitSize)
}

func (r *ASClaim) GetRange() *string {
return r.Spec.Range
}
Expand Down Expand Up @@ -349,17 +329,19 @@ func (r *ASClaim) GetClaimResponse() string {
return ""
}

func (r *ASClaim) GetClaimSet(typ string) (sets.Set[tree.ID], error) {
func (r *ASClaim) GetClaimSet(typ string) (map[string]tree.ID, sets.Set[string], error) {
arange, err := r.GetRangeID(typ)
if err != nil {
return nil, fmt.Errorf("cannot get range from claim: %v", err)
return nil, nil, fmt.Errorf("cannot get range from claim: %v", err)
}
// claim set represents the new entries
newClaimSet := sets.New[tree.ID]()
newClaimSet := sets.New[string]()
newClaimMap := map[string]tree.ID{}
for _, rangeID := range arange.IDs() {
newClaimSet.Insert(rangeID)
newClaimSet.Insert(rangeID.String())
newClaimMap[rangeID.String()] = rangeID
}
return newClaimSet, nil
return newClaimMap, newClaimSet, nil
}

func (r *ASClaim) GetChoreoAPIVersion() string {
Expand Down
7 changes: 4 additions & 3 deletions apis/backend/as/asindex_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/kuidio/kuid/apis/backend"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -113,7 +114,7 @@ func (r *ASIndex) GetMinClaim() backend.ClaimObject {
Name: r.GetMinClaimNSN().Name,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: r.APIVersion,
APIVersion: schema.GroupVersion{Group: SchemeGroupVersion.Group, Version: "v1alpha1"}.Identifier(),
Kind: r.Kind,
Name: r.Name,
UID: r.UID,
Expand All @@ -135,8 +136,8 @@ func (r *ASIndex) GetMaxClaim() backend.ClaimObject {
Name: r.GetMaxClaimNSN().Name,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: r.APIVersion,
Kind: r.Kind,
APIVersion: schema.GroupVersion{Group: SchemeGroupVersion.Group, Version: "v1alpha1"}.Identifier(),
Kind: ASIndexKind,
Name: r.Name,
UID: r.UID,
},
Expand Down
4 changes: 2 additions & 2 deletions apis/backend/claim_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func GetClaimTypeFromString(s string) ClaimType {
}

const (
IndexReservedMinName = "rangeReservedMin"
IndexReservedMaxName = "rangeReservedMax"
IndexReservedMinName = "rangereservedmin"
IndexReservedMaxName = "rangereservedmax"
)
62 changes: 22 additions & 40 deletions apis/backend/extcomm/extcommclaim_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,38 +179,24 @@ func (r *EXTCOMMClaim) GetIndex() string { return r.Spec.Index }
func (r *EXTCOMMClaim) GetSelector() *metav1.LabelSelector { return r.Spec.Selector }

func (r *EXTCOMMClaim) IsOwner(labels labels.Set) bool {
ownerLabels := r.getOnwerLabels()
for k, v := range ownerLabels {
for k, v := range r.getOwnerLabels() {
if val, ok := labels[k]; !ok || val != v {
return false
}
}
return true
}

func (r *EXTCOMMClaim) getOnwerLabels() map[string]string {
claimName := r.Name
claimKind := r.Kind
claimUID := r.UID
for _, owner := range r.GetOwnerReferences() {
if owner.APIVersion == SchemeGroupVersion.Identifier() &&
owner.Kind == EXTCOMMIndexKind {
claimName = owner.Name
claimKind = owner.Kind
claimUID = owner.UID
}
}

func (r *EXTCOMMClaim) getOwnerLabels() map[string]string {
return map[string]string{
backend.KuidClaimNameKey: claimName,
backend.KuidClaimUIDKey: string(claimUID),
backend.KuidOwnerKindKey: claimKind,
backend.KuidClaimNameKey: r.Name,
backend.KuidClaimUIDKey: string(r.UID),
}
}

// GetOwnerSelector selects the route bVLANed on the name of the claim
func (r *EXTCOMMClaim) GetOwnerSelector() (labels.Selector, error) {
l := r.getOnwerLabels()
l := r.getOwnerLabels()

fullselector := labels.NewSelector()
for k, v := range l {
Expand All @@ -228,24 +214,11 @@ func (r *EXTCOMMClaim) GetLabelSelector() (labels.Selector, error) { return r.Sp
func (r *EXTCOMMClaim) GetClaimLabels() labels.Set {
labels := r.Spec.GetUserDefinedLabels()

// for claims originated from the index we need to use the ownerreferences, since these claims
// are never stored in the apiserver, the ip entries need to reference the index instead
claimName := r.Name
claimKind := EXTCOMMClaimKind
claimUID := r.UID
for _, owner := range r.GetOwnerReferences() {
if owner.APIVersion == SchemeGroupVersion.Identifier() &&
owner.Kind == EXTCOMMIndexKind {
claimName = owner.Name
claimKind = owner.Kind
claimUID = owner.UID
}
}
// system defined labels
labels[backend.KuidClaimTypeKey] = string(r.GetClaimType())
labels[backend.KuidClaimNameKey] = claimName
labels[backend.KuidClaimUIDKey] = string(claimUID)
labels[backend.KuidOwnerKindKey] = claimKind
labels[backend.KuidClaimNameKey] = r.Name
labels[backend.KuidClaimUIDKey] = string(r.UID)
labels[backend.KuidOwnerKindKey] = r.Kind
return labels
}

Expand Down Expand Up @@ -302,6 +275,13 @@ func (r *EXTCOMMClaim) GetClaimID(t string, id uint64) tree.ID {
return id16.NewID(uint16(id), id16.IDBitSize)
}

func (r *EXTCOMMClaim) GetStatusClaimID() tree.ID {
if r.Status.ID == nil {
return nil
}
return id16.NewID(uint16(*r.Status.ID), id16.IDBitSize)
}

func (r *EXTCOMMClaim) GetRange() *string {
return r.Spec.Range
}
Expand Down Expand Up @@ -358,17 +338,19 @@ func (r *EXTCOMMClaim) GetClaimResponse() string {
return ""
}

func (r *EXTCOMMClaim) GetClaimSet(typ string) (sets.Set[tree.ID], error) {
func (r *EXTCOMMClaim) GetClaimSet(typ string) (map[string]tree.ID, sets.Set[string], error) {
arange, err := r.GetRangeID(typ)
if err != nil {
return nil, fmt.Errorf("cannot get range from claim: %v", err)
return nil, nil, fmt.Errorf("cannot get range from claim: %v", err)
}
// claim set represents the new entries
newClaimSet := sets.New[tree.ID]()
newClaimSet := sets.New[string]()
newClaimMap := map[string]tree.ID{}
for _, rangeID := range arange.IDs() {
newClaimSet.Insert(rangeID)
newClaimSet.Insert(rangeID.String())
newClaimMap[rangeID.String()] = rangeID
}
return newClaimSet, nil
return newClaimMap, newClaimSet, nil
}

func (r *EXTCOMMClaim) GetChoreoAPIVersion() string {
Expand Down
7 changes: 4 additions & 3 deletions apis/backend/extcomm/extcommindex_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/kuidio/kuid/apis/backend"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -131,7 +132,7 @@ func (r *EXTCOMMIndex) GetMinClaim() backend.ClaimObject {
Name: r.GetMinClaimNSN().Name,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: r.APIVersion,
APIVersion: schema.GroupVersion{Group: SchemeGroupVersion.Group, Version: "v1alpha1"}.Identifier(),
Kind: r.Kind,
Name: r.Name,
UID: r.UID,
Expand All @@ -153,8 +154,8 @@ func (r *EXTCOMMIndex) GetMaxClaim() backend.ClaimObject {
Name: r.GetMaxClaimNSN().Name,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: r.APIVersion,
Kind: r.Kind,
APIVersion: schema.GroupVersion{Group: SchemeGroupVersion.Group, Version: "v1alpha1"}.Identifier(),
Kind: EXTCOMMIndexKind,
Name: r.Name,
UID: r.UID,
},
Expand Down
63 changes: 23 additions & 40 deletions apis/backend/genid/genidclaim_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,38 +165,24 @@ func (r *GENIDClaim) GetIndex() string { return r.Spec.Index }
func (r *GENIDClaim) GetSelector() *metav1.LabelSelector { return r.Spec.Selector }

func (r *GENIDClaim) IsOwner(labels labels.Set) bool {
ownerLabels := r.getOnwerLabels()
for k, v := range ownerLabels {
for k, v := range r.getOwnerLabels() {
if val, ok := labels[k]; !ok || val != v {
return false
}
}
return true
}

func (r *GENIDClaim) getOnwerLabels() map[string]string {
claimName := r.Name
claimKind := r.Kind
claimUID := r.UID
for _, owner := range r.GetOwnerReferences() {
if owner.APIVersion == SchemeGroupVersion.Identifier() &&
owner.Kind == GENIDIndexKind {
claimName = owner.Name
claimKind = owner.Kind
claimUID = owner.UID
}
}

func (r *GENIDClaim) getOwnerLabels() map[string]string {
return map[string]string{
backend.KuidClaimNameKey: claimName,
backend.KuidClaimUIDKey: string(claimUID),
backend.KuidOwnerKindKey: claimKind,
backend.KuidClaimNameKey: r.Name,
backend.KuidClaimUIDKey: string(r.UID),
}
}

// GetOwnerSelector selects the route bVLANed on the name of the claim
func (r *GENIDClaim) GetOwnerSelector() (labels.Selector, error) {
l := r.getOnwerLabels()
l := r.getOwnerLabels()

fullselector := labels.NewSelector()
for k, v := range l {
Expand All @@ -214,25 +200,13 @@ func (r *GENIDClaim) GetLabelSelector() (labels.Selector, error) { return r.Spec
func (r *GENIDClaim) GetClaimLabels() labels.Set {
labels := r.Spec.GetUserDefinedLabels()

// for claims originated from the index we need to use the ownerreferences, since these claims
// are never stored in the apiserver, the ip entries need to reference the index instead
claimName := r.Name
claimKind := GENIDClaimKind
claimUID := r.UID
for _, owner := range r.GetOwnerReferences() {
if owner.APIVersion == SchemeGroupVersion.Identifier() &&
owner.Kind == GENIDIndexKind {
claimName = owner.Name
claimKind = owner.Kind
claimUID = owner.UID
}
}
// system defined labels
labels[backend.KuidClaimTypeKey] = string(r.GetClaimType())
labels[backend.KuidClaimNameKey] = claimName
labels[backend.KuidClaimUIDKey] = string(claimUID)
labels[backend.KuidOwnerKindKey] = claimKind
labels[backend.KuidClaimNameKey] = r.Name
labels[backend.KuidClaimUIDKey] = string(r.UID)
labels[backend.KuidOwnerKindKey] = r.Kind
return labels

}

func (r *GENIDClaim) ValidateOwner(labels labels.Set) error {
Expand Down Expand Up @@ -288,6 +262,13 @@ func (r *GENIDClaim) GetClaimID(t string, id uint64) tree.ID {
return id16.NewID(uint16(id), id16.IDBitSize)
}

func (r *GENIDClaim) GetStatusClaimID() tree.ID {
if r.Status.ID == nil {
return nil
}
return id16.NewID(uint16(*r.Status.ID), id16.IDBitSize)
}

func (r *GENIDClaim) GetRange() *string {
return r.Spec.Range
}
Expand Down Expand Up @@ -344,17 +325,19 @@ func (r *GENIDClaim) GetClaimResponse() string {
return ""
}

func (r *GENIDClaim) GetClaimSet(typ string) (sets.Set[tree.ID], error) {
func (r *GENIDClaim) GetClaimSet(typ string) (map[string]tree.ID, sets.Set[string], error) {
arange, err := r.GetRangeID(typ)
if err != nil {
return nil, fmt.Errorf("cannot get range from claim: %v", err)
return nil, nil, fmt.Errorf("cannot get range from claim: %v", err)
}
// claim set represents the new entries
newClaimSet := sets.New[tree.ID]()
newClaimSet := sets.New[string]()
newClaimMap := map[string]tree.ID{}
for _, rangeID := range arange.IDs() {
newClaimSet.Insert(rangeID)
newClaimSet.Insert(rangeID.String())
newClaimMap[rangeID.String()] = rangeID
}
return newClaimSet, nil
return newClaimMap, newClaimSet, nil
}

func (r *GENIDClaim) GetChoreoAPIVersion() string {
Expand Down
Loading

0 comments on commit 95c2742

Please sign in to comment.