Skip to content

Commit

Permalink
Merge branch 'master' into cni110
Browse files Browse the repository at this point in the history
  • Loading branch information
s1061123 committed May 20, 2024
2 parents 52ef802 + 75c0245 commit 50d9403
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 17 deletions.
26 changes: 18 additions & 8 deletions cmd/thin_entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,32 @@ const multusConfTemplate = `{
}
`

func (o *Options) createMultusConfig(prevMasterConfigFileHash []byte) (string, []byte, error) {
// find master file from MultusAutoconfigDir
func (o *Options) getMasterConfigPath() (string, error) {
// Master config file is specified
if o.MultusMasterCNIFileName != "" {
return filepath.Join(o.MultusAutoconfigDir, o.MultusMasterCNIFileName), nil
}

// Pick the alphabetically first config file from MultusAutoconfigDir
files, err := libcni.ConfFiles(o.MultusAutoconfigDir, []string{".conf", ".conflist"})
if err != nil {
return "", nil, fmt.Errorf("cannot find master CNI config in %q: %v", o.MultusAutoconfigDir, err)
return "", fmt.Errorf("cannot find master CNI config in %q: %v", o.MultusAutoconfigDir, err)
}

masterConfigPath := ""
for _, filename := range files {
if !strings.HasPrefix(filepath.Base(filename), "00-multus.conf") {
masterConfigPath = filename
break
return filename, nil
}
}
if masterConfigPath == "" {
return "", nil, fmt.Errorf("cannot find valid master CNI config in %q", o.MultusAutoconfigDir)

// No config file found
return "", fmt.Errorf("cannot find valid master CNI config in %q", o.MultusAutoconfigDir)
}

func (o *Options) createMultusConfig(prevMasterConfigFileHash []byte) (string, []byte, error) {
masterConfigPath, err := o.getMasterConfigPath()
if err != nil {
return "", nil, err
}

masterConfigBytes, masterConfigFileHash, err := getFileAndHash(masterConfigPath)
Expand Down
61 changes: 61 additions & 0 deletions cmd/thin_entrypoint/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,65 @@ var _ = Describe("thin entrypoint testing", func() {
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})

It("Run createMultusConfig(), with options, conflist", func() {
// create directory and files
tmpDir, err := os.MkdirTemp("", "multus_thin_entrypoint_tmp")
Expect(err).NotTo(HaveOccurred())

multusAutoConfigDir := fmt.Sprintf("%s/auto_conf", tmpDir)
cniConfDir := fmt.Sprintf("%s/cni_conf", tmpDir)

Expect(os.Mkdir(multusAutoConfigDir, 0755)).To(Succeed())
Expect(os.Mkdir(cniConfDir, 0755)).To(Succeed())

// create master CNI config
masterCNIConfigFileName := "10-testcni.conf"
masterCNIConfig := `
{
"cniVersion": "1.0.0",
"name": "test1",
"type": "cnitesttype"
}`
Expect(os.WriteFile(fmt.Sprintf("%s/%s", multusAutoConfigDir, masterCNIConfigFileName), []byte(masterCNIConfig), 0755)).To(Succeed())

// create another CNI config
anotherCNIConfigFileName := "09-test2cni.conf" // Alphabetically before masterCNIConfigFileName
anotherCNIConfig := `
{
"cniVersion": "1.0.0",
"name": "test2",
"type": "cnitest2type"
}`
Expect(os.WriteFile(fmt.Sprintf("%s/%s", multusAutoConfigDir, anotherCNIConfigFileName), []byte(anotherCNIConfig), 0755)).To(Succeed())

masterConfigPath, masterConfigHash, err := (&Options{
MultusAutoconfigDir: multusAutoConfigDir,
MultusMasterCNIFileName: masterCNIConfigFileName,
CNIConfDir: cniConfDir,
MultusKubeConfigFileHost: "/etc/foobar_kubeconfig",
}).createMultusConfig(nil)
Expect(err).NotTo(HaveOccurred())

Expect(masterConfigPath).NotTo(Equal(""))
Expect(masterConfigHash).NotTo(Equal(""))

expectedResult :=
`{
"cniVersion": "1.0.0",
"name": "multus-cni-network",
"plugins": [ {
"type": "multus",
"logToStderr": false,
"kubeconfig": "/etc/foobar_kubeconfig",
"delegates": [
{"cniVersion":"1.0.0","name":"test1","type":"cnitesttype"}
]
}]
}
`
conf, err := os.ReadFile(fmt.Sprintf("%s/00-multus.conflist", cniConfDir))
Expect(string(conf)).To(Equal(expectedResult))

Expect(os.RemoveAll(tmpDir)).To(Succeed())
})
})
7 changes: 6 additions & 1 deletion pkg/netutils/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ func deleteDefaultGWResult(result map[string]interface{}, ipv4, ipv6 bool) (map[
return nil, err
}
}
result["routes"] = routes

if len(routes) == 0 {
delete(result, "routes")
} else {
result["routes"] = routes
}

return result, nil
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/netutils/netutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,39 @@ var _ = Describe("netutil cnicache function testing", func() {
Expect(len(result.Result.Routes)).To(Equal(5))
})

It("verify ipv4 default gateway from single routes is removed/added from CNI 1.0.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"cniVersion": "1.0.0",
"dns": {},
"interfaces": [
{
"mac": "0a:c2:e6:3d:45:17",
"name": "net1",
"sandbox": "/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5"
}
],
"ips": [
{
"address": "10.1.1.103/24",
"interface": 0
}
],
"routes": [
{
"dst": "0.0.0.0/0",
"gw": "10.1.1.1"
}
]
}
}`)
newResult1, err := deleteDefaultGWCacheBytes(origResult, true, false)
Expect(err).NotTo(HaveOccurred())
_, err = addDefaultGWCacheBytes(newResult1, []net.IP{net.ParseIP("10.1.1.1")})
Expect(err).NotTo(HaveOccurred())
})

It("verify ipv6 default gateway is removed from CNI 1.0.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
Expand Down Expand Up @@ -713,6 +746,39 @@ var _ = Describe("netutil cnicache function testing", func() {
Expect(len(result.Result.Routes)).To(Equal(5))
})

It("verify ipv6 default gateway from single routes is removed/added from CNI 1.0.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"cniVersion": "1.0.0",
"dns": {},
"interfaces": [
{
"mac": "0a:c2:e6:3d:45:17",
"name": "net1",
"sandbox": "/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5"
}
],
"ips": [
{
"address": "10::1:1:103/64",
"interface": 0
}
],
"routes": [
{
"dst": "::0/0",
"gw": "10::1:1:1"
}
]
}
}`)
newResult1, err := deleteDefaultGWCacheBytes(origResult, false, true)
Expect(err).NotTo(HaveOccurred())
_, err = addDefaultGWCacheBytes(newResult1, []net.IP{net.ParseIP("10::1:1:1")})
Expect(err).NotTo(HaveOccurred())
})

