Skip to content

Commit

Permalink
k8s,node: Reuse retrieveNodeInformation to retrieve node labels
Browse files Browse the repository at this point in the history
When creating the host endpoint, we need to retrieve existing node
labels. Instead of making a new API call, we can reuse
retrieveNodeInformation() to get those labels.

Signed-off-by: Paul Chaignon <paul@cilium.io>
  • Loading branch information
pchaigno committed May 26, 2020
1 parent 85e67a5 commit b202364
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
16 changes: 5 additions & 11 deletions pkg/endpointmanager/manager.go
Expand Up @@ -27,14 +27,14 @@ import (
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/endpointmanager/idallocator"
"github.com/cilium/cilium/pkg/identity/cache"
"github.com/cilium/cilium/pkg/k8s"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/labelsfilter"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/metrics"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/policy"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -479,16 +479,10 @@ func (mgr *EndpointManager) AddHostEndpoint(ctx context.Context, owner regenerat
epLabels := labels.Labels{}
epLabels.MergeLabels(labels.LabelHost)

if k8s.IsEnabled() {
// Retrieve k8s labels.
if k8sNode, err := k8s.GetNode(k8s.Client(), nodeName); err != nil {
log.WithError(err).Warning("Kubernetes node resource representing own node is not available, cannot set Labels")
} else {
newLabels := labels.Map2Labels(k8sNode.GetLabels(), labels.LabelSourceK8s)
newIdtyLabels, _ := labelsfilter.Filter(newLabels)
epLabels.MergeLabels(newIdtyLabels)
}
}
// Initialize with known node labels.
newLabels := labels.Map2Labels(node.GetLabels(), labels.LabelSourceK8s)
newIdtyLabels, _ := labelsfilter.Filter(newLabels)
epLabels.MergeLabels(newIdtyLabels)

// Give the endpoint a security identity
newCtx, cancel := context.WithTimeout(ctx, launchTime)
Expand Down
2 changes: 2 additions & 0 deletions pkg/k8s/init.go
Expand Up @@ -226,6 +226,8 @@ func GetNodeSpec(nodeName string) error {
if nodeIP6 != nil {
node.SetIPv6(nodeIP6)
}

node.SetLabels(n.Labels)
} else {
// if node resource could not be received, fail if
// PodCIDR requirement has been requested
Expand Down
2 changes: 2 additions & 0 deletions pkg/k8s/node.go
Expand Up @@ -204,6 +204,8 @@ func ParseNode(k8sNode *slim_corev1.Node, source source.Source) *nodeTypes.Node
}
}

newNode.Labels = k8sNode.GetLabels()

return newNode
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/k8s/node_test.go
Expand Up @@ -38,6 +38,9 @@ func (s *K8sSuite) TestParseNode(c *C) {
annotation.V4CIDRName: "10.254.0.0/16",
annotation.V6CIDRName: "f00d:aaaa:bbbb:cccc:dddd:eeee::/112",
},
Labels: map[string]string{
"type": "m5.xlarge",
},
},
Spec: slim_corev1.NodeSpec{
PodCIDR: "10.1.0.0/16",
Expand All @@ -50,6 +53,7 @@ func (s *K8sSuite) TestParseNode(c *C) {
c.Assert(n.IPv4AllocCIDR.String(), Equals, "10.1.0.0/16")
c.Assert(n.IPv6AllocCIDR, NotNil)
c.Assert(n.IPv6AllocCIDR.String(), Equals, "f00d:aaaa:bbbb:cccc:dddd:eeee::/112")
c.Assert(n.Labels["type"], Equals, "m5.xlarge")

// No IPv6 annotation
k8sNode = &slim_corev1.Node{
Expand Down
29 changes: 29 additions & 0 deletions pkg/node/labels.go
@@ -0,0 +1,29 @@
// Copyright 2020 Authors of Cilium
//
// 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 node

var (
labels map[string]string
)

// GetLabels returns the labels of this node.
func GetLabels() map[string]string {
return labels
}

// SetLabels sets the labels of this node.
func SetLabels(l map[string]string) {
labels = l
}
3 changes: 3 additions & 0 deletions pkg/node/types/node.go
Expand Up @@ -166,6 +166,9 @@ type Node struct {

// Key index used for transparent encryption or 0 for no encryption
EncryptionKey uint8

// Node labels
Labels map[string]string
}

// Fullname returns the node's full name including the cluster name if a
Expand Down
7 changes: 7 additions & 0 deletions pkg/node/types/zz_generated.deepcopy.go

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

0 comments on commit b202364

Please sign in to comment.