github.com/openshift/installer@v1.4.17/pkg/gather/gcp/gcp.go (about) 1 package gcp 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "path/filepath" 8 "strings" 9 "time" 10 11 "github.com/pkg/errors" 12 "github.com/sirupsen/logrus" 13 googleoauth "golang.org/x/oauth2/google" 14 compute "google.golang.org/api/compute/v1" 15 "google.golang.org/api/option" 16 utilerrors "k8s.io/apimachinery/pkg/util/errors" 17 18 gcpsession "github.com/openshift/installer/pkg/asset/installconfig/gcp" 19 "github.com/openshift/installer/pkg/gather" 20 "github.com/openshift/installer/pkg/gather/providers" 21 "github.com/openshift/installer/pkg/types" 22 ) 23 24 // Gather holds options for resources we want to gather. 25 type Gather struct { 26 credentials *googleoauth.Credentials 27 clusterName string 28 clusterID string 29 infraID string 30 logger logrus.FieldLogger 31 serialLogBundle string 32 bootstrap string 33 masters []string 34 directory string 35 } 36 37 // New returns a GCP Gather from ClusterMetadata. 38 func New(logger logrus.FieldLogger, serialLogBundle string, bootstrap string, masters []string, metadata *types.ClusterMetadata) (providers.Gather, error) { 39 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) 40 defer cancel() 41 42 session, err := gcpsession.GetSession(ctx) 43 if err != nil { 44 return nil, err 45 } 46 47 return &Gather{ 48 credentials: session.Credentials, 49 clusterName: metadata.ClusterName, 50 clusterID: metadata.ClusterID, 51 infraID: metadata.InfraID, 52 logger: logger, 53 serialLogBundle: serialLogBundle, 54 bootstrap: bootstrap, 55 masters: masters, 56 directory: filepath.Dir(serialLogBundle), 57 }, nil 58 } 59 60 // Run is the entrypoint to start the gather process. 61 func (g *Gather) Run() error { 62 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) 63 defer cancel() 64 65 svc, err := compute.NewService(ctx, option.WithCredentials(g.credentials)) 66 if err != nil { 67 return err 68 } 69 isvc := compute.NewInstancesService(svc) 70 71 var files []string 72 var errs []error 73 74 serialLogBundleDir := strings.TrimSuffix(filepath.Base(g.serialLogBundle), ".tar.gz") 75 filePathDir := filepath.Join(g.directory, serialLogBundleDir) 76 err = os.MkdirAll(filePathDir, 0755) 77 if err != nil && !errors.Is(err, os.ErrExist) { 78 return err 79 } 80 81 req := svc.Instances.AggregatedList(g.credentials.ProjectID).Filter(fmt.Sprintf("name = %s-*", g.infraID)) 82 err = req.Pages(ctx, func(list *compute.InstanceAggregatedList) error { 83 for _, aggListItem := range list.Items { 84 for _, instance := range aggListItem.Instances { 85 filename := filepath.Join(filePathDir, fmt.Sprintf("%s-serial.log", instance.Name)) 86 87 serialOutput, err := isvc.GetSerialPortOutput(g.credentials.ProjectID, filepath.Base(instance.Zone), instance.Name).Port(1).Do() 88 if err != nil { 89 errs = append(errs, err) 90 continue 91 } 92 93 file, err := os.Create(filename) 94 if err != nil { 95 errs = append(errs, err) 96 continue 97 } 98 defer file.Close() 99 100 _, err = file.Write([]byte(serialOutput.Contents)) 101 if err != nil { 102 errs = append(errs, err) 103 continue 104 } 105 106 files = append(files, filename) 107 } 108 } 109 return nil 110 }) 111 if err != nil { 112 errs = append(errs, err) 113 } 114 115 if len(files) > 0 { 116 err := gather.CreateArchive(files, g.serialLogBundle) 117 if err != nil { 118 g.logger.Debugf("failed to create archive: %s", err.Error()) 119 } 120 } 121 122 err = gather.DeleteArchiveDirectory(filePathDir) 123 if err != nil { 124 g.logger.Debugf("failed to remove archive directory: %v", err) 125 } 126 127 return utilerrors.NewAggregate(errs) 128 }