Skip to content

Commit ae21a76

Browse files
Merge pull request #8441 from patrickdillon/capi-etcd-kas-logs
OCPBUGS-35182: write etcd and kube-apiserver logs
2 parents c35559a + 7e453b8 commit ae21a76

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

cmd/openshift-install/gather.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424
"github.com/openshift/installer/pkg/asset/installconfig"
2525
assetstore "github.com/openshift/installer/pkg/asset/store"
2626
"github.com/openshift/installer/pkg/asset/tls"
27+
"github.com/openshift/installer/pkg/clusterapi"
2728
serialgather "github.com/openshift/installer/pkg/gather"
2829
"github.com/openshift/installer/pkg/gather/service"
2930
"github.com/openshift/installer/pkg/gather/ssh"
3031
"github.com/openshift/installer/pkg/infrastructure"
31-
"github.com/openshift/installer/pkg/infrastructure/clusterapi"
3232
infra "github.com/openshift/installer/pkg/infrastructure/platform"
3333

3434
_ "github.com/openshift/installer/pkg/gather/aws"
@@ -144,7 +144,7 @@ func gatherBootstrap(bootstrap string, port int, masters []string, directory str
144144
gatherID := time.Now().Format("20060102150405")
145145
archives := map[string]string{}
146146

147-
if capiManifestsBundlePath, err := gatherCAPIManifests(directory, gatherID); err != nil {
147+
if capiManifestsBundlePath, err := gatherCAPIArtifacts(directory, gatherID); err != nil {
148148
// Do not fail the whole gather if we can't find capi manifests (we can be running terraform)
149149
logrus.Infof("Failed to gather Cluster API manifests: %s", err.Error())
150150
} else {
@@ -247,36 +247,44 @@ func logClusterOperatorConditions(ctx context.Context, config *rest.Config) erro
247247
return nil
248248
}
249249

250-
func gatherCAPIManifests(directory, gatherID string) (string, error) {
251-
logrus.Infoln("Pulling Cluster API manifests")
250+
func gatherCAPIArtifacts(directory, gatherID string) (string, error) {
251+
logrus.Infoln("Pulling Cluster API artifacts")
252252
dir, err := filepath.Abs(directory)
253253
if err != nil {
254254
return "", fmt.Errorf("failed to get absolute path for %s: %w", directory, err)
255255
}
256256

257-
capiDir := filepath.Join(dir, clusterapi.CAPIArtifactsDir)
257+
capiDir := filepath.Join(dir, clusterapi.ArtifactsDir)
258258
if _, err := os.Stat(capiDir); err != nil {
259259
if errors.Is(err, fs.ErrNotExist) {
260260
return "", fmt.Errorf("either Cluster API manifests not generated or terraform provision")
261261
}
262262
return "", fmt.Errorf("failed to stat Cluster API output directory: %w", err)
263263
}
264264

265-
bundleDir := filepath.Join(dir, fmt.Sprintf("capi-manifests-bundle-%s", gatherID))
266-
// Symlink the hidden directory so the manifests are not hidden in the archive
265+
bundleDir := filepath.Join(dir, fmt.Sprintf("capi-artifacts-bundle-%s", gatherID))
266+
// Symlink the hidden directory so the artifacts are not hidden in the archive
267267
if err := os.Symlink(capiDir, bundleDir); err != nil {
268-
return "", fmt.Errorf("failed to copy Cluster API manifests: %w", err)
268+
return "", fmt.Errorf("failed to copy Cluster API artifacts: %w", err)
269269
}
270270
defer os.Remove(bundleDir)
271271

272-
capiManifests, err := filepath.Glob(filepath.Join(bundleDir, "*.yaml"))
272+
var capiArtifacts []string
273+
manifests, err := filepath.Glob(filepath.Join(bundleDir, "*.yaml"))
273274
if err != nil {
274275
return "", fmt.Errorf("failed to gather Cluster API manifests: %w", err)
275276
}
277+
capiArtifacts = append(capiArtifacts, manifests...)
276278

277-
capiManifestsBundlePath := fmt.Sprintf("%s.tar.gz", bundleDir)
278-
if err := serialgather.CreateArchive(capiManifests, capiManifestsBundlePath); err != nil {
279+
logs, err := filepath.Glob(filepath.Join(bundleDir, "*.log"))
280+
if err != nil {
281+
return "", fmt.Errorf("failed to gather Cluster API control plane logs: %w", err)
282+
}
283+
capiArtifacts = append(capiArtifacts, logs...)
284+
285+
capiArtifactsBundlePath := fmt.Sprintf("%s.tar.gz", bundleDir)
286+
if err := serialgather.CreateArchive(capiArtifacts, capiArtifactsBundlePath); err != nil {
279287
return "", fmt.Errorf("failed to create clusterapi bundle file: %w", err)
280288
}
281-
return capiManifestsBundlePath, nil
289+
return capiArtifactsBundlePath, nil
282290
}

pkg/clusterapi/localcontrolplane.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type localControlPlane struct {
5555
Cfg *rest.Config
5656
BinDir string
5757
KubeconfigPath string
58+
EtcdLog *os.File
59+
APIServerLog *os.File
5860
}
5961

6062
// Run launches the local control plane.
@@ -68,21 +70,38 @@ func (c *localControlPlane) Run(ctx context.Context) error {
6870
return fmt.Errorf("failed to unpack envtest binaries: %w", err)
6971
}
7072

73+
// Write etcd & kube-apiserver output to log files.
74+
var err error
75+
if err := os.MkdirAll(filepath.Join(command.RootOpts.Dir, ArtifactsDir), 0750); err != nil {
76+
return fmt.Errorf("error creating artifacts dir: %w", err)
77+
}
78+
if c.EtcdLog, err = os.Create(filepath.Join(command.RootOpts.Dir, ArtifactsDir, "etcd.log")); err != nil {
79+
return fmt.Errorf("failed to create etcd log file: %w", err)
80+
}
81+
if c.APIServerLog, err = os.Create(filepath.Join(command.RootOpts.Dir, ArtifactsDir, "kube-apiserver.log")); err != nil {
82+
return fmt.Errorf("failed to create kube-apiserver log file: %w", err)
83+
}
84+
7185
log.SetLogger(klog.NewKlogr())
7286
logrus.Info("Started local control plane with envtest")
7387
c.Env = &envtest.Environment{
7488
Scheme: Scheme,
75-
AttachControlPlaneOutput: false,
89+
AttachControlPlaneOutput: true,
7690
BinaryAssetsDirectory: c.BinDir,
7791
ControlPlaneStartTimeout: 10 * time.Second,
7892
ControlPlaneStopTimeout: 10 * time.Second,
7993
ControlPlane: envtest.ControlPlane{
8094
Etcd: &envtest.Etcd{
8195
DataDir: c.BinDir,
96+
Out: c.EtcdLog,
97+
Err: c.EtcdLog,
98+
},
99+
APIServer: &envtest.APIServer{
100+
Out: c.APIServerLog,
101+
Err: c.APIServerLog,
82102
},
83103
},
84104
}
85-
var err error
86105
c.Cfg, err = c.Env.Start()
87106
if err != nil {
88107
return err

pkg/clusterapi/system.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const (
4747
SystemStateRunning SystemState = "running"
4848
// SystemStateStopped indicates the system is stopped.
4949
SystemStateStopped SystemState = "stopped"
50+
51+
// ArtifactsDir is the directory where output (manifests, kubeconfig, etc.)
52+
// related to CAPI-based installs are stored.
53+
ArtifactsDir = ".clusterapi_output"
5054
)
5155

5256
// Interface is the interface for the cluster-api system.
@@ -365,6 +369,10 @@ func (c *system) Teardown() {
365369
// Clean up the binary directory.
366370
defer os.RemoveAll(c.lcp.BinDir)
367371

372+
// Clean up log file handles.
373+
defer c.lcp.EtcdLog.Close()
374+
defer c.lcp.APIServerLog.Close()
375+
368376
// Proceed to shutdown.
369377
c.teardownOnce.Do(func() {
370378
c.cancel()

pkg/infrastructure/clusterapi/clusterapi.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ const (
5353
ignitionStage = "Bootstrap Ignition Provisioning"
5454
machineStage = "Machine Provisioning"
5555
postProvisionStage = "Infrastructure Post-provisioning"
56-
57-
// CAPIArtifactsDir is the directory where the manifests generated by CAPI are stored.
58-
CAPIArtifactsDir = ".clusterapi_output"
5956
)
6057

6158
// InfraProvider implements common Cluster API logic and
@@ -463,7 +460,7 @@ func extractIPAddress(manifestPath string) (string, error) {
463460

464461
// ExtractHostAddresses extracts the IPs of the bootstrap and control plane machines.
465462
func (i *InfraProvider) ExtractHostAddresses(dir string, config *types.InstallConfig, ha *infrastructure.HostAddresses) error {
466-
manifestsDir := filepath.Join(dir, CAPIArtifactsDir)
463+
manifestsDir := filepath.Join(dir, clusterapi.ArtifactsDir)
467464
logrus.Debugf("Looking for machine manifests in %s", manifestsDir)
468465

469466
bootstrapFiles, err := filepath.Glob(filepath.Join(manifestsDir, "Machine\\-openshift\\-cluster\\-api\\-guests\\-*\\-bootstrap.yaml"))
@@ -544,7 +541,7 @@ func (i *InfraProvider) collectManifests(ctx context.Context, cl client.Client)
544541
errorList = append(errorList, fmt.Errorf("failed to get GVK for manifest %s: %w", m.GetName(), err))
545542
continue
546543
}
547-
fileName := filepath.Join(CAPIArtifactsDir, fmt.Sprintf("%s-%s-%s.yaml", gvk.Kind, m.GetNamespace(), m.GetName()))
544+
fileName := filepath.Join(clusterapi.ArtifactsDir, fmt.Sprintf("%s-%s-%s.yaml", gvk.Kind, m.GetNamespace(), m.GetName()))
548545
objData, err := yaml.Marshal(m)
549546
if err != nil {
550547
errorList = append(errorList, fmt.Errorf("failed to marshal manifest %s: %w", fileName, err))

0 commit comments

Comments
 (0)