diff --git a/go.mod b/go.mod index 108d0bd7..fd29d90e 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ replace ( require ( github.com/harvester/harvester v1.3.0 + github.com/harvester/harvester-load-balancer v0.3.0 github.com/harvester/harvester-network-controller v0.3.5 github.com/hashicorp/terraform-plugin-docs v0.4.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.6.1 @@ -69,7 +70,10 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cisco-open/operator-tools v0.29.0 // indirect github.com/containernetworking/cni v1.1.2 // indirect + github.com/containernetworking/plugins v1.1.1 // indirect + github.com/coreos/go-iptables v0.6.0 // indirect github.com/coreos/prometheus-operator v0.38.3 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/deckarep/golang-set/v2 v2.1.0 // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect @@ -181,12 +185,18 @@ require ( github.com/rancher/system-upgrade-controller/pkg/apis v0.0.0-20210727200656-10b094e30007 // indirect github.com/robfig/cron v1.2.0 // indirect github.com/russross/blackfriday v1.6.0 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1 // indirect github.com/shirou/gopsutil/v3 v3.23.7 // indirect github.com/shopspring/decimal v1.3.1 // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/tevino/tcp-shaker v0.0.0-20191112104505-00eab0aefc80 // indirect github.com/ulikunitz/xz v0.5.10 // indirect + github.com/urfave/cli v1.22.13 // indirect + github.com/vishvananda/netlink v1.2.1-beta.2 // indirect + github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect github.com/zclconf/go-cty v1.8.2 // indirect diff --git a/go.sum b/go.sum index ebfd21df..93d18d04 100644 --- a/go.sum +++ b/go.sum @@ -1358,6 +1358,7 @@ github.com/Azure/go-autorest/autorest/validation v0.2.1-0.20191028180845-3492b2a github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc= github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= @@ -1551,11 +1552,15 @@ github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u9 github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= github.com/containernetworking/cni v1.1.2 h1:wtRGZVv7olUHMOqouPpn3cXJWpJgM6+EUl31EQbXALQ= github.com/containernetworking/cni v1.1.2/go.mod h1:sDpYKmGVENF3s6uvMvGgldDWeG8dMxakj/u+i9ht9vw= +github.com/containernetworking/plugins v1.1.1 h1:+AGfFigZ5TiQH00vhR8qPeSatj53eNGz0C1d3wVYlHE= +github.com/containernetworking/plugins v1.1.1/go.mod h1:Sr5TH/eBsGLXK/h71HeLfX19sZPp3ry5uHSkI4LPxV8= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/etcd v3.3.15+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= +github.com/coreos/go-iptables v0.6.0 h1:is9qnZMPYjLd8LYqmm/qlE+wwEgJIkTYdhV3rfZo4jk= +github.com/coreos/go-iptables v0.6.0/go.mod h1:Qe8Bv2Xik5FyTXwgIbLAnv2sWSBmvWdFETJConOQ//Q= github.com/coreos/go-oidc v2.1.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -1566,10 +1571,12 @@ github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfc github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/coreos/prometheus-operator v0.38.3 h1:EaiEJFZ6uEV5PcuvZ272kB3Z/Ki8kViMeVhMDKYx7jI= github.com/coreos/prometheus-operator v0.38.3/go.mod h1:nHyLLa5yyGbUNTcRXIbofXLsrg90aIxt2HQp5tT9Fy0= +github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -2076,6 +2083,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 h1:Wqo399gCIufwto+VfwCSvsnfGpF github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYpldc7gVz2KMQwJ/QYCDIa7XU= github.com/harvester/harvester v1.3.0 h1:+0Ls8hjD1fIF2SyT8GPAYjaxbdO/224IwafW2maWtLs= github.com/harvester/harvester v1.3.0/go.mod h1:R0aCUSN4C6P6lcIyse4xdSXkpbB4s9eLz5ok6TTE98w= +github.com/harvester/harvester-load-balancer v0.3.0 h1:R4ymvCTFraic+zIMNYFPtIf8KAR3S9dnHdPRgHn85gk= +github.com/harvester/harvester-load-balancer v0.3.0/go.mod h1:bbsdXuGkVeoMuzb2yLrrc8RXmm4hpIe9zdoreziUUaA= github.com/harvester/harvester-network-controller v0.3.5 h1:p6c+JK4v3YmLEsuGs4pX8xww9ykW0Wx89PKqbHqGKmE= github.com/harvester/harvester-network-controller v0.3.5/go.mod h1:VW3a3dnZTp1eFMfxUUXWTvgqroSEHrXbahsOvM7MieE= github.com/harvester/node-manager v0.1.5-0.20230614075852-de2da3ef3aca h1:ySmceYltYR2wmeAvifoXtHQOK/N6v7z7Rkaq/6IPJ5A= @@ -2701,11 +2710,14 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww= github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1 h1:ZFfeKAhIQiiOrQaI3/znw0gOmYpO28Tcu1YaqMa/jtQ= +github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4= github.com/samuel/go-zookeeper v0.0.0-20190810000440-0ceca61e4d75/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4= @@ -2806,6 +2818,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/substrait-io/substrait-go v0.4.2/go.mod h1:qhpnLmrcvAnlZsUyPXZRqldiHapPTXC3t7xFgDi3aQg= +github.com/tevino/tcp-shaker v0.0.0-20191112104505-00eab0aefc80 h1:sb31WKdNlj2prwQwgjHHkayI3cKUlS+V4mmImc1GGpw= +github.com/tevino/tcp-shaker v0.0.0-20191112104505-00eab0aefc80/go.mod h1:2F2ToSBehSmJcaQPE0lNhfH3xv+9Z2vH1a/d9fFw1nE= github.com/thanos-io/thanos v0.11.0/go.mod h1:N/Yes7J68KqvmY+xM6J5CJqEvWIvKSR5sqGtmuD6wDc= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= @@ -2827,8 +2841,15 @@ github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/urfave/cli v1.22.13 h1:wsLILXG8qCJNse/qAgLNf23737Cx05GflHg/PJGe1Ok= +github.com/urfave/cli v1.22.13/go.mod h1:VufqObjsMTF2BBwKawpx9R8eAneNEWhoO0yx8Vd+FkE= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw= +github.com/vishvananda/netlink v1.2.1-beta.2 h1:Llsql0lnQEbHj0I1OuKyp8otXp0r3q0mPkuhwHfStVs= +github.com/vishvananda/netlink v1.2.1-beta.2/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho= +github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= +github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg= +github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= @@ -3288,6 +3309,7 @@ golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191025021431-6c3a3bfe00ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191110163157-d32e6e3b99c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191112214154-59a1497f0cea/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -3303,6 +3325,7 @@ golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -3315,6 +3338,7 @@ golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/provider/ippool/datasource_ippool.go b/internal/provider/ippool/datasource_ippool.go new file mode 100644 index 00000000..21de9701 --- /dev/null +++ b/internal/provider/ippool/datasource_ippool.go @@ -0,0 +1,34 @@ +package ippool + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/harvester/terraform-provider-harvester/pkg/client" + "github.com/harvester/terraform-provider-harvester/pkg/constants" +) + +func DataSourceIPPool() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIPPoolRead, + Schema: DataSourceSchema(), + } +} + +func dataSourceIPPoolRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*client.Client) + name := data.Get(constants.FieldCommonName).(string) + + ippool, err := c.HarvesterLoadbalancerClient. + LoadbalancerV1beta1(). + IPPools(). + Get(ctx, name, metav1.GetOptions{}) + if err != nil { + return diag.FromErr(err) + } + return diag.FromErr(resourceIPPoolImport(data, ippool)) +} diff --git a/internal/provider/ippool/resource_ippool.go b/internal/provider/ippool/resource_ippool.go new file mode 100644 index 00000000..08a3096d --- /dev/null +++ b/internal/provider/ippool/resource_ippool.go @@ -0,0 +1,127 @@ +package ippool + +import ( + "context" + "time" + + loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/harvester/terraform-provider-harvester/internal/util" + "github.com/harvester/terraform-provider-harvester/pkg/client" + "github.com/harvester/terraform-provider-harvester/pkg/constants" + "github.com/harvester/terraform-provider-harvester/pkg/helper" + "github.com/harvester/terraform-provider-harvester/pkg/importer" +) + +func ResourceIPPool() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceIPPoolCreate, + ReadContext: resourceIPPoolRead, + UpdateContext: resourceIPPoolUpdate, + DeleteContext: resourceIPPoolDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + Schema: Schema(), + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(2 * time.Minute), + Read: schema.DefaultTimeout(2 * time.Minute), + Update: schema.DefaultTimeout(2 * time.Minute), + Delete: schema.DefaultTimeout(2 * time.Minute), + Default: schema.DefaultTimeout(2 * time.Minute), + }, + } +} + +func resourceIPPoolCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*client.Client) + name := data.Get(constants.FieldCommonName).(string) + toCreate, err := util.ResourceConstruct(data, Creator(name)) + if err != nil { + return diag.FromErr(err) + } + ippool, err := c.HarvesterLoadbalancerClient. + LoadbalancerV1beta1(). + IPPools(). + Create(ctx, toCreate.(*loadbalancerv1.IPPool), metav1.CreateOptions{}) + if err != nil { + return diag.FromErr(err) + } + data.SetId(helper.BuildID("", name)) + return diag.FromErr(resourceIPPoolImport(data, ippool)) +} + +func resourceIPPoolRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*client.Client) + _, name, err := helper.IDParts(data.Id()) + if err != nil { + return diag.FromErr(err) + } + + ippool, err := c.HarvesterLoadbalancerClient. + LoadbalancerV1beta1(). + IPPools().Get(ctx, name, metav1.GetOptions{}) + if err != nil { + return diag.FromErr(err) + } + + return diag.FromErr(resourceIPPoolImport(data, ippool)) +} + +func resourceIPPoolUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*client.Client) + _, name, err := helper.IDParts(data.Id()) + if err != nil { + return diag.FromErr(err) + } + + obj, err := c.HarvesterLoadbalancerClient. + LoadbalancerV1beta1(). + IPPools().Get(ctx, name, metav1.GetOptions{}) + if err != nil { + return diag.FromErr(err) + } + + toUpdate, err := util.ResourceConstruct(data, Updater(obj)) + if err != nil { + return diag.FromErr(err) + } + + ippool, err := c.HarvesterLoadbalancerClient. + LoadbalancerV1beta1(). + IPPools().Update(ctx, toUpdate.(*loadbalancerv1.IPPool), metav1.UpdateOptions{}) + if err != nil { + return diag.FromErr(err) + } + return diag.FromErr(resourceIPPoolImport(data, ippool)) +} + +func resourceIPPoolDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*client.Client) + _, name, err := helper.IDParts(data.Id()) + if err != nil { + return diag.FromErr(err) + } + + err = c.HarvesterLoadbalancerClient. + LoadbalancerV1beta1(). + IPPools().Delete(ctx, name, metav1.DeleteOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + return diag.FromErr(err) + } + + return diag.FromErr(nil) +} + +func resourceIPPoolImport(data *schema.ResourceData, obj *loadbalancerv1.IPPool) error { + stateGetter, err := importer.ResourceIPPoolStateGetter(obj) + if err != nil { + return err + } + return util.ResourceStatesSet(data, stateGetter) +} diff --git a/internal/provider/ippool/resource_ippool_constructor.go b/internal/provider/ippool/resource_ippool_constructor.go new file mode 100644 index 00000000..fc26644f --- /dev/null +++ b/internal/provider/ippool/resource_ippool_constructor.go @@ -0,0 +1,93 @@ +package ippool + +import ( + loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1" + + "github.com/harvester/terraform-provider-harvester/internal/util" + "github.com/harvester/terraform-provider-harvester/pkg/constants" +) + +var ( + _ util.Constructor = &Constructor{} +) + +type Constructor struct { + IPPool *loadbalancerv1.IPPool +} + +func (c *Constructor) Setup() util.Processors { + processors := util.NewProcessors(). + Tags(&c.IPPool.Labels). + String(constants.FieldIPPoolDescription, &c.IPPool.Spec.Description, false) + + subresourceProcessors := []util.Processor{ + { + Field: constants.SubresourceTypeIPPoolRange, + Parser: c.subresourceIPPoolRangeParser, + Required: true, + }, + { + Field: constants.SubresourceTypeIPPoolSelector, + Parser: c.subresourceIPPoolSelectorParser, + Required: false, + }, + } + + return append(processors, subresourceProcessors...) +} + +func (c *Constructor) Validate() error { + return nil +} + +func (c *Constructor) Result() (interface{}, error) { + return c.IPPool, nil +} + +func newIPPoolConstructor(ippool *loadbalancerv1.IPPool) util.Constructor { + return &Constructor{ + IPPool: ippool, + } +} + +func Creator(name string) util.Constructor { + ippool := &loadbalancerv1.IPPool{ + ObjectMeta: util.NewObjectMeta("", name), + } + return newIPPoolConstructor(ippool) +} + +func Updater(ippool *loadbalancerv1.IPPool) util.Constructor { + ippool.Spec.Ranges = []loadbalancerv1.Range{} + ippool.Spec.Selector = loadbalancerv1.Selector{} + return newIPPoolConstructor(ippool) +} + +func (c *Constructor) subresourceIPPoolRangeParser(data interface{}) error { + ippoolRange := data.(map[string]interface{}) + start := ippoolRange[constants.FieldRangeStart].(string) + end := ippoolRange[constants.FieldRangeEnd].(string) + subnet := ippoolRange[constants.FieldRangeSubnet].(string) + gateway := ippoolRange[constants.FieldRangeGateway].(string) + + c.IPPool.Spec.Ranges = append(c.IPPool.Spec.Ranges, loadbalancerv1.Range{ + RangeStart: start, + RangeEnd: end, + Subnet: subnet, + Gateway: gateway, + }) + return nil +} + +func (c *Constructor) subresourceIPPoolSelectorParser(data interface{}) error { + ippoolSelector := data.(map[string]interface{}) + + priority := uint32(ippoolSelector[constants.FieldSelectorPriority].(int)) + network := ippoolSelector[constants.FieldSelectorNetwork].(string) + + c.IPPool.Spec.Selector = loadbalancerv1.Selector{ + Priority: priority, + Network: network, + } + return nil +} diff --git a/internal/provider/ippool/schema_ippool.go b/internal/provider/ippool/schema_ippool.go new file mode 100644 index 00000000..44a6a67f --- /dev/null +++ b/internal/provider/ippool/schema_ippool.go @@ -0,0 +1,111 @@ +package ippool + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/harvester/terraform-provider-harvester/internal/util" + "github.com/harvester/terraform-provider-harvester/pkg/constants" +) + +func Schema() map[string]*schema.Schema { + s := map[string]*schema.Schema{ + constants.FieldIPPoolDescription: { + Type: schema.TypeString, + Optional: true, + }, + constants.SubresourceTypeIPPoolRange: { + Type: schema.TypeList, + Required: true, + MinItems: 1, + Elem: &schema.Resource{ + Schema: subresourceSchemaIPPoolRange(), + }, + Description: "IP Range belonging to this pool, can be given multiple times", + }, + constants.SubresourceTypeIPPoolSelector: { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: subresourceSchemaIPPoolSelector(), + }, + }, + } + util.NonNamespacedSchemaWrap(s) + return s +} + +func DataSourceSchema() map[string]*schema.Schema { + return util.DataSourceSchemaWrap(Schema()) +} + +func subresourceSchemaIPPoolRange() map[string]*schema.Schema { + s := map[string]*schema.Schema{ + constants.FieldRangeStart: { + Type: schema.TypeString, + Required: true, + Description: "", + }, + constants.FieldRangeEnd: { + Type: schema.TypeString, + Required: true, + Description: "", + }, + constants.FieldRangeSubnet: { + Type: schema.TypeString, + Required: true, + Description: "", + }, + constants.FieldRangeGateway: { + Type: schema.TypeString, + Required: true, + Description: "", + }, + } + return s +} + +func subresourceSchemaIPPoolSelector() map[string]*schema.Schema { + s := map[string]*schema.Schema{ + constants.FieldSelectorPriority: { + Type: schema.TypeInt, + Optional: true, + Description: "Priority of the IP pool. Large numbers have higher priority", + }, + constants.FieldSelectorNetwork: { + Type: schema.TypeString, + Optional: true, + Description: "Namespace/name of the VM network", + }, + constants.FieldSelectorScope: { + Type: schema.TypeList, + Optional: true, + Description: "Scope of the IP pool", + Elem: &schema.Resource{ + Schema: subresourceSchemaIPPoolSelectorScope(), + }, + }, + } + return s +} + +func subresourceSchemaIPPoolSelectorScope() map[string]*schema.Schema { + s := map[string]*schema.Schema{ + constants.FieldScopeProject: { + Type: schema.TypeString, + Optional: true, + Description: "Name of the project", + }, + constants.FieldScopeNamespace: { + Type: schema.TypeString, + Optional: true, + Description: "Namespace of the VMs of the guest cluster", + }, + constants.FieldScopeGuestCluster: { + Type: schema.TypeString, + Optional: true, + Description: "Name of the guest cluster", + }, + } + return s +} diff --git a/internal/provider/loadbalancer/datasource_loadbalancer.go b/internal/provider/loadbalancer/datasource_loadbalancer.go new file mode 100644 index 00000000..7fdd7b7e --- /dev/null +++ b/internal/provider/loadbalancer/datasource_loadbalancer.go @@ -0,0 +1,35 @@ +package loadbalancer + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/harvester/terraform-provider-harvester/pkg/client" + "github.com/harvester/terraform-provider-harvester/pkg/constants" +) + +func DataSourceLoadBalancer() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceLoadBalancerRead, + Schema: DataSourceSchema(), + } +} + +func dataSourceLoadBalancerRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*client.Client) + namespace := data.Get(constants.FieldCommonNamespace).(string) + name := data.Get(constants.FieldCommonName).(string) + + loadbalancer, err := c.HarvesterLoadbalancerClient. + LoadbalancerV1beta1(). + LoadBalancers(namespace). + Get(ctx, name, metav1.GetOptions{}) + if err != nil { + return diag.FromErr(err) + } + return diag.FromErr(resourceLoadBalancerImport(data, loadbalancer)) +} diff --git a/internal/provider/loadbalancer/resource_loadbalancer.go b/internal/provider/loadbalancer/resource_loadbalancer.go new file mode 100644 index 00000000..6e439c8f --- /dev/null +++ b/internal/provider/loadbalancer/resource_loadbalancer.go @@ -0,0 +1,77 @@ +package loadbalancer + +import ( + "context" + "time" + + loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/harvester/terraform-provider-harvester/internal/util" + "github.com/harvester/terraform-provider-harvester/pkg/client" + "github.com/harvester/terraform-provider-harvester/pkg/constants" + "github.com/harvester/terraform-provider-harvester/pkg/helper" + "github.com/harvester/terraform-provider-harvester/pkg/importer" +) + +func ResourceLoadBalancer() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceLoadBalancerCreate, + ReadContext: resourceLoadBalancerRead, + UpdateContext: resourceLoadBalancerUpdate, + DeleteContext: resourceLoadBalancerDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + Schema: Schema(), + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(2 * time.Minute), + Read: schema.DefaultTimeout(2 * time.Minute), + Update: schema.DefaultTimeout(2 * time.Minute), + Delete: schema.DefaultTimeout(2 * time.Minute), + Default: schema.DefaultTimeout(2 * time.Minute), + }, + } +} + +func resourceLoadBalancerCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*client.Client) + namespace := data.Get(constants.FieldCommonNamespace).(string) + name := data.Get(constants.FieldCommonName).(string) + toCreate, err := util.ResourceConstruct(data, Creator(namespace, name)) + if err != nil { + return diag.FromErr(err) + } + lb, err := c.HarvesterLoadbalancerClient. + LoadbalancerV1beta1(). + LoadBalancers(namespace). + Create(ctx, toCreate.(*loadbalancerv1.LoadBalancer), metav1.CreateOptions{}) + if err != nil { + return diag.FromErr(err) + } + data.SetId(helper.BuildID(namespace, name)) + return diag.FromErr(resourceLoadBalancerImport(data, lb)) +} + +func resourceLoadBalancerRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + return diag.FromErr(nil) +} + +func resourceLoadBalancerUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + return diag.FromErr(nil) +} + +func resourceLoadBalancerDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + return diag.FromErr(nil) +} + +func resourceLoadBalancerImport(data *schema.ResourceData, obj *loadbalancerv1.LoadBalancer) error { + stateGetter, err := importer.ResourceLoadBalancerStateGetter(obj) + if err != nil { + return err + } + return util.ResourceStatesSet(data, stateGetter) +} diff --git a/internal/provider/loadbalancer/resource_loadbalancer_constructor.go b/internal/provider/loadbalancer/resource_loadbalancer_constructor.go new file mode 100644 index 00000000..16300c58 --- /dev/null +++ b/internal/provider/loadbalancer/resource_loadbalancer_constructor.go @@ -0,0 +1,48 @@ +package loadbalancer + +import ( + loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1" + + "github.com/harvester/terraform-provider-harvester/internal/util" + "github.com/harvester/terraform-provider-harvester/pkg/constants" +) + +var ( + _ util.Constructor = &Constructor{} +) + +type Constructor struct { + LoadBalancer *loadbalancerv1.LoadBalancer +} + +func (c *Constructor) Setup() util.Processors { + return util.NewProcessors(). + Tags(&c.LoadBalancer.Labels). + Description(&c.LoadBalancer.Annotations). + String(constants.FieldLoadBalancerDescription, &c.LoadBalancer.Spec.Description, false) +} + +func (c *Constructor) Validate() error { + return nil +} + +func (c *Constructor) Result() (interface{}, error) { + return c.LoadBalancer, nil +} + +func newLoadBalancerConstructor(loadbalancer *loadbalancerv1.LoadBalancer) util.Constructor { + return &Constructor{ + LoadBalancer: loadbalancer, + } +} + +func Creator(namespace, name string) util.Constructor { + loadbalancer := &loadbalancerv1.LoadBalancer{ + ObjectMeta: util.NewObjectMeta(namespace, name), + } + return newLoadBalancerConstructor(loadbalancer) +} + +func Updater(loadbalancer *loadbalancerv1.LoadBalancer) util.Constructor { + return newLoadBalancerConstructor(loadbalancer) +} diff --git a/internal/provider/loadbalancer/schema_loadbalancer.go b/internal/provider/loadbalancer/schema_loadbalancer.go new file mode 100644 index 00000000..041175c3 --- /dev/null +++ b/internal/provider/loadbalancer/schema_loadbalancer.go @@ -0,0 +1,100 @@ +package loadbalancer + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + + "github.com/harvester/terraform-provider-harvester/internal/util" + "github.com/harvester/terraform-provider-harvester/pkg/constants" +) + +func Schema() map[string]*schema.Schema { + s := map[string]*schema.Schema{ + constants.FieldLoadBalancerDescription: { + Type: schema.TypeString, + Optional: true, + }, + constants.FieldLoadBalancerWorkloadType: { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + constants.LoadBalancerWorkloadTypeVM, + constants.LoadBalancerWorkloadTypeCluster, + }, false), + Description: "Can be `vm` or `cluster`", + }, + constants.FieldLoadBalancerIPAM: { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + constants.LoadBalancerIPAMPool, + constants.LoadBalancerIPAMDHCP, + }, false), + Description: "Where the load balancer gets its IP address from. Can be `dhcp` or `pool`.", + }, + constants.FieldLoadBalancerIPPool: { + Type: schema.TypeString, + Optional: true, + Description: "Which IP pool to get the IP address from.", + }, + constants.SubresourceTypeLoadBalancerListener: { + Type: schema.TypeList, + Required: true, + MinItems: 1, + Description: "", + Elem: &schema.Resource{ + Schema: subresourceSchemaLoadBalancerListener(), + }, + }, + constants.FieldLoadBalancerBackendServerSelector: { + Type: schema.TypeMap, + Optional: true, + Description: "", + }, + constants.SubresourceTypeLoadBalancerHealthCheck: { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Description: "", + Elem: &schema.Resource{ + Schema: subresourceSchemaLoadBalancerHealthCheck(), + }, + }, + } + util.NamespacedSchemaWrap(s, false) + return s +} + +func DataSourceSchema() map[string]*schema.Schema { + return util.DataSourceSchemaWrap(Schema()) +} + +func subresourceSchemaLoadBalancerListener() map[string]*schema.Schema { + s := map[string]*schema.Schema{ + constants.FieldListenerName: { + Type: schema.TypeString, + Optional: true, + }, + constants.FieldListenerPort: { + Type: schema.TypeInt, + Required: true, + Description: "", + }, + constants.FieldListenerProtocol: { + Type: schema.TypeString, + Required: true, + Description: "", + }, + constants.FieldListenerBackendPort: { + Type: schema.TypeInt, + Required: true, + Description: "", + }, + } + return s +} + +func subresourceSchemaLoadBalancerHealthCheck() map[string]*schema.Schema { + s := map[string]*schema.Schema{} + return s +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 0206e983..9a66e3ad 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -13,7 +13,9 @@ import ( "github.com/harvester/terraform-provider-harvester/internal/provider/cloudinitsecret" "github.com/harvester/terraform-provider-harvester/internal/provider/clusternetwork" "github.com/harvester/terraform-provider-harvester/internal/provider/image" + "github.com/harvester/terraform-provider-harvester/internal/provider/ippool" "github.com/harvester/terraform-provider-harvester/internal/provider/keypair" + "github.com/harvester/terraform-provider-harvester/internal/provider/loadbalancer" "github.com/harvester/terraform-provider-harvester/internal/provider/network" "github.com/harvester/terraform-provider-harvester/internal/provider/storageclass" "github.com/harvester/terraform-provider-harvester/internal/provider/virtualmachine" @@ -40,26 +42,30 @@ func Provider() *schema.Provider { }, }, DataSourcesMap: map[string]*schema.Resource{ + constants.ResourceTypeCloudInitSecret: cloudinitsecret.DataSourceCloudInitSecret(), + constants.ResourceTypeClusterNetwork: clusternetwork.DataSourceClusterNetwork(), constants.ResourceTypeImage: image.DataSourceImage(), + constants.ResourceTypeIPPool: ippool.DataSourceIPPool(), constants.ResourceTypeKeyPair: keypair.DataSourceKeypair(), + constants.ResourceTypeLoadBalancer: loadbalancer.DataSourceLoadBalancer(), constants.ResourceTypeNetwork: network.DataSourceNetwork(), - constants.ResourceTypeVirtualMachine: virtualmachine.DataSourceVirtualMachine(), - constants.ResourceTypeVolume: volume.DataSourceVolume(), - constants.ResourceTypeClusterNetwork: clusternetwork.DataSourceClusterNetwork(), constants.ResourceTypeStorageClass: storageclass.DataSourceStorageClass(), constants.ResourceTypeVLANConfig: vlanconfig.DataSourceVLANConfig(), - constants.ResourceTypeCloudInitSecret: cloudinitsecret.DataSourceCloudInitSecret(), + constants.ResourceTypeVirtualMachine: virtualmachine.DataSourceVirtualMachine(), + constants.ResourceTypeVolume: volume.DataSourceVolume(), }, ResourcesMap: map[string]*schema.Resource{ + constants.ResourceTypeCloudInitSecret: cloudinitsecret.ResourceCloudInitSecret(), + constants.ResourceTypeClusterNetwork: clusternetwork.ResourceClusterNetwork(), constants.ResourceTypeImage: image.ResourceImage(), + constants.ResourceTypeIPPool: ippool.ResourceIPPool(), constants.ResourceTypeKeyPair: keypair.ResourceKeypair(), + constants.ResourceTypeLoadBalancer: loadbalancer.ResourceLoadBalancer(), constants.ResourceTypeNetwork: network.ResourceNetwork(), - constants.ResourceTypeVirtualMachine: virtualmachine.ResourceVirtualMachine(), - constants.ResourceTypeVolume: volume.ResourceVolume(), - constants.ResourceTypeClusterNetwork: clusternetwork.ResourceClusterNetwork(), constants.ResourceTypeStorageClass: storageclass.ResourceStorageClass(), constants.ResourceTypeVLANConfig: vlanconfig.ResourceVLANConfig(), - constants.ResourceTypeCloudInitSecret: cloudinitsecret.ResourceCloudInitSecret(), + constants.ResourceTypeVirtualMachine: virtualmachine.ResourceVirtualMachine(), + constants.ResourceTypeVolume: volume.ResourceVolume(), }, ConfigureContextFunc: providerConfig, } diff --git a/internal/tests/resource_clusternetwork_test.go b/internal/tests/resource_clusternetwork_test.go new file mode 100644 index 00000000..4160b494 --- /dev/null +++ b/internal/tests/resource_clusternetwork_test.go @@ -0,0 +1,15 @@ +package tests + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestClusterNetworkRead(t *testing.T) { + resource.Test(t, resource.TestCase{ + Steps: []resource.TestStep{ + {}, + }, + }) +} diff --git a/pkg/client/client.go b/pkg/client/client.go index 539dd5bb..32989de5 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -9,15 +9,18 @@ import ( "k8s.io/client-go/kubernetes" storageclient "k8s.io/client-go/kubernetes/typed/storage/v1" "k8s.io/client-go/rest" + + harvloadbalancerclient "github.com/harvester/harvester-load-balancer/pkg/generated/clientset/versioned" ) type Client struct { - RestConfig *rest.Config - KubeVirtSubresourceClient *rest.RESTClient - KubeClient *kubernetes.Clientset - StorageClassClient *storageclient.StorageV1Client - HarvesterClient *harvclient.Clientset - HarvesterNetworkClient *harvnetworkclient.Clientset + RestConfig *rest.Config + KubeVirtSubresourceClient *rest.RESTClient + KubeClient *kubernetes.Clientset + StorageClassClient *storageclient.StorageV1Client + HarvesterClient *harvclient.Clientset + HarvesterNetworkClient *harvnetworkclient.Clientset + HarvesterLoadbalancerClient *harvloadbalancerclient.Clientset } func NewClient(kubeConfig, kubeContext string) (*Client, error) { @@ -50,12 +53,17 @@ func NewClient(kubeConfig, kubeContext string) (*Client, error) { if err != nil { return nil, err } + harvLoadbalancerClient, err := harvloadbalancerclient.NewForConfig(restConfig) + if err != nil { + return nil, err + } return &Client{ - RestConfig: restConfig, - KubeVirtSubresourceClient: restClient, - KubeClient: kubeClient, - StorageClassClient: storageClassClient, - HarvesterClient: harvClient, - HarvesterNetworkClient: harvNetworkClient, + RestConfig: restConfig, + KubeVirtSubresourceClient: restClient, + KubeClient: kubeClient, + StorageClassClient: storageClassClient, + HarvesterClient: harvClient, + HarvesterNetworkClient: harvNetworkClient, + HarvesterLoadbalancerClient: harvLoadbalancerClient, }, nil } diff --git a/pkg/constants/constants_ippool.go b/pkg/constants/constants_ippool.go new file mode 100644 index 00000000..e2f847bf --- /dev/null +++ b/pkg/constants/constants_ippool.go @@ -0,0 +1,32 @@ +package constants + +const ( + ResourceTypeIPPool = "harvester_ippool" + + FieldIPPoolDescription = "description" +) + +const ( + SubresourceTypeIPPoolRange = "range" + + FieldRangeStart = "start" + FieldRangeEnd = "end" + FieldRangeSubnet = "subnet" + FieldRangeGateway = "gateway" +) + +const ( + SubresourceTypeIPPoolSelector = "selector" + + FieldSelectorPriority = "priority" + FieldSelectorNetwork = "network" + FieldSelectorScope = "scope" +) + +const ( + SubresourceTypeIPPoolSelectorScope = "scope" + + FieldScopeProject = "project" + FieldScopeNamespace = "namespace" + FieldScopeGuestCluster = "guest_cluster" +) diff --git a/pkg/constants/constants_loadbalancer.go b/pkg/constants/constants_loadbalancer.go new file mode 100644 index 00000000..d1a46d64 --- /dev/null +++ b/pkg/constants/constants_loadbalancer.go @@ -0,0 +1,40 @@ +package constants + +const ( + ResourceTypeLoadBalancer = "harvester_loadbalancer" + + FieldLoadBalancerDescription = "description" + FieldLoadBalancerWorkloadType = "workload_type" + FieldLoadBalancerIPAM = "ipam" + FieldLoadBalancerIPPool = "ippool" + FieldLoadBalancerBackendServerSelector = "backend_server_selector" +) + +const ( + SubresourceTypeLoadBalancerListener = "listener" + + FieldListenerName = "name" + FieldListenerPort = "port" + FieldListenerProtocol = "protocol" + FieldListenerBackendPort = "backend_port" +) + +const ( + SubresourceTypeLoadBalancerHealthCheck = "healthcheck" + + FieldHealthcheckPort = "port" + FieldHealthcheckSuccessThreshold = "success_threshold" + FieldHealthcheckFailureThreshold = "failure_threshold" + FieldHealthcheckPeriodSeconds = "period_seconds" + FieldHealthcheckTimeoutSeconds = "timeout_seconds" +) + +const ( + LoadBalancerWorkloadTypeVM = "vm" + LoadBalancerWorkloadTypeCluster = "cluster" +) + +const ( + LoadBalancerIPAMPool = "pool" + LoadBalancerIPAMDHCP = "dhcp" +) diff --git a/pkg/importer/resource_ippool_importer.go b/pkg/importer/resource_ippool_importer.go new file mode 100644 index 00000000..b114cdf6 --- /dev/null +++ b/pkg/importer/resource_ippool_importer.go @@ -0,0 +1,20 @@ +package importer + +import ( + loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1" + + "github.com/harvester/terraform-provider-harvester/pkg/constants" + "github.com/harvester/terraform-provider-harvester/pkg/helper" +) + +func ResourceIPPoolStateGetter(obj *loadbalancerv1.IPPool) (*StateGetter, error) { + states := map[string]interface{}{ + constants.FieldCommonName: obj.Name, + } + return &StateGetter{ + ID: helper.BuildID("", obj.Name), + Name: obj.Name, + ResourceType: constants.ResourceTypeIPPool, + States: states, + }, nil +} diff --git a/pkg/importer/resource_loadbalancer_importer.go b/pkg/importer/resource_loadbalancer_importer.go new file mode 100644 index 00000000..71471435 --- /dev/null +++ b/pkg/importer/resource_loadbalancer_importer.go @@ -0,0 +1,21 @@ +package importer + +import ( + loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1" + + "github.com/harvester/terraform-provider-harvester/pkg/constants" + "github.com/harvester/terraform-provider-harvester/pkg/helper" +) + +func ResourceLoadBalancerStateGetter(obj *loadbalancerv1.LoadBalancer) (*StateGetter, error) { + states := map[string]interface{}{ + constants.FieldCommonNamespace: obj.Namespace, + constants.FieldCommonName: obj.Name, + } + return &StateGetter{ + ID: helper.BuildID(obj.Namespace, obj.Name), + Name: obj.Name, + ResourceType: constants.ResourceTypeLoadBalancer, + States: states, + }, nil +}