Skip to content

Commit

Permalink
use k8s name generator
Browse files Browse the repository at this point in the history
  • Loading branch information
rquitales committed Jun 25, 2024
1 parent a3654c0 commit 24c4a0e
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions provider/pkg/await/await.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/client-go/dynamic"
k8sopenapi "k8s.io/kubectl/pkg/util/openapi"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
Expand Down Expand Up @@ -223,19 +224,10 @@ func Creation(c CreateConfig) (*unstructured.Unstructured, error) {
// creates with SSA enabled. This means that we currently always upsert the object.
if c.Preview &&
(apierrors.IsInvalid(err) && strings.Contains(err.Error(), apivalidation.FieldImmutableErrorMsg)) {
const previewSuffix = "-preview"
objName := c.Inputs.GetName()

// Set the name to have the "-preview" suffix. Ensure we don't have a string over 253 characters, which is
// the maximum length for a name in Kubernetes.
var previewName string
if len(objName) > 253-len(previewSuffix) {
previewName = objName[:253-len(previewSuffix)] + previewSuffix
} else {
previewName = objName + previewSuffix
}

previewName := names.SimpleNameGenerator.GenerateName(c.Inputs.GetName())
c.Host.Log(c.Context, diag.Info, c.URN, fmt.Sprintf("Preview creation failed due to immutable fields; retrying with name %q", previewName))

Check failure on line 228 in provider/pkg/await/await.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `c.Host.Log` is not checked (errcheck)
c.Inputs.SetName(previewName)

objYAML, errYaml := yaml.Marshal(c.Inputs.Object)
if errYaml != nil {
return err
Expand Down

0 comments on commit 24c4a0e

Please sign in to comment.