forked from vmware-tanzu/velero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vmware-tanzu#4028 from zubron/add-restore-item-act…
…ion-to-skip-automanaged-apiservices Skip restore of APIServices managed by Kubernetes
- Loading branch information
Showing
7 changed files
with
136 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a RestoreItemAction plugin (`velero.io/apiservice`) which skips the restore of any `APIService` which is managed by Kubernetes. These are identified using the `kube-aggregator.kubernetes.io/automanaged` label. |
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,51 @@ | ||
/* | ||
Copyright 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 restore | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/kube-aggregator/pkg/controllers/autoregister" | ||
|
||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
) | ||
|
||
type APIServiceAction struct { | ||
logger logrus.FieldLogger | ||
} | ||
|
||
// NewAPIServiceAction returns an APIServiceAction which is a RestoreItemAction plugin | ||
// that will skip the restore of any APIServices which are managed by Kubernetes. This | ||
// is determined by looking for the "kube-aggregator.kubernetes.io/automanaged" label on | ||
// the APIService. | ||
func NewAPIServiceAction(logger logrus.FieldLogger) *APIServiceAction { | ||
return &APIServiceAction{logger: logger} | ||
} | ||
|
||
func (a *APIServiceAction) AppliesTo() (velero.ResourceSelector, error) { | ||
return velero.ResourceSelector{ | ||
IncludedResources: []string{"apiservices"}, | ||
LabelSelector: autoregister.AutoRegisterManagedLabel, | ||
}, nil | ||
} | ||
|
||
func (a *APIServiceAction) Execute(input *velero.RestoreItemActionExecuteInput) (*velero.RestoreItemActionExecuteOutput, error) { | ||
a.logger.Info("Executing APIServiceAction") | ||
defer a.logger.Info("Done executing APIServiceAction") | ||
|
||
a.logger.Infof("Skipping restore of APIService as it is managed by Kubernetes") | ||
return velero.NewRestoreItemActionExecuteOutput(input.Item).WithoutRestore(), nil | ||
} |
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,53 @@ | ||
/* | ||
Copyright 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 restore | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1" | ||
|
||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
velerotest "github.com/vmware-tanzu/velero/pkg/test" | ||
) | ||
|
||
func TestAPIServiceActionExecuteSkipsRestore(t *testing.T) { | ||
obj := apiregistrationv1.APIService{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "v1.test.velero.io", | ||
}, | ||
} | ||
|
||
unstructuredAPIService, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&obj) | ||
require.NoError(t, err) | ||
|
||
action := NewAPIServiceAction(velerotest.NewLogger()) | ||
res, err := action.Execute(&velero.RestoreItemActionExecuteInput{ | ||
Item: &unstructured.Unstructured{Object: unstructuredAPIService}, | ||
ItemFromBackup: &unstructured.Unstructured{Object: unstructuredAPIService}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
var apiService apiregistrationv1.APIService | ||
require.NoError(t, runtime.DefaultUnstructuredConverter.FromUnstructured(res.UpdatedItem.UnstructuredContent(), &apiService)) | ||
require.Equal(t, obj, apiService) | ||
require.Equal(t, true, res.SkipRestore) | ||
} |
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