Files
velero/pkg/restore/actions/service_action.go
T
214b63f6f7
Run the E2E test on kind / setup-test-matrix (push) Failing after 2s
Scorecard supply-chain security / Scorecard analysis (push) Skipped
e2e-test-kind.yaml / extract (push) Failing after 6s
Run the E2E test on kind / get-go-version (push) Failing after 7s
Run the E2E test on kind / build (push) Skipped
Run the E2E test on kind / run-e2e-test (push) Skipped
push.yml / extract (push) Failing after 6s
Main CI / get-go-version (push) Failing after 7s
Main CI / Build (push) Skipped
Do not assume a port name is a string when clearing node ports (#10132)
* Do not assume a port name is a string when clearing node ports

deleteNodePorts reads the last-applied-configuration annotation, which
is free-form JSON controlled by whoever produced the backup, and cast
p["name"] to string without checking. A port whose name is a number
crashed the restore of that Service with an interface conversion panic.
Every sibling in the same loop already uses the comma-ok form.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>

* Add changelog file

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>

* Convert name to string by Sprint.

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

---------

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>
Co-authored-by: Xun Jiang <xun.jiang@broadcom.com>
2026-09-15 09:50:10 -04:00

258 lines
7.1 KiB
Go

/*
Copyright 2017 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 actions
import (
"encoding/json"
"fmt"
"strconv"
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
corev1api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
"github.com/vmware-tanzu/velero/pkg/util/boolptr"
)
const annotationLastAppliedConfig = "kubectl.kubernetes.io/last-applied-configuration"
type ServiceAction struct {
log logrus.FieldLogger
}
func NewServiceAction(logger logrus.FieldLogger) *ServiceAction {
return &ServiceAction{log: logger}
}
func (a *ServiceAction) AppliesTo() (velero.ResourceSelector, error) {
return velero.ResourceSelector{
IncludedResources: []string{"services"},
}, nil
}
func (a *ServiceAction) Execute(input *velero.RestoreItemActionExecuteInput) (*velero.RestoreItemActionExecuteOutput, error) {
service := new(corev1api.Service)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(input.Item.UnstructuredContent(), service); err != nil {
return nil, errors.WithStack(err)
}
if service.Spec.ClusterIP != "None" {
service.Spec.ClusterIP = ""
service.Spec.ClusterIPs = nil
}
/* Do not delete NodePorts if restore triggered with "--preserve-nodeports" flag */
if boolptr.IsSetToTrue(input.Restore.Spec.PreserveNodePorts) {
a.log.Info("Restoring Services with original NodePort(s)")
} else {
if err := deleteNodePorts(service); err != nil {
return nil, err
}
if err := deleteHealthCheckNodePort(service); err != nil {
return nil, err
}
}
res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(service)
if err != nil {
return nil, errors.WithStack(err)
}
return velero.NewRestoreItemActionExecuteOutput(&unstructured.Unstructured{Object: res}), nil
}
func deleteHealthCheckNodePort(service *corev1api.Service) error {
// Check service type and external traffic policy setting,
// if the setting is not applicable for HealthCheckNodePort, return early.
if service.Spec.ExternalTrafficPolicy != corev1api.ServiceExternalTrafficPolicyTypeLocal ||
service.Spec.Type != corev1api.ServiceTypeLoadBalancer {
return nil
}
// HealthCheckNodePort is already 0, return.
if service.Spec.HealthCheckNodePort == 0 {
return nil
}
// Search HealthCheckNodePort from server's last-applied-configuration
// annotation(HealthCheckNodePort is specified by `kubectl apply` command)
if lastAppliedConfig, ok := service.Annotations[annotationLastAppliedConfig]; ok {
var appliedConfig struct {
Spec struct {
HealthCheckNodePort *int32 `json:"healthCheckNodePort"`
} `json:"spec"`
}
if err := json.Unmarshal([]byte(lastAppliedConfig), &appliedConfig); err != nil {
return errors.WithStack(err)
}
if appliedConfig.Spec.HealthCheckNodePort != nil && *appliedConfig.Spec.HealthCheckNodePort != 0 {
return nil
}
}
// Search HealthCheckNodePort from ManagedFields(HealthCheckNodePort
// is specified by `kubectl apply --server-side` command).
for _, entry := range service.GetManagedFields() {
if entry.FieldsV1 == nil {
continue
}
fields := new(map[string]any)
if err := json.Unmarshal(entry.FieldsV1.GetRawBytes(), fields); err != nil {
return errors.WithStack(err)
}
_, exist, err := unstructured.NestedMap(*fields, "f:spec", "f:healthCheckNodePort")
if err != nil {
return errors.WithStack(err)
}
if !exist {
continue
}
// Because the format in ManagedFields is `f:healthCheckNodePort: {}`,
// cannot get the value, check whether exists is enough.
// Found healthCheckNodePort in ManagedFields.
// No need to delete. Return.
return nil
}
// Cannot find HealthCheckNodePort from Annotation and
// ManagedFields, which means it's auto-generated. Delete it.
service.Spec.HealthCheckNodePort = 0
return nil
}
func deleteNodePorts(service *corev1api.Service) error {
if service.Spec.Type == corev1api.ServiceTypeExternalName {
return nil
}
// find any NodePorts whose values were explicitly specified according
// to the last-applied-config annotation. We'll retain these values, and
// clear out any other (presumably auto-assigned) NodePort values.
lastAppliedConfig, ok := service.Annotations[annotationLastAppliedConfig]
if ok {
explicitNodePorts := sets.NewString()
unnamedPortInts := sets.NewInt()
appliedServiceUnstructured := new(map[string]any)
if err := json.Unmarshal([]byte(lastAppliedConfig), appliedServiceUnstructured); err != nil {
return errors.WithStack(err)
}
ports, bool, err := unstructured.NestedSlice(*appliedServiceUnstructured, "spec", "ports")
if err != nil {
return errors.WithStack(err)
}
if bool {
for _, port := range ports {
p, ok := port.(map[string]any)
if !ok {
continue
}
nodePort, nodePortBool, err := unstructured.NestedFieldNoCopy(p, "nodePort")
if err != nil {
return errors.WithStack(err)
}
if nodePortBool {
nodePortInt := 0
switch nodePort := nodePort.(type) {
case int32:
nodePortInt = int(nodePort)
case float64:
nodePortInt = int(nodePort)
case string:
nodePortInt, err = strconv.Atoi(nodePort)
if err != nil {
return errors.WithStack(err)
}
}
if nodePortInt > 0 {
portName, ok := p["name"]
if !ok {
// unnamed port
unnamedPortInts.Insert(nodePortInt)
} else {
explicitNodePorts.Insert(fmt.Sprint(portName))
}
}
}
}
}
for i, port := range service.Spec.Ports {
if port.Name != "" {
if !explicitNodePorts.Has(port.Name) {
service.Spec.Ports[i].NodePort = 0
}
} else {
if !unnamedPortInts.Has(int(port.NodePort)) {
service.Spec.Ports[i].NodePort = 0
}
}
}
return nil
}
explicitNodePorts := sets.NewString()
for _, entry := range service.GetManagedFields() {
if entry.FieldsV1 == nil {
continue
}
fields := new(map[string]any)
if err := json.Unmarshal(entry.FieldsV1.GetRawBytes(), fields); err != nil {
return errors.WithStack(err)
}
ports, exist, err := unstructured.NestedMap(*fields, "f:spec", "f:ports")
if err != nil {
return errors.WithStack(err)
}
if !exist {
continue
}
for key, port := range ports {
p, ok := port.(map[string]any)
if !ok {
continue
}
if _, exist := p["f:nodePort"]; exist {
explicitNodePorts.Insert(key)
}
}
}
for i, port := range service.Spec.Ports {
k := portKey(port)
if !explicitNodePorts.Has(k) {
service.Spec.Ports[i].NodePort = 0
}
}
return nil
}
func portKey(port corev1api.ServicePort) string {
return fmt.Sprintf(`k:{"port":%d,"protocol":"%s"}`, port.Port, port.Protocol)
}