forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.go
144 lines (128 loc) · 4.53 KB
/
helpers.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
// Copyright 2018 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 groups
import (
"fmt"
"github.com/cilium/cilium/pkg/k8s"
cilium_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/policy/api"
"k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
cnpKindName = "derivative"
parentCNP = "io.cilium.network.policy.parent.uuid"
cnpKindKey = "io.cilium.network.policy.kind"
)
var (
blockOwnerDeletionPtr = true
)
func getDerivativeName(cnp *cilium_v2.CiliumNetworkPolicy) string {
return fmt.Sprintf(
"%s-togroups-%s",
cnp.GetObjectMeta().GetName(),
cnp.GetObjectMeta().GetUID())
}
// createDerivativeCNP will return a new CNP based on the given rule.
func createDerivativeCNP(cnp *cilium_v2.CiliumNetworkPolicy) (*cilium_v2.CiliumNetworkPolicy, error) {
// CNP informer may provide a CNP object without APIVersion or Kind.
// Setting manually to make sure that the derivative policy works ok.
derivativeCNP := &cilium_v2.CiliumNetworkPolicy{
ObjectMeta: v1.ObjectMeta{
Name: getDerivativeName(cnp),
Namespace: cnp.ObjectMeta.Namespace,
OwnerReferences: []v1.OwnerReference{{
APIVersion: cilium_v2.SchemeGroupVersion.String(),
Kind: cilium_v2.CNPKindDefinition,
Name: cnp.ObjectMeta.Name,
UID: cnp.ObjectMeta.UID,
BlockOwnerDeletion: &blockOwnerDeletionPtr,
}},
Labels: map[string]string{
parentCNP: string(cnp.ObjectMeta.UID),
cnpKindKey: cnpKindName,
},
},
}
rules, err := cnp.Parse()
if err != nil {
return nil, fmt.Errorf("Cannot parse policies: %s", err)
}
derivativeCNP.Specs = make(api.Rules, len(rules))
for i, rule := range rules {
if rule.RequiresDerivative() {
derivativeCNP.Specs[i] = denyEgressRule()
}
}
for i, rule := range rules {
if !rule.RequiresDerivative() {
derivativeCNP.Specs[i] = rule
continue
}
newRule, err := rule.CreateDerivative()
if err != nil {
return derivativeCNP, err
}
derivativeCNP.Specs[i] = newRule
}
return derivativeCNP, nil
}
func denyEgressRule() *api.Rule {
return &api.Rule{
Egress: []api.EgressRule{},
}
}
func updateOrCreateCNP(cnp *cilium_v2.CiliumNetworkPolicy) (*cilium_v2.CiliumNetworkPolicy, error) {
k8sCNP, err := k8s.CiliumClient().CiliumV2().CiliumNetworkPolicies(cnp.ObjectMeta.Namespace).
Get(cnp.ObjectMeta.Name, v1.GetOptions{})
if err == nil {
k8sCNP.ObjectMeta.Labels = cnp.ObjectMeta.Labels
k8sCNP.Spec = cnp.Spec
k8sCNP.Specs = cnp.Specs
k8sCNP.Status = cilium_v2.CiliumNetworkPolicyStatus{}
return k8s.CiliumClient().CiliumV2().CiliumNetworkPolicies(cnp.ObjectMeta.Namespace).Update(k8sCNP)
}
return k8s.CiliumClient().CiliumV2().CiliumNetworkPolicies(cnp.ObjectMeta.Namespace).Create(cnp)
}
func updateDerivativeStatus(cnp *cilium_v2.CiliumNetworkPolicy, derivativeName string, err error) error {
status := cilium_v2.CiliumNetworkPolicyNodeStatus{
LastUpdated: cilium_v2.NewTimestamp(),
Enforcing: false,
}
if err != nil {
status.OK = false
status.Error = err.Error()
} else {
status.OK = true
}
// This CNP can be modified by cilium agent or operator. To be able to push
// the status correctly fetch the last version to avoid updates issues.
k8sCNPStatus, clientErr := k8s.CiliumClient().CiliumV2().
CiliumNetworkPolicies(cnp.ObjectMeta.Namespace).
Get(cnp.ObjectMeta.Name, v1.GetOptions{})
if clientErr != nil {
return fmt.Errorf("Cannot get Kubernetes policy: %s", clientErr)
}
if k8sCNPStatus.ObjectMeta.UID != cnp.ObjectMeta.UID {
// This case should not happen, but if the UID does not match make sure
// that the new policy is not in the cache to not loop over it. The
// kubernetes watcher should take care about that.
groupsCNPCache.DeleteCNP(k8sCNPStatus)
return fmt.Errorf("Policy UID mistmatch")
}
k8sCNPStatus.SetDerivedPolicyStatus(derivativeName, status)
groupsCNPCache.UpdateCNP(k8sCNPStatus)
// TODO: switch to JSON Patch
_, err = k8s.CiliumClient().CiliumV2().CiliumNetworkPolicies(cnp.ObjectMeta.Namespace).UpdateStatus(cnp)
return err
}