main.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015 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. package hostlocal
  15. import (
  16. "github.com/containernetworking/cni/plugins/ipam/host-local/backend/disk"
  17. "github.com/containernetworking/cni/pkg/skel"
  18. "github.com/containernetworking/cni/pkg/types"
  19. )
  20. func Main() {
  21. skel.PluginMain(cmdAdd, cmdDel)
  22. }
  23. func cmdAdd(args *skel.CmdArgs) error {
  24. ipamConf, err := LoadIPAMConfig(args.StdinData, args.Args)
  25. if err != nil {
  26. return err
  27. }
  28. store, err := disk.New(ipamConf.Name)
  29. if err != nil {
  30. return err
  31. }
  32. defer store.Close()
  33. allocator, err := NewIPAllocator(ipamConf, store)
  34. if err != nil {
  35. return err
  36. }
  37. ipConf, err := allocator.Get(args.ContainerID)
  38. if err != nil {
  39. return err
  40. }
  41. r := &types.Result{
  42. IP4: ipConf,
  43. }
  44. return r.Print()
  45. }
  46. func cmdDel(args *skel.CmdArgs) error {
  47. ipamConf, err := LoadIPAMConfig(args.StdinData, args.Args)
  48. if err != nil {
  49. return err
  50. }
  51. store, err := disk.New(ipamConf.Name)
  52. if err != nil {
  53. return err
  54. }
  55. defer store.Close()
  56. allocator, err := NewIPAllocator(ipamConf, store)
  57. if err != nil {
  58. return err
  59. }
  60. return allocator.Release(args.ContainerID)
  61. }