-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (149 loc) · 5.2 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
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
/*
* This file is taken and modified from the KubeVirt project
*
* 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.
*
* Copyright 2018 Red Hat, Inc.
*
*/
package main
import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"net"
"os"
"github.com/spf13/pflag"
"google.golang.org/grpc"
vmSchema "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/hooks"
hooksInfo "kubevirt.io/kubevirt/pkg/hooks/info"
hooksV1alpha1 "kubevirt.io/kubevirt/pkg/hooks/v1alpha1"
hooksV1alpha2 "kubevirt.io/kubevirt/pkg/hooks/v1alpha2"
domainSchema "kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"
)
const (
serialConsoleFileAnnotation = "serial.vm.kubevirt.io/serialConsoleFile"
onDefineDomainLoggingMessage = "Hook's OnDefineDomain callback method has been called"
)
type infoServer struct {
Version string
}
func (s infoServer) Info(ctx context.Context, params *hooksInfo.InfoParams) (*hooksInfo.InfoResult, error) {
log.Log.Info("Hook's Info method has been called")
return &hooksInfo.InfoResult{
Name: "smbios",
Versions: []string{
s.Version,
},
HookPoints: []*hooksInfo.HookPoint{
&hooksInfo.HookPoint{
Name: hooksInfo.OnDefineDomainHookPointName,
Priority: 0,
},
},
}, nil
}
type v1alpha1Server struct{}
type v1alpha2Server struct{}
func (s v1alpha2Server) OnDefineDomain(ctx context.Context, params *hooksV1alpha2.OnDefineDomainParams) (*hooksV1alpha2.OnDefineDomainResult, error) {
log.Log.Info(onDefineDomainLoggingMessage)
newDomainXML, err := onDefineDomain(params.GetVmi(), params.GetDomainXML())
if err != nil {
return nil, err
}
return &hooksV1alpha2.OnDefineDomainResult{
DomainXML: newDomainXML,
}, nil
}
func (s v1alpha2Server) PreCloudInitIso(_ context.Context, params *hooksV1alpha2.PreCloudInitIsoParams) (*hooksV1alpha2.PreCloudInitIsoResult, error) {
return &hooksV1alpha2.PreCloudInitIsoResult{
CloudInitData: params.GetCloudInitData(),
}, nil
}
func (s v1alpha1Server) OnDefineDomain(ctx context.Context, params *hooksV1alpha1.OnDefineDomainParams) (*hooksV1alpha1.OnDefineDomainResult, error) {
log.Log.Info(onDefineDomainLoggingMessage)
newDomainXML, err := onDefineDomain(params.GetVmi(), params.GetDomainXML())
if err != nil {
return nil, err
}
return &hooksV1alpha1.OnDefineDomainResult{
DomainXML: newDomainXML,
}, nil
}
func onDefineDomain(vmiJSON []byte, domainXML []byte) ([]byte, error) {
log.Log.Info(onDefineDomainLoggingMessage)
vmiSpec := vmSchema.VirtualMachineInstance{}
err := json.Unmarshal(vmiJSON, &vmiSpec)
if err != nil {
log.Log.Reason(err).Errorf("Failed to unmarshal given VMI spec: %s", vmiJSON)
panic(err)
}
annotations := vmiSpec.GetAnnotations()
if annon, found := annotations[serialConsoleFileAnnotation]; !found {
log.Log.Infof("Serial console hook sidecar was requested, but no attributes provided. Returning original domain spec[%s]", annon)
return domainXML, nil
}
domainSpec := domainSchema.DomainSpec{}
err = xml.Unmarshal(domainXML, &domainSpec)
if err != nil {
log.Log.Reason(err).Errorf("Failed to unmarshal given domain spec: %s", domainXML)
panic(err)
}
var serialPort uint = 0
if serialConsoleFile, found := annotations[serialConsoleFileAnnotation]; found {
domainSpec.Devices.Serials = []domainSchema.Serial{
{
Type: "file",
Target: &domainSchema.SerialTarget{
Port: &serialPort,
},
Source: &domainSchema.SerialSource{
Path: serialConsoleFile,
},
},
}
}
newDomainXML, err := xml.Marshal(domainSpec)
if err != nil {
log.Log.Reason(err).Errorf("Failed to marshal updated domain spec: %+v", domainSpec)
panic(err)
}
log.Log.Info("Successfully updated original domain spec with requested serial console attributes")
return newDomainXML, nil
}
func main() {
log.InitializeLogging("serial-console-hook-sidecar")
var version string
pflag.StringVar(&version, "version", "", "hook version to use")
pflag.Parse()
socketPath := hooks.HookSocketsSharedDirectory + "/serial.sock"
socket, err := net.Listen("unix", socketPath)
if err != nil {
log.Log.Reason(err).Errorf("Failed to initialized socket on path: %s", socket)
log.Log.Error("Check whether given directory exists and socket name is not already taken by other file")
panic(err)
}
defer os.Remove(socketPath)
server := grpc.NewServer([]grpc.ServerOption{}...)
if version == "" {
panic(fmt.Errorf("usage: \n /kubevirt-sidecar --version v1alpha1|v1alpha2"))
}
hooksInfo.RegisterInfoServer(server, infoServer{Version: version})
hooksV1alpha1.RegisterCallbacksServer(server, v1alpha1Server{})
hooksV1alpha2.RegisterCallbacksServer(server, v1alpha2Server{})
log.Log.Infof("Starting hook server exposing 'info' and 'v1alpha1' services on socket %s", socketPath)
server.Serve(socket)
}