66 "encoding/json"
77 goflag "flag"
88 "fmt"
9+ "maps"
910 "net"
1011 "net/url"
1112 "os"
@@ -19,11 +20,12 @@ import (
1920 "github.com/octago/sflags/gen/gpflag"
2021 "github.com/spf13/pflag"
2122
22- "k8s.io/client-go/tools/clientcmd"
23- "k8s.io/klog/v2"
2423 "sigs.k8s.io/kubetest2/pkg/artifacts"
2524 "sigs.k8s.io/kubetest2/pkg/types"
2625
26+ "k8s.io/client-go/tools/clientcmd"
27+ "k8s.io/klog/v2"
28+
2729 "sigs.k8s.io/provider-ibmcloud-test-infra/kubetest2-tf/deployer/options"
2830 "sigs.k8s.io/provider-ibmcloud-test-infra/kubetest2-tf/pkg/ansible"
2931 "sigs.k8s.io/provider-ibmcloud-test-infra/kubetest2-tf/pkg/build"
@@ -76,10 +78,10 @@ type deployer struct {
7678
7779 RepoRoot string `desc:"The path to the root of the local kubernetes repo. Necessary to call certain scripts. Defaults to the current directory. If operating in legacy mode, this should be set to the local kubernetes/kubernetes repo."`
7880 IgnoreClusterDir bool `desc:"Ignore the cluster folder if exists"`
79- AutoApprove bool `desc:"Terraform Auto Approve "`
81+ AutoApprove bool `desc:"Auto-approve the deployment of infrastructure through terraform" flag:",deprecated "`
8082 RetryOnTfFailure int `desc:"Retry on Terraform Apply Failure"`
8183 BreakKubetestOnUpfail bool `desc:"Breaks kubetest2 when up fails"`
82- Playbook string `desc:"name of ansible playbook to be run"`
84+ Playbook string `desc:"Name of ansible playbook to be run"`
8385 ExtraVars map [string ]string `desc:"Passes extra-vars to ansible playbook, enter a string of key=value pairs"`
8486 SetKubeconfig bool `desc:"Flag to set kubeconfig"`
8587 TargetProvider string `desc:"provider value to be used(powervs, vpc)"`
@@ -96,7 +98,7 @@ func (d *deployer) init() error {
9698}
9799
98100func (d * deployer ) initialize () error {
99- fmt . Println ("Check if package dependencies are installed in the environment" )
101+ klog . Info ("Check if package dependencies are installed in the environment" )
100102 if d .commonOptions .ShouldBuild () {
101103 if err := d .verifyBuildFlags (); err != nil {
102104 return fmt .Errorf ("init failed to check build flags: %s" , err )
@@ -151,17 +153,16 @@ func New(opts types.Options) (types.Deployer, *pflag.FlagSet) {
151153 }
152154 klog .InitFlags (nil )
153155 flagSet .AddGoFlagSet (goflag .CommandLine )
154- fs := bindFlags (d )
156+ fs := bindFlags ()
155157 flagSet .AddFlagSet (fs )
156158 return d , flagSet
157159}
158160
159- func bindFlags (d * deployer ) * pflag.FlagSet {
161+ func bindFlags () * pflag.FlagSet {
160162 flags := pflag .NewFlagSet (Name , pflag .ContinueOnError )
161163 common .CommonProvider .BindFlags (flags )
162164 vpc .VPCProvider .BindFlags (flags )
163165 powervs .PowerVSProvider .BindFlags (flags )
164-
165166 return flags
166167}
167168
@@ -179,9 +180,8 @@ func (d *deployer) Up() error {
179180 if err != nil {
180181 return fmt .Errorf ("failed to dumpconfig to: %s and err: %+v" , d .tmpDir , err )
181182 }
182-
183183 for i := 0 ; i <= d .RetryOnTfFailure ; i ++ {
184- path , err := terraform .Apply (d .tmpDir , d .TargetProvider , d . AutoApprove )
184+ path , err := terraform .Apply (d .tmpDir , d .TargetProvider )
185185 op , oerr := terraform .Output (d .tmpDir , d .TargetProvider )
186186 if err != nil {
187187 if i == d .RetryOnTfFailure {
@@ -201,24 +201,27 @@ func (d *deployer) Up() error {
201201 }
202202 }
203203 inventory := AnsibleInventory {}
204+ tfMetaOutput , err := terraform .Output (d .tmpDir , d .TargetProvider )
205+ if err != nil {
206+ return err
207+ }
208+ var tfOutput map [string ][]interface {}
209+ data , err := json .Marshal (tfMetaOutput )
210+ if err != nil {
211+ return fmt .Errorf ("error while marshaling data %v" , err )
212+ }
213+ json .Unmarshal (data , & tfOutput )
204214 for _ , machineType := range []string {"Masters" , "Workers" } {
205- var tmp []interface {}
206- op , err := terraform .Output (d .tmpDir , d .TargetProvider , "-json" , strings .ToLower (machineType ))
207-
208- if err != nil {
209- return fmt .Errorf ("terraform.Output failed: %v" , err )
210- }
211- klog .Infof ("%s: %s" , strings .ToLower (machineType ), op )
212- err = json .Unmarshal ([]byte (op ), & tmp )
213- if err != nil {
214- return fmt .Errorf ("failed to unmarshal: %v" , err )
215- }
216- for index := range tmp {
217- inventory .addMachine (machineType , tmp [index ].(string ))
218- d .machineIPs = append (d .machineIPs , tmp [index ].(string ))
215+ if machineIps , ok := tfOutput [strings .ToLower (machineType )]; ! ok {
216+ return fmt .Errorf ("error while unmarshaling machine IPs from terraform output" )
217+ } else {
218+ for _ , machineIp := range machineIps {
219+ inventory .addMachine (machineType , machineIp .(string ))
220+ d .machineIPs = append (d .machineIPs , machineIp .(string ))
221+ }
219222 }
220223 }
221- klog .Infof ("inventory: %v" , inventory )
224+ klog .Infof ("Kubernetes cluster node inventory: %+ v" , inventory )
222225 t := template .New ("Ansible inventory file" )
223226
224227 t , err = t .Parse (inventoryTemplate )
@@ -232,50 +235,44 @@ func (d *deployer) Up() error {
232235 return fmt .Errorf ("failed to create inventory file: %v" , err )
233236 }
234237
235- err = t .Execute (inventoryFile , inventory )
236- if err != nil {
238+ if err = t .Execute (inventoryFile , inventory ); err != nil {
237239 return fmt .Errorf ("template execute failed: %v" , err )
238240 }
239241
240242 common .CommonProvider .ExtraCerts = strings .Join (inventory .Masters , "," )
241243
242- commonJSON , err := json .Marshal (common .CommonProvider )
244+ ansibleParams , err := json .Marshal (common .CommonProvider )
243245 if err != nil {
244246 return fmt .Errorf ("failed to marshal provider into JSON: %v" , err )
245247 }
246- klog .Infof ("commonJSON: %v" , string (commonJSON ))
247- //Unmarshalling commonJSON into map to add extra-vars
248- final := map [string ]interface {}{}
249- json .Unmarshal (commonJSON , & final )
250- //Iterating through extra-vars and adding them to map
251- for k := range d .ExtraVars {
252- final [k ] = d .ExtraVars [k ]
253- }
254- //Marshalling back the map to JSON
255- finalJSON , err := json .Marshal (final )
256- if err != nil {
257- return fmt .Errorf ("failed to marshal provider into JSON: %v" , err )
248+ klog .Infof ("Ansible params exposed under groupvars: %v" , string (ansibleParams ))
249+ // Unmarshalling commonJSON into map to add extra-vars
250+ combinedAnsibleVars := map [string ]string {}
251+ if err = json .Unmarshal (ansibleParams , & combinedAnsibleVars ); err != nil {
252+ return fmt .Errorf ("failed to unmarshal ansible group variables: %v" , err )
258253 }
259- klog .Infof ("finalJSON with extra vars: %v" , string (finalJSON ))
260254
261- exitcode , err := ansible .Playbook (d .tmpDir , filepath .Join (d .tmpDir , "hosts" ), string (finalJSON ), d .Playbook )
255+ // Add-in the extra-vars set to the final set.
256+ maps .Insert (combinedAnsibleVars , maps .All (d .ExtraVars ))
257+ klog .Infof ("Updated ansible variables with extra vars: %+v" , combinedAnsibleVars )
258+ err = ansible .Playbook (d .tmpDir , filepath .Join (d .tmpDir , "hosts" ), d .Playbook , combinedAnsibleVars )
262259 if err != nil {
263- return fmt .Errorf ("failed to run ansible playbook: %v\n with exit code: %d " , err , exitcode )
260+ return fmt .Errorf ("failed to run ansible playbook: %v" , err )
264261 }
265262
266263 if d .SetKubeconfig {
267264 if err = setKubeconfig (inventory .Masters [0 ]); err != nil {
268265 return fmt .Errorf ("failed to setKubeconfig: %v" , err )
269266 }
270- fmt . Printf ("KUBECONFIG set to: %s\n " , os .Getenv ("KUBECONFIG" ))
267+ klog . Infof ("KUBECONFIG set to: %s" , os .Getenv ("KUBECONFIG" ))
271268 }
272269
273270 if isUp , err := d .IsUp (); err != nil {
274271 klog .Warningf ("failed to check if cluster is up: %v" , err )
275272 } else if isUp {
276- klog .V (1 ).Infof ("cluster reported as up" )
273+ klog .V (1 ).Info ("cluster reported as up" )
277274 } else {
278- klog .Errorf ("cluster reported as down" )
275+ klog .Error ("cluster reported as down" )
279276 }
280277
281278 klog .Infof ("Dumping cluster info.." )
@@ -294,7 +291,7 @@ func setKubeconfig(host string) error {
294291
295292 config , err := clientcmd .LoadFromFile (common .CommonProvider .KubeconfigPath )
296293 if err != nil {
297- klog .Error ("failed to load the kubeconfig file" )
294+ klog .Errorf ("failed to load the kubeconfig file. error: %v" , err )
298295 }
299296 for i := range config .Clusters {
300297 surl , err := url .Parse (config .Clusters [i ].Server )
@@ -324,7 +321,7 @@ func (d *deployer) Down() error {
324321 if err := d .init (); err != nil {
325322 return fmt .Errorf ("down failed to init: %s" , err )
326323 }
327- err := terraform .Destroy (d .tmpDir , d .TargetProvider , d . AutoApprove )
324+ err := terraform .Destroy (d .tmpDir , d .TargetProvider )
328325 if err != nil {
329326 if common .CommonProvider .IgnoreDestroy {
330327 klog .Infof ("terraform.Destroy failed: %v" , err )
0 commit comments