-
Notifications
You must be signed in to change notification settings - Fork 346
/
transform.go
107 lines (99 loc) · 3.28 KB
/
transform.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
// Copyright 2020 Antrea Authors
//
// 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.
package version
import (
"encoding/json"
"io"
k8sversion "k8s.io/apimachinery/pkg/version"
"k8s.io/klog/v2"
clusterinfov1beta1 "antrea.io/antrea/pkg/apis/crd/v1beta1"
antreaversion "antrea.io/antrea/pkg/version"
)
type Response struct {
AgentVersion string `json:"agentVersion,omitempty"`
ControllerVersion string `json:"controllerVersion,omitempty"`
FlowAggregatorVersion string `json:"flowAggregatorVersion,omitempty"`
AntctlVersion string `json:"antctlVersion,omitempty"`
}
// AgentVersion is the AddonTransform for the version command. This function
// will try to parse the response as a AgentVersionResponse and then populate
// it with the version of antctl to a transformedVersionResponse object.
func AgentTransform(reader io.Reader, _ bool, _ map[string]string) (interface{}, error) {
b, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
klog.Infof("version transform received: %s", string(b))
v := new(k8sversion.Info)
err = json.Unmarshal(b, v)
if err != nil {
return nil, err
}
agentVersion := v.GitVersion
if len(v.GitCommit) > 0 {
agentVersion += "-" + v.GitCommit
}
if len(v.GitTreeState) > 0 {
agentVersion += "." + v.GitTreeState
}
resp := &Response{
AgentVersion: agentVersion,
AntctlVersion: antreaversion.GetFullVersion(),
}
return resp, nil
}
func ControllerTransform(reader io.Reader, _ bool, _ map[string]string) (interface{}, error) {
b, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
klog.Infof("version transform received: %s", string(b))
controllerInfo := new(clusterinfov1beta1.AntreaControllerInfo)
err = json.Unmarshal(b, controllerInfo)
if err != nil {
return nil, err
}
resp := &Response{
ControllerVersion: controllerInfo.Version,
AntctlVersion: antreaversion.GetFullVersion(),
}
return resp, nil
}
// FlowAggregatorTransform is the AddonTransform for the flow aggregator version command.
// This function will try to parse the response as a FlowAggregatorVersionResponse and
// then populate it with the version of antctl to a transformedVersionResponse object.
func FlowAggregatorTransform(reader io.Reader, _ bool, _ map[string]string) (interface{}, error) {
b, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
klog.Infof("version transform received: %s", string(b))
v := new(k8sversion.Info)
err = json.Unmarshal(b, v)
if err != nil {
return nil, err
}
flowAggregatorVersion := v.GitVersion
if len(v.GitCommit) > 0 {
flowAggregatorVersion += "-" + v.GitCommit
}
if len(v.GitTreeState) > 0 {
flowAggregatorVersion += "." + v.GitTreeState
}
resp := &Response{
FlowAggregatorVersion: flowAggregatorVersion,
AntctlVersion: antreaversion.GetFullVersion(),
}
return resp, nil
}