Files
velero/pkg/builder/container_builder.go
Michal Pryc 4b6708de2c
Some checks failed
Run the E2E test on kind / get-go-version (push) Failing after 1m5s
Run the E2E test on kind / build (push) Has been skipped
Run the E2E test on kind / setup-test-matrix (push) Successful in 4s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / get-go-version (push) Successful in 18s
Main CI / Build (push) Failing after 40s
Close stale issues and PRs / stale (push) Successful in 11s
Trivy Nightly Scan / Trivy nightly scan (velero, main) (push) Failing after 1m45s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-aws, main) (push) Failing after 1m14s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-gcp, main) (push) Failing after 1m7s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-microsoft-azure, main) (push) Failing after 1m6s
Fix plugin init container names exceeding DNS-1123 limit (#9445)
Ensure plugin init container names satisfy DNS-1123 label constraints
(max 63 chars). Long names are truncated with an 8-char hash suffix to
maintain uniqueness.

Fixes: #9444

Signed-off-by: Michal Pryc <mpryc@redhat.com>
2025-12-19 13:39:27 -05:00

157 lines
4.6 KiB
Go

/*
Copyright 2019 the Velero contributors.
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 builder
import (
"encoding/json"
"strings"
corev1api "k8s.io/api/core/v1"
apimachineryRuntime "k8s.io/apimachinery/pkg/runtime"
"github.com/vmware-tanzu/velero/pkg/label"
)
// ContainerBuilder builds Container objects
type ContainerBuilder struct {
object *corev1api.Container
}
// ForContainer is the constructor for ContainerBuilder.
func ForContainer(name, image string) *ContainerBuilder {
return &ContainerBuilder{
object: &corev1api.Container{
Name: name,
Image: image,
},
}
}
// ForPluginContainer is a helper builder specifically for plugin init containers
func ForPluginContainer(image string, pullPolicy corev1api.PullPolicy) *ContainerBuilder {
volumeMount := ForVolumeMount("plugins", "/target").Result()
return ForContainer(getName(image), image).PullPolicy(pullPolicy).VolumeMounts(volumeMount)
}
// getName returns the 'name' component of a docker image that includes the entire string
// except the registry name, and transforms the combined string into a DNS-1123 compatible name
// that fits within the 63-character limit for Kubernetes container names.
func getName(image string) string {
slashIndex := strings.Index(image, "/")
slashCount := 0
if slashIndex >= 0 {
slashCount = strings.Count(image[slashIndex:], "/")
}
start := 0
if slashCount > 1 || slashIndex == 0 {
// always start after the first slash when there is a registry name
// or if the string starts with a slash.
start = slashIndex + 1
}
// If the image spec is by digest, remove the digest.
// If it is by tag, remove the tag.
// Otherwise (implicit :latest) leave it alone.
end := len(image)
atIndex := strings.LastIndex(image, "@")
if atIndex > 0 {
end = atIndex
} else {
colonIndex := strings.LastIndex(image, ":")
if colonIndex > 0 {
end = colonIndex
}
}
// https://github.com/distribution/distribution/blob/main/docs/spec/api.md#overview
// valid repository names match the regex [a-z0-9]+(?:[._-][a-z0-9]+)*
// image repository names can container [._] but [._] are not allowed in RFC-1123 labels.
// replace '/', '_' and '.' with '-'
re := strings.NewReplacer("/", "-",
"_", "-",
".", "-")
name := re.Replace(image[start:end])
// Ensure the name doesn't exceed Kubernetes container name length limit
return label.GetValidName(name)
}
// Result returns the built Container.
func (b *ContainerBuilder) Result() *corev1api.Container {
return b.object
}
// ResultRawExtension returns the Container as runtime.RawExtension.
func (b *ContainerBuilder) ResultRawExtension() apimachineryRuntime.RawExtension {
result, err := json.Marshal(b.object)
if err != nil {
return apimachineryRuntime.RawExtension{}
}
return apimachineryRuntime.RawExtension{
Raw: result,
}
}
// Args sets the container's Args.
func (b *ContainerBuilder) Args(args ...string) *ContainerBuilder {
b.object.Args = append(b.object.Args, args...)
return b
}
// VolumeMounts sets the container's VolumeMounts.
func (b *ContainerBuilder) VolumeMounts(volumeMounts ...*corev1api.VolumeMount) *ContainerBuilder {
for _, v := range volumeMounts {
b.object.VolumeMounts = append(b.object.VolumeMounts, *v)
}
return b
}
// Resources sets the container's Resources.
func (b *ContainerBuilder) Resources(resources *corev1api.ResourceRequirements) *ContainerBuilder {
b.object.Resources = *resources
return b
}
// SecurityContext sets the container's SecurityContext.
func (b *ContainerBuilder) SecurityContext(securityContext *corev1api.SecurityContext) *ContainerBuilder {
b.object.SecurityContext = securityContext
return b
}
func (b *ContainerBuilder) Env(vars ...*corev1api.EnvVar) *ContainerBuilder {
for _, v := range vars {
b.object.Env = append(b.object.Env, *v)
}
return b
}
func (b *ContainerBuilder) PullPolicy(pullPolicy corev1api.PullPolicy) *ContainerBuilder {
b.object.ImagePullPolicy = pullPolicy
return b
}
func (b *ContainerBuilder) Command(command []string) *ContainerBuilder {
if b.object.Command == nil {
b.object.Command = []string{}
}
b.object.Command = append(b.object.Command, command...)
return b
}