github.phpd.cn/cilium/cilium@v1.6.12/pkg/workloads/defaults.go (about) 1 // Copyright 2018-2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package workloads 16 17 import ( 18 "context" 19 "time" 20 21 "github.com/cilium/cilium/api/v1/models" 22 "github.com/cilium/cilium/pkg/endpoint" 23 "github.com/cilium/cilium/pkg/endpointmanager" 24 "github.com/cilium/cilium/pkg/k8s" 25 "github.com/cilium/cilium/pkg/labels" 26 27 k8sLbls "k8s.io/kubernetes/pkg/kubelet/types" 28 ) 29 30 const ( 31 // EndpointCorrelationMaxRetries is the number of retries to correlate 32 // a workload start event with an existing endpoint before giving up. 33 EndpointCorrelationMaxRetries = 20 34 ) 35 36 var ( 37 workloadStatusDisabled = &models.Status{ 38 State: models.StatusStateOk, 39 Msg: models.StatusStateDisabled, 40 } 41 ) 42 43 // EndpointCorrelationSleepTime returns the sleep time between correlation 44 // attempts 45 func EndpointCorrelationSleepTime(try int) time.Duration { 46 return time.Duration(try) * time.Second 47 } 48 49 func shortContainerID(id string) string { 50 if len(id) < 10 { 51 return id 52 } 53 return id[:10] 54 } 55 56 func getFilteredLabels(containerID string, allLabels map[string]string) (identityLabels, informationLabels labels.Labels) { 57 combinedLabels := labels.Map2Labels(allLabels, labels.LabelSourceContainer) 58 59 k8sNormalLabels, err := fetchK8sLabels(containerID, allLabels) 60 if err != nil { 61 log.WithError(err).Warn("Error while getting Kubernetes labels") 62 } else if k8sNormalLabels != nil { 63 k8sLbls := labels.Map2Labels(k8sNormalLabels, labels.LabelSourceK8s) 64 combinedLabels.MergeLabels(k8sLbls) 65 } 66 67 return labels.FilterLabels(combinedLabels) 68 } 69 70 func processCreateWorkload(ep *endpoint.Endpoint, containerID string, allLabels map[string]string) { 71 ep.SetContainerID(containerID) 72 73 // FIXME: Remove this in 2019-06: GH-6526 74 if k8s.IsEnabled() && ep.GetK8sPodName() == "" { 75 ep.SetK8sNamespace(k8sLbls.GetPodNamespace(allLabels)) 76 ep.SetK8sPodName(k8sLbls.GetPodName(allLabels)) 77 } 78 79 // Update map allowing to lookup endpoint by endpoint 80 // attributes with new attributes set on endpoint 81 endpointmanager.UpdateReferences(ep) 82 83 identityLabels, informationLabels := getFilteredLabels(containerID, allLabels) 84 ep.UpdateLabels(context.Background(), identityLabels, informationLabels, false) 85 }