Debian Codegraph @

plugins/debug/main.go

raw
1 // Copyright 2021 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 main
16
17 import (
18 "encoding/json"
19 "fmt"
20 "io"
21 "os"
22 "os/exec"
23
24 "github.com/containernetworking/plugins/pkg/ns"
25 bv "github.com/containernetworking/plugins/pkg/utils/buildversion"
26
27 "github.com/containernetworking/cni/pkg/skel"
28 "github.com/containernetworking/cni/pkg/types"
29 type100 "github.com/containernetworking/cni/pkg/types/100"
30 "github.com/containernetworking/cni/pkg/version"
31 )
32
33 type NetConf struct {
34 types.NetConf
35 CNIOutput string `json:"cniOutput,omitempty"`
36 AddHooks [][]string `json:"addHooks,omitempty"`
37 DelHooks [][]string `json:"delHooks,omitempty"`
38 CheckHooks [][]string `json:"checkHooks,omitempty"`
39 }
40
41 func main() {
42 skel.PluginMain(cmdAdd, cmdCheck, cmdDel, version.All, bv.BuildString("none"))
43 }
44
45 func outputCmdArgs(fp io.Writer, args *skel.CmdArgs) {
46 fmt.Fprintf(fp, `ContainerID: %s
47 Netns: %s
48 IfName: %s
49 Args: %s
50 Path: %s
51 StdinData: %s
52 ----------------------
53 `,
54 args.ContainerID,
55 args.Netns,
56 args.IfName,
57 args.Args,
58 args.Path,
59 string(args.StdinData))
60 }
61
62 func parseConf(data []byte) (*NetConf, error) {
63 conf := &NetConf{}
64 if err := json.Unmarshal(data, &conf); err != nil {
65 return nil, fmt.Errorf("failed to parse")
66 }
67
68 return conf, nil
69 }
70
71 func getResult(netConf *NetConf) *type100.Result {
72 if netConf.RawPrevResult == nil {
73 return &type100.Result{}
74 }
75
76 version.ParsePrevResult(&netConf.NetConf)
77 result, _ := type100.NewResultFromResult(netConf.PrevResult)
78 return result
79 }
80
81 func executeHooks(netnsName string, hooks [][]string) {
82 netns, err := ns.GetNS(netnsName)
83 if err != nil {
84 return
85 }
86 defer netns.Close()
87
88 netns.Do(func(_ ns.NetNS) error {
89 for _, hookStrs := range hooks {
90 hookCmd := hookStrs[0]
91 hookArgs := hookStrs[1:]
92 output, err := exec.Command(hookCmd, hookArgs...).Output()
93 if err != nil {
94 fmt.Fprintf(os.Stderr, "OUTPUT: %v", output)
95 fmt.Fprintf(os.Stderr, "ERR: %v", err)
96 }
97 }
98 return nil
99 })
100 }
101
102 func cmdAdd(args *skel.CmdArgs) error {
103 netConf, _ := parseConf(args.StdinData)
104 // Output CNI
105 if netConf.CNIOutput != "" {
106 fp, _ := os.OpenFile(netConf.CNIOutput, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
107 defer fp.Close()
108 fmt.Fprintf(fp, "CmdAdd\n")
109 outputCmdArgs(fp, args)
110 }
111 // call hooks
112 if netConf.AddHooks != nil {
113 executeHooks(args.Netns, netConf.AddHooks)
114 }
115 return types.PrintResult(getResult(netConf), netConf.CNIVersion)
116 }
117
118 func cmdDel(args *skel.CmdArgs) error {
119 netConf, _ := parseConf(args.StdinData)
120 // Output CNI
121 if netConf.CNIOutput != "" {
122 fp, _ := os.OpenFile(netConf.CNIOutput, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
123 defer fp.Close()
124 fmt.Fprintf(fp, "CmdDel\n")
125 outputCmdArgs(fp, args)
126 }
127 // call hooks
128 if netConf.DelHooks != nil {
129 executeHooks(args.Netns, netConf.DelHooks)
130 }
131 return types.PrintResult(&type100.Result{}, netConf.CNIVersion)
132 }
133
134 func cmdCheck(args *skel.CmdArgs) error {
135 netConf, _ := parseConf(args.StdinData)
136 // Output CNI
137 if netConf.CNIOutput != "" {
138 fp, _ := os.OpenFile(netConf.CNIOutput, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
139 defer fp.Close()
140 fmt.Fprintf(fp, "CmdCheck\n")
141 outputCmdArgs(fp, args)
142 }
143 // call hooks
144 if netConf.CheckHooks != nil {
145 executeHooks(args.Netns, netConf.CheckHooks)
146 }
147 return types.PrintResult(&type100.Result{}, netConf.CNIVersion)
148 }