-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (115 loc) · 4.32 KB
/
main.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
// This package implements the Fed4Fire Aggregate Manager API for EdgeNet/Kubernetes.
//
// Specifically, it implements the GENI AM API v3 as specified in
// https://groups.geni.net/geni/wiki/GAPI_AM_API_V3.
package main
import (
"flag"
"github.com/EdgeNet-project/fed4fire/pkg/constants"
"github.com/EdgeNet-project/fed4fire/pkg/gc"
versioned "github.com/EdgeNet-project/fed4fire/pkg/generated/clientset/versioned"
"github.com/EdgeNet-project/fed4fire/pkg/identifiers"
"github.com/EdgeNet-project/fed4fire/pkg/service"
"github.com/EdgeNet-project/fed4fire/pkg/utils"
"github.com/gorilla/rpc"
"github.com/maxmouchet/gorilla-xmlrpc/xml"
"io/ioutil"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"net/http"
"os"
"strings"
"time"
)
var showHelp bool
var absoluteUrl string
var authorityName string
var containerImages utils.ArrayFlags
var containerCpuLimit string
var containerMemoryLimit string
var kubeconfigFile string
var listenAddr string
var namespace string
var trustedCerts utils.ArrayFlags
func beforeFunc(i *rpc.RequestInfo) {
escapedCert := i.Request.Header.Get(constants.HttpHeaderCertificate)
urn, err := utils.GetUserUrnFromEscapedCert(escapedCert)
if err == nil {
i.Request.Header.Set(constants.HttpHeaderUser, urn)
} else {
klog.ErrorS(err, "Failed to get user URN from header")
}
klog.InfoS(
"Received XML-RPC request",
"user-urn", i.Request.Header.Get(constants.HttpHeaderUser),
"rpc-method", i.Method,
)
}
func main() {
klog.InitFlags(nil)
flag.BoolVar(&showHelp, "help", false, "show this message")
flag.StringVar(&absoluteUrl, "absoluteUrl", "https://localhost:9443", "URL used by external clients to reach this server")
flag.StringVar(&authorityName, "authorityName", "example.org", "authority name to use in URNs")
flag.Var(&containerImages, "containerImage", "name:image of a container image that can be deployed; can be specified multiple times")
flag.StringVar(&containerCpuLimit, "containerCpuLimit", "2", "maximum amount of CPU that can be used by a container")
flag.StringVar(&containerMemoryLimit, "containerMemoryLimit", "2Gi", "maximum amount of memory that can be used by a container")
flag.StringVar(&kubeconfigFile, "kubeconfig", "", "path to the kubeconfig file used to communicate with the Kubernetes API")
flag.StringVar(&listenAddr, "listenAddr", "localhost:9443", "host:port on which to listen")
flag.StringVar(&namespace, "namespace", "", "kubernetes namespaces in which to create resources")
flag.Var(&trustedCerts, "trustedCert", "path to a trusted certificate for authenticating users; can be specified multiple times")
flag.Parse()
if showHelp {
flag.PrintDefaults()
os.Exit(0)
}
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigFile)
utils.Check(err)
f4fclient, err := versioned.NewForConfig(config)
utils.Check(err)
kubeclient, err := kubernetes.NewForConfig(config)
utils.Check(err)
authorityIdentifier := identifiers.Identifier{
Authorities: []string{authorityName},
ResourceType: identifiers.ResourceTypeAuthority,
ResourceName: "am",
}
containerImages_ := make(map[string]string)
for _, s := range containerImages {
arr := strings.SplitN(s, ":", 2)
containerImages_[arr[0]] = arr[1]
klog.InfoS("Parsed container image name", "name", arr[0], "image", arr[1])
}
trustedCerts_ := make([][]byte, 0)
for _, s := range trustedCerts {
b, err := ioutil.ReadFile(s)
utils.Check(err)
trustedCerts_ = append(trustedCerts_, utils.PEMDecodeMany(b)...)
}
s := &service.Service{
AbsoluteURL: absoluteUrl,
AuthorityIdentifier: authorityIdentifier,
ContainerImages: containerImages_,
ContainerCpuLimit: containerCpuLimit,
ContainerMemoryLimit: containerMemoryLimit,
Namespace: namespace,
TrustedCertificates: trustedCerts_,
Fed4FireClient: f4fclient,
KubernetesClient: kubeclient,
}
xmlrpcCodec := xml.NewCodec()
xmlrpcCodec.SetPrefix("Service.")
RPC := rpc.NewServer()
RPC.RegisterBeforeFunc(beforeFunc)
RPC.RegisterCodec(xmlrpcCodec, "text/xml")
utils.Check(RPC.RegisterService(s, ""))
gc.GC{
Fed4FireClient: f4fclient,
KubernetesClient: kubeclient,
Interval: 5 * time.Second,
Timeout: 30 * time.Second,
Namespace: namespace,
}.Start()
klog.InfoS("Listening", "address", listenAddr)
utils.Check(http.ListenAndServe(listenAddr, RPC))
}