-
Notifications
You must be signed in to change notification settings - Fork 6
/
scc.go
202 lines (170 loc) · 5.35 KB
/
scc.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
195
196
197
198
199
200
201
202
package aaq_operator
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"kubevirt.io/application-aware-quota/pkg/util"
aaqv1 "kubevirt.io/application-aware-quota/staging/src/kubevirt.io/application-aware-quota-api/pkg/apis/core/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/go-logr/logr"
secv1 "github.com/openshift/api/security/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/source"
sdk "kubevirt.io/controller-lifecycle-operator-sdk/pkg/sdk"
)
const sccName = "application-aware-quota"
func setSCC(scc *secv1.SecurityContextConstraints) {
scc.AllowHostDirVolumePlugin = true
scc.AllowHostIPC = true
scc.AllowHostNetwork = true
scc.AllowHostPID = true
scc.AllowHostPorts = true
scc.AllowPrivilegeEscalation = pointer.Bool(true)
scc.AllowPrivilegedContainer = true
scc.AllowedCapabilities = []corev1.Capability{
"*",
}
scc.AllowedUnsafeSysctls = []string{
"*",
}
scc.DefaultAddCapabilities = nil
scc.RunAsUser = secv1.RunAsUserStrategyOptions{
Type: secv1.RunAsUserStrategyRunAsAny,
}
scc.SELinuxContext = secv1.SELinuxContextStrategyOptions{
Type: secv1.SELinuxStrategyRunAsAny,
}
scc.SeccompProfiles = []string{
"*",
}
scc.SupplementalGroups = secv1.SupplementalGroupsStrategyOptions{
Type: secv1.SupplementalGroupsStrategyRunAsAny,
}
scc.Volumes = []secv1.FSType{
"*",
}
}
func ensureSCCExists(ctx context.Context, logger logr.Logger, c client.Client, saNamespace, saName string) error {
scc := &secv1.SecurityContextConstraints{}
userName := fmt.Sprintf("system:serviceaccount:%s:%s", saNamespace, saName)
err := c.Get(ctx, client.ObjectKey{Name: sccName}, scc)
if meta.IsNoMatchError(err) {
// not in openshift
logger.V(3).Info("No match error for SCC, must not be in openshift")
return nil
} else if errors.IsNotFound(err) {
cr, err := util.GetActiveAAQ(c)
if err != nil {
return err
}
if cr == nil {
return fmt.Errorf("no active MTQ")
}
installerLabels := util.GetRecommendedInstallerLabelsFromCr(cr)
scc = &secv1.SecurityContextConstraints{
ObjectMeta: metav1.ObjectMeta{
Name: sccName,
Labels: map[string]string{
"aaq.kubevirt.io": "",
},
},
Users: []string{
userName,
},
}
setSCC(scc)
util.SetRecommendedLabels(scc, installerLabels, "aaq-operator")
if err = SetOwnerRuntime(c, scc); err != nil {
return err
}
return c.Create(ctx, scc)
} else if err != nil {
return err
}
origSCC := scc.DeepCopy()
setSCC(scc)
if !sdk.ContainsStringValue(scc.Users, userName) {
scc.Users = append(scc.Users, userName)
}
if !apiequality.Semantic.DeepEqual(origSCC, scc) {
return c.Update(context.TODO(), scc)
}
return nil
}
func (r *ReconcileAAQ) watchSecurityContextConstraints() error {
err := r.uncachedClient.List(context.TODO(), &secv1.SecurityContextConstraintsList{}, &client.ListOptions{
Limit: 1,
})
if err == nil {
return r.controller.Watch(source.Kind(r.getCache(), &secv1.SecurityContextConstraints{}), enqueueAAQ(r.client))
}
if meta.IsNoMatchError(err) {
log.Info("Not watching SecurityContextConstraints")
return nil
}
log.Info("GOODBYE SCC")
return err
}
func enqueueAAQ(c client.Client) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(
func(_ context.Context, _ client.Object) []reconcile.Request {
var rrs []reconcile.Request
aaqList := &aaqv1.AAQList{}
if err := c.List(context.TODO(), aaqList, &client.ListOptions{}); err != nil {
log.Error(err, "Error listing all AAQ objects")
return nil
}
for _, aaq := range aaqList.Items {
rr := reconcile.Request{
NamespacedName: types.NamespacedName{Namespace: aaq.Namespace, Name: aaq.Name},
}
rrs = append(rrs, rr)
}
return rrs
},
)
}
// SetOwnerRuntime makes the current "active" AAQ CR the owner of the object using runtime lib client
func SetOwnerRuntime(client client.Client, object metav1.Object) error {
namespace := util.GetNamespace()
configMap := &corev1.ConfigMap{}
if err := client.Get(context.TODO(), types.NamespacedName{Name: util.ConfigMapName, Namespace: namespace}, configMap); err != nil {
klog.Warningf("ConfigMap %s does not exist, so not assigning owner", util.ConfigMapName)
return nil
}
return SetConfigAsOwner(configMap, object)
}
// SetConfigAsOwner sets the passed in config map as owner of the object
func SetConfigAsOwner(configMap *corev1.ConfigMap, object metav1.Object) error {
configMapOwner := getController(configMap.GetOwnerReferences())
if configMapOwner == nil {
return fmt.Errorf("configmap has no owner")
}
for _, o := range object.GetOwnerReferences() {
if o.Controller != nil && *o.Controller {
if o.UID == configMapOwner.UID {
// already set to current obj
return nil
}
return fmt.Errorf("object %+v already owned by %+v", object, o)
}
}
object.SetOwnerReferences(append(object.GetOwnerReferences(), *configMapOwner))
return nil
}
func getController(owners []metav1.OwnerReference) *metav1.OwnerReference {
for _, owner := range owners {
if owner.Controller != nil && *owner.Controller {
return &owner
}
}
return nil
}