Skip to content

Commit c8b5dce

Browse files
committed
added an option to create a node label if epc memory is present
updated README for SGX device plugin Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@gmail.com>
1 parent 1b7a35c commit c8b5dce

File tree

7 files changed

+117
-56
lines changed

7 files changed

+117
-56
lines changed

cmd/sgx_epchook/main.go

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"flag"
2121
"fmt"
2222
"os"
23+
"os/signal"
24+
"syscall"
2325

2426
"github.com/klauspost/cpuid/v2"
2527
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -30,45 +32,83 @@ import (
3032
)
3133

3234
const (
33-
namespace = "sgx.intel.com"
34-
epc = "epc"
35-
pathPrefix = "/status/capacity"
35+
namespace = "sgx.intel.com"
36+
epc = "epc"
37+
capable = "capable"
3638
)
3739

38-
type patchExtendedResource struct {
39-
Op string `json:"op"`
40-
Path string `json:"path"`
41-
Value uint64 `json:"value"`
40+
type patchNodeOp struct {
41+
Op string `json:"op"`
42+
Path string `json:"path"`
43+
Value interface{} `json:"value"`
4244
}
4345

4446
func main() {
45-
var register, affirm bool
47+
var register, affirm, label, daemon bool
4648
flag.BoolVar(&register, "register", false, "register EPC as extended resource")
4749
flag.BoolVar(&affirm, "affirm", false, "return error if EPC is not available")
50+
flag.BoolVar(&label, "node-label", false, "create node label")
51+
flag.BoolVar(&daemon, "daemon", false, "run as a daemon")
4852
flag.Parse()
4953

54+
klog.Infof("starting sgx_epchook")
55+
5056
// get the EPC size
5157
var epcSize uint64
5258
if cpuid.CPU.SGX.Available {
5359
for _, s := range cpuid.CPU.SGX.EPCSections {
5460
epcSize += s.EPCSize
5561
}
5662
}
63+
klog.Infof("epc capacity: %d bytes", epcSize)
5764

5865
if epcSize == 0 && affirm {
5966
klog.Fatal("SGX EPC is not available")
6067
}
6168

62-
if register {
63-
if err := registerExtendedResource(epcSize); err != nil {
64-
klog.Fatal(err.Error())
65-
}
66-
} else {
69+
if err := updateNode(epcSize, register, label); err != nil {
70+
klog.Fatal(err.Error())
71+
}
72+
73+
// if the "register" flag is FALSE, we assume that sgx_epchook is used as NFD hook
74+
if !register {
6775
fmt.Printf("%s/%s=%d", namespace, epc, epcSize)
6876
}
77+
78+
if daemon {
79+
klog.Info("waiting for termination signal")
80+
term := make(chan os.Signal, 1)
81+
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
82+
<-term
83+
}
6984
}
7085

71-
func registerExtendedResource(epcSize uint64) error {
86+
func updateNode(epcSize uint64, register, label bool) error {
87+
// create patch payload
88+
payload := []patchNodeOp{}
89+
if register {
90+
payload = append(payload, patchNodeOp{
91+
Op: "add",
92+
Path: fmt.Sprintf("/status/capacity/%s~1%s", namespace, epc),
93+
Value: epcSize,
94+
})
95+
}
96+
if label && epcSize > 0 {
97+
payload = append(payload, patchNodeOp{
98+
Op: "add",
99+
Path: fmt.Sprintf("/metadata/labels/%s~1%s", namespace, capable),
100+
Value: "true",
101+
})
102+
}
103+
if len(payload) == 0 {
104+
return nil
105+
}
106+
107+
payloadBytes, err := json.Marshal(payload)
108+
if err != nil {
109+
return err
110+
}
111+
72112
// create the in-cluster config
73113
config, err := rest.InClusterConfig()
74114
if err != nil {
@@ -87,19 +127,7 @@ func registerExtendedResource(epcSize uint64) error {
87127
return err
88128
}
89129

90-
// create and send patch request
91-
payload := []patchExtendedResource{{
92-
Op: "add",
93-
Path: fmt.Sprintf("%s/%s~1%s", pathPrefix, namespace, epc),
94-
Value: epcSize,
95-
}}
96-
payloadBytes, err := json.Marshal(payload)
97-
if err != nil {
98-
return err
99-
}
130+
// patch the node
100131
_, err = clientset.CoreV1().Nodes().Patch(context.TODO(), node.Name, types.JSONPatchType, payloadBytes, metav1.PatchOptions{}, "status")
101-
if err != nil {
102-
return err
103-
}
104-
return nil
132+
return err
105133
}

cmd/sgx_plugin/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ Successfully tagged intel/intel-sgx-initcontainer:devel
174174

175175
#### Deploy the DaemonSet
176176

177-
Deploying the plugin involves the deployment of the
178-
[SGX DaemonSet YAML](/deployments/sgx_plugin/base/intel-sgx-plugin.yaml)
177+
There are two alternative ways to deploy SGX device plugin.
178+
179+
The first approach involves deployment of the [SGX DaemonSet YAML](/deployments/sgx_plugin/base/intel-sgx-plugin.yaml)
179180
and [node-feature-discovery](/deployments/sgx_nfd/kustomization.yaml)
180181
with the necessary configuration.
181182

@@ -184,6 +185,13 @@ There is a kustomization for deploying everything:
184185
$ kubectl apply -k ${INTEL_DEVICE_PLUGINS_SRC}/deployments/sgx_plugin/overlays/epc-nfd/
185186
```
186187

188+
The second approach has a lesser deployment footprint. It does not deploy NFD, but a helper daemonset that creates `sgx.intel.com/capable='true'` node label and advertises EPC capacity to the API server.
189+
190+
The following kustomization is used for this approach:
191+
```bash
192+
$ kubectl apply -k ${INTEL_DEVICE_PLUGINS_SRC}/deployments/sgx_plugin/overlays/epc-register/
193+
```
194+
187195
#### Verify SGX device plugin is registered:
188196

189197
Verification of the plugin deployment and detection of SGX hardware can be confirmed by

deployments/sgx_plugin/overlays/epc-register/add-epc-register-initcontainer.yaml

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: apps/v1
2+
kind: DaemonSet
3+
metadata:
4+
name: intel-sgx-plugin
5+
spec:
6+
template:
7+
spec:
8+
nodeSelector:
9+
sgx.intel.com/capable: 'true'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apiVersion: apps/v1
2+
kind: DaemonSet
3+
metadata:
4+
name: sgx-node-init
5+
labels:
6+
app: sgx-node-init
7+
spec:
8+
selector:
9+
matchLabels:
10+
app: sgx-node-init
11+
template:
12+
metadata:
13+
labels:
14+
app: sgx-node-init
15+
spec:
16+
serviceAccountName: sgx-plugin
17+
containers:
18+
- name: sgx-node-init
19+
image: intel/intel-sgx-initcontainer:devel
20+
imagePullPolicy: IfNotPresent
21+
command:
22+
- /usr/local/bin/sgx-sw/intel-sgx-epchook
23+
- -register
24+
- -node-label
25+
- -daemon
26+
env:
27+
- name: NODE_NAME
28+
valueFrom:
29+
fieldRef:
30+
fieldPath: spec.nodeName
31+
securityContext:
32+
allowPrivilegeEscalation: false
33+
capabilities:
34+
drop:
35+
- ALL

deployments/sgx_plugin/overlays/epc-register/kustomization.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ bases:
33
namespace: kube-system
44
resources:
55
- service-account.yaml
6+
- init-daemonset.yaml
67
patches:
7-
- add-epc-register-initcontainer.yaml
8+
- add-node-selector.yaml

deployments/sgx_plugin/overlays/epc-register/service-account.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
kind: ServiceAccount
22
apiVersion: v1
33
metadata:
4-
name: sgx-epc-extres
4+
name: sgx-plugin
55
namespace: kube-system
66
---
77
apiVersion: rbac.authorization.k8s.io/v1
88
kind: ClusterRole
99
metadata:
10-
name: sgx-epc-extres-rd
10+
name: sgx-plugin
1111
rules:
1212
- apiGroups:
1313
- ""
@@ -22,12 +22,12 @@ rules:
2222
apiVersion: rbac.authorization.k8s.io/v1
2323
kind: ClusterRoleBinding
2424
metadata:
25-
name: sgx-epc-extres-rd
25+
name: sgx-plugin
2626
roleRef:
2727
apiGroup: rbac.authorization.k8s.io
2828
kind: ClusterRole
29-
name: sgx-epc-extres-rd
29+
name: sgx-plugin
3030
subjects:
3131
- kind: ServiceAccount
32-
name: sgx-epc-extres
32+
name: sgx-plugin
3333
namespace: kube-system

0 commit comments

Comments
 (0)