Skip to content

Commit

Permalink
Make UUID Generator Library (#80)
Browse files Browse the repository at this point in the history
THings other than APIs need to be able to generate Kubernetes name
compatible UUIDs, mainly for initial system configuration.
  • Loading branch information
spjmurray authored Sep 23, 2024
1 parent c784ba3 commit c9afe3a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
4 changes: 2 additions & 2 deletions charts/core/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ description: A Helm chart for deploying Unikorn Core

type: application

version: v0.1.70
appVersion: v0.1.70
version: v0.1.71
appVersion: v0.1.71

icon: https://assets.unikorn-cloud.org/images/logos/dark-on-light/icon.svg

Expand Down
17 changes: 2 additions & 15 deletions pkg/server/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import (
"errors"
"fmt"
"time"
"unicode"

unikornv1 "github.com/unikorn-cloud/core/pkg/apis/unikorn/v1alpha1"
"github.com/unikorn-cloud/core/pkg/constants"
"github.com/unikorn-cloud/core/pkg/openapi"
"github.com/unikorn-cloud/core/pkg/util"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
)

var (
Expand Down Expand Up @@ -134,26 +133,14 @@ func ProjectScopedResourceReadMetadata(in metav1.Object, status openapi.Resource
return out
}

// generateResourceID creates a valid Kubernetes name from a UUID.
func generateResourceID() string {
for {
// NOTE: Kubernetes UUIDs are based on version 4, aka random,
// so the first character will be a letter eventually, like
// a 6/16 chance: tl;dr infinite loops are... improbable.
if id := uuid.NewUUID(); unicode.IsLetter(rune(id[0])) {
return string(id)
}
}
}

// ObjectMetadata implements a builder pattern.
type ObjectMetadata metav1.ObjectMeta

// NewObjectMetadata requests the bare minimum to build an object metadata object.
func NewObjectMetadata(metadata *openapi.ResourceWriteMetadata, namespace, actor string) *ObjectMetadata {
o := &ObjectMetadata{
Namespace: namespace,
Name: generateResourceID(),
Name: util.GenerateResourceID(),
Labels: map[string]string{
constants.NameLabel: metadata.Name,
},
Expand Down
35 changes: 35 additions & 0 deletions pkg/util/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"unicode"

"k8s.io/apimachinery/pkg/util/uuid"
)

// GenerateResourceID creates a valid Kubernetes name from a UUID.
func GenerateResourceID() string {
for {
// NOTE: Kubernetes UUIDs are based on version 4, aka random,
// so the first character will be a letter eventually, like
// a 6/16 chance: tl;dr infinite loops are... improbable.
if id := uuid.NewUUID(); unicode.IsLetter(rune(id[0])) {
return string(id)
}
}
}

0 comments on commit c9afe3a

Please sign in to comment.