Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #742 from irfanurrehman/federate-fixes
Browse files Browse the repository at this point in the history
Kubefed2 Federate fixes
  • Loading branch information
k8s-ci-robot authored Apr 10, 2019
2 parents 022976e + 42bfb4e commit 0d12bc3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
4 changes: 2 additions & 2 deletions pkg/kubefed2/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubefed2/enable/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down
52 changes: 30 additions & 22 deletions pkg/kubefed2/federate/federate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,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
)

Expand Down Expand Up @@ -107,12 +107,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)
}
},
}
Expand Down Expand Up @@ -239,32 +239,40 @@ func getTargetResource(hostConfig *rest.Config, typeConfig typeconfig.Interface,
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.
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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/kubefed2/federate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ 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)
unstructured.RemoveNestedField(resource.Object, "spec", "template", "metadata", field)
}
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) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubefed2/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubefed2/unjoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down

0 comments on commit 0d12bc3

Please sign in to comment.