2017-08-02 17:27:17 +00:00
|
|
|
/*
|
2017-11-21 17:24:43 +00:00
|
|
|
Copyright 2017 the Heptio Ark contributors.
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-11-21 17:24:43 +00:00
|
|
|
package restore
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
import (
|
2018-07-27 08:53:05 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-11-21 17:24:43 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-07-27 08:53:05 +00:00
|
|
|
corev1api "k8s.io/api/core/v1"
|
2019-01-07 22:07:53 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2017-08-02 17:27:17 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2019-01-07 22:07:53 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-08-02 17:27:17 +00:00
|
|
|
)
|
|
|
|
|
2018-07-27 08:53:05 +00:00
|
|
|
const annotationLastAppliedConfig = "kubectl.kubernetes.io/last-applied-configuration"
|
|
|
|
|
2017-11-21 17:24:43 +00:00
|
|
|
type serviceAction struct {
|
|
|
|
log logrus.FieldLogger
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
func NewServiceAction(logger logrus.FieldLogger) ItemAction {
|
|
|
|
return &serviceAction{log: logger}
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 17:24:43 +00:00
|
|
|
func (a *serviceAction) AppliesTo() (ResourceSelector, error) {
|
|
|
|
return ResourceSelector{
|
|
|
|
IncludedResources: []string{"services"},
|
|
|
|
}, nil
|
|
|
|
}
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-12-05 13:22:04 +00:00
|
|
|
func (a *serviceAction) Execute(input *RestoreItemActionExecuteInput) (*RestoreItemActionExecuteOutput, error) {
|
2019-01-07 22:07:53 +00:00
|
|
|
service := new(corev1api.Service)
|
2018-12-05 13:22:04 +00:00
|
|
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(input.Item.UnstructuredContent(), service); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 22:07:53 +00:00
|
|
|
if service.Spec.ClusterIP != "None" {
|
|
|
|
service.Spec.ClusterIP = ""
|
2018-09-24 19:32:22 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 22:07:53 +00:00
|
|
|
if err := deleteNodePorts(service); err != nil {
|
2018-12-05 13:22:04 +00:00
|
|
|
return nil, err
|
2018-07-27 08:53:05 +00:00
|
|
|
}
|
2019-01-07 22:07:53 +00:00
|
|
|
|
|
|
|
res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(service)
|
|
|
|
if err != nil {
|
2018-12-05 13:22:04 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-01-07 22:07:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:22:04 +00:00
|
|
|
return NewRestoreItemActionExecuteOutput(&unstructured.Unstructured{Object: res}), nil
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
2018-07-27 08:53:05 +00:00
|
|
|
|
2019-01-07 22:07:53 +00:00
|
|
|
func deleteNodePorts(service *corev1api.Service) error {
|
|
|
|
if service.Spec.Type == corev1api.ServiceTypeExternalName {
|
2018-09-30 18:43:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 22:07:53 +00:00
|
|
|
// 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.
|
|
|
|
explicitNodePorts := sets.NewString()
|
|
|
|
lastAppliedConfig, ok := service.Annotations[annotationLastAppliedConfig]
|
|
|
|
if ok {
|
|
|
|
appliedService := new(corev1api.Service)
|
|
|
|
if err := json.Unmarshal([]byte(lastAppliedConfig), appliedService); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2018-09-25 12:38:57 +00:00
|
|
|
|
2019-01-07 22:07:53 +00:00
|
|
|
for _, port := range appliedService.Spec.Ports {
|
|
|
|
if port.NodePort > 0 {
|
|
|
|
explicitNodePorts.Insert(port.Name)
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 12:38:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 22:07:53 +00:00
|
|
|
for i, port := range service.Spec.Ports {
|
|
|
|
if !explicitNodePorts.Has(port.Name) {
|
|
|
|
service.Spec.Ports[i].NodePort = 0
|
2018-09-25 12:38:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-07 22:07:53 +00:00
|
|
|
|
2018-09-25 12:38:57 +00:00
|
|
|
return nil
|
|
|
|
}
|