Skip to content

Commit

Permalink
don't fail loading tsKinds if directory doesn't exist (#4212)
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig O'Donnell authored Dec 12, 2023
1 parent 8452ca5 commit cf17c12
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
7 changes: 7 additions & 0 deletions pkg/kotsutil/troubleshoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
)

func LoadTSKindsFromPath(dir string) (*troubleshootloader.TroubleshootKinds, error) {
if _, err := os.Stat(dir); err != nil {
if !os.IsNotExist(err) {
return nil, errors.Wrapf(err, "failed to stat dir: %s", dir)
}
return &troubleshootloader.TroubleshootKinds{}, nil
}

var renderedFiles []string
err := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
Expand Down
14 changes: 7 additions & 7 deletions pkg/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
)

func Run(appID string, appSlug string, sequence int64, isAirgap bool, archiveDir string) error {
renderedKotsKinds, err := kotsutil.LoadKotsKindsFromPath(filepath.Join(archiveDir, "upstream"))
upstreamKotsKinds, err := kotsutil.LoadKotsKindsFromPath(filepath.Join(archiveDir, "upstream"))
if err != nil {
return errors.Wrap(err, "failed to load rendered kots kinds")
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func Run(appID string, appSlug string, sequence int64, isAirgap bool, archiveDir
preflight = troubleshootpreflight.ConcatPreflightSpec(preflight, &v)
}

injectDefaultPreflights(preflight, renderedKotsKinds, registrySettings)
injectDefaultPreflights(preflight, upstreamKotsKinds, registrySettings)

numAnalyzers := 0
for _, analyzer := range preflight.Spec.Analyzers {
Expand All @@ -99,15 +99,15 @@ func Run(appID string, appSlug string, sequence int64, isAirgap bool, archiveDir
}
}
runPreflights = numAnalyzers > 0
} else if renderedKotsKinds.Preflight != nil {
} else if upstreamKotsKinds.Preflight != nil {
// render the preflight file
// we need to convert to bytes first, so that we can reuse the renderfile function
renderedMarshalledPreflights, err := renderedKotsKinds.Marshal("troubleshoot.replicated.com", "v1beta1", "Preflight")
renderedMarshalledPreflights, err := upstreamKotsKinds.Marshal("troubleshoot.replicated.com", "v1beta1", "Preflight")
if err != nil {
return errors.Wrap(err, "failed to marshal rendered preflight")
}

renderedPreflight, err := render.RenderFile(renderedKotsKinds, registrySettings, appSlug, sequence, isAirgap, util.PodNamespace, []byte(renderedMarshalledPreflights))
renderedPreflight, err := render.RenderFile(upstreamKotsKinds, registrySettings, appSlug, sequence, isAirgap, util.PodNamespace, []byte(renderedMarshalledPreflights))
if err != nil {
return errors.Wrap(err, "failed to render preflights")
}
Expand All @@ -116,7 +116,7 @@ func Run(appID string, appSlug string, sequence int64, isAirgap bool, archiveDir
return errors.Wrap(err, "failed to load rendered preflight")
}

injectDefaultPreflights(preflight, renderedKotsKinds, registrySettings)
injectDefaultPreflights(preflight, upstreamKotsKinds, registrySettings)

numAnalyzers := 0
for _, analyzer := range preflight.Spec.Analyzers {
Expand Down Expand Up @@ -153,7 +153,7 @@ func Run(appID string, appSlug string, sequence int64, isAirgap bool, archiveDir
}
}

collectors, err := registry.UpdateCollectorSpecsWithRegistryData(preflight.Spec.Collectors, registrySettings, renderedKotsKinds.Installation, renderedKotsKinds.License, &renderedKotsKinds.KotsApplication)
collectors, err := registry.UpdateCollectorSpecsWithRegistryData(preflight.Spec.Collectors, registrySettings, upstreamKotsKinds.Installation, upstreamKotsKinds.License, &upstreamKotsKinds.KotsApplication)
if err != nil {
preflightErr = errors.Wrap(err, "failed to rewrite images in preflight")
return preflightErr
Expand Down
17 changes: 14 additions & 3 deletions pkg/tests/kotsutil/troubleshoot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (

func Test_LoadTSKindsFromPath(t *testing.T) {
tests := []struct {
name string
archiveDir string
wantedPreflights int
name string
archiveDir string
wantedPreflights int
wantNilPreflights bool
}{
{
name: "load single preflight from path",
Expand All @@ -29,13 +30,23 @@ func Test_LoadTSKindsFromPath(t *testing.T) {
archiveDir: "cases/no-preflights-in-helm-chart/rendered",
wantedPreflights: 0,
},
{
name: "don't fail if path doesn't exist",
archiveDir: "cases/not-a-real-dir/rendered",
wantedPreflights: 0,
wantNilPreflights: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := kotsutil.LoadTSKindsFromPath(tt.archiveDir)
require.NoError(t, err)

assert.Equal(t, tt.wantedPreflights, len(got.PreflightsV1Beta2))

if tt.wantNilPreflights {
assert.Nil(t, got.PreflightsV1Beta2)
}
})
}
}

0 comments on commit cf17c12

Please sign in to comment.