Skip to content

Commit

Permalink
Merge pull request #398 from gunjan5/g5-tweak-names
Browse files Browse the repository at this point in the history
Update for new label format and new profile naming format
  • Loading branch information
robbrockbank authored Oct 23, 2017
2 parents c24a112 + aae17b7 commit 62b1557
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 34 deletions.
2 changes: 1 addition & 1 deletion calico.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func cmdAdd(args *skel.CmdArgs) error {
if wepIDs.Orchestrator == "k8s" {
inboundRules = []api.Rule{{Action: "allow"}}
} else {
inboundRules = []api.Rule{{Action: "allow", Source: api.EntityRule{Tag: conf.Name}}}
inboundRules = []api.Rule{{Action: "allow", Source: api.EntityRule{Selector: fmt.Sprintf("has(%s)", conf.Name)}}}
}

profile := &api.Profile{
Expand Down
85 changes: 65 additions & 20 deletions calico_cni_k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,17 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec).Should(Equal(api.WorkloadEndpointSpec{
Pod: name,
InterfaceName: interfaceName,
IPNetworks: []string{result.IPs[0].Address.String()},
MAC: mac.String(),
Profiles: []string{"k8s_ns.test"},
Profiles: []string{"kns.test"},
Node: hostname,
Endpoint: "eth0",
Workload: "",
Expand Down Expand Up @@ -295,13 +298,16 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))
Expect(endpoints.Items[0].Spec).Should(Equal(api.WorkloadEndpointSpec{
Pod: name,
InterfaceName: interfaceName,
IPNetworks: []string{result.IPs[0].Address.String()},
MAC: mac.String(),
Profiles: []string{"k8s_ns.test"},
Profiles: []string{"kns.test"},
Node: hostname,
Endpoint: "eth0",
Workload: "",
Expand Down Expand Up @@ -510,14 +516,17 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec).Should(Equal(api.WorkloadEndpointSpec{
Pod: name,
InterfaceName: interfaceName,
IPNetworks: []string{assignIP.String() + "/32"},
MAC: mac.String(),
Profiles: []string{"k8s_ns.test"},
Profiles: []string{"kns.test"},
Node: hostname,
Endpoint: "eth0",
Workload: "",
Expand Down Expand Up @@ -617,14 +626,17 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec).Should(Equal(api.WorkloadEndpointSpec{
Pod: name,
InterfaceName: interfaceName,
IPNetworks: []string{assignIP.String() + "/32"},
MAC: mac.String(),
Profiles: []string{"k8s_ns.test"},
Profiles: []string{"kns.test"},
Node: hostname,
Endpoint: "eth0",
Workload: "",
Expand Down Expand Up @@ -722,14 +734,17 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec).Should(Equal(api.WorkloadEndpointSpec{
Pod: name,
InterfaceName: interfaceName,
IPNetworks: []string{assignIP.String() + "/32"},
MAC: mac.String(),
Profiles: []string{"k8s_ns.test"},
Profiles: []string{"kns.test"},
Node: hostname,
Endpoint: "eth0",
Workload: "",
Expand Down Expand Up @@ -916,7 +931,10 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkloadX))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec.ContainerID).Should(Equal(cniContainerIDX))

Expand Down Expand Up @@ -960,7 +978,10 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkloadY))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec.ContainerID).Should(Equal(cniContainerIDY))

Expand All @@ -976,7 +997,10 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkloadY))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec.ContainerID).Should(Equal(cniContainerIDY))

Expand Down Expand Up @@ -1071,7 +1095,10 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkloadX))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec.ContainerID).Should(Equal(cniContainerIDX))

Expand Down Expand Up @@ -1104,7 +1131,10 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkloadY))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec.ContainerID).Should(Equal(cniContainerIDY))

Expand All @@ -1120,7 +1150,10 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkloadY))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec.ContainerID).Should(Equal(cniContainerIDY))

Expand Down Expand Up @@ -1215,7 +1248,10 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec.ContainerID).Should(Equal(containerID))

Expand Down Expand Up @@ -1374,7 +1410,10 @@ var _ = Describe("CalicoCni", func() {

Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

Expect(endpoints.Items[0].Spec.ContainerID).Should(Equal(containerID))

Expand All @@ -1393,7 +1432,10 @@ var _ = Describe("CalicoCni", func() {
// Calico CNI currently only supports one endpoint (interface) per pod.
Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(endpoints.Items[0].Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

// Explicitly assert that endpoint name is still 'eth0' (which was the case from the first ADD)
Expect(endpoints.Items[0].Spec.Endpoint).Should(Equal("eth0"))
Expand Down Expand Up @@ -1471,7 +1513,10 @@ var _ = Describe("CalicoCni", func() {
// Calico CNI currently only supports one endpoint (interface) per pod.
Expect(ep.Name).Should(Equal(wrkload2))
Expect(ep.Namespace).Should(Equal(testutils.K8S_TEST_NS))
Expect(ep.Labels).Should(Equal(map[string]string{"calico/k8s_ns": "test"}))
Expect(ep.Labels).Should(Equal(map[string]string{
"projectcalico.org/namespace": "test",
"projectcalico.org/orchestrator": "k8s",
}))

// Assert this WEP has the new containerID for the second pod.
Expect(ep.Spec.ContainerID).Should(Equal(containerID2))
Expand Down
2 changes: 1 addition & 1 deletion calico_cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var _ = Describe("CalicoCni", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(profile.Labels).Should(Equal(map[string]string{"net1": ""}))
Expect(profile.Spec.EgressRules).Should(Equal([]api.Rule{{Action: "allow"}}))
Expect(profile.Spec.IngressRules).Should(Equal([]api.Rule{{Action: "allow", Source: api.EntityRule{Tag: "net1"}}}))
Expect(profile.Spec.IngressRules).Should(Equal([]api.Rule{{Action: "allow", Source: api.EntityRule{Selector: "has(net1)"}}}))

// The endpoint is created in etcd
endpoints, err := calicoClient.WorkloadEndpoints().List(ctx, options.ListOptions{})
Expand Down
6 changes: 3 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import:
subpackages:
- gexec
- package: github.com/projectcalico/libcalico-go
version: master
version: 80853cf81b74dbde7b6c55dae0d52cfe8e0001c9
subpackages:
- lib/apiconfig
- lib/apis/v2
Expand Down
10 changes: 2 additions & 8 deletions k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func CmdAddK8s(ctx context.Context, args *skel.CmdArgs, conf types.NetConf, epID
// If it's not, then just use the network name (which is the normal behavior)
// otherwise use one based on the Kubernetes pod's Namespace.
if conf.Policy.PolicyType == "k8s" {
endpoint.Spec.Profiles = []string{fmt.Sprintf("k8s_ns.%s", epIDs.Namespace)}
endpoint.Spec.Profiles = []string{k8sconversion.NamespaceProfileNamePrefix + epIDs.Namespace}
} else {
endpoint.Spec.Profiles = []string{conf.Name}
}
Expand Down Expand Up @@ -632,20 +632,14 @@ func getK8sPodInfo(client *kubernetes.Clientset, podName, podNamespace string) (
return nil, nil, nil, err
}

labels = pod.Labels
if labels == nil {
labels = make(map[string]string)
}

labels["calico/k8s_ns"] = podNamespace

var c k8sconversion.Converter
kvp, err := c.PodToWorkloadEndpoint(pod)
if err != nil {
return nil, nil, nil, err
}

ports = kvp.Value.(*api.WorkloadEndpoint).Spec.Ports
labels = kvp.Value.(*api.WorkloadEndpoint).Labels

return labels, pod.Annotations, ports, nil
}
Expand Down

0 comments on commit 62b1557

Please sign in to comment.