From 76e6c021a5674493ac67189cffa25647588e3556 Mon Sep 17 00:00:00 2001 From: GunaKKIBM <92353386+GunaKKIBM@users.noreply.github.com> Date: Mon, 30 Jan 2023 19:48:02 +0530 Subject: [PATCH] Fixing client code - passing secret provider (#68) * Fixing client code - passing secret provider * Updating lib * Rebasing with master branch * Adding getclusterId to cloud provider interface * Adding getclusterID to fake volume provider * Fixing UT * Fixing UT * Fixing UT * Fixing UT * Updating lib * Fix done while testing * Removing secret provider * Addressing comments * Fixing fake volume provider * Passing only k8s client * Updating lib * Updating lib - removing GC dependency, removing unused error messages * Updating lib * Updating lib * Updating lib --- go.mod | 15 ++-- go.sum | 40 ++++++---- pkg/ibmcloudprovider/fake_volume_provider.go | 29 ++++++-- pkg/ibmcloudprovider/ibm_volume_provider.go | 3 +- pkg/ibmcloudprovider/volume_provider.go | 43 ++++------- pkg/ibmcloudprovider/volume_provider_test.go | 77 ++++++++++++++++---- pkg/messages/messages_en.go | 18 ----- pkg/utils/{get_key.go => cluster_info.go} | 27 ------- pkg/watcher/pv_watcher_test.go | 25 +++++-- test-fixtures/provider-disabled.toml | 29 ++++++++ test-fixtures/slconfig.toml | 39 +++++----- 11 files changed, 199 insertions(+), 146 deletions(-) rename pkg/utils/{get_key.go => cluster_info.go} (54%) create mode 100644 test-fixtures/provider-disabled.toml diff --git a/go.mod b/go.mod index 2f77bda2..ae80d6e0 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,9 @@ module github.com/IBM/ibm-csi-common go 1.18 require ( - github.com/IBM/ibmcloud-volume-interface v1.1.1 - github.com/IBM/ibmcloud-volume-vpc v1.1.2 + github.com/IBM/ibmcloud-volume-interface v1.1.2 + github.com/IBM/ibmcloud-volume-vpc v1.1.3 + github.com/IBM/secret-utils-lib v1.1.2 github.com/container-storage-interface/spec v1.6.0 github.com/gofrs/uuid v4.2.0+incompatible github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b @@ -30,8 +31,7 @@ require ( github.com/BurntSushi/toml v1.0.0 // indirect github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7 // indirect github.com/IBM/go-sdk-core/v5 v5.9.1 // indirect - github.com/IBM/secret-common-lib v1.1.1 // indirect - github.com/IBM/secret-utils-lib v1.1.1 // indirect + github.com/IBM/secret-common-lib v1.1.2 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect @@ -52,8 +52,9 @@ require ( github.com/go-openapi/jsonreference v0.19.5 // indirect github.com/go-openapi/strfmt v0.21.1 // indirect github.com/go-openapi/swag v0.19.14 // indirect - github.com/go-playground/locales v0.13.0 // indirect - github.com/go-playground/universal-translator v0.17.0 // indirect + github.com/go-playground/locales v0.14.0 // indirect + github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/go-playground/validator/v10 v10.11.1 // indirect github.com/go-stack/stack v1.8.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect @@ -73,7 +74,7 @@ require ( github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/kelseyhightower/envconfig v1.4.0 // indirect - github.com/leodido/go-urn v1.2.0 // indirect + github.com/leodido/go-urn v1.2.1 // indirect github.com/mailru/easyjson v0.7.6 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect github.com/mitchellh/mapstructure v1.4.1 // indirect diff --git a/go.sum b/go.sum index 56d40bda..2965035c 100644 --- a/go.sum +++ b/go.sum @@ -60,14 +60,14 @@ github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7 h1:eHgfQl6IeSmzWUyiSi13CvoFYsovoyq github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs= github.com/IBM/go-sdk-core/v5 v5.9.1 h1:06pXbD9Rgmqqe2HA5YAeQbB4eYRRFgIoOT+Kh3cp1zo= github.com/IBM/go-sdk-core/v5 v5.9.1/go.mod h1:axE2JrRq79gIJTjKPBwV6gWHswvVptBjbcvvCPIxARM= -github.com/IBM/ibmcloud-volume-interface v1.1.1 h1:RlwEj8bq+aASdb90Lng4rINMYR96/G6yUvyk/ATmptA= -github.com/IBM/ibmcloud-volume-interface v1.1.1/go.mod h1:coaQ/FD5NRgFfaAnjkoxv+e6MNSl2UAeQwC38szQ5G8= -github.com/IBM/ibmcloud-volume-vpc v1.1.2 h1:LbgRC/CQRptt6+lfg6k/LL2W2GyDNrxirzJXZtDY9ik= -github.com/IBM/ibmcloud-volume-vpc v1.1.2/go.mod h1:M3ouKpMDAh2IRtUrVRHRSiwTUOVlC8vFUJsxkYTWNZM= -github.com/IBM/secret-common-lib v1.1.1 h1:Ijm7lRnbChQzFtDKKwsUg9ojxQ2KFw06hD/P+bmso10= -github.com/IBM/secret-common-lib v1.1.1/go.mod h1:XAMi3upx2DZRblj8UII+LRopY5lpW1sd3d/vs92DWoA= -github.com/IBM/secret-utils-lib v1.1.1 h1:hipBsSa7FqWTI015BaAmxEwBIL2ZffGB6vrF4GfpYtI= -github.com/IBM/secret-utils-lib v1.1.1/go.mod h1:Yfla8xgZ938htp3tgw1NVYPKa8UOGq3QcRIpBE3gLgU= +github.com/IBM/ibmcloud-volume-interface v1.1.2 h1:7IByqGhG09a3aycsf3TQn9vkQDncMJYdvpo4hGQAN5A= +github.com/IBM/ibmcloud-volume-interface v1.1.2/go.mod h1:H6bm3Tzq7cjN0De207du/A8mzrirEgxwV89j3ouwCwg= +github.com/IBM/ibmcloud-volume-vpc v1.1.3 h1:nYb45M8xnKz2ae8qnW77OIGTrxVmsKwxbvt71jd/wzw= +github.com/IBM/ibmcloud-volume-vpc v1.1.3/go.mod h1:BYMZUyE7sci6iFSMNA7VbChRHS2ceKyjForZOdZ5fjQ= +github.com/IBM/secret-common-lib v1.1.2 h1:wBcsxukRdAn0JkZE4SvOEEyHd3E1sqViMtxYr1Pp6QQ= +github.com/IBM/secret-common-lib v1.1.2/go.mod h1:M5eindyf0mG0kKR2za65AQZegAnI1ppQdEJQNv84kWc= +github.com/IBM/secret-utils-lib v1.1.2 h1:CQXQiH2YeYTjDamSfYVAHFJ0iHp/nTYuL5pTV9q4hoU= +github.com/IBM/secret-utils-lib v1.1.2/go.mod h1:fRbx/4DCxqwResj6kjsRUMK3Al6BnzueBSf7llIolU8= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= @@ -176,10 +176,16 @@ github.com/go-openapi/strfmt v0.21.1/go.mod h1:I/XVKeLc5+MM5oPNN7P6urMOpuLXEcNrC github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= github.com/go-openapi/swag v0.19.14 h1:gm3vOOXfiuw5i9p5N9xJvfjvuofpyvLA9Wr6QfK5Fng= github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= -github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= -github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= +github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= +github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= +github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ= +github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= @@ -325,14 +331,18 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxv github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 h1:nHHjmvjitIiyPlUHk/ofpgvBcNcawJLtf4PYHORLjAA= github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0/go.mod h1:YBCo4DoEeDndqvAn6eeu0vWM7QdXmHEeI9cFWplmBys= -github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= @@ -362,7 +372,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= @@ -390,6 +399,7 @@ github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3I github.com/opencontainers/selinux v1.10.0 h1:rAiKF8hTcgLI3w0DHm6i0ylVVcOrlgR1kK99DRLDhyU= github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -421,6 +431,9 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= @@ -917,8 +930,9 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= diff --git a/pkg/ibmcloudprovider/fake_volume_provider.go b/pkg/ibmcloudprovider/fake_volume_provider.go index ed54106a..c834f750 100644 --- a/pkg/ibmcloudprovider/fake_volume_provider.go +++ b/pkg/ibmcloudprovider/fake_volume_provider.go @@ -19,15 +19,17 @@ package ibmcloudprovider import ( "bytes" + "os" + "path/filepath" "testing" - "github.com/IBM/ibm-csi-common/pkg/utils" "github.com/IBM/ibmcloud-volume-interface/config" "github.com/IBM/ibmcloud-volume-interface/lib/provider" "github.com/IBM/ibmcloud-volume-interface/lib/provider/fake" "github.com/IBM/ibmcloud-volume-interface/provider/local" provider_util "github.com/IBM/ibmcloud-volume-vpc/block/utils" vpcconfig "github.com/IBM/ibmcloud-volume-vpc/block/vpcconfig" + "github.com/IBM/secret-utils-lib/pkg/k8s_utils" "go.uber.org/zap" "go.uber.org/zap/zapcore" "golang.org/x/net/context" @@ -152,7 +154,18 @@ func GetTestProvider(t *testing.T, logger *zap.Logger) (*IBMCloudStorageProvider } // Prepare provider registry - registry, err := provider_util.InitProviders(vpcBlockConfig, logger) + k8sClient, _ := k8s_utils.FakeGetk8sClientSet() + pwd, err := os.Getwd() + if err != nil { + logger.Fatal("Failed to get current working directory, test related to read config will fail, error: %v", local.ZapError(err)) + } + + clusterConfPath := filepath.Join(pwd, "..", "..", "test-fixtures", "valid", "cluster_info", "cluster-config.json") + _ = k8s_utils.FakeCreateCM(k8sClient, clusterConfPath) + + secretConfPath := filepath.Join(pwd, "..", "..", "test-fixtures", "slconfig.toml") + _ = k8s_utils.FakeCreateSecret(k8sClient, "DEFAULT", secretConfPath) + registry, err := provider_util.InitProviders(vpcBlockConfig, &k8sClient, logger) if err != nil { logger.Fatal("Error configuring providers", local.ZapError(err)) } @@ -161,7 +174,7 @@ func GetTestProvider(t *testing.T, logger *zap.Logger) (*IBMCloudStorageProvider ProviderName: "vpc-classic", ProviderConfig: conf, Registry: registry, - ClusterInfo: nil, + ClusterID: "", } logger.Info("Successfully read provider configuration...") return cloudProvider, nil @@ -171,7 +184,7 @@ func GetTestProvider(t *testing.T, logger *zap.Logger) (*IBMCloudStorageProvider type FakeIBMCloudStorageProvider struct { ProviderName string ProviderConfig *config.Config - ClusterInfo *utils.ClusterInfo + ClusterID string fakeSession *fake.FakeSession } @@ -181,7 +194,7 @@ var _ CloudProviderInterface = &FakeIBMCloudStorageProvider{} func NewFakeIBMCloudStorageProvider(configPath string, logger *zap.Logger) (*FakeIBMCloudStorageProvider, error) { return &FakeIBMCloudStorageProvider{ProviderName: "FakeIBMCloudStorageProvider", ProviderConfig: &config.Config{VPC: &config.VPCProviderConfig{VPCBlockProviderName: "VPCFakeProvider"}}, - ClusterInfo: &utils.ClusterInfo{}, fakeSession: &fake.FakeSession{}}, nil + ClusterID: "fake-clusterID", fakeSession: &fake.FakeSession{}}, nil } // GetProviderSession ... @@ -194,7 +207,7 @@ func (ficp *FakeIBMCloudStorageProvider) GetConfig() *config.Config { return ficp.ProviderConfig } -// GetClusterInfo ... -func (ficp *FakeIBMCloudStorageProvider) GetClusterInfo() *utils.ClusterInfo { - return ficp.ClusterInfo +// GetClusterID ... +func (ficp *FakeIBMCloudStorageProvider) GetClusterID() string { + return ficp.ClusterID } diff --git a/pkg/ibmcloudprovider/ibm_volume_provider.go b/pkg/ibmcloudprovider/ibm_volume_provider.go index 11775315..98c4c5c0 100644 --- a/pkg/ibmcloudprovider/ibm_volume_provider.go +++ b/pkg/ibmcloudprovider/ibm_volume_provider.go @@ -18,7 +18,6 @@ package ibmcloudprovider import ( - "github.com/IBM/ibm-csi-common/pkg/utils" "github.com/IBM/ibmcloud-volume-interface/config" "github.com/IBM/ibmcloud-volume-interface/lib/provider" "go.uber.org/zap" @@ -29,5 +28,5 @@ import ( type CloudProviderInterface interface { GetProviderSession(ctx context.Context, logger *zap.Logger) (provider.Session, error) GetConfig() *config.Config - GetClusterInfo() *utils.ClusterInfo + GetClusterID() string } diff --git a/pkg/ibmcloudprovider/volume_provider.go b/pkg/ibmcloudprovider/volume_provider.go index a91aaae4..a9057079 100644 --- a/pkg/ibmcloudprovider/volume_provider.go +++ b/pkg/ibmcloudprovider/volume_provider.go @@ -23,13 +23,14 @@ import ( "strings" "time" - "github.com/IBM/ibm-csi-common/pkg/utils" "github.com/IBM/ibmcloud-volume-interface/config" "github.com/IBM/ibmcloud-volume-interface/lib/provider" "github.com/IBM/ibmcloud-volume-interface/provider/local" provider_util "github.com/IBM/ibmcloud-volume-vpc/block/utils" vpcconfig "github.com/IBM/ibmcloud-volume-vpc/block/vpcconfig" "github.com/IBM/ibmcloud-volume-vpc/common/registry" + utilsConfig "github.com/IBM/secret-utils-lib/pkg/config" + "github.com/IBM/secret-utils-lib/pkg/k8s_utils" "go.uber.org/zap" "golang.org/x/net/context" ) @@ -39,27 +40,21 @@ type IBMCloudStorageProvider struct { ProviderName string ProviderConfig *config.Config Registry registry.Providers - ClusterInfo *utils.ClusterInfo + ClusterID string } var _ CloudProviderInterface = &IBMCloudStorageProvider{} // NewIBMCloudStorageProvider ... -func NewIBMCloudStorageProvider(configPath string, clusterVolumeLabel string, logger *zap.Logger) (*IBMCloudStorageProvider, error) { +func NewIBMCloudStorageProvider(clusterVolumeLabel string, k8sClient *k8s_utils.KubernetesClient, logger *zap.Logger) (*IBMCloudStorageProvider, error) { logger.Info("NewIBMCloudStorageProvider-Reading provider configuration...") // Load config file - conf, err := config.ReadConfig(configPath, logger) + conf, err := config.ReadConfig(*k8sClient, logger) if err != nil { - logger.Fatal("Error loading configuration") + logger.Error("Error loading configuration") return nil, err } - // Correct if the G2EndpointURL is of the form "http://". - conf.VPC.G2EndpointURL = getEndpointURL(conf.VPC.G2EndpointURL, logger) - - // Correct if the G2TokenExchangeURL is of the form "http://" - conf.VPC.G2TokenExchangeURL = getEndpointURL(conf.VPC.G2TokenExchangeURL, logger) - // Get only VPC_API_VERSION, in "2019-07-02T00:00:00.000Z" case vpc need only 2019-07-02" dateTime, err := time.Parse(time.RFC3339, conf.VPC.APIVersion) if err == nil { @@ -69,12 +64,12 @@ func NewIBMCloudStorageProvider(configPath string, clusterVolumeLabel string, lo conf.VPC.APIVersion = "2022-11-11" // setting default values } - var clusterInfo = &utils.ClusterInfo{} + var clusterInfo utilsConfig.ClusterConfig logger.Info("Fetching clusterInfo") if conf.IKS != nil && conf.IKS.Enabled || os.Getenv("IKS_ENABLED") == "True" { - clusterInfo, err = utils.NewClusterInfo(logger) + clusterInfo, err = utilsConfig.GetClusterInfo(*k8sClient, logger) if err != nil { - logger.Fatal("Unable to load ClusterInfo", local.ZapError(err)) + logger.Error("Unable to load ClusterInfo", local.ZapError(err)) return nil, err } logger.Info("Fetched clusterInfo..") @@ -83,16 +78,6 @@ func NewIBMCloudStorageProvider(configPath string, clusterVolumeLabel string, lo //Initialize the clusterVolumeLabel once which will be used for tagging by the library. conf.VPC.ClusterVolumeLabel = clusterVolumeLabel - // Update the CSRF Token - if conf.Bluemix.PrivateAPIRoute != "" { - conf.Bluemix.CSRFToken = string([]byte{}) // TODO~ Need to remove it - } - - if conf.API == nil { - conf.API = &config.APIConfig{ - PassthroughSecret: string([]byte{}), // // TODO~ Need to remove it - } - } vpcBlockConfig := &vpcconfig.VPCBlockConfig{ VPCConfig: conf.VPC, IKSConfig: conf.IKS, @@ -100,7 +85,7 @@ func NewIBMCloudStorageProvider(configPath string, clusterVolumeLabel string, lo ServerConfig: conf.Server, } // Prepare provider registry - registry, err := provider_util.InitProviders(vpcBlockConfig, logger) + registry, err := provider_util.InitProviders(vpcBlockConfig, k8sClient, logger) if err != nil { logger.Error("Error configuring providers", local.ZapError(err)) return nil, err @@ -117,7 +102,7 @@ func NewIBMCloudStorageProvider(configPath string, clusterVolumeLabel string, lo ProviderName: providerName, ProviderConfig: conf, Registry: registry, - ClusterInfo: clusterInfo, + ClusterID: clusterInfo.ClusterID, } logger.Info("Successfully read provider configuration") return cloudProvider, nil @@ -164,9 +149,9 @@ func (icp *IBMCloudStorageProvider) GetConfig() *config.Config { return icp.ProviderConfig } -// GetClusterInfo ... -func (icp *IBMCloudStorageProvider) GetClusterInfo() *utils.ClusterInfo { - return icp.ClusterInfo +// GetClusterID ... +func (icp *IBMCloudStorageProvider) GetClusterID() string { + return icp.ClusterID } // CorrectEndpointURL corrects endpoint url if it is of form "http://" diff --git a/pkg/ibmcloudprovider/volume_provider_test.go b/pkg/ibmcloudprovider/volume_provider_test.go index 2217071b..71c57be9 100644 --- a/pkg/ibmcloudprovider/volume_provider_test.go +++ b/pkg/ibmcloudprovider/volume_provider_test.go @@ -19,10 +19,12 @@ package ibmcloudprovider import ( "context" + "errors" "os" "path/filepath" "testing" + "github.com/IBM/secret-utils-lib/pkg/k8s_utils" "github.com/stretchr/testify/assert" ) @@ -31,23 +33,66 @@ func TestNewIBMCloudStorageProvider(t *testing.T) { logger, teardown := GetTestLogger(t) defer teardown() - pwd, err := os.Getwd() - if err != nil { - t.Errorf("Failed to get current working directory, some unit tests will fail") + testcases := []struct { + testcasename string + clusterConfPath string + secretConfPath string + iksEnabled string + expectedError error + }{ + { + testcasename: "Successful initialization of cloud storage provider", + clusterConfPath: "test-fixtures/valid/cluster_info/cluster-config.json", + secretConfPath: "test-fixtures/slconfig.toml", + iksEnabled: "false", + expectedError: nil, + }, + { + testcasename: "Invalid cluster config", + clusterConfPath: "test-fixtures/invalid/cluster_info/non-exist.json", + secretConfPath: "test-fixtures/slconfig.toml", + iksEnabled: "True", + expectedError: errors.New("not nil"), + }, + { + testcasename: "Secret config doesn't exist", + clusterConfPath: "test-fixtures/valid/cluster_info/cluster-config.json", + secretConfPath: "test-fixtures/non-exist.toml", + iksEnabled: "True", + expectedError: errors.New("not nil"), + }, + { + testcasename: "Both IKS and VPC disabled", + clusterConfPath: "test-fixtures/valid/cluster_info/cluster-config.json", + secretConfPath: "test-fixtures/provider-disabled.toml", + iksEnabled: "False", + expectedError: errors.New("not nil"), + }, } - // As its required by NewIBMCloudStorageProvider - secretConfigPath := filepath.Join(pwd, "..", "..", "test-fixtures", "valid") - err = os.Setenv("SECRET_CONFIG_PATH", secretConfigPath) - defer os.Unsetenv("SECRET_CONFIG_PATH") - if err != nil { - t.Errorf("This test will fail because of %v", err) + for _, testcase := range testcases { + t.Run(testcase.testcasename, func(t *testing.T) { + kc, _ := k8s_utils.FakeGetk8sClientSet() + pwd, err := os.Getwd() + if err != nil { + t.Errorf("Failed to get current working directory, test related to read config will fail, error: %v", err) + } + + clusterConfPath := filepath.Join(pwd, "..", "..", testcase.clusterConfPath) + _ = k8s_utils.FakeCreateCM(kc, clusterConfPath) + + secretConfPath := filepath.Join(pwd, "..", "..", testcase.secretConfPath) + _ = k8s_utils.FakeCreateSecret(kc, "DEFAULT", secretConfPath) + + os.Setenv("IKS_ENABLED", testcase.iksEnabled) + _, err = NewIBMCloudStorageProvider("test", &kc, logger) + if testcase.expectedError != nil { + assert.NotNil(t, err) + } else { + assert.Nil(t, err) + } + }) } - - configPath := filepath.Join(pwd, "..", "..", "test-fixtures", "slconfig.toml") - ibmCloudProvider, err := NewIBMCloudStorageProvider(configPath, "test", logger) - assert.NotNil(t, err) - assert.Nil(t, ibmCloudProvider) } func TestNewFakeIBMCloudStorageProvider(t *testing.T) { @@ -80,8 +125,8 @@ func TestNewFakeIBMCloudStorageProvider(t *testing.T) { cloudProviderConfig := ibmFakeCloudProvider.GetConfig() assert.NotNil(t, cloudProviderConfig) - clusterInfo := ibmFakeCloudProvider.GetClusterInfo() - assert.NotNil(t, clusterInfo) + clusterID := ibmFakeCloudProvider.GetClusterID() + assert.NotNil(t, clusterID) } func TestCorrectEndpointURL(t *testing.T) { diff --git a/pkg/messages/messages_en.go b/pkg/messages/messages_en.go index 4fe64575..f72f9fd0 100644 --- a/pkg/messages/messages_en.go +++ b/pkg/messages/messages_en.go @@ -313,24 +313,6 @@ var messagesEn = map[string]Message{ }, } -// Following are the error messages expected while opening a session -const ( - // APIKeyNotFound ... - APIKeyNotFound = "api key could not be found" - - // UserNotFound ... - UserNotFound = "user not found or active" -) - -// Following are the error messages published in when api key update fails -const ( - // ErrAPIKeyNotFound ... - ErrAPIKeyNotFound = "API key is not found, reset the api key. If reset, retry after few minutes" - - // ErrUpdatingAPIKey ... - ErrUpdatingAPIKey = "Error updating the new API key" -) - // InitMessages ... func InitMessages() map[string]Message { return messagesEn diff --git a/pkg/utils/get_key.go b/pkg/utils/cluster_info.go similarity index 54% rename from pkg/utils/get_key.go rename to pkg/utils/cluster_info.go index e38ebd56..d17a3627 100644 --- a/pkg/utils/get_key.go +++ b/pkg/utils/cluster_info.go @@ -17,15 +17,6 @@ // Package utils ... package utils -import ( - "encoding/json" - "io/ioutil" - "path/filepath" - - "github.com/IBM/ibmcloud-volume-interface/config" - "go.uber.org/zap" -) - // ClusterInfo contains the cluster information type ClusterInfo struct { ClusterID string `json:"cluster_id"` @@ -33,21 +24,3 @@ type ClusterInfo struct { DataCenter string `json:"datacenter,omitempty"` CustomerID string `json:"customer_id,omitempty"` } - -// NewClusterInfo loads cluster info -func NewClusterInfo(logger *zap.Logger) (*ClusterInfo, error) { - configBasePath := config.GetConfPathDir() - clusterInfo := &ClusterInfo{} - clusterInfoFile := filepath.Join(configBasePath, ClusterInfoPath) - clusterInfoContent, err := ioutil.ReadFile(filepath.Clean(clusterInfoFile)) - if err != nil { - logger.Error("Error while reading cluster-config.json", zap.Error(err)) - return nil, err - } - err = json.Unmarshal(clusterInfoContent, clusterInfo) - if err != nil { - logger.Error("Error while parsing cluster-config", zap.Error(err)) - return nil, err - } - return clusterInfo, nil -} diff --git a/pkg/watcher/pv_watcher_test.go b/pkg/watcher/pv_watcher_test.go index 4fe8d263..ae1a5d3c 100644 --- a/pkg/watcher/pv_watcher_test.go +++ b/pkg/watcher/pv_watcher_test.go @@ -24,16 +24,21 @@ import ( "strings" "testing" + cloudprovider "github.com/IBM/ibm-csi-common/pkg/ibmcloudprovider" + "github.com/IBM/ibm-csi-common/pkg/utils" + "github.com/IBM/ibmcloud-volume-interface/config" + "github.com/golang/glog" "github.com/onsi/gomega/ghttp" "github.com/stretchr/testify/assert" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "github.com/IBM/ibm-csi-common/pkg/utils" - "github.com/IBM/ibmcloud-volume-interface/config" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + "k8s.io/client-go/kubernetes/fake" + "k8s.io/client-go/kubernetes/scheme" + v1core "k8s.io/client-go/kubernetes/typed/core/v1" + "k8s.io/client-go/tools/record" ) func TestNew(t *testing.T) { @@ -73,10 +78,20 @@ func TestAddTags(t *testing.T) { }, } logger, _ := GetTestLogger(t) + fakeIBMCloudStorageProvider, _ := cloudprovider.NewFakeIBMCloudStorageProvider("configPath", logger) + + broadcaster := record.NewBroadcaster() + broadcaster.StartLogging(glog.Infof) + clientset := fake.NewSimpleClientset() + eventInterface := clientset.CoreV1().Events("") + broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: eventInterface}) + pvw := &PVWatcher{ provisionerName: "ibm-csi-driver", logger: logger, config: conf, + cloudProvider: fakeIBMCloudStorageProvider, + recorder: broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "pod-name"}), } pv := &v1.PersistentVolume{ ObjectMeta: metav1.ObjectMeta{ diff --git a/test-fixtures/provider-disabled.toml b/test-fixtures/provider-disabled.toml new file mode 100644 index 00000000..0aad9d59 --- /dev/null +++ b/test-fixtures/provider-disabled.toml @@ -0,0 +1,29 @@ +[server] + debug_trace = false + +[bluemix] + iam_url = "iam_rul" + iam_client_id = "bx" + iam_client_secret = "bx" + iam_api_key = "XXX" + refresh_token = "" + containers_api_route = "api_endpoint" + containers_api_route_private = "private_api_endpoint" + +[vpc] + vpc_enabled = false + vpc_block_provider_name = "vpc-classic" + gc_token_exchange_endpoint_url = "token_exchange_url" + gc_riaas_endpoint_url = "endpoint_url" + gc_resource_group_id = "" + gc_api_key = "" + encryption = true + vpc_api_timeout = "120s" + max_retry_attempt = 10 # 10 times with exponential re-try with max gap max_retry_gap + max_retry_gap = 120 # 2 minutes + api_version = "2019-07-02" + vpc_api_generation = 1 + +[IKS] + iks_enabled = false + iks_block_provider_name = "iks-vpc-classic" diff --git a/test-fixtures/slconfig.toml b/test-fixtures/slconfig.toml index 26c966cd..d2aeda9a 100644 --- a/test-fixtures/slconfig.toml +++ b/test-fixtures/slconfig.toml @@ -1,29 +1,26 @@ [server] debug_trace = false - -[bluemix] - iam_url = "iam_rul" - iam_client_id = "bx" - iam_client_secret = "bx" - iam_api_key = "XXX" - refresh_token = "" - containers_api_route = "api_endpoint" - containers_api_route_private = "private_api_endpoint" - [vpc] vpc_enabled = true - vpc_block_provider_name = "vpc-classic" - gc_token_exchange_endpoint_url = "token_exchange_url" - gc_riaas_endpoint_url = "endpoint_url" - gc_resource_group_id = "" - gc_api_key = "" - encryption = true - vpc_api_timeout = "120s" - max_retry_attempt = 10 # 10 times with exponential re-try with max gap max_retry_gap + g2_token_exchange_endpoint_url = "https://iam.stage1.bluemix.net" + g2_riaas_endpoint_url = "https://us-south-stage01.iaasdev.cloud.ibm.com/" + g2_riaas_endpoint_private_url = "https://us-south-stage01.iaasdev.cloud.ibm.com" + g2_resource_group_id = "" + g2_api_key = "api-key" + provider_type = "g2" + vpc_block_provider_name = "vpc" + vpc_volume_type="vpc-block" + encryption = false + iks_token_exchange_endpoint_private_url = "https://containers.test.cloud.ibm.com" + containers_api_csrf_token = "" + max_retry_attempt = 2 # 10 times with exponential re-try with max gap max_retry_gap max_retry_gap = 120 # 2 minutes - api_version = "2019-07-02" - vpc_api_generation = 1 - + api_version = "2020-07-02" #"2019-07-02" + vpc_api_generation = 2 + vpc_api_timeout = "120s" [IKS] iks_enabled = false iks_block_provider_name = "iks-vpc-classic" + +[API] + PassthroughSecret = ""