It("verify ipv4 default gateway is added to CNI 0.1.0/0.2.0 results without routes", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
Expand Down
9 changes: 9 additions & 0 deletions pkg/server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,12 @@ func WaitUntilAPIReady(socketPath string) error {
return err == nil, nil
})
}

// CheckAPIReadyNow checks API readiness once
func CheckAPIReadyNow(socketPath string) error {
_, err := DoCNI(GetAPIEndpoint(MultusHealthAPIEndpoint), nil, SocketPath(socketPath))
if err != nil {
return fmt.Errorf("CheckAPIReadyNow: Daemon not reachable over socketfile: %v", err)
}
return nil
}
19 changes: 11 additions & 8 deletions pkg/server/api/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ type ShimNetConf struct {
LogToStderr bool `json:"logToStderr,omitempty"`
}

// Define a type for API readiness check functions

Check warning on line 43 in pkg/server/api/shim.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

comment on exported type APIReadyCheckFunc should be of the form "APIReadyCheckFunc ..." (with optional leading article)

Check warning on line 43 in pkg/server/api/shim.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

comment on exported type APIReadyCheckFunc should be of the form "APIReadyCheckFunc ..." (with optional leading article)
type APIReadyCheckFunc func(string) error

Check warning on line 44 in pkg/server/api/shim.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

type name will be used as api.APIReadyCheckFunc by other packages, and that stutters; consider calling this ReadyCheckFunc

Check warning on line 44 in pkg/server/api/shim.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

type name will be used as api.APIReadyCheckFunc by other packages, and that stutters; consider calling this ReadyCheckFunc

// CmdAdd implements the CNI spec ADD command handler
func CmdAdd(args *skel.CmdArgs) error {
response, cniVersion, err := postRequest(args)
response, cniVersion, err := postRequest(args, WaitUntilAPIReady)
if err != nil {
return logging.Errorf("CmdAdd (shim): %v", err)
}
Expand All @@ -53,7 +56,7 @@ func CmdAdd(args *skel.CmdArgs) error {

// CmdCheck implements the CNI spec CHECK command handler
func CmdCheck(args *skel.CmdArgs) error {
_, _, err := postRequest(args)
_, _, err := postRequest(args, WaitUntilAPIReady)
if err != nil {
return logging.Errorf("CmdCheck (shim): %v", err)
}
Expand All @@ -63,7 +66,7 @@ func CmdCheck(args *skel.CmdArgs) error {

// CmdDel implements the CNI spec DEL command handler
func CmdDel(args *skel.CmdArgs) error {
_, _, err := postRequest(args)
_, _, err := postRequest(args, CheckAPIReadyNow)
if err != nil {
// No error in DEL (as of CNI spec)
logging.Errorf("CmdDel (shim): %v", err)
Expand All @@ -73,7 +76,7 @@ func CmdDel(args *skel.CmdArgs) error {

// CmdGC implements the CNI spec GC command handler
func CmdGC(args *skel.CmdArgs) error {
_, _, err := postRequest(args)
_, _, err := postRequest(args, WaitUntilAPIReady)
if err != nil {
return logging.Errorf("CmdGC (shim): %v", err)
}
Expand All @@ -82,21 +85,21 @@ func CmdGC(args *skel.CmdArgs) error {

// CmdStatus implements the CNI spec STATUS command handler
func CmdStatus(args *skel.CmdArgs) error {
_, _, err := postRequest(args)
_, _, err := postRequest(args, WaitUntilAPIReady)
if err != nil {
return logging.Errorf("CmdStatus (shim): %v", err)
}
return nil
}

func postRequest(args *skel.CmdArgs) (*Response, string, error) {
func postRequest(args *skel.CmdArgs, readinessCheck APIReadyCheckFunc) (*Response, string, error) {
multusShimConfig, err := shimConfig(args.StdinData)
if err != nil {
return nil, "", fmt.Errorf("invalid CNI configuration passed to multus-shim: %w", err)
}

// check API readiness
if err := WaitUntilAPIReady(multusShimConfig.MultusSocketDir); err != nil {
// Execute the readiness check as necessary (e.g. don't wait on CNI DEL)
if err := readinessCheck(multusShimConfig.MultusSocketDir); err != nil {
return nil, multusShimConfig.CNIVersion, err
}

Expand Down

0 comments on commit 50d9403

Please sign in to comment.