-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a slim sprig package to avoid additional dependencies and move the common logic to util package
- Loading branch information
1 parent
15dcc9a
commit ed589aa
Showing
10 changed files
with
191 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2025 The Kubernetes 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 ( | ||
"bytes" | ||
"fmt" | ||
"text/template" | ||
|
||
sprig "github.com/go-task/slim-sprig/v3" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
maxNameLength = 63 | ||
) | ||
|
||
var nameTemplateFuncs = map[string]any{ | ||
"trimSuffix": sprig.GenericFuncMap()["trimSuffix"], | ||
"trunc": sprig.GenericFuncMap()["trunc"], | ||
} | ||
|
||
var nameTpl = template.New("name generator").Funcs(nameTemplateFuncs).Option("missingkey=error") | ||
|
||
// GenerateNameFromMachineNameTemplate generate a name from machine name and a naming strategy template. | ||
// the template supports only `trimSuffix` and `trunc` functions. | ||
func GenerateNameFromMachineNameTemplate(machineName string, nameTemplate *string) (string, error) { | ||
if machineName == "" { | ||
return "", fmt.Errorf("machine name can not be emmpty") | ||
} | ||
|
||
if nameTemplate == nil { | ||
return machineName, nil | ||
} | ||
|
||
data := map[string]interface{}{ | ||
"machine": map[string]interface{}{ | ||
"name": machineName, | ||
}, | ||
} | ||
|
||
tpl, err := nameTpl.Parse(*nameTemplate) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to generate name: failed to parse template %q", *nameTemplate) | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := tpl.Execute(&buf, data); err != nil { | ||
return "", errors.Wrap(err, "failed to generate name") | ||
} | ||
|
||
name := buf.String() | ||
|
||
// If the name exceeds the maxNameLength, trim to maxNameLength. | ||
// Note: we're not adding a random suffix as the name has to be deterministic. | ||
if len(name) > maxNameLength { | ||
name = name[:maxNameLength] | ||
} | ||
|
||
return name, nil | ||
} |
Oops, something went wrong.