-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add remaining BGP configuration types
- Loading branch information
Rob Brockbank
committed
Jul 5, 2017
1 parent
bbbeb8a
commit 2704959
Showing
18 changed files
with
924 additions
and
207 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,97 @@ | ||
// Copyright (c) 2017 Tigera, Inc. All rights reserved. | ||
|
||
// 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 resources | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/projectcalico/libcalico-go/lib/backend/k8s/thirdparty" | ||
"github.com/projectcalico/libcalico-go/lib/backend/model" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
const ( | ||
GlobalBgpConfigResourceName = "GlobalBgpConfigs" | ||
GlobalBgpConfigTPRName = "global-bgp-config.projectcalico.org" | ||
) | ||
|
||
func NewGlobalBGPConfigClient(c *kubernetes.Clientset, r *rest.RESTClient) K8sResourceClient { | ||
return &customK8sResourceClient{ | ||
clientSet: c, | ||
restClient: r, | ||
name: GlobalBgpConfigTPRName, | ||
resource: GlobalBgpConfigResourceName, | ||
description: "Calico Global Configuration", | ||
k8sResourceType: reflect.TypeOf(thirdparty.GlobalBgpConfig{}), | ||
k8sListType: reflect.TypeOf(thirdparty.GlobalBgpConfigList{}), | ||
converter: GlobalBgpConfigConverter{}, | ||
} | ||
} | ||
|
||
// GlobalBgpConfigConverter implements the K8sResourceConverter interface. | ||
type GlobalBgpConfigConverter struct { | ||
} | ||
|
||
func (_ GlobalBgpConfigConverter) ListInterfaceToKey(l model.ListInterface) model.Key { | ||
pl := l.(model.GlobalBGPConfigListOptions) | ||
if pl.Name != "" { | ||
return model.GlobalBGPConfigKey{Name: pl.Name} | ||
} | ||
return nil | ||
} | ||
|
||
func (_ GlobalBgpConfigConverter) KeyToName(k model.Key) (string, error) { | ||
return strings.ToLower(k.(model.GlobalBGPConfigKey).Name), nil | ||
} | ||
|
||
func (_ GlobalBgpConfigConverter) NameToKey(name string) (model.Key, error) { | ||
return nil, fmt.Errorf("Mapping of Name to Key is not possible for global config") | ||
} | ||
|
||
func (c GlobalBgpConfigConverter) ToKVPair(r CustomK8sResource) (*model.KVPair, error) { | ||
t := r.(*thirdparty.GlobalBgpConfig) | ||
return &model.KVPair{ | ||
Key: model.GlobalBGPConfigKey{ | ||
Name: t.Spec.Name, | ||
}, | ||
Value: t.Spec.Value, | ||
Revision: t.Metadata.ResourceVersion, | ||
}, nil | ||
} | ||
|
||
func (c GlobalBgpConfigConverter) FromKVPair(kvp *model.KVPair) (CustomK8sResource, error) { | ||
name, err := c.KeyToName(kvp.Key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tpr := thirdparty.GlobalBgpConfig{ | ||
Metadata: metav1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
Spec: thirdparty.GlobalBgpConfigSpec{ | ||
Name: kvp.Key.(model.GlobalBGPConfigKey).Name, | ||
Value: kvp.Value.(string), | ||
}, | ||
} | ||
if kvp.Revision != nil { | ||
tpr.Metadata.ResourceVersion = kvp.Revision.(string) | ||
} | ||
return &tpr, 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,98 @@ | ||
// Copyright (c) 2017 Tigera, Inc. All rights reserved. | ||
|
||
// 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 resources_test | ||
|
||
import ( | ||
"github.com/projectcalico/libcalico-go/lib/backend/k8s/resources" | ||
"github.com/projectcalico/libcalico-go/lib/backend/k8s/thirdparty" | ||
"github.com/projectcalico/libcalico-go/lib/backend/model" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Global BGP config conversion methods", func() { | ||
|
||
converter := resources.GlobalBgpConfigConverter{} | ||
|
||
// Define some useful test data. | ||
listIncomplete := model.GlobalBGPConfigListOptions{} | ||
|
||
// Compatible set of list, key and name (used for Key to Name conversion) | ||
list1 := model.GlobalBGPConfigListOptions{ | ||
Name: "AbCd", | ||
} | ||
key1 := model.GlobalBGPConfigKey{ | ||
Name: "AbCd", | ||
} | ||
name1 := "abcd" | ||
|
||
// Compatible set of KVPair and Kubernetes Resource. | ||
value1 := "test" | ||
kvp1 := &model.KVPair{ | ||
Key: key1, | ||
Value: &value1, | ||
Revision: "rv", | ||
} | ||
res1 := &thirdparty.GlobalBgpConfig{ | ||
Metadata: metav1.ObjectMeta{ | ||
Name: name1, | ||
ResourceVersion: "rv", | ||
}, | ||
Spec: thirdparty.GlobalBgpConfigSpec{ | ||
Name: key1.Name, | ||
Value: value1, | ||
}, | ||
} | ||
|
||
It("should convert an incomplete ListInterface to no Key", func() { | ||
Expect(converter.ListInterfaceToKey(listIncomplete)).To(BeNil()) | ||
}) | ||
|
||
It("should convert a qualified ListInterface to the equivalent Key", func() { | ||
Expect(converter.ListInterfaceToKey(list1)).To(Equal(key1)) | ||
}) | ||
|
||
It("should convert a Key to the equivalent resource name", func() { | ||
n, err := converter.KeyToName(key1) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(n).To(Equal(name1)) | ||
}) | ||
|
||
It("should not convert a resource name to the equivalent Key - this is not possible due to case switching", func() { | ||
_, err := converter.NameToKey("test") | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
It("should convert between a KVPair and the equivalent Kubernetes resource", func() { | ||
r, err := converter.FromKVPair(kvp1) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(r.GetObjectMeta().GetName()).To(Equal(res1.Metadata.Name)) | ||
Expect(r.GetObjectMeta().GetResourceVersion()).To(Equal(res1.Metadata.ResourceVersion)) | ||
Expect(r).To(BeAssignableToTypeOf(&thirdparty.GlobalBgpConfig{})) | ||
Expect(r.(*thirdparty.GlobalBgpConfig).Spec).To(Equal(res1.Spec)) | ||
}) | ||
|
||
It("should convert between a Kuberenetes resource and the equivalent KVPair", func() { | ||
kvp, err := converter.ToKVPair(res1) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(kvp.Key).To(Equal(kvp1.Key)) | ||
Expect(kvp.Revision).To(Equal(kvp1.Revision)) | ||
Expect(kvp.Value).To(BeAssignableToTypeOf(&value1)) | ||
Expect(kvp.Value).To(Equal(kvp1.Value)) | ||
}) | ||
}) |
Oops, something went wrong.