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"
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2017-08-02 17:27:17 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
|
|
|
api "github.com/heptio/ark/pkg/apis/ark/v1"
|
|
|
|
"github.com/heptio/ark/pkg/util/collections"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2017-11-21 17:24:43 +00:00
|
|
|
func (a *serviceAction) Execute(obj runtime.Unstructured, restore *api.Restore) (runtime.Unstructured, error, error) {
|
2017-08-02 17:27:17 +00:00
|
|
|
spec, err := collections.GetMap(obj.UnstructuredContent(), "spec")
|
|
|
|
if err != nil {
|
2017-08-18 22:11:42 +00:00
|
|
|
return nil, nil, err
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 00:20:08 +00:00
|
|
|
// Since clusterIP is an optional key, we can ignore 'not found' errors. Also assuming it was a string already.
|
|
|
|
if val, _ := collections.GetString(spec, "clusterIP"); val != "None" {
|
|
|
|
delete(spec, "clusterIP")
|
|
|
|
}
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-07-27 08:53:05 +00:00
|
|
|
preservedPorts, err := getPreservedPorts(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-02 17:27:17 +00:00
|
|
|
ports, err := collections.GetSlice(obj.UnstructuredContent(), "spec.ports")
|
|
|
|
if err != nil {
|
2017-08-18 22:11:42 +00:00
|
|
|
return nil, nil, err
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range ports {
|
|
|
|
p := port.(map[string]interface{})
|
2018-07-27 08:53:05 +00:00
|
|
|
var name string
|
|
|
|
if nameVal, ok := p["name"]; ok {
|
|
|
|
name = nameVal.(string)
|
|
|
|
}
|
|
|
|
if preservedPorts[name] {
|
|
|
|
continue
|
|
|
|
}
|
2017-08-02 17:27:17 +00:00
|
|
|
delete(p, "nodePort")
|
|
|
|
}
|
|
|
|
|
2017-08-18 22:11:42 +00:00
|
|
|
return obj, nil, nil
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
2018-07-27 08:53:05 +00:00
|
|
|
|
|
|
|
func getPreservedPorts(obj runtime.Unstructured) (map[string]bool, error) {
|
|
|
|
preservedPorts := map[string]bool{}
|
|
|
|
metadata, err := meta.Accessor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
if lac, ok := metadata.GetAnnotations()[annotationLastAppliedConfig]; ok {
|
|
|
|
var svc corev1api.Service
|
|
|
|
if err := json.Unmarshal([]byte(lac), &svc); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
for _, port := range svc.Spec.Ports {
|
|
|
|
if port.NodePort > 0 {
|
|
|
|
preservedPorts[port.Name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return preservedPorts, nil
|
|
|
|
}
|