enable.go 2.9 KB
Newer Older
Z
zryfish 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/*
Copyright 2019 The Kubernetes Authors.

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 orphaning

import (
	"io"

	"github.com/pkg/errors"
	"github.com/spf13/cobra"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/klog"

	ctlutil "sigs.k8s.io/kubefed/pkg/controller/util"
	"sigs.k8s.io/kubefed/pkg/kubefedctl/util"
)

var (
	orphaning_enable_long = `
		Prevents the removal of managed resources from member clusters when their managing federated 
		resource is removed. This is accomplished by adding 'kubefed.io/orphan: true' as an annotation to the 
		federated resource.

		Current context is assumed to be a Kubernetes cluster hosting
		the kubefed control plane. Please use the
		--host-cluster-context flag otherwise.`

	orphan_enable_example = `
		# Enable the orphaning mode for a federated resource of type FederatedDeployment and named foo 
		kubefedctl orphaning enable FederatedDeployment foo --host-cluster-context=cluster1`
)

// newCmdEnableOrphaning adds 'kubefed.io/orphan: true' as an annotation to the federated resource
func newCmdEnableOrphaning(cmdOut io.Writer, config util.FedConfig) *cobra.Command {
	opts := &orphanResource{}
	cmd := &cobra.Command{
		Use:     "enable <resource type> <resource name>",
		Short:   "Enable the orphaning (i.e. retention) of resources managed by a federated resource upon its removal.",
		Long:    orphaning_enable_long,
		Example: orphan_enable_example,
		Run: func(cmd *cobra.Command, args []string) {
			err := opts.Complete(args, config)
			if err != nil {
				klog.Fatalf("Error: %v", err)
			}

			err = opts.RunEnable(cmdOut, config)
			if err != nil {
				klog.Fatalf("Error: %v", err)
			}
		},
	}

	flags := cmd.Flags()
	opts.GlobalSubcommandBind(flags)
	err := opts.Bind(flags)
	if err != nil {
		klog.Fatalf("Error: %v", err)
	}

	return cmd
}

// RunEnable implements the `enable` command.
func (o *orphanResource) RunEnable(cmdOut io.Writer, config util.FedConfig) error {
	resourceClient, err := o.GetResourceClient(config, cmdOut)
	if err != nil {
		return err
	}
	fedResource, err := o.GetFederatedResource(resourceClient)
	if err != nil {
		return err
	}
	if ctlutil.IsOrphaningEnabled(fedResource) {
		return nil
	}
	ctlutil.EnableOrphaning(fedResource)
	_, err = resourceClient.Update(fedResource, metav1.UpdateOptions{})
	if err != nil {
		return errors.Wrapf(err, "Failed to update resource %s %q", fedResource.GetKind(),
			ctlutil.QualifiedName{Name: fedResource.GetName(), Namespace: fedResource.GetNamespace()})
	}

	return nil
}