Skip to content

Commit 7de7fae

Browse files
Merge pull request #1961 from tmshort/add-np
OPRUN-3883: Add NetworkPolicies to the list of resources
2 parents 5184403 + 3c53c54 commit 7de7fae

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

pkg/operator/resource/resourceapply/generic.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
appsv1 "k8s.io/api/apps/v1"
1010

1111
corev1 "k8s.io/api/core/v1"
12+
networkingv1 "k8s.io/api/networking/v1"
1213
policyv1 "k8s.io/api/policy/v1"
1314
rbacv1 "k8s.io/api/rbac/v1"
1415
storagev1 "k8s.io/api/storage/v1"
@@ -142,6 +143,12 @@ func ApplyDirectly(ctx context.Context, clients *ClientHolder, recorder events.R
142143
} else {
143144
result.Result, result.Changed, result.Error = ApplySecretImproved(ctx, client, recorder, t, cache)
144145
}
146+
case *networkingv1.NetworkPolicy:
147+
if clients.kubeClient == nil {
148+
result.Error = fmt.Errorf("missing kubeClient")
149+
} else {
150+
result.Result, result.Changed, result.Error = ApplyNetworkPolicy(ctx, clients.kubeClient.NetworkingV1(), recorder, t)
151+
}
145152
case *rbacv1.ClusterRole:
146153
if clients.kubeClient == nil {
147154
result.Error = fmt.Errorf("missing kubeClient")
@@ -295,6 +302,12 @@ func DeleteAll(ctx context.Context, clients *ClientHolder, recorder events.Recor
295302
} else {
296303
_, result.Changed, result.Error = DeleteSecret(ctx, client, recorder, t)
297304
}
305+
case *networkingv1.NetworkPolicy:
306+
if clients.kubeClient == nil {
307+
result.Error = fmt.Errorf("missing kubeClient")
308+
} else {
309+
_, result.Changed, result.Error = DeleteNetworkPolicy(ctx, clients.kubeClient.NetworkingV1(), recorder, t)
310+
}
298311
case *rbacv1.ClusterRole:
299312
if clients.kubeClient == nil {
300313
result.Error = fmt.Errorf("missing kubeClient")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package resourceapply
2+
3+
import (
4+
"context"
5+
6+
networkingv1 "k8s.io/api/networking/v1"
7+
"k8s.io/apimachinery/pkg/api/equality"
8+
apierrors "k8s.io/apimachinery/pkg/api/errors"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
networkingclientv1 "k8s.io/client-go/kubernetes/typed/networking/v1"
11+
"k8s.io/klog/v2"
12+
13+
"github.com/openshift/library-go/pkg/operator/events"
14+
"github.com/openshift/library-go/pkg/operator/resource/resourcehelper"
15+
"github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
16+
)
17+
18+
// ApplyClusterRole merges objectmeta, does not worry about anything else
19+
func ApplyNetworkPolicy(ctx context.Context, client networkingclientv1.NetworkPoliciesGetter, recorder events.Recorder, required *networkingv1.NetworkPolicy) (*networkingv1.NetworkPolicy, bool, error) {
20+
existing, err := client.NetworkPolicies(required.Namespace).Get(ctx, required.Name, metav1.GetOptions{})
21+
if apierrors.IsNotFound(err) {
22+
requiredCopy := required.DeepCopy()
23+
actual, err := client.NetworkPolicies(required.Namespace).Create(
24+
ctx, resourcemerge.WithCleanLabelsAndAnnotations(requiredCopy).(*networkingv1.NetworkPolicy), metav1.CreateOptions{})
25+
resourcehelper.ReportCreateEvent(recorder, required, err)
26+
return actual, true, err
27+
}
28+
if err != nil {
29+
return nil, false, err
30+
}
31+
32+
modified := false
33+
existingCopy := existing.DeepCopy()
34+
35+
resourcemerge.EnsureObjectMeta(&modified, &existingCopy.ObjectMeta, required.ObjectMeta)
36+
if equality.Semantic.DeepEqual(existingCopy.Spec, required.Spec) && !modified {
37+
return existingCopy, false, nil
38+
}
39+
40+
if klog.V(2).Enabled() {
41+
klog.Infof("NetworkPolicy %q changes: %v", required.Name, JSONPatchNoError(existing, existingCopy))
42+
}
43+
44+
actual, err := client.NetworkPolicies(existingCopy.Namespace).Update(ctx, existingCopy, metav1.UpdateOptions{})
45+
resourcehelper.ReportUpdateEvent(recorder, required, err)
46+
return actual, true, err
47+
}
48+
49+
func DeleteNetworkPolicy(ctx context.Context, client networkingclientv1.NetworkPoliciesGetter, recorder events.Recorder, required *networkingv1.NetworkPolicy) (*networkingv1.NetworkPolicy, bool, error) {
50+
err := client.NetworkPolicies(required.Namespace).Delete(ctx, required.Name, metav1.DeleteOptions{})
51+
if err != nil && apierrors.IsNotFound(err) {
52+
return nil, false, nil
53+
}
54+
if err != nil {
55+
return nil, false, err
56+
}
57+
resourcehelper.ReportDeleteEvent(recorder, required, err)
58+
return nil, true, nil
59+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package resourceread
2+
3+
import (
4+
networkingv1 "k8s.io/api/networking/v1"
5+
"k8s.io/apimachinery/pkg/runtime"
6+
"k8s.io/apimachinery/pkg/runtime/serializer"
7+
)
8+
9+
var (
10+
netScheme = runtime.NewScheme()
11+
netCodecs = serializer.NewCodecFactory(netScheme)
12+
)
13+
14+
func init() {
15+
if err := networkingv1.AddToScheme(netScheme); err != nil {
16+
panic(err)
17+
}
18+
}
19+
20+
func ReadNetworkPolicyV1OrDie(objBytes []byte) *networkingv1.NetworkPolicy {
21+
requiredObj, err := runtime.Decode(coreCodecs.UniversalDecoder(networkingv1.SchemeGroupVersion), objBytes)
22+
if err != nil {
23+
panic(err)
24+
}
25+
return requiredObj.(*networkingv1.NetworkPolicy)
26+
}

pkg/operator/staticresourcecontroller/static_resource_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/client-go/restmapper"
1818

1919
corev1 "k8s.io/api/core/v1"
20+
networkingv1 "k8s.io/api/networking/v1"
2021
policyv1 "k8s.io/api/policy/v1"
2122
rbacv1 "k8s.io/api/rbac/v1"
2223
storagev1 "k8s.io/api/storage/v1"
@@ -236,6 +237,8 @@ func (c *StaticResourceController) AddKubeInformers(kubeInformersByNamespace v1h
236237
ret = ret.AddInformer(informer.Core().V1().ConfigMaps().Informer())
237238
case *corev1.Secret:
238239
ret = ret.AddInformer(informer.Core().V1().Secrets().Informer())
240+
case *networkingv1.NetworkPolicy:
241+
ret = ret.AddInformer(informer.Networking().V1().NetworkPolicies().Informer())
239242
case *rbacv1.ClusterRole:
240243
ret = ret.AddInformer(informer.Rbac().V1().ClusterRoles().Informer())
241244
case *rbacv1.ClusterRoleBinding:

0 commit comments

Comments
 (0)