forked from cilium/cilium
/
third_party.go
194 lines (163 loc) · 6.62 KB
/
third_party.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2016-2017 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 k8s
import (
"fmt"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/policy/api"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
log "github.com/Sirupsen/logrus"
)
// CiliumNetworkPolicy is a Kubernetes third-party resource with an extended version
// of NetworkPolicy
type CiliumNetworkPolicy struct {
metav1.TypeMeta `json:",inline"`
// +optional
Metadata metav1.ObjectMeta `json:"metadata"`
// Spec is the desired Cilium specific rule specification.
Spec *api.Rule `json:"spec,omitempty"`
// Specs is a list of desired Cilium specific rule specification.
Specs api.Rules `json:"specs,omitempty"`
}
// GetObjectKind returns the kind of the object
func (r *CiliumNetworkPolicy) GetObjectKind() schema.ObjectKind {
return &r.TypeMeta
}
// GetObjectMeta returns the metadata of the object
func (r *CiliumNetworkPolicy) GetObjectMeta() metav1.Object {
return &r.Metadata
}
// parseToCilium returns an api.Rule with all the labels parsed into cilium
// labels.
func parseToCilium(namespace, name string, r *api.Rule) *api.Rule {
retRule := &api.Rule{}
if r.EndpointSelector.LabelSelector != nil {
retRule.EndpointSelector = api.NewESFromK8sLabelSelector("", r.EndpointSelector.LabelSelector)
// The PodSelector should only reflect to the same namespace
// the policy is being stored, thus we add the namespace to
// the MatchLabels map.
if retRule.EndpointSelector.LabelSelector.MatchLabels == nil {
retRule.EndpointSelector.LabelSelector.MatchLabels = map[string]string{}
}
userNamespace, ok := retRule.EndpointSelector.LabelSelector.MatchLabels[labels.LabelSourceK8sKeyPrefix+PodNamespaceLabel]
if ok && userNamespace != namespace {
log.Warningf("k8s: CiliumNetworkPolicy %s/%s contains illegal namespace match '%s' in EndpointSelector."+
" EndpointSelector always applies in namespace of the policy resource, removing namespace match '%s'.",
namespace, name, userNamespace, userNamespace)
}
retRule.EndpointSelector.LabelSelector.MatchLabels[labels.LabelSourceK8sKeyPrefix+PodNamespaceLabel] = namespace
}
if r.Ingress != nil {
retRule.Ingress = make([]api.IngressRule, len(r.Ingress))
for i, ing := range r.Ingress {
if ing.FromEndpoints != nil {
retRule.Ingress[i].FromEndpoints = make([]api.EndpointSelector, len(ing.FromEndpoints))
for j, ep := range ing.FromEndpoints {
retRule.Ingress[i].FromEndpoints[j] = api.NewESFromK8sLabelSelector("", ep.LabelSelector)
if retRule.Ingress[i].FromEndpoints[j].MatchLabels == nil {
retRule.Ingress[i].FromEndpoints[j].MatchLabels = map[string]string{}
}
// There's no need to prefixed K8s
// prefix for reserved labels
if retRule.Ingress[i].FromEndpoints[j].HasKeyPrefix(labels.LabelSourceReservedKeyPrefix) {
continue
}
// The user can explicitly specify the namespace in the
// FromEndpoints selector. If omitted, we limit the
// scope to the namespace the policy lives in.
if _, ok := retRule.Ingress[i].FromEndpoints[j].MatchLabels[labels.LabelSourceK8sKeyPrefix+PodNamespaceLabel]; !ok {
retRule.Ingress[i].FromEndpoints[j].MatchLabels[labels.LabelSourceK8sKeyPrefix+PodNamespaceLabel] = namespace
}
}
}
if ing.ToPorts != nil {
retRule.Ingress[i].ToPorts = make([]api.PortRule, len(ing.ToPorts))
copy(retRule.Ingress[i].ToPorts, ing.ToPorts)
}
if ing.FromCIDR != nil {
retRule.Ingress[i].FromCIDR = make([]api.CIDR, len(ing.FromCIDR))
copy(retRule.Ingress[i].FromCIDR, ing.FromCIDR)
}
if ing.FromRequires != nil {
retRule.Ingress[i].FromRequires = make([]api.EndpointSelector, len(ing.FromRequires))
for j, ep := range ing.FromRequires {
retRule.Ingress[i].FromRequires[j] = api.NewESFromK8sLabelSelector("", ep.LabelSelector)
if retRule.Ingress[i].FromRequires[j].MatchLabels == nil {
retRule.Ingress[i].FromRequires[j].MatchLabels = map[string]string{}
}
// The user can explicitly specify the namespace in the
// FromEndpoints selector. If omitted, we limit the
// scope to the namespace the policy lives in.
if _, ok := retRule.Ingress[i].FromRequires[j].MatchLabels[labels.LabelSourceK8sKeyPrefix+PodNamespaceLabel]; !ok {
retRule.Ingress[i].FromRequires[j].MatchLabels[labels.LabelSourceK8sKeyPrefix+PodNamespaceLabel] = namespace
}
}
}
}
}
if r.Egress != nil {
retRule.Egress = make([]api.EgressRule, len(r.Egress))
copy(retRule.Egress, r.Egress)
}
// Convert resource name to a Cilium policy rule label
label := fmt.Sprintf("%s=%s", PolicyLabelName, name)
// TODO: Warn about overwritten labels?
retRule.Labels = labels.ParseLabelArray(label)
retRule.Description = r.Description
return retRule
}
// Parse parses a CiliumNetworkPolicy and returns a list of cilium policy
// rules.
func (r *CiliumNetworkPolicy) Parse() (api.Rules, error) {
if r.Metadata.Name == "" {
return nil, fmt.Errorf("CiliumNetworkPolicy must have name")
}
namespace := ExtractNamespace(&r.Metadata)
name := r.Metadata.Name
retRules := api.Rules{}
if r.Spec != nil {
if err := r.Spec.Validate(); err != nil {
return nil, fmt.Errorf("Invalid spec: %s", err)
}
cr := parseToCilium(namespace, name, r.Spec)
retRules = append(retRules, cr)
}
if r.Specs != nil {
for _, rule := range r.Specs {
if err := rule.Validate(); err != nil {
return nil, fmt.Errorf("Invalid specs: %s", err)
}
cr := parseToCilium(namespace, name, rule)
retRules = append(retRules, cr)
}
}
return retRules, nil
}
// CiliumNetworkPolicyList is a list of CiliumNetworkPolicy objects
type CiliumNetworkPolicyList struct {
metav1.TypeMeta `json:",inline"`
// +optional
Metadata metav1.ListMeta `json:"metadata"`
// Items is a list of CiliumNetworkPolicy
Items []CiliumNetworkPolicy `json:"items"`
}
// GetObjectKind returns the kind of the object
func (r *CiliumNetworkPolicyList) GetObjectKind() schema.ObjectKind {
return &r.TypeMeta
}
// GetListMeta returns the metadata of the object
func (r *CiliumNetworkPolicyList) GetListMeta() metav1.List {
return &r.Metadata
}