Debian Codegraph @

pkg/types/020/types.go

raw
1 // Copyright 2016 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package types020
16
17 import (
18 "encoding/json"
19 "fmt"
20 "io"
21 "net"
22 "os"
23
24 "github.com/containernetworking/cni/pkg/types"
25 convert "github.com/containernetworking/cni/pkg/types/internal"
26 )
27
28 const ImplementedSpecVersion string = "0.2.0"
29
30 var supportedVersions = []string{"", "0.1.0", ImplementedSpecVersion}
31
32 // Register converters for all versions less than the implemented spec version
33 func init() {
34 convert.RegisterConverter("0.1.0", []string{ImplementedSpecVersion}, convertFrom010)
35 convert.RegisterConverter(ImplementedSpecVersion, []string{"0.1.0"}, convertTo010)
36
37 // Creator
38 convert.RegisterCreator(supportedVersions, NewResult)
39 }
40
41 // Compatibility types for CNI version 0.1.0 and 0.2.0
42
43 // NewResult creates a new Result object from JSON data. The JSON data
44 // must be compatible with the CNI versions implemented by this type.
45 func NewResult(data []byte) (types.Result, error) {
46 result := &Result{}
47 if err := json.Unmarshal(data, result); err != nil {
48 return nil, err
49 }
50 for _, v := range supportedVersions {
51 if result.CNIVersion == v {
52 if result.CNIVersion == "" {
53 result.CNIVersion = "0.1.0"
54 }
55 return result, nil
56 }
57 }
58 return nil, fmt.Errorf("result type supports %v but unmarshalled CNIVersion is %q",
59 supportedVersions, result.CNIVersion)
60 }
61
62 // GetResult converts the given Result object to the ImplementedSpecVersion
63 // and returns the concrete type or an error
64 func GetResult(r types.Result) (*Result, error) {
65 result020, err := convert.Convert(r, ImplementedSpecVersion)
66 if err != nil {
67 return nil, err
68 }
69 result, ok := result020.(*Result)
70 if !ok {
71 return nil, fmt.Errorf("failed to convert result")
72 }
73 return result, nil
74 }
75
76 func convertFrom010(from types.Result, toVersion string) (types.Result, error) {
77 if toVersion != "0.2.0" {
78 panic("only converts to version 0.2.0")
79 }
80 fromResult := from.(*Result)
81 return &Result{
82 CNIVersion: ImplementedSpecVersion,
83 IP4: fromResult.IP4.Copy(),
84 IP6: fromResult.IP6.Copy(),
85 DNS: *fromResult.DNS.Copy(),
86 }, nil
87 }
88
89 func convertTo010(from types.Result, toVersion string) (types.Result, error) {
90 if toVersion != "0.1.0" {
91 panic("only converts to version 0.1.0")
92 }
93 fromResult := from.(*Result)
94 return &Result{
95 CNIVersion: "0.1.0",
96 IP4: fromResult.IP4.Copy(),
97 IP6: fromResult.IP6.Copy(),
98 DNS: *fromResult.DNS.Copy(),
99 }, nil
100 }
101
102 // Result is what gets returned from the plugin (via stdout) to the caller
103 type Result struct {
104 CNIVersion string `json:"cniVersion,omitempty"`
105 IP4 *IPConfig `json:"ip4,omitempty"`
106 IP6 *IPConfig `json:"ip6,omitempty"`
107 DNS types.DNS `json:"dns,omitempty"`
108 }
109
110 func (r *Result) Version() string {
111 return r.CNIVersion
112 }
113
114 func (r *Result) GetAsVersion(version string) (types.Result, error) {
115 // If the creator of the result did not set the CNIVersion, assume it
116 // should be the highest spec version implemented by this Result
117 if r.CNIVersion == "" {
118 r.CNIVersion = ImplementedSpecVersion
119 }
120 return convert.Convert(r, version)
121 }
122
123 func (r *Result) Print() error {
124 return r.PrintTo(os.Stdout)
125 }
126
127 func (r *Result) PrintTo(writer io.Writer) error {
128 data, err := json.MarshalIndent(r, "", " ")
129 if err != nil {
130 return err
131 }
132 _, err = writer.Write(data)
133 return err
134 }
135
136 // IPConfig contains values necessary to configure an interface
137 type IPConfig struct {
138 IP net.IPNet
139 Gateway net.IP
140 Routes []types.Route
141 }
142
143 func (i *IPConfig) Copy() *IPConfig {
144 if i == nil {
145 return nil
146 }
147
148 var routes []types.Route
149 for _, fromRoute := range i.Routes {
150 routes = append(routes, *fromRoute.Copy())
151 }
152 return &IPConfig{
153 IP: i.IP,
154 Gateway: i.Gateway,
155 Routes: routes,
156 }
157 }
158
159 // net.IPNet is not JSON (un)marshallable so this duality is needed
160 // for our custom IPNet type
161
162 // JSON (un)marshallable types
163 type ipConfig struct {
164 IP types.IPNet `json:"ip"`
165 Gateway net.IP `json:"gateway,omitempty"`
166 Routes []types.Route `json:"routes,omitempty"`
167 }
168
169 func (c *IPConfig) MarshalJSON() ([]byte, error) {
170 ipc := ipConfig{
171 IP: types.IPNet(c.IP),
172 Gateway: c.Gateway,
173 Routes: c.Routes,
174 }
175
176 return json.Marshal(ipc)
177 }
178
179 func (c *IPConfig) UnmarshalJSON(data []byte) error {
180 ipc := ipConfig{}
181 if err := json.Unmarshal(data, &ipc); err != nil {
182 return err
183 }
184
185 c.IP = net.IPNet(ipc.IP)
186 c.Gateway = ipc.Gateway
187 c.Routes = ipc.Routes
188 return nil
189 }