@@ -25,6 +25,7 @@ import (
2525
2626 "k8s.io/klog/v2"
2727 api "k8s.io/kops/pkg/apis/kops/v1alpha2"
28+ "k8s.io/kops/pkg/cloudinstances"
2829 "sigs.k8s.io/kubetest2/pkg/exec"
2930)
3031
@@ -61,6 +62,28 @@ func GetCluster(kopsBinary, clusterName string, env []string) (*api.Cluster, err
6162 return cluster , nil
6263}
6364
65+ // GetCluster will retrieve the specified Cluster from the state store.
66+ func GetInstances (kopsBinary , clusterName string , env []string ) ([]* cloudinstances.CloudInstance , error ) {
67+ args := []string {
68+ kopsBinary , "get" , "instances" , "--name" , clusterName , "-ojson" ,
69+ }
70+ c := exec .Command (args [0 ], args [1 :]... )
71+ c .SetEnv (env ... )
72+ var stdout bytes.Buffer
73+ c .SetStdout (& stdout )
74+ var stderr bytes.Buffer
75+ c .SetStderr (& stderr )
76+ if err := c .Run (); err != nil {
77+ klog .Warningf ("failed to run %s; stderr=%s" , strings .Join (args , " " ), stderr .String ())
78+ return nil , fmt .Errorf ("error querying instances from %s: %w" , strings .Join (args , " " ), err )
79+ }
80+ var instances []* cloudinstances.CloudInstance
81+ if err := json .Unmarshal (stdout .Bytes (), & instances ); err != nil {
82+ return nil , fmt .Errorf ("error parsing instance groups json: %w" , err )
83+ }
84+ return instances , nil
85+ }
86+
6487// GetInstanceGroups will retrieve the instance groups for the specified Cluster from the state store.
6588func GetInstanceGroups (kopsBinary , clusterName string , env []string ) ([]* api.InstanceGroup , error ) {
6689 args := []string {
0 commit comments