mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-06 01:20:40 +00:00
events: ensure the name is valid
The event Object is created from the referenced Object name, however, there is no guarantee that the name from the referenced Object will be a valid Event Name. For those Objects with name not valid for an Event, generate a new valid name using an UUID. Kubernetes-commit: ee36b817df06f84ce1a48ef4d65ed559c3775404
This commit is contained in:
committed by
Kubernetes Publisher
parent
d067ae26c7
commit
023460f5a8
@@ -17,10 +17,14 @@ limitations under the License.
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
|
||||
)
|
||||
|
||||
// ValidateEventType checks that eventtype is an expected type of event
|
||||
@@ -38,3 +42,16 @@ func IsKeyNotFoundError(err error) bool {
|
||||
|
||||
return statusErr != nil && statusErr.Status().Code == http.StatusNotFound
|
||||
}
|
||||
|
||||
// GenerateEventName generates a valid Event name from the referenced name and the passed UNIX timestamp.
|
||||
// The referenced Object name may not be a valid name for Events and cause the Event to fail
|
||||
// to be created, so we need to generate a new one in that case.
|
||||
// Ref: https://issues.k8s.io/127594
|
||||
func GenerateEventName(refName string, unixNano int64) string {
|
||||
name := fmt.Sprintf("%s.%x", refName, unixNano)
|
||||
if errs := apimachineryvalidation.NameIsDNSSubdomain(name, false); len(errs) > 0 {
|
||||
// Using an uuid guarantees uniqueness and correctness
|
||||
name = uuid.New().String()
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
Reference in New Issue
Block a user