From e7f4276e6805b6ab1124feb37bf96791cb55e7a1 Mon Sep 17 00:00:00 2001 From: Irfan Ur Rehman Date: Wed, 10 Apr 2019 22:43:26 +0530 Subject: [PATCH 1/4] Error message starts with a capital letter --- pkg/kubefed2/disable.go | 4 ++-- pkg/kubefed2/enable/enable.go | 4 ++-- pkg/kubefed2/federate/federate.go | 4 ++-- pkg/kubefed2/join.go | 4 ++-- pkg/kubefed2/unjoin.go | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/kubefed2/disable.go b/pkg/kubefed2/disable.go index d0fe78fad0..85bbc1cff5 100644 --- a/pkg/kubefed2/disable.go +++ b/pkg/kubefed2/disable.go @@ -89,12 +89,12 @@ func NewCmdTypeDisable(cmdOut io.Writer, config util.FedConfig) *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := opts.Complete(args) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } err = opts.Run(cmdOut, config) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } }, } diff --git a/pkg/kubefed2/enable/enable.go b/pkg/kubefed2/enable/enable.go index a36d345809..a1a902e11e 100644 --- a/pkg/kubefed2/enable/enable.go +++ b/pkg/kubefed2/enable/enable.go @@ -102,12 +102,12 @@ func NewCmdTypeEnable(cmdOut io.Writer, config util.FedConfig) *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := opts.Complete(args) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } err = opts.Run(cmdOut, config) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } }, } diff --git a/pkg/kubefed2/federate/federate.go b/pkg/kubefed2/federate/federate.go index 96b36e2a9f..ec6bffd532 100644 --- a/pkg/kubefed2/federate/federate.go +++ b/pkg/kubefed2/federate/federate.go @@ -106,12 +106,12 @@ func NewCmdFederateResource(cmdOut io.Writer, config util.FedConfig) *cobra.Comm Run: func(cmd *cobra.Command, args []string) { err := opts.Complete(args) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } err = opts.Run(cmdOut, config) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } }, } diff --git a/pkg/kubefed2/join.go b/pkg/kubefed2/join.go index d2cba3cf1e..1cddf3cae2 100644 --- a/pkg/kubefed2/join.go +++ b/pkg/kubefed2/join.go @@ -125,12 +125,12 @@ func NewCmdJoin(cmdOut io.Writer, config util.FedConfig) *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := opts.Complete(args) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } err = opts.Run(cmdOut, config) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } }, } diff --git a/pkg/kubefed2/unjoin.go b/pkg/kubefed2/unjoin.go index 8eee601f74..a06bf2fa19 100644 --- a/pkg/kubefed2/unjoin.go +++ b/pkg/kubefed2/unjoin.go @@ -93,12 +93,12 @@ func NewCmdUnjoin(cmdOut io.Writer, config util.FedConfig) *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := opts.Complete(args) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } err = opts.Run(cmdOut, config) if err != nil { - glog.Fatalf("error: %v", err) + glog.Fatalf("Error: %v", err) } }, } From 057f116a079cfeca00bbf709cdea7534e1225a61 Mon Sep 17 00:00:00 2001 From: Irfan Ur Rehman Date: Wed, 10 Apr 2019 23:12:55 +0530 Subject: [PATCH 2/4] Ensure unnecessary fields are not populated in template --- pkg/kubefed2/federate/federate.go | 41 ++++++++++++++++++------------- pkg/kubefed2/federate/util.go | 4 ++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/pkg/kubefed2/federate/federate.go b/pkg/kubefed2/federate/federate.go index ec6bffd532..71c23e86a1 100644 --- a/pkg/kubefed2/federate/federate.go +++ b/pkg/kubefed2/federate/federate.go @@ -229,28 +229,35 @@ func FederatedResourceFromTargetResource(typeConfig typeconfig.Interface, target fedAPIResource := typeConfig.GetFederatedType() // Special handling is needed for some controller set fields. - if typeConfig.GetTarget().Kind == ctlutil.ServiceAccountKind { - unstructured.RemoveNestedField(targetResource.Object, ctlutil.SecretsField) - } - - if typeConfig.GetTarget().Kind == ctlutil.ServiceKind { - var targetPorts []interface{} - targetPorts, ok, err := unstructured.NestedSlice(targetResource.Object, "spec", "ports") - if err != nil { - return nil, err + switch typeConfig.GetTarget().Kind { + case ctlutil.NamespaceKind: + { + unstructured.RemoveNestedField(targetResource.Object, "spec", "finalizers") } - if ok { - for index := range targetPorts { - port := targetPorts[index].(map[string]interface{}) - delete(port, "nodePort") - targetPorts[index] = port - } - err := unstructured.SetNestedSlice(targetResource.Object, targetPorts, "spec", "ports") + case ctlutil.ServiceAccountKind: + { + unstructured.RemoveNestedField(targetResource.Object, ctlutil.SecretsField) + } + case ctlutil.ServiceKind: + { + var targetPorts []interface{} + targetPorts, ok, err := unstructured.NestedSlice(targetResource.Object, "spec", "ports") if err != nil { return nil, err } + if ok { + for index := range targetPorts { + port := targetPorts[index].(map[string]interface{}) + delete(port, "nodePort") + targetPorts[index] = port + } + err := unstructured.SetNestedSlice(targetResource.Object, targetPorts, "spec", "ports") + if err != nil { + return nil, err + } + } + unstructured.RemoveNestedField(targetResource.Object, "spec", "clusterIP") } - unstructured.RemoveNestedField(targetResource.Object, "spec", "clusterIP") } qualifiedName := ctlutil.NewQualifiedName(targetResource) diff --git a/pkg/kubefed2/federate/util.go b/pkg/kubefed2/federate/util.go index 09129d4de2..f46a156e3c 100644 --- a/pkg/kubefed2/federate/util.go +++ b/pkg/kubefed2/federate/util.go @@ -25,7 +25,6 @@ import ( var systemMetadataFields = []string{"selfLink", "uid", "resourceVersion", "generation", "creationTimestamp", "deletionTimestamp", "deletionGracePeriodSeconds"} func RemoveUnwantedFields(resource *unstructured.Unstructured) { - unstructured.RemoveNestedField(resource.Object, "status") for _, field := range systemMetadataFields { unstructured.RemoveNestedField(resource.Object, "metadata", field) // For resources with pod template subresource (jobs, deployments, replicasets) @@ -33,6 +32,9 @@ func RemoveUnwantedFields(resource *unstructured.Unstructured) { } unstructured.RemoveNestedField(resource.Object, "metadata", "name") unstructured.RemoveNestedField(resource.Object, "metadata", "namespace") + unstructured.RemoveNestedField(resource.Object, "apiVersion") + unstructured.RemoveNestedField(resource.Object, "kind") + unstructured.RemoveNestedField(resource.Object, "status") } func SetBasicMetaFields(resource *unstructured.Unstructured, apiResource metav1.APIResource, name, namespace, generateName string) { From 23af8d555c161ee23fa29df38c2a778488127694 Mon Sep 17 00:00:00 2001 From: Irfan Ur Rehman Date: Wed, 10 Apr 2019 23:24:55 +0530 Subject: [PATCH 3/4] Do not modify passed target object in FederateResource --- pkg/kubefed2/federate/federate.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/kubefed2/federate/federate.go b/pkg/kubefed2/federate/federate.go index 71c23e86a1..3691511f18 100644 --- a/pkg/kubefed2/federate/federate.go +++ b/pkg/kubefed2/federate/federate.go @@ -225,8 +225,9 @@ func getTargetResource(hostConfig *rest.Config, typeConfig *fedv1a1.FederatedTyp return resource, nil } -func FederatedResourceFromTargetResource(typeConfig typeconfig.Interface, targetResource *unstructured.Unstructured) (*unstructured.Unstructured, error) { +func FederatedResourceFromTargetResource(typeConfig typeconfig.Interface, resource *unstructured.Unstructured) (*unstructured.Unstructured, error) { fedAPIResource := typeConfig.GetFederatedType() + targetResource := resource.DeepCopy() // Special handling is needed for some controller set fields. switch typeConfig.GetTarget().Kind { From 42bfb4e7e95e2d8c1d7c93dc2124cc192a1ad5c2 Mon Sep 17 00:00:00 2001 From: Irfan Ur Rehman Date: Wed, 10 Apr 2019 23:28:20 +0530 Subject: [PATCH 4/4] Fix example message for federate resource --- pkg/kubefed2/federate/federate.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/kubefed2/federate/federate.go b/pkg/kubefed2/federate/federate.go index 3691511f18..a292276a66 100644 --- a/pkg/kubefed2/federate/federate.go +++ b/pkg/kubefed2/federate/federate.go @@ -53,8 +53,8 @@ var ( flag otherwise.` federate_example = ` - # Federate resource named "my-dep" in namespace "my-ns" of kubernetes type "deploy" - kubefed2 federate deploy my-dep -n "my-ns" --host-cluster-context=cluster1` + # Federate resource named "my-cm" in namespace "my-ns" of kubernetes type "configmaps" (identified by short name "cm") + kubefed2 federate cm "my-cm" -n "my-ns" --host-cluster-context=cluster1` // TODO(irfanurrehman): implement —contents flag applicable to namespaces